package org.springblade.flow.visual.onlinedev.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springblade.flow.base.entity.DictionaryDataEntity;
import org.springblade.flow.base.entity.DictionaryTypeEntity;
import org.springblade.flow.base.entity.ProvinceEntity;
import org.springblade.flow.base.service.DataInterfaceService;
import org.springblade.flow.base.service.DictionaryDataService;
import org.springblade.flow.base.service.DictionaryTypeService;
import org.springblade.flow.base.service.ProvinceService;
import org.springblade.flow.database.exception.DataException;
import org.springblade.flow.database.model.DbLinkEntity;
import org.springblade.flow.model.visiual.ColumnDataModel;
import org.springblade.flow.model.visiual.DataTypeConst;
import org.springblade.flow.model.visiual.JnpfKeyConsts;
import org.springblade.flow.model.visiual.OnlineDevData;
import org.springblade.flow.model.visiual.TimeControl;
import org.springblade.flow.model.visiual.fields.FieldsModel;
import org.springblade.flow.model.visiual.fields.config.ConfigModel;
import org.springblade.flow.model.visiual.fields.slot.SlotModel;
import org.springblade.flow.permission.entity.OrganizeEntity;
import org.springblade.flow.permission.entity.PositionEntity;
import org.springblade.flow.permission.entity.UserEntity;
import org.springblade.flow.permission.service.OrganizeService;
import org.springblade.flow.permission.service.PositionService;
import org.springblade.flow.permission.service.User4FlowService;
import org.springblade.flow.util.CacheKeyUtil;
import org.springblade.flow.util.DateUtil;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.JsonUtilEx;
import org.springblade.flow.util.RedisUtil;
import org.springblade.flow.util.StringUtil;
import org.springblade.flow.util.context.SpringContext;
import org.springblade.flow.visual.base.entity.VisualdevEntity;
import org.springblade.flow.visual.base.model.template6.ColumnListField;
import org.springblade.flow.visual.base.service.VisualdevService;
import org.springblade.flow.visual.base.util.DynamicUtil;
import org.springblade.flow.visual.base.util.VisualUtils;
import org.springblade.flow.visual.onlinedev.entity.VisualdevModelDataEntity;
import org.springblade.flow.visual.onlinedev.model.KeyDataConst;
import org.springblade.flow.visual.onlinedev.model.VisualdevModelDataInfoVO;
import org.springblade.flow.visual.onlinedev.service.VisualdevModelDataService;

import com.alibaba.fastjson.JSONArray;

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理KeyData
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021年3月15日10:16:01
 */
@Slf4j
public class KeyDataUtil {

    private static OrganizeService organizeService;
    private static DictionaryDataService dictionaryDataService;
    private static User4FlowService userService;
    private static DictionaryTypeService dictionaryTypeService;
    private static PositionService positionService;
    private static RedisUtil redisUtil;
    private static ProvinceService provinceService;
    private static VisualdevModelDataService visualdevModelDataService;
    private static VisualdevService visualdevService;


    //初始化

    public static void init() {
        dictionaryDataService = SpringContext.getBean(DictionaryDataService.class);
        userService = SpringContext.getBean(User4FlowService.class);
        dictionaryTypeService = SpringContext.getBean(DictionaryTypeService.class);
        organizeService = SpringContext.getBean(OrganizeService.class);
        positionService = SpringContext.getBean(PositionService.class);
        redisUtil = SpringContext.getBean(RedisUtil.class);
        provinceService = SpringContext.getBean(ProvinceService.class);
        visualdevModelDataService = SpringContext.getBean(VisualdevModelDataService.class);
        visualdevService = SpringContext.getBean(VisualdevService.class);
    }


    /**
     * 获取有表list
     *
     * @param list
     * @param mainTable
     * @param modelList
     * @param columnData
     * @return
     * @throws DataException
     * @throws SQLException
     */
    public static List<VisualdevModelDataEntity> getHasTableList(List<VisualdevModelDataEntity> list, String mainTable, List<ColumnListField> modelList, ColumnDataModel columnData, DbLinkEntity linkEntity) throws DataException, SQLException {
        @Cleanup Connection conn = VisualUtils.getDataConn(linkEntity);
        //获取主键
        String pKeyName = VisualUtils.getpKey(conn, mainTable);

        StringBuilder feilds = new StringBuilder();
        for (ColumnListField model : modelList) {
            feilds.append(model.getProp() + ",");
        }
        if (modelList.size() > 0) {
            feilds.deleteCharAt(feilds.length() - 1);
        }

        String feildsBool = feilds.toString().toLowerCase().trim();
        //判断字段是否存在主键
        Boolean keyFlag = VisualUtils.existKey(feildsBool, pKeyName);

        //获取查询语句
        String listResultSql = VisualUtils.getListResultSql(keyFlag, feilds.toString(), mainTable, pKeyName, columnData);
        //获取有表列表数据
        list = VisualUtils.getTableDataList(conn, listResultSql, pKeyName);
        //Id赋值
        list = VisualUtils.setDataId(pKeyName, list);
        return list;
    }


    /**
     * 转换分组数据
     *
     * @param columnDataModel
     * @param realList
     * @return
     */
    public static List<Map<String, Object>> changeGroupDataList(ColumnDataModel columnDataModel, List<Map<String, Object>> realList) {
        List<Map<String, Object>> columnList = JsonUtil.getJsonToListMap(columnDataModel.getColumnList());
        String firstField = "";
        for (Map<String, Object> modelMap : columnList) {
            if (!columnDataModel.getGroupField().equals(modelMap.get("prop").toString())) {
                firstField = modelMap.get("prop").toString();
                break;
            }
        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        //用来转换空值的List
        List<Map<String, Object>> nullList = new ArrayList<>(realList.stream().filter(t -> "null".equals(String.valueOf(t.get(columnDataModel.getGroupField())))).collect(Collectors.toList()));
        Map<String, Object> nullMap = new HashMap<>(16);
        nullMap.put(firstField, "");
        nullMap.put("top", true);
        nullMap.put("id","null");
        nullMap.put("children", nullList);
        resultList.add(nullMap);
        if (nullList.size()==0){
            resultList.remove(nullMap);
        }
        List<String> groupField = realList.stream().map(t -> String.valueOf(t.get(columnDataModel.getGroupField()))).collect(Collectors.toList());
        HashSet<String> parents = new HashSet<String>(groupField);
        parents.remove("null");
        for (String parent : parents) {
            Map<String, Object> dataMap = new HashMap<>(16);
            List<Map<String, Object>> resultMapOneList = realList.stream().filter(t -> parent.equals(t.get(columnDataModel.getGroupField()))).collect(Collectors.toList());
            for (Map<String, Object> resultMapOneMap : resultMapOneList) {
                if (resultMapOneMap.containsKey(columnDataModel.getGroupField())) {
                    resultMapOneMap.remove(columnDataModel.getGroupField());
                }
            }
            dataMap.put(firstField, parent);
            dataMap.put("id",parent);
            dataMap.put("top", true);
            dataMap.put("children", resultMapOneList);
            resultList.add(dataMap);

        }
        return resultList;
    }


    /**
     * 详情信息需要进行数据转换
     *
     * @param id
     * @param modelId
     * @return
     * @throws DataException
     * @throws ParseException
     * @throws IOException
     * @throws SQLException
     */
    public static VisualdevModelDataInfoVO infoWithDataChange(String id, String modelId) throws DataException, ParseException, IOException, SQLException {
        VisualdevEntity visualdevEntity = visualdevService.getInfo(modelId);
        //有表
        if (!StringUtil.isEmpty(visualdevEntity.getTables()) && !OnlineDevData.TABLE_CONST.equals(visualdevEntity.getTables())) {
            VisualdevModelDataInfoVO vo = visualdevModelDataService.tableInfoDataChange(id, visualdevEntity);
            return vo;
        }
        //无表
        VisualdevModelDataInfoVO vo = visualdevModelDataService.infoDataChange(id, visualdevEntity);
        return vo;
    }


    /**
     * 将关键字key查询传输的id转换成名称，还有动态数据id成名称
     *
     * @return Map<String, Object>
     */
    public static Map<String, Object> getKeyData(List<FieldsModel> formData, Map<String, Object> keyJsonMap, List<VisualdevModelDataEntity> list, String visualDevId,Integer method) throws IOException, ParseException, DataException, SQLException {
        init();
        //创建时间类关键词查询对应的字段实体
        TimeControl timeControl = new TimeControl();
        //存储查询条件
        Map<String, Object> dicKeyMap = new HashMap<>(16);
        Map<String, Object> orgKeyMap = new HashMap<>(16);
        Map<String, Object> posKeyMap = new HashMap<>(16);
        Map<String, Object> userKeyMap = new HashMap<>(16);
        Map<String, Object> provKeyMap = new HashMap<>(16);
        Map<String, Object> dicTypeKeyMap = new HashMap<>(16);
        Map<String, Object> visualKeyMap = new HashMap<>(16);

        //存储查询结果
        HashSet<String> dicKeyList = new HashSet<>(16);
        HashSet<String> orgKeyList = new HashSet<>(16);
        HashSet<String> posKeyList = new HashSet<>(16);
        HashSet<String> userKeyList = new HashSet<>(16);
        HashSet<String> provKeyList = new HashSet<>(16);
        HashSet<String> dicTypeKeyList = new HashSet<>(16);
        HashSet<String> visualKeyList = new HashSet<>(16);
        //将查询map也转换成数据放进去进行数据转换
        if (keyJsonMap != null) {
            VisualdevModelDataEntity keyEntity = new VisualdevModelDataEntity();
            keyEntity.setId(DataTypeConst.KEY_JSON_MAP);
            keyEntity.setData(JsonUtilEx.getObjectToString(keyJsonMap));
            list.add(keyEntity);
        }
        //记录将要去数据库查询的所有id
        for (VisualdevModelDataEntity entity : list) {
            //数据库保存的F_Data
            Map<String, Object> dataMap = JsonUtil.stringToMap(entity.getData());
            for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
                for (FieldsModel model : formData) {
                    if (model != null && model.getVModel() != null && entry.getValue() != null) {
                        ConfigModel configModel = model.getConfig();
                        String field = model.getVModel();
                        if (entry.getKey().equals(field)) {
                            String type = configModel.getJnpfKey();
                            switch (type) {
                                //单选框
                                case JnpfKeyConsts.RADIO:
                                    //下拉框
                                case JnpfKeyConsts.SELECT:
                                    if (DataTypeConst.DICTIONARY.equals(configModel.getDataType())) {
                                        dicKeyList.add(String.valueOf(entry.getValue()));
                                    }
                                    break;
                                //复选框
                                case JnpfKeyConsts.CHECKBOX:
                                    if (DataTypeConst.DICTIONARY.equals(configModel.getDataType())) {
                                        //字段数据id
                                        List<String> add = VisualUtils.analysisField(String.valueOf(entry.getValue()));
                                        String addStr = String.valueOf(entry.getValue());
                                        if (add.size() > 0) {
                                            for (String str : add) {
                                                dicKeyList.add(str);
                                            }
                                        } else {
                                            dicKeyList.add(addStr);
                                        }
                                    }
                                    break;
                                //公司
                                case JnpfKeyConsts.COMSELECT:
                                    //部门
                                case JnpfKeyConsts.DEPSELECT:
                                    if (String.valueOf(entry.getValue()).contains(",")) {
                                        String[] depSelects = String.valueOf(entry.getValue()).split(",");
                                        for (String depSelect : depSelects) {
                                            orgKeyList.add(depSelect);
                                        }
                                    } else {
                                        orgKeyList.add(String.valueOf(entry.getValue()));
                                    }
                                    break;
                                //岗位
                                case JnpfKeyConsts.POSSELECT:
                                    if (String.valueOf(entry.getValue()).contains(",")) {
                                        String[] posSelects = String.valueOf(entry.getValue()).split(",");
                                        for (String posSelect : posSelects) {
                                            posKeyList.add(posSelect);
                                        }
                                    } else {
                                        posKeyList.add(String.valueOf(entry.getValue()));
                                    }
                                    break;
                                //用户
                                case JnpfKeyConsts.USERSELECT:
                                    if (String.valueOf(entry.getValue()).contains(",")) {
                                        String[] userSelects = String.valueOf(entry.getValue()).split(",");
                                        for (String userSelect : userSelects) {
                                            userKeyList.add(userSelect);
                                        }
                                    } else {
                                        userKeyList.add(String.valueOf(entry.getValue()));
                                    }
                                    break;
                                //数据字典
                                case JnpfKeyConsts.DICSELECT:
                                    dicTypeKeyList.add(String.valueOf(entry.getValue()));
                                    break;
                                //省市区
                                case JnpfKeyConsts.ADDRESS:
                                    provKeyList.add(String.valueOf(entry.getValue()));
                                    break;
                                //关联表单
                                case JnpfKeyConsts.RELATIONFORM:
                                    if (StringUtil.isNotEmpty(String.valueOf(entry.getValue()))) {
                                        visualKeyList.add(entry.getValue() + "_" + model.getModelId() + "_" + entry.getKey());
                                    }
                                    break;
                                default:
                            }
                        }
                    }
                }
            }
        }

        //从数据库或redis获取所有数据
        if (!redisUtil.exists(CacheKeyUtil.VISIUALDATA + visualDevId)) {
            Map<String, Object> dataMap = new HashMap<>(16);
            if (dicKeyList.size() > 0) {
                List<DictionaryDataEntity> dictionaryDataEntities = dictionaryDataService.listByIds(dicKeyList);
                for (DictionaryDataEntity entity : dictionaryDataEntities) {
                    dicKeyMap.put(entity.getId(), entity.getFullName());
                }
                dataMap.put(KeyDataConst.DICKEYMAP, dicKeyMap);
            }
            if (dicTypeKeyList.size() > 0) {
                List<DictionaryTypeEntity> dictionaryTypeEntities = dictionaryTypeService.listByIds(dicTypeKeyList);
                for (DictionaryTypeEntity entity : dictionaryTypeEntities) {
                    dicTypeKeyMap.put(entity.getId(), entity.getFullName());
                }
                dataMap.put(KeyDataConst.DICTYPEKEYMAP, dicTypeKeyMap);
            }
            if (userKeyList.size() > 0) {
                List<UserEntity> userEntities = userService.listByIds(userKeyList);
                for (UserEntity entity : userEntities) {
                    userKeyMap.put(entity.getId(), entity.getRealName() + "/" + entity.getAccount());
                }
                dataMap.put(KeyDataConst.USERKEYMAP, userKeyMap);
            }
            if (orgKeyList.size() > 0) {
                List<OrganizeEntity> organizeEntities = organizeService.listByIds(orgKeyList);
                for (OrganizeEntity entity : organizeEntities) {
                    orgKeyMap.put(entity.getId(), entity.getFullName());
                }
                dataMap.put(KeyDataConst.ORGKEYMAP, orgKeyMap);
            }
            if (provKeyList.size() > 0) {
                List<ProvinceEntity> provinceEntities = provinceService.listByIds(provKeyList);
                for (ProvinceEntity entity : provinceEntities) {
                    provKeyMap.put(entity.getId(), entity.getFullName());
                }
                dataMap.put(KeyDataConst.PROVKEYMAP, provKeyMap);
            }
            if (posKeyList.size() > 0) {
                List<PositionEntity> positionEntities = positionService.listByIds(posKeyList);
                for (PositionEntity entity : positionEntities) {
                    posKeyMap.put(entity.getId(), entity.getFullName());
                }
                dataMap.put(KeyDataConst.POSKEYMAP, posKeyMap);
            }
            if (visualKeyList.size() > 0) {
                for (String visualId : visualKeyList) {
                    String[] data = visualId.split("_");
                    if (data.length == 3) {
                        VisualdevModelDataInfoVO visualdevModelDataInfoVO = infoWithDataChange(data[0], data[1]);
                        if (visualdevModelDataInfoVO != null) {
                            Map<String, Object> viDataMap = JsonUtil.stringToMap(visualdevModelDataInfoVO.getData());
                            for (FieldsModel fieLdsModel : formData) {
                                for (Map.Entry<String, Object> entry : viDataMap.entrySet()) {
                                    if ((data[1] + entry.getKey()).equals(fieLdsModel.getModelId() + fieLdsModel.getRelationField())) {
                                        visualKeyMap.put(data[2] + "_" + visualdevModelDataInfoVO.getId(), entry.getValue());
                                    }
                                }
                            }
                        }
                    }
                }
                dataMap.put(KeyDataConst.VISUALKEYMAP, visualKeyMap);
            }
        } else {
            Map<String, Object> dataMap = JsonUtil.stringToMap(redisUtil.getString(CacheKeyUtil.VISIUALDATA + visualDevId).toString());
            if (dataMap.containsKey(KeyDataConst.DICKEYMAP)) {
                dicKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.DICKEYMAP)));
            }
            if (dataMap.containsKey(KeyDataConst.DICTYPEKEYMAP)) {
                dicTypeKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.DICTYPEKEYMAP)));
            }
            if (dataMap.containsKey(KeyDataConst.USERKEYMAP)) {
                userKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.USERKEYMAP)));
            }
            if (dataMap.containsKey(KeyDataConst.ORGKEYMAP)) {
                orgKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.ORGKEYMAP)));
            }
            if (dataMap.containsKey(KeyDataConst.PROVKEYMAP)) {
                provKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.PROVKEYMAP)));
            }
            if (dataMap.containsKey(KeyDataConst.POSKEYMAP)) {
                posKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.POSKEYMAP)));
            }
            if (dataMap.containsKey(KeyDataConst.VISUALKEYMAP)) {
                visualKeyMap.putAll(JsonUtil.entityToMap(dataMap.get(KeyDataConst.VISUALKEYMAP)));
            }
        }
        //数据赋值
        for (VisualdevModelDataEntity entity : list) {
            Map<String, Object> relaMap = new HashMap<>(16);
            //数据库保存的F_Data
            Map<String, Object> dataMap = JsonUtil.stringToMap(entity.getData());
            for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
                for (FieldsModel model : formData) {
                    if (model != null && model.getVModel() != null && entry.getValue() != null) {
                        ConfigModel configModel = model.getConfig();
                        String field = model.getVModel();
                        if (entry.getKey().equals(field)) {
                            String type = configModel.getJnpfKey();
                            switch (type) {
                                //开关
                                case JnpfKeyConsts.SWITCH:
                                    if (method.equals(0)){
                                        if ("1".equals(String.valueOf(entry.getValue()))) {
                                            dataMap.put(field, "开");
                                        } else {
                                            dataMap.put(field, "关");
                                        }
                                    }else {
                                        if ("1".equals(String.valueOf(entry.getValue()))) {
                                            dataMap.put(field, 1);
                                        } else {
                                            dataMap.put(field, 0);
                                        }
                                    }
                                    //单选框
                                case JnpfKeyConsts.RADIO:
                                    //下拉框
                                case JnpfKeyConsts.SELECT:
                                    if (DataTypeConst.DICTIONARY.equals(configModel.getDataType())) {
                                        if (dicKeyMap.containsKey(String.valueOf(entry.getValue()))) {
                                            dataMap.put(field, dicKeyMap.get(String.valueOf(entry.getValue())));
                                        }
                                    }
                                    if (DataTypeConst.STATIC.equals(configModel.getDataType())) {
                                        SlotModel slotModel = model.getSlot();
                                        if (slotModel != null) {
                                            List<Map<String, Object>> modelOpt = JsonUtil.getJsonToListMap(slotModel.getOptions());
                                            for (Map<String, Object> map : modelOpt) {
                                                if (map.get(model.getConfig().getProps().getValue()).toString().equals(dataMap.get(field).toString())) {
                                                    dataMap.put(field, map.get(model.getConfig().getProps().getLabel()).toString());
                                                }
                                            }
                                        }
                                    }
                                    if (DataTypeConst.DYNAMIC.equals(configModel.getDataType())) {
                                        DynamicUtil dynamicUtil = new DynamicUtil();
                                        dataMap = dynamicUtil.dynamicKeyData(model, dataMap);
                                    }
                                    break;
                                //复选框
                                case JnpfKeyConsts.CHECKBOX:
                                    if (DataTypeConst.DICTIONARY.equals(configModel.getDataType())) {
                                        //字段数据id
                                        List<String> add = VisualUtils.analysisField(String.valueOf(entry.getValue()));
                                        String addStr = String.valueOf(entry.getValue());
                                        StringBuilder addName = new StringBuilder();
                                        if (add.size() > 0) {
                                            for (String str : add) {
                                                if (dicKeyMap.containsKey(str)) {
                                                    addName.append(dicKeyMap.get(str));
                                                }
                                            }
                                        } else {
                                            if (dicKeyMap.containsKey(addStr)) {
                                                addName.append(dicKeyMap.get(addStr));
                                            }
                                        }
                                        if (addName.length() != 0) {
                                            dataMap.put(field, addName);
                                        }
                                    }
                                    if (DataTypeConst.STATIC.equals(configModel.getDataType())) {
                                        if (model.getSlot() != null && model.getSlot().getOptions() != null) {
                                            List<Map<String, Object>> modelOpt = JsonUtil.getJsonToListMap(model.getSlot().getOptions());
                                            for (Map<String, Object> map : modelOpt) {
                                                if (map.get(model.getConfig().getProps().getValue()).toString().equals(dataMap.get(field).toString())) {
                                                    dataMap.put(field, map.get(model.getConfig().getProps().getLabel()).toString());
                                                }

                                            }
                                        }
                                    }
                                    if (DataTypeConst.DYNAMIC.equals(configModel.getDataType())) {
                                        //获取最新远端数据转换远端数据查询关键词
                                        DynamicUtil dynamicUtil = new DynamicUtil();
                                        dataMap = dynamicUtil.dynamicKeyData(model, dataMap);
                                    }
                                    break;
                                //公司
                                case JnpfKeyConsts.COMSELECT:
                                    //部门
                                case JnpfKeyConsts.DEPSELECT:
                                    if (String.valueOf(entry.getValue()).contains(",")) {
                                        String[] depSelects = String.valueOf(entry.getValue()).split(",");
                                        String[] newDepSelects = new String[depSelects.length];
                                        int i = 0;
                                        for (String depSelect : depSelects) {
                                            if (orgKeyMap.containsKey(depSelect)) {
                                                newDepSelects[i] = String.valueOf(orgKeyMap.get(depSelect));
                                            }
                                            i++;
                                        }
                                        dataMap.put(field, newDepSelects);
                                    } else {
                                        String str = String.valueOf(entry.getValue());
                                        if (orgKeyMap.containsKey(str)) {
                                            dataMap.put(field, orgKeyMap.get(str));
                                        }
                                    }
                                    break;
                                //岗位
                                case JnpfKeyConsts.POSSELECT:
                                    if (String.valueOf(entry.getValue()).contains(",")) {
                                        String[] posSelects = String.valueOf(entry.getValue()).split(",");
                                        String[] newposSelects = new String[posSelects.length];
                                        int i = 0;
                                        for (String posSelect : posSelects) {
                                            if (posKeyMap.containsKey(posSelect)) {
                                                newposSelects[i] = String.valueOf(posKeyMap.get(posSelect));
                                            }
                                            i++;
                                        }
                                        dataMap.put(field, newposSelects);
                                    } else {
                                        String str = String.valueOf(entry.getValue());
                                        if (posKeyMap.containsKey(str)) {
                                            dataMap.put(field, posKeyMap.get(str));
                                        }
                                    }
                                    break;
                                //用户
                                case JnpfKeyConsts.USERSELECT:
                                    if (String.valueOf(entry.getValue()).contains(",")) {
                                        String[] userSelects = String.valueOf(entry.getValue()).split(",");
                                        String[] newuserSelects = new String[userSelects.length];
                                        int i = 0;
                                        for (String userSelect : userSelects) {
                                            if (userKeyMap.containsKey(userSelect)) {
                                                newuserSelects[i] = String.valueOf(userKeyMap.get(userSelect));
                                            }
                                            i++;
                                        }
                                        dataMap.put(field, newuserSelects);
                                    } else {
                                        String str = String.valueOf(entry.getValue());
                                        if (userKeyMap.containsKey(str)) {
                                            dataMap.put(field, userKeyMap.get(str));
                                        }
                                    }
                                    break;
                                //数据字典
                                case JnpfKeyConsts.DICSELECT:
                                    if (dicTypeKeyMap.containsKey(String.valueOf(entry.getValue()))) {
                                        dataMap.put(field, dicTypeKeyMap.get(String.valueOf(entry.getValue())));
                                    }
                                    break;
                                //省市区
                                case JnpfKeyConsts.ADDRESS:
                                    String str = String.valueOf(entry.getValue());
                                    if (provKeyMap.containsKey(str)) {
                                        dataMap.put(field, provKeyMap.get(str));
                                    }
                                    break;
                                //时间范围
                                case JnpfKeyConsts.TIMERANGE:
                                    JSONArray jsonArrayTimeRange = JsonUtil.getJsonToJsonArray(String.valueOf(dataMap.get(field)));
                                    jsonArrayTimeRange = DateUtil.addCon(jsonArrayTimeRange, JnpfKeyConsts.TIMERANGE, "HH:mm:ss");
                                    dataMap.put(field, jsonArrayTimeRange);
                                    timeControl.setTimeRange(timeControl.getTimeRange() + field);
                                    break;
                                //时间选择
                                case JnpfKeyConsts.TIME:
                                    timeControl.setTime(timeControl.getTimeRange() + field);
                                    break;
                                //日期选择
                                case JnpfKeyConsts.DATE:
                                    String dateStr=String.valueOf(dataMap.get(field));
                                    List<String> dataAll = new ArrayList<>();
                                    if (dateStr.contains("[") || dateStr.contains("]")) {
                                        List<String> data = JsonUtil.getJsonToList(dateStr, String.class);
                                        dataAll.addAll(data);
                                    }else {
                                        dataAll.add(dateStr);
                                    }
                                    List<String> result = new ArrayList<>();
                                    for(String date : dataAll) {
                                        try {
                                            long time;
                                            time = Long.parseLong(date);
                                            DateTimeFormatter ftf = DateTimeFormatter.ofPattern(model.getFormat());
                                            String value = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.of("+8")));
                                            result.add(value);
                                            timeControl.setDate(timeControl.getDate() + field);
                                        } catch (Exception e) {
                                            result.add(date);
                                            log.error(e.getMessage());
                                        }
                                    }
                                    dataMap.put(field, String.join(",",result));
                                    break;
                                 //创建时间
                                case JnpfKeyConsts.CREATETIME:
                                    timeControl.setCreateTime(timeControl.getCreateTime()+field);
                                    break;
                                //修改时间
                                case JnpfKeyConsts.MODIFYTIME:
                                    timeControl.setModifyTime(timeControl.getModifyTime() + field);
                                    break;
                                //日期范围
                                case JnpfKeyConsts.DATERANGE:
                                    if(String.valueOf(dataMap.get(field)).contains(",")){
                                        JSONArray jsonArray = JsonUtil.getJsonToJsonArray(String.valueOf(dataMap.get(field)));
                                        jsonArray = DateUtil.addCon(jsonArray, JnpfKeyConsts.DATERANGE, model.getFormat());
                                        dataMap.put(field, jsonArray);
                                        timeControl.setDateRange(timeControl.getDateRange() + field);
                                    }
                                    break;
                                //关联表单
                                case JnpfKeyConsts.RELATIONFORM:
                                    Object mapKey = visualKeyMap.get(entry.getKey() + "_" + entry.getValue());
                                    if (mapKey != null) {
                                        relaMap.put(field + OnlineDevData.INFO_ID, String.valueOf(entry.getValue()));
                                        dataMap.put(field, mapKey);
                                    }
                                    break;
                                //弹窗控件
                                case JnpfKeyConsts.POPUPSELECT:
                                    DataInterfaceService dataInterfaceService = SpringContext.getBean(DataInterfaceService.class);
                                    Object data = dataInterfaceService.infoToId(model.getInterfaceId()).getData();
                                    List<Map<String, Object>> mapList =(List<Map<String, Object>>) data;
                                    for (Map<String,Object> map :mapList){
                                        if (map.get(model.getPropsValue()).equals(entry.getValue())){
                                            //列表默认取第一个字段的值
                                            String s = model.getColumnOptions().get(0).getValue();
                                            entry.setValue(map.get(s));
                                        }
                                    }
                                    dataMap.put(entry.getKey(),entry.getValue());
                                    break;
                                default:

                            }
                        }
                    }

                }
            }
            dataMap.putAll(relaMap);
            entity.setData(JsonUtilEx.getObjectToString(dataMap));
        }
        if (keyJsonMap != null) {
            VisualdevModelDataEntity entity = list.stream().filter(x -> DataTypeConst.KEY_JSON_MAP.equals(x.getId())).findFirst().get();
            list.remove(entity);
            keyJsonMap = JsonUtil.stringToMap(entity.getData());
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put(DataTypeConst.LIST, list);
        map.put(DataTypeConst.TIME_CONTROL, timeControl);
        map.put(DataTypeConst.KEY_JSON_MAP, keyJsonMap);
        return map;
    }


}
