package com.hzya.frame.mdm.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.mdm.entity.*;
import com.hzya.frame.mdm.mdmModule.dao.IMdmModuleDao;
import com.hzya.frame.mdm.mdmModule.entity.MdmModuleEntity;
import com.hzya.frame.mdm.mdmModuleDb.dao.IMdmModuleDbDao;
import com.hzya.frame.mdm.mdmModuleDb.entity.MdmModuleDbEntity;
import com.hzya.frame.mdm.mdmModuleDbFileds.dao.IMdmModuleDbFiledsDao;
import com.hzya.frame.mdm.mdmModuleDbFileds.entity.MdmModuleDbFiledsEntity;
import com.hzya.frame.mdm.mdmModuleDbFiledsRule.dao.IMdmModuleDbFiledsRuleDao;
import com.hzya.frame.mdm.mdmModuleDbFiledsRule.entity.MdmModuleDbFiledsRuleEntity;
import com.hzya.frame.mdm.mdmModuleDistribute.dao.IMdmModuleDistributeDao;
import com.hzya.frame.mdm.mdmModuleDistribute.entity.MdmModuleDistributeEntity;
import com.hzya.frame.mdm.mdmModuleDistributeDetail.dao.IMdmModuleDistributeDetailDao;
import com.hzya.frame.mdm.mdmModuleDistributeDetail.entity.MdmModuleDistributeDetailEntity;
import com.hzya.frame.mdm.mdmModuleDistributeTripartite.dao.IMdmModuleDistributeTripartiteDao;
import com.hzya.frame.mdm.mdmModuleDistributeTripartite.entity.MdmModuleDistributeTripartiteEntity;
import com.hzya.frame.mdm.mdmModuleOptionLog.dao.IMdmModuleOptionLogDao;
import com.hzya.frame.mdm.mdmModuleOptionLog.entity.MdmModuleOptionLogEntity;
import com.hzya.frame.mdm.mdmModuleSendLog.dao.IMdmModuleSendLogDao;
import com.hzya.frame.mdm.mdmModuleSendLog.entity.MdmModuleSendLogEntity;
import com.hzya.frame.mdm.mdmModuleView.dao.IMdmModuleViewDao;
import com.hzya.frame.mdm.mdmModuleView.entity.MdmModuleViewEntity;
import com.hzya.frame.mdm.mdmModuleViewButton.dao.IMdmModuleViewButtonDao;
import com.hzya.frame.mdm.mdmModuleViewDetail.dao.IMdmModuleViewDetailDao;
import com.hzya.frame.mdm.mdmModuleViewDetail.entity.MdmModuleViewDetailEntity;
import com.hzya.frame.mdm.mdmTableCodeRule.dao.IMdmTableCodeRuleDao;
import com.hzya.frame.mdm.mdmTableCodeRule.entity.MdmTableCodeRuleEntity;
import com.hzya.frame.mdm.service.IMdmService;
import com.hzya.frame.mdm.service.IMdmServiceCache;
import com.hzya.frame.sys.entity.FormmainDeleteDto;
import com.hzya.frame.sys.sysenum.SysEnum;
import com.hzya.frame.sysnew.application.api.dao.ISysApplicationApiDao;
import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiEntity;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.application.script.dao.ISysApplicationScriptDao;
import com.hzya.frame.sysnew.application.script.entity.SysApplicationScriptEntity;
import com.hzya.frame.sysnew.buttonConfig.dao.ISysButtonConfigDao;
import com.hzya.frame.sysnew.grovy.service.IGroovyIntegrationService;
import com.hzya.frame.sysnew.integtationTaskLivingDetails.entity.IntegrationTaskLivingDetailsEntity;
import com.hzya.frame.sysnew.integtationTaskLivingDetails.service.IIntegrationTaskLivingDetailsService;
import com.hzya.frame.sysnew.menuConfig.dao.ISysMenuConfigDao;
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.uuid.UUIDUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 主数据服务实现类
 */
@Service(value = "mdmService")
public class MdmServiceImpl implements IMdmService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ISysApplicationDao sysApplicationDao;
    @Resource
    private ISysApplicationApiDao sysApplicationApiDao;
    @Resource
    private ISysApplicationScriptDao sysApplicationScriptDao;
    @Resource
    private IMdmModuleDao mdmModuleDao;
    @Resource
    private IMdmModuleDistributeTripartiteDao mdmModuleDistributeTripartiteDao;
    @Resource
    private IIntegrationTaskLivingDetailsService taskLivingDetailsService;
    @Resource
    private IGroovyIntegrationService groovyIntegrationService;
    @Resource
    private IMdmModuleSendLogDao mdmModuleSendLogDao;
    @Resource
    private IMdmModuleOptionLogDao mdmModuleOptionLogDao;
    @Resource
    private IMdmServiceCache mdmServiceCache;
    @Resource
    private IMdmModuleDbDao mdmModuleDbDao;
    @Resource
    private ISysUserDao sysUserDao;
    @Resource
    private IMdmModuleDbFiledsDao mdmModuleDbFiledsDao;
    @Resource
    private IMdmModuleDbFiledsRuleDao mdmModuleDbFiledsRuleDao;
    @Resource
    private IMdmTableCodeRuleDao mdmTableCodeRuleDao;
    @Resource
    private IMdmModuleDistributeDao mdmModuleDistributeDao;
    @Resource
    private IMdmModuleDistributeDetailDao mdmModuleDistributeDetailDao;
    @Resource
    private IMdmModuleViewDao mdmModuleViewDao;
    @Resource
    private IMdmModuleViewButtonDao mdmModuleViewButtonDao;
    @Resource
    private IMdmModuleViewDetailDao mdmModuleViewDetailDao;
    //多线程请求加锁 HttpRequest 构造方法是静态的
    private final Object lock = new Object();
    @Value("${zt.url}")
    private String url;

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据列表显示 树、查询条件、列表字段、按钮
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShow(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询模版
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        if (mdmModuleEntity == null || mdmModuleEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }

        //设置模版
        MdmViewVo mdmViewVo = new MdmViewVo();
        mdmViewVo.setMdmModuleEntity(mdmModuleEntity);
        //查询展示类型
        MdmModuleViewEntity mdmModuleViewEntity = new MdmModuleViewEntity();
        mdmModuleViewEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleViewEntity.setSts("Y");
        List<MdmModuleViewEntity> moduleViewEntityList = mdmModuleViewDao.queryBase(mdmModuleViewEntity);
        if (moduleViewEntityList == null || moduleViewEntityList.size() != 1) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        mdmModuleViewEntity = moduleViewEntityList.get(0);
        mdmViewVo.setMdmModuleViewEntity(mdmModuleViewEntity);

        //查询数据源主表
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询数据源表下面的字段
        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbFiledsEntity.setSts("Y");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
        if (mdmModuleDbFiledsEntityList == null || mdmModuleDbFiledsEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询字段下的规则
        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbFiledsRuleEntity.setSts("Y");
        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntityList = mdmModuleDbFiledsRuleDao.queryBase(mdmModuleDbFiledsRuleEntity);
        if (mdmModuleDbFiledsRuleEntityList == null || mdmModuleDbFiledsRuleEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }

        //查询设置的查询字段
        MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
        mdmModuleViewDetailEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleViewDetailEntity.setSts("Y");
        List<MdmModuleViewDetailEntity> mdmModuleViewDetailEntities = mdmModuleViewDetailDao.queryBase(mdmModuleViewDetailEntity);
        if (mdmModuleViewDetailEntities == null || mdmModuleViewDetailEntities.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmViewFiledVo> queryList = getFiledByType("1", mdmModuleViewDetailEntities, mdmModuleDbEntityList, mdmModuleDbFiledsEntityList, mdmModuleDbFiledsRuleEntityList);
        mdmViewVo.setQueryList(queryList);
        List<MdmViewFiledVo> listList = getFiledByType("2", mdmModuleViewDetailEntities, mdmModuleDbEntityList, mdmModuleDbFiledsEntityList, mdmModuleDbFiledsRuleEntityList);
        mdmViewVo.setListList(listList);
        return BaseResult.getSuccessMessageEntity("获取字段成功", mdmViewVo);
    }

    /**
     * @param viewType                        类型
     * @param mdmModuleViewDetailEntities     显示设置的字段
     * @param mdmModuleDbEntityList           表
     * @param mdmModuleDbFiledsEntityList     表字段
     * @param mdmModuleDbFiledsRuleEntityList 字段规则
     * @return java.util.List<com.hzya.frame.mdm.entity.MdmViewFiledVo>
     * @Author lvleigang
     * @Description 根据类型获取字段
     * @Date 9:18 上午 2024/6/6
     **/
    private List<MdmViewFiledVo> getFiledByType(String viewType, List<MdmModuleViewDetailEntity> mdmModuleViewDetailEntities, List<MdmModuleDbEntity> mdmModuleDbEntityList, List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList, List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntityList) {
        List<MdmViewFiledVo> mdmViewFiledVos = new ArrayList<>();
        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                MdmViewFiledVo rule = new MdmViewFiledVo();
                rule.setDbName(mdmModuleDbEntityList.get(i).getDbName());
                rule.setDbType("1");
                rule.setMdmId(mdmModuleDbEntityList.get(i).getMdmId());
                rule.setDbId(mdmModuleDbEntityList.get(i).getId());
                rule.setChName("单据规则");
                rule.setEnName("document_rule");
                mdmViewFiledVos.add(rule);
                List<MdmModuleDbFiledsRuleEntity> msgRule = new ArrayList<>();
                MdmModuleDbFiledsRuleEntity msgRuleTitie = new MdmModuleDbFiledsRuleEntity();
                msgRuleTitie.setFormName("optionRuleForm");
                msgRuleTitie.setRuleCode("title");
                msgRuleTitie.setRuleName("显示名");
                msgRuleTitie.setRuleType("2");
                msgRuleTitie.setRuleValue("单据编码");
                MdmModuleDbFiledsRuleEntity msgRulerow = new MdmModuleDbFiledsRuleEntity();
                msgRulerow.setFormName("optionRuleForm");
                msgRulerow.setRuleCode("row");
                msgRulerow.setRuleName("宽度");
                msgRulerow.setRuleType("2");
                msgRulerow.setRuleValue("12");
                MdmModuleDbFiledsRuleEntity msgRuletype = new MdmModuleDbFiledsRuleEntity();
                msgRuletype.setFormName("optionRuleForm");
                msgRuletype.setRuleCode("type");
                msgRuletype.setRuleName("数据类型");
                msgRuletype.setRuleType("2");
                msgRuletype.setRuleValue("input");
                MdmModuleDbFiledsRuleEntity msgRulerequired = new MdmModuleDbFiledsRuleEntity();
                msgRulerequired.setFormName("optionRuleForm");
                msgRulerequired.setRuleCode("required");
                msgRulerequired.setRuleName("必填");
                msgRulerequired.setRuleType("2");
                MdmModuleDbFiledsRuleEntity withs = new MdmModuleDbFiledsRuleEntity();
                withs.setFormName("optionRuleForm");
                withs.setRuleCode("width");
                withs.setRuleName("宽度");
                withs.setRuleType("2");
                withs.setRuleValue("200");
                msgRule.add(withs);
                msgRule.add(msgRulerequired);
                msgRule.add(msgRuletype);
                msgRule.add(msgRulerow);
                msgRule.add(msgRuleTitie);
                rule.setRuleList(msgRule);
            }
        }
        if (mdmModuleViewDetailEntities != null && mdmModuleViewDetailEntities.size() > 0) {
            for (int i = 0; i < mdmModuleViewDetailEntities.size(); i++) {
                if (viewType.equals(mdmModuleViewDetailEntities.get(i).getViewType())) {
                    //类型一致
                    MdmViewFiledVo mdmViewFiledVo = new MdmViewFiledVo();
                    mdmViewFiledVo.setId(mdmModuleViewDetailEntities.get(i).getViewFiled());
                    for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                        if (mdmModuleDbFiledsEntityList.get(i1).getId().equals(mdmModuleViewDetailEntities.get(i).getViewFiled())) {
                            for (int i2 = 0; i2 < mdmModuleDbEntityList.size(); i2++) {
                                if (mdmModuleDbEntityList.get(i2).getId().equals(mdmModuleDbFiledsEntityList.get(i1).getDbId())) {
                                    mdmViewFiledVo.setDbName(mdmModuleDbEntityList.get(i2).getDbName());
                                    mdmViewFiledVo.setDbType(mdmModuleDbEntityList.get(i2).getDbType());
                                    mdmViewFiledVo.setMdmId(mdmModuleDbEntityList.get(i2).getMdmId());
                                    mdmViewFiledVo.setDbId(mdmModuleDbEntityList.get(i2).getId());

                                    break;
                                }
                            }
                            mdmViewFiledVo.setChName(mdmModuleDbFiledsEntityList.get(i1).getChName());
                            mdmViewFiledVo.setEnName(mdmModuleDbFiledsEntityList.get(i1).getEnName());

                            mdmViewFiledVo.setFiledType(mdmModuleDbFiledsEntityList.get(i1).getFiledType());
                            //字段规则
                            if (mdmModuleDbFiledsRuleEntityList != null && mdmModuleDbFiledsRuleEntityList.size() > 0) {
                                List<MdmModuleDbFiledsRuleEntity> ruleList = new ArrayList<>();
                                for (int i2 = 0; i2 < mdmModuleDbFiledsRuleEntityList.size(); i2++) {
                                    if (mdmModuleDbFiledsEntityList.get(i1).getId().equals(mdmModuleDbFiledsRuleEntityList.get(i2).getFiledId())) {
                                        ruleList.add(mdmModuleDbFiledsRuleEntityList.get(i2));
                                    }
                                }
                                mdmViewFiledVo.setRuleList(ruleList);
                                if ("data_status".equals(mdmModuleDbFiledsEntityList.get(i1).getEnName())) {
                                    List<MdmModuleDbFiledsRuleEntity> a = new ArrayList<>();

                                    MdmModuleDbFiledsRuleEntity msgRuleTitie = new MdmModuleDbFiledsRuleEntity();
                                    msgRuleTitie.setFormName("optionRuleForm");
                                    msgRuleTitie.setRuleCode("title");
                                    msgRuleTitie.setRuleName("显示名");
                                    msgRuleTitie.setRuleType("2");
                                    msgRuleTitie.setRuleValue("数据状态");
                                    MdmModuleDbFiledsRuleEntity msgRulerow = new MdmModuleDbFiledsRuleEntity();
                                    msgRulerow.setFormName("optionRuleForm");
                                    msgRulerow.setRuleCode("row");
                                    msgRulerow.setRuleName("宽度");
                                    msgRulerow.setRuleType("2");
                                    msgRulerow.setRuleValue("12");
                                    MdmModuleDbFiledsRuleEntity msgRuletype = new MdmModuleDbFiledsRuleEntity();
                                    msgRuletype.setFormName("optionRuleForm");
                                    msgRuletype.setRuleCode("type");
                                    msgRuletype.setRuleName("数据类型");
                                    msgRuletype.setRuleType("2");
                                    msgRuletype.setRuleValue("input");
                                    MdmModuleDbFiledsRuleEntity msgRulerequired = new MdmModuleDbFiledsRuleEntity();
                                    msgRulerequired.setFormName("optionRuleForm");
                                    msgRulerequired.setRuleCode("required");
                                    msgRulerequired.setRuleName("必填");
                                    msgRulerequired.setRuleType("2");
                                    MdmModuleDbFiledsRuleEntity withs = new MdmModuleDbFiledsRuleEntity();
                                    withs.setFormName("optionRuleForm");
                                    withs.setRuleCode("width");
                                    withs.setRuleName("宽度");
                                    withs.setRuleType("2");
                                    withs.setRuleValue("200");
                                    a.add(withs);
                                    a.add(msgRulerequired);
                                    a.add(msgRuletype);
                                    a.add(msgRulerow);
                                    a.add(msgRuleTitie);
                                    mdmViewFiledVo.setRuleList(a);
                                }
                            }
                            break;
                        }
                    }
                    mdmViewFiledVos.add(mdmViewFiledVo);
                }
            }
        }
        if (mdmViewFiledVos != null && mdmViewFiledVos.size() > 0) {
            for (int i = 0; i < mdmViewFiledVos.size(); i++) {
                mdmViewFiledVos.get(i).setSorts(i);
            }
        }
        return mdmViewFiledVos;
    }
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据查询所有字段
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShowAll(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询设置各种类型字段
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        //查询模版数据源
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        List<MdmDbFiledVo> listFiled = new ArrayList<>();
        if (mdmModuleDbEntityList != null && mdmModuleDbEntityList.size() > 0) {
            MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
            mdmModuleDbFiledsEntity.setMdmId(mdmModuleEntity.getId());
            mdmModuleDbFiledsEntity.setSts("Y");
            List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
            //
            if (mdmModuleDbEntityList != null && mdmModuleDbEntityList.size() > 0) {
                for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                    if ("1".equals(mdmModuleDbEntityList.get(i).getDbType()) || "2".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                        if (mdmModuleDbFiledsEntityList != null && mdmModuleDbFiledsEntityList.size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                                if (mdmModuleDbEntityList.get(i).getId().equals(mdmModuleDbFiledsEntityList.get(i1).getDbId())) {
                                    if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                                        //1、主表
                                        listFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【主表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                    } else {
                                        //2、明细
                                        listFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【子表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询视图成功", listFiled);
    }
    //
    ///**
    // * @param mdmModuleRoleButtonEntities
    // * @return java.util.List<com.hzya.frame.mdm.entity.MdmViewButtonVo>
    // * @Author lvleigang
    // * @Description 设置返回按钮
    // * @Date 4:48 下午 2023/10/26
    // **/
    //private List<MdmViewButtonVo> setButton(List<MdmModuleRoleButtonEntity> mdmModuleRoleButtonEntities) {
    //    List<MdmViewButtonVo> mdmViewButtonVoList = new ArrayList<>();
    //    if (mdmModuleRoleButtonEntities != null && mdmModuleRoleButtonEntities.size() > 0) {
    //        for (int i = 0; i < mdmModuleRoleButtonEntities.size(); i++) {
    //            mdmViewButtonVoList.add(new MdmViewButtonVo(mdmModuleRoleButtonEntities.get(i).getButtonType(), mdmModuleRoleButtonEntities.get(i).getButtonName()));
    //        }
    //    }
    //    return mdmViewButtonVoList;
    //}
    //
    ///**
    // * @param allRecords
    // * @param checkedMeters
    // * @return java.util.List<com.hzya.frame.mdm.mdmModuleViewDetail.entity.MdmModuleViewDetailEntity>
    // * @Author lvleigang
    // * @Description 取出重复字段数据
    // * @Date 4:34 下午 2023/10/26
    // **/
    //private List<MdmModuleViewDetailEntity> getDolist(List<MdmModuleViewDetailEntity> allRecords, List<MdmModuleUserViewEntity> checkedMeters) {
    //    List<MdmModuleViewDetailEntity> meterPlan = allRecords.stream()
    //            .filter(item -> checkedMeters.stream()
    //                    .map(e -> {
    //                        if (!Objects.equals(item.getViewFiled(), e.getViewDetailId())) {
    //                            return null;
    //                        } else {
    //                            return item.getViewFiled();
    //                        }
    //                    })
    //                    .collect(Collectors.toList())
    //                    .contains(item.getViewFiled()))
    //            .collect(Collectors.toList());
    //    return meterPlan;
    //}
    //
    ///**
    // * @param mdmModuleDbEntityList           数据源
    // * @param mdmModuleDbFiledsEntityList     数据源下的字段表
    // * @param mdmModuleDbFiledsRuleEntityList 数据源字段的规则表
    // * @param queryList                       需要的字段
    // * @return java.util.List<com.hzya.frame.mdm.entity.MdmViewFiledVo>
    // * @Author lvleigang
    // * @Description 根据字段设置返回前台字段
    // * @Date 4:11 下午 2023/10/26
    // **/
    //private List<MdmViewFiledVo> setQueryList(List<MdmModuleDbEntity> mdmModuleDbEntityList, List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList, List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntityList, List<MdmModuleViewDetailEntity> queryList, boolean flag) {
    //    List<MdmViewFiledVo> mdmViewFiledVos = new ArrayList<>();
    //
    //    if (queryList != null && queryList.size() > 0) {
    //        for (int i = 0; i < queryList.size(); i++) {
    //            MdmViewFiledVo mdmViewFiledVo = new MdmViewFiledVo();
    //            mdmViewFiledVo.setId(queryList.get(i).getViewFiled());//字段id
    //            mdmViewFiledVo.setSorts(i + 2);//排序
    //            for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
    //                if (queryList.get(i).getViewFiled().equals(mdmModuleDbFiledsEntityList.get(i1).getId())) {
    //                    mdmViewFiledVo.setFiledType(mdmModuleDbFiledsEntityList.get(i1).getFiledType());//英文名
    //                    mdmViewFiledVo.setEnName(mdmModuleDbFiledsEntityList.get(i1).getEnName());//英文名
    //                    mdmViewFiledVo.setChName(mdmModuleDbFiledsEntityList.get(i1).getChName());//中文名
    //                    for (int i2 = 0; i2 < mdmModuleDbEntityList.size(); i2++) {
    //                        if (mdmModuleDbFiledsEntityList.get(i1).getDbId().equals(mdmModuleDbEntityList.get(i2).getId())) {
    //                            mdmViewFiledVo.setDbType(mdmModuleDbEntityList.get(i2).getDbType());//类型 1、主表 2、明细
    //                            mdmViewFiledVo.setMdmId(mdmModuleDbEntityList.get(i2).getMdmId());//主数据模版ID
    //                            mdmViewFiledVo.setDbId(mdmModuleDbEntityList.get(i2).getId());//模版数据库id
    //                            mdmViewFiledVo.setDbName(mdmModuleDbEntityList.get(i2).getDbName());//表名
    //
    //                            break;
    //                        }
    //                    }
    //                    break;
    //                }
    //            }
    //            //设置字段规则
    //            List<MdmModuleDbFiledsRuleEntity> ruleList = new ArrayList<>();
    //            if (mdmModuleDbFiledsRuleEntityList != null && mdmModuleDbFiledsRuleEntityList.size() > 0) {
    //                for (int i1 = 0; i1 < mdmModuleDbFiledsRuleEntityList.size(); i1++) {
    //                    if (queryList.get(i).getViewFiled().equals(mdmModuleDbFiledsRuleEntityList.get(i1).getFiledId())) {
    //                        ruleList.add(mdmModuleDbFiledsRuleEntityList.get(i1));
    //                    }
    //                }
    //            }
    //            mdmViewFiledVo.setRuleList(ruleList);
    //            mdmViewFiledVos.add(mdmViewFiledVo);
    //        }
    //    }
    //    MdmViewFiledVo mdmViewFiledVo = new MdmViewFiledVo();
    //    mdmViewFiledVo.setSorts(1);//排序
    //    mdmViewFiledVo.setFiledType("3");
    //    mdmViewFiledVo.setEnName("document_rule");//英文名
    //    mdmViewFiledVo.setChName("单据编码");//中文名
    //    mdmViewFiledVo.setDbType("1");//类型 1、主表 2、明细
    //    if (mdmModuleDbEntityList != null && mdmModuleDbEntityList.size() > 0) {
    //        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
    //            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
    //                mdmViewFiledVo.setDbName(mdmModuleDbEntityList.get(i).getDbName());//表名
    //            }
    //        }
    //    }
    //    List<MdmModuleDbFiledsRuleEntity> msgRule = new ArrayList<>();
    //    MdmModuleDbFiledsRuleEntity msgRuleTitie = new MdmModuleDbFiledsRuleEntity();
    //    msgRuleTitie.setFormName("optionRuleForm");
    //    msgRuleTitie.setRuleCode("title");
    //    msgRuleTitie.setRuleName("显示名");
    //    msgRuleTitie.setRuleType("2");
    //    msgRuleTitie.setRuleValue("单据编码");
    //    MdmModuleDbFiledsRuleEntity msgRulerow = new MdmModuleDbFiledsRuleEntity();
    //    msgRulerow.setFormName("optionRuleForm");
    //    msgRulerow.setRuleCode("row");
    //    msgRulerow.setRuleName("宽度");
    //    msgRulerow.setRuleType("2");
    //    msgRulerow.setRuleValue("12");
    //    MdmModuleDbFiledsRuleEntity msgRuletype = new MdmModuleDbFiledsRuleEntity();
    //    msgRuletype.setFormName("optionRuleForm");
    //    msgRuletype.setRuleCode("type");
    //    msgRuletype.setRuleName("数据类型");
    //    msgRuletype.setRuleType("2");
    //    msgRuletype.setRuleValue("input");
    //    MdmModuleDbFiledsRuleEntity msgRulerequired = new MdmModuleDbFiledsRuleEntity();
    //    msgRulerequired.setFormName("optionRuleForm");
    //    msgRulerequired.setRuleCode("required");
    //    msgRulerequired.setRuleName("必填");
    //    msgRulerequired.setRuleType("2");
    //    MdmModuleDbFiledsRuleEntity withs = new MdmModuleDbFiledsRuleEntity();
    //    withs.setFormName("optionRuleForm");
    //    withs.setRuleCode("width");
    //    withs.setRuleName("宽度");
    //    withs.setRuleType("2");
    //    withs.setRuleValue("200");
    //    msgRule.add(withs);
    //    msgRule.add(msgRulerequired);
    //    msgRule.add(msgRuletype);
    //    msgRule.add(msgRulerow);
    //    msgRule.add(msgRuleTitie);
    //    mdmViewFiledVo.setRuleList(msgRule);
    //    mdmViewFiledVos.add(0, mdmViewFiledVo);
    //
    //    //添加分发数量
    //    if (flag) {
    //        addFfsl(mdmModuleDbEntityList, mdmViewFiledVos);
    //    }
    //    return mdmViewFiledVos;
    //}
    //
    //private void addFfsl(List<MdmModuleDbEntity> mdmModuleDbEntityList, List<MdmViewFiledVo> mdmViewFiledVos) {
    //
    //    MdmViewFiledVo mdmViewFiledVo = new MdmViewFiledVo();
    //    mdmViewFiledVo.setSorts(1);//排序
    //    mdmViewFiledVo.setFiledType("1");
    //    mdmViewFiledVo.setEnName("qwqwasdasdzxasda");//英文名
    //    mdmViewFiledVo.setChName("分发数量");//中文名
    //    mdmViewFiledVo.setDbType("1");//类型 1、主表 2、明细
    //    if (mdmModuleDbEntityList != null && mdmModuleDbEntityList.size() > 0) {
    //        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
    //            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
    //                mdmViewFiledVo.setDbName(mdmModuleDbEntityList.get(i).getDbName());//表名
    //            }
    //        }
    //    }
    //    List<MdmModuleDbFiledsRuleEntity> msgRule = new ArrayList<>();
    //    MdmModuleDbFiledsRuleEntity msgRuleTitie = new MdmModuleDbFiledsRuleEntity();
    //    msgRuleTitie.setFormName("optionRuleForm");
    //    msgRuleTitie.setRuleCode("title");
    //    msgRuleTitie.setRuleName("显示名");
    //    msgRuleTitie.setRuleType("2");
    //    msgRuleTitie.setRuleValue("分发数量");
    //    MdmModuleDbFiledsRuleEntity msgRulerow = new MdmModuleDbFiledsRuleEntity();
    //    msgRulerow.setFormName("optionRuleForm");
    //    msgRulerow.setRuleCode("row");
    //    msgRulerow.setRuleName("宽度");
    //    msgRulerow.setRuleType("2");
    //    msgRulerow.setRuleValue("6");
    //    MdmModuleDbFiledsRuleEntity msgRuletype = new MdmModuleDbFiledsRuleEntity();
    //    msgRuletype.setFormName("optionRuleForm");
    //    msgRuletype.setRuleCode("type");
    //    msgRuletype.setRuleName("数据类型");
    //    msgRuletype.setRuleType("2");
    //    msgRuletype.setRuleValue("input");
    //    MdmModuleDbFiledsRuleEntity msgRulerequired = new MdmModuleDbFiledsRuleEntity();
    //    msgRulerequired.setFormName("optionRuleForm");
    //    msgRulerequired.setRuleCode("required");
    //    msgRulerequired.setRuleName("必填");
    //    msgRulerequired.setRuleType("2");
    //    msgRule.add(msgRulerequired);
    //    msgRule.add(msgRuletype);
    //    msgRule.add(msgRulerow);
    //    msgRule.add(msgRuleTitie);
    //    mdmViewFiledVo.setRuleList(msgRule);
    //    mdmViewFiledVos.add(mdmViewFiledVo);
    //}
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据列表显示 业务数据
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShowData(JSONObject object) {
        MdmQuery entity = getData("jsonStr", object, MdmQuery.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmCode() != null && !"".equals(entity.getMdmCode())) {
            MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
            if (mdmModuleEntity == null || mdmModuleEntity.getId() == null) {
                return BaseResult.getFailureMessageEntity("主数据设置错误");
            }
            MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
            mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
            mdmModuleDbEntity.setSts("Y");
            List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
            MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
            mdmModuleDbFiledsEntity.setMdmId(mdmModuleEntity.getId());
            mdmModuleDbFiledsEntity.setSts("Y");
            List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
            if (mdmModuleDbEntities == null || mdmModuleDbEntities.size() == 0) {
                return BaseResult.getFailureMessageEntity("主数据设置错误");
            }
            if (mdmModuleDbFiledsEntities == null || mdmModuleDbFiledsEntities.size() == 0) {
                return BaseResult.getFailureMessageEntity("主数据设置错误");
            }
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if ("1".equals(mdmModuleDbEntities.get(i).getDbType())) {
                    List<String> returnField = new ArrayList<>();
                    for (int i1 = 0; i1 < mdmModuleDbFiledsEntities.size(); i1++) {
                        if (mdmModuleDbEntities.get(i).getId().equals(mdmModuleDbFiledsEntities.get(i1).getDbId())) {
                            returnField.add(mdmModuleDbFiledsEntities.get(i1).getEnName());
                        }
                    }
                    entity.setReturnField(returnField);
                    entity.setTableName(mdmModuleDbEntities.get(i).getDbName());
                    break;
                }
            }
        }
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<HashMap<String, Object>> businessResult = mdmModuleDao.queryMdmShowData(entity);
        //强制把数据库字段转换小写
        if (null != businessResult && businessResult.size() > 0) {
            convertKeysToLowerCase(businessResult);
        }
        PageInfo pageInfo = new PageInfo(businessResult);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    //强制转换数据库字段为小写,出问题在说
    public void convertKeysToLowerCase(List<HashMap<String, Object>> list) {
        for (HashMap<String, Object> map : list) {
            HashMap<String, Object> lowerCaseMap = new HashMap<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                lowerCaseMap.put(entry.getKey().toLowerCase(), entry.getValue());
            }
            map.clear(); // 清空原来的Map，避免重复存储
            map.putAll(lowerCaseMap); // 将转换后的Map全部放回原位
        }
    }

    //强制转换数据库字段为小写,出问题在说
    public void convertKeysToLowerCase(HashMap<String, Object> list) {
        HashMap<String, Object> lowerCaseMap = new HashMap<>();
        for (Map.Entry<String, Object> entry : list.entrySet()) {
            lowerCaseMap.put(entry.getKey().toLowerCase(), entry.getValue());
        }
        list.clear(); // 清空原来的Map，避免重复存储
        list.putAll(lowerCaseMap); // 将转换后的Map全部放回原位
    }
    //
    ///**
    // * @param object
    // * @return com.hzya.frame.web.entity.JsonResultEntity
    // * @Author lvleigang
    // * @Description 主数据列表显示 业务数据
    // * @Date 9:40 上午 2023/10/18
    // **/
    //@Override
    //public JsonResultEntity queryMdmShowDistribute(JSONObject object) {
    //    MdmDto entity = getData("jsonStr", object, MdmDto.class);
    //    if (entity == null) {
    //        return BaseResult.getFailureMessageEntity("参数不允许为空");
    //    }
    //    if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
    //        return BaseResult.getFailureMessageEntity("系统错误");
    //    }
    //    if (entity.getId() == null || "".equals(entity.getId())) {
    //        return BaseResult.getFailureMessageEntity("系统错误");
    //    }
    //    //查询模版
    //    MdmModuleEntity mdmModuleEntity = new MdmModuleEntity();
    //    mdmModuleEntity.setMdmCode(entity.getMdmCode());
    //    mdmModuleEntity.setSts("Y");
    //    List<MdmModuleEntity> mdmModuleEntities = mdmModuleDao.queryBase(mdmModuleEntity);
    //    if (mdmModuleEntities == null || mdmModuleEntities.size() != 1) {
    //        return BaseResult.getFailureMessageEntity("系统错误");
    //    }
    //
    //    //查询数据源主表
    //    MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
    //    mdmModuleDbEntity.setMdmId(mdmModuleEntities.get(0).getId());
    //    mdmModuleDbEntity.setDbType("1");
    //    mdmModuleDbEntity.setSts("Y");
    //    List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmModuleDbDao.queryBase(mdmModuleDbEntity);
    //    if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
    //        return BaseResult.getFailureMessageEntity("系统错误");
    //    }
    //    mdmModuleDbEntity = mdmModuleDbEntityList.get(0);
    //    Map<String, Object> queryData = new HashMap<>();
    //    queryData.put("tableName", mdmModuleDbEntity.getDbName() + "_distribute");//表名
    //    queryData.put("id", entity.getId());//字段
    //    List<MdmDistributeVo> datas = mdmModuleDbDao.getServiceByDistributeIdNoCase(queryData);
    //    List<MdmDistributeVo> returnData = new ArrayList<>();
    //
    //    //查询分发表
    //    MdmModuleDistributeEntity mdmModuleDistributeEntity = new MdmModuleDistributeEntity();
    //    mdmModuleDistributeEntity.setMdmId(mdmModuleEntities.get(0).getId());
    //    mdmModuleDistributeEntity.setSts("Y");
    //    List<MdmModuleDistributeEntity> mdmModuleDistributeEntities = mdmModuleDistributeDao.queryBase(mdmModuleDistributeEntity);
    //    if (datas != null && datas.size() > 0) {
    //        if (mdmModuleDistributeEntities != null && mdmModuleDistributeEntities.size() > 0) {
    //            for (int i = 0; i < mdmModuleDistributeEntities.size(); i++) {
    //                boolean flag = false;
    //                for (int i1 = 0; i1 < datas.size(); i1++) {
    //                    if (datas.get(i1).getAppId().equals(mdmModuleDistributeEntities.get(i).getAppId())) {
    //                        flag = true;
    //                        break;
    //                    }
    //                }
    //                if (!flag) {
    //                    MdmDistributeVo mdmDistributeVo = new MdmDistributeVo();
    //                    mdmDistributeVo.setAppId(mdmModuleDistributeEntities.get(i).getAppId());
    //                    mdmDistributeVo.setStatus("4");
    //                    mdmDistributeVo.setMsg("未发送");
    //                    returnData.add(mdmDistributeVo);
    //                }
    //            }
    //        }
    //        returnData.addAll(datas);
    //    } else {
    //        if (mdmModuleDistributeEntities != null && mdmModuleDistributeEntities.size() > 0) {
    //            for (int i = 0; i < mdmModuleDistributeEntities.size(); i++) {
    //                MdmDistributeVo mdmDistributeVo = new MdmDistributeVo();
    //                mdmDistributeVo.setAppId(mdmModuleDistributeEntities.get(i).getAppId());
    //                mdmDistributeVo.setStatus("4");
    //                mdmDistributeVo.setMsg("未发送");
    //                returnData.add(mdmDistributeVo);
    //            }
    //        }
    //    }
    //    return BaseResult.getSuccessMessageEntity("查询数据成功", returnData);
    //}
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询数据（list 或者 分页）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity queryTemplateData(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        //校验是否有servecr传参
        if (!checkStr(entity.getTableName())) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkStr(entity.getValue())) {
            return BaseResult.getFailureMessageEntity("请先传递存储字段");
        }
        if (!checkStr(entity.getLabel())) {
            return BaseResult.getFailureMessageEntity("请先传递显示字段");
        }
        if (entity.getPageNum() != null && entity.getPageSize() != null) {
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<HashMap<String, Object>> mapList = mdmModuleDao.querySelectData(entity);
            convertKeysToLowerCase(mapList);
            PageInfo pageInfo = new PageInfo(mapList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        } else {
            List<HashMap<String, Object>> mapList = mdmModuleDao.querySelectData(entity);
            convertKeysToLowerCase(mapList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", mapList);
        }
    }
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 业务数据树形结构，数据查询
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShowTreeData(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        //if (entity.getTableName() == null || "".equals(entity.getTableName())) {
        //    return BaseResult.getFailureMessageEntity("系统错误");
        //}
        if (entity.getLabel() == null || "".equals(entity.getLabel())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getUpId() == null || "".equals(entity.getUpId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmCode() != null && !"".equals(entity.getMdmCode())) {
            //查询模版
            MdmModuleEntity mdmModuleEntities = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
            if (mdmModuleEntities == null) {
                return BaseResult.getFailureMessageEntity("系统错误");
            }
            //查询数据源主表
            MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
            mdmModuleDbEntity.setMdmId(mdmModuleEntities.getId());
            mdmModuleDbEntity.setSts("Y");
            List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
            if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
                return BaseResult.getFailureMessageEntity("系统错误");
            }
            for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    mdmModuleDbEntity = mdmModuleDbEntityList.get(i);
                    break;
                }
            }

            entity.setTableName(mdmModuleDbEntity.getDbName());
        }

        if (checkStr(entity.getId())) {
            if (!checkStr(entity.getValue())) {
                return BaseResult.getFailureMessageEntity("请先传递存储字段");
            }
            if (entity.getId().contains(",")) {
                String[] strArray = entity.getId().split(",");  // 使用逗号和空格作为分隔符
                entity.setIds(strArray);
                List<HashMap<String, Object>> mapList = mdmModuleDao.queryTemplateDataMore(entity);
                if (mapList == null || mapList.size() == 0) {
                    List<HashMap<String, Object>> returnList = new ArrayList<>();
                    return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
                }
                convertKeysToLowerCase(mapList);
                List<HashMap<String, Object>> returnList = gettochilder(strArray, 0, mapList);
                return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
            } else {
                HashMap<String, Object> mapList = mdmModuleDao.queryTemplateDataOne(entity);
                if (mapList == null || mapList.size() == 0) {
                    List<HashMap<String, Object>> returnList = new ArrayList<>();
                    return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
                }
                HashMap<String, Object> lowerCaseMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : mapList.entrySet()) {
                    lowerCaseMap.put(entry.getKey().toLowerCase(), entry.getValue());
                }
                List<HashMap<String, Object>> returnList = new ArrayList<>();
                returnList.add(lowerCaseMap);
                return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
            }
        } else {
            List<HashMap<String, Object>> mapList = mdmModuleDao.queryDataAll(entity);
            List<HashMap<String, Object>> returnList = new ArrayList<>();

            if (mapList != null && mapList.size() > 0) {
                convertKeysToLowerCase(mapList);
                for (int i = 0; i < mapList.size(); i++) {
                    //默认为根
                    if (mapList.get(i).get(entity.getUpId()) == null || "".equals(mapList.get(i).get(entity.getUpId()))) {
                        List<HashMap<String, Object>> children = getchilder(mapList.get(i).get("qsdfg").toString(), mapList, entity);
                        HashMap<String, Object> re = new HashMap<>();
                        re.put("id", mapList.get(i).get("qsdfg"));
                        re.put("label", mapList.get(i).get(entity.getLabel()));
                        re.put("upID", mapList.get(i).get(entity.getUpId()));
                        if (children != null && children.size() > 0) {
                            re.put("children", children);
                        }
                        returnList.add(re);
                    }
                }
            }
            return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
        }
    }

    //
    private List<HashMap<String, Object>> gettochilder(String[] strArray, int i, List<HashMap<String, Object>> mapList) {
        List<HashMap<String, Object>> maps = new ArrayList<>();
        for (int i1 = 0; i1 < mapList.size(); i1++) {
            if (strArray[i].equals(mapList.get(i1).get("id").toString())) {
                if (strArray.length >= (i + 2)) {
                    mapList.get(i1).put("children", gettochilder(strArray, i + 1, mapList));
                }
                maps.add(mapList.get(i1));
                break;
            }
        }
        return maps;
    }

    //
    //private HashMap<String, Object> getFathers(List<HashMap<String, Object>> mapListAll, HashMap<String, Object> mapList, MdmDto entity) {
    //    if (mapList.get("upID") == null) {
    //        return mapList;
    //    }
    //    HashMap<String, Object> retList = new HashMap<>();
    //    if (mapListAll != null && mapListAll.size() > 0) {
    //        for (int i = 0; i < mapListAll.size(); i++) {
    //            //等于上级id的时候
    //            if (mapListAll.get(i).get("qsdfg").equals(mapList.get("upID"))) {
    //                retList.put("id", mapListAll.get(i).get("qsdfg"));
    //                retList.put("label", mapListAll.get(i).get(entity.getLabel()));
    //                retList.put("upID", mapListAll.get(i).get(entity.getUpId()));
    //                List<HashMap<String, Object>> returnList = new ArrayList<>();
    //                returnList.add(mapList);
    //                retList.put("children", returnList);
    //                break;
    //            }
    //        }
    //    }
    //    HashMap<String, Object> a = getFathers(mapListAll, retList, entity);
    //    return a;
    //}
    //
    private List<HashMap<String, Object>> getchilder(String stringObjectHashMap, List<HashMap<String, Object>> mapList, MdmDto entity) {
        List<HashMap<String, Object>> returnList = new ArrayList<>();
        if (mapList != null && mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                //等于上级id的时候
                if (stringObjectHashMap.equals(mapList.get(i).get(entity.getUpId()))) {
                    List<HashMap<String, Object>> children = getchilder(mapList.get(i).get(entity.getUpId()) + "," + mapList.get(i).get("qsdfg"), mapList, entity);
                    HashMap<String, Object> re = new HashMap<>();
                    re.put("id", mapList.get(i).get("qsdfg"));
                    re.put("label", mapList.get(i).get(entity.getLabel()));
                    re.put("upID", mapList.get(i).get(entity.getUpId()));
                    if (children != null && children.size() > 0) {
                        re.put("children", children);
                    }
                    returnList.add(re);
                }
            }
        }
        return returnList;
    }
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据详情 区分类型 新增、修改、查看
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShowDetails(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getShowType() == null || "".equals(entity.getShowType())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询模版
        //List<MdmModuleEntity> mdmModuleEntities = mdmModuleDao.queryBase(mdmModuleEntity);
        MdmModuleEntity mdmModuleEntities = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        if (mdmModuleEntities == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmDetailViewVo mdmViewVo = new MdmDetailViewVo();
        mdmViewVo.setMdmModuleEntity(mdmModuleEntities);

        //查询数据源主表
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntities.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询数据源表下面的字段
        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setMdmId(mdmModuleEntities.getId());
        mdmModuleDbFiledsEntity.setSts("Y");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
        if (mdmModuleDbFiledsEntityList == null || mdmModuleDbFiledsEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询字段下的规则
        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setMdmId(mdmModuleEntities.getId());
        mdmModuleDbFiledsRuleEntity.setSts("Y");
        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntityList = mdmModuleDbFiledsRuleDao.queryBase(mdmModuleDbFiledsRuleEntity);
        if (mdmModuleDbFiledsRuleEntityList == null || mdmModuleDbFiledsRuleEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
        mdmModuleViewDetailEntity.setMdmId(mdmModuleEntities.getId());
        mdmModuleViewDetailEntity.setSts("Y");
        mdmModuleViewDetailEntity.setViewType(entity.getShowType());//设置显示字段类型
        List<MdmModuleViewDetailEntity> queryAll = mdmModuleViewDetailDao.queryBase(mdmModuleViewDetailEntity);
        List<MdmModuleViewDetailEntity> mdmModuleViewDetailEntityList = new ArrayList<>();
        if (queryAll != null && queryAll.size() > 0) {
            for (int i = 0; i < queryAll.size(); i++) {
                if (entity.getShowType().equals(queryAll.get(i).getViewType())) {
                    mdmModuleViewDetailEntityList.add(queryAll.get(i));
                }
            }
        }
        //组装数据
        assembleMdmShowDetails(mdmViewVo, mdmModuleDbEntityList, mdmModuleDbFiledsEntityList, mdmModuleDbFiledsRuleEntityList, mdmModuleViewDetailEntityList, entity.getShowType());
        return BaseResult.getSuccessMessageEntity("获取字段成功", mdmViewVo);
    }

    /**
     * @param mdmViewVo                       返回对象
     * @param mdmModuleDbEntityList           数据源表
     * @param mdmModuleDbFiledsEntityList     数据源字段
     * @param mdmModuleDbFiledsRuleEntityList 数据源字段规则
     * @param mdmModuleViewDetailEntityList   数据源显示字段
     * @return void
     * @Author lvleigang
     * @Description 组装显示字段
     * @Date 9:10 上午 2023/11/6
     **/
    private void assembleMdmShowDetails(MdmDetailViewVo mdmViewVo, List<MdmModuleDbEntity> mdmModuleDbEntityList, List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList, List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntityList, List<MdmModuleViewDetailEntity> mdmModuleViewDetailEntityList, String showType) {
        //主数据主表
        MdmModuleDbEntity mainMdmModuleDb = new MdmModuleDbEntity();
        List<MdmModuleDbFiledsEntity> mainMdmModuleDbFileds = new ArrayList<>();
        //主数据子表
        List<MdmModuleDbEntity> sublistMdmModuleDb = new ArrayList<>();

        if (mdmModuleViewDetailEntityList != null && mdmModuleViewDetailEntityList.size() > 0) {
            //先循环数据源表，设置出数据字段
            for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    //1、主表 2、明细
                    mainMdmModuleDb = mdmModuleDbEntityList.get(i);
                    //循环字段表
                    for (int b = 0; b < mdmModuleViewDetailEntityList.size(); b++) {
                        if (mdmModuleDbFiledsEntityList != null && mdmModuleDbFiledsEntityList.size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                                if (mdmModuleDbEntityList.get(i).getId().equals(mdmModuleDbFiledsEntityList.get(i1).getDbId())) {
                                    if (mdmModuleViewDetailEntityList.get(b).getViewFiled().equals(mdmModuleDbFiledsEntityList.get(i1).getId())) {
                                        mainMdmModuleDbFileds.add(mdmModuleDbFiledsEntityList.get(i1));
                                        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRules = new ArrayList<>();
                                        //循环字段属性
                                        if (mdmModuleDbFiledsRuleEntityList != null && mdmModuleDbFiledsRuleEntityList.size() > 0) {
                                            for (int i2 = 0; i2 < mdmModuleDbFiledsRuleEntityList.size(); i2++) {
                                                if (mdmModuleDbFiledsEntityList.get(i1).getDbId().equals(mdmModuleDbFiledsRuleEntityList.get(i2).getDbId())
                                                        && mdmModuleDbFiledsEntityList.get(i1).getId().equals(mdmModuleDbFiledsRuleEntityList.get(i2).getFiledId())) {
                                                    mdmModuleDbFiledsRules.add(mdmModuleDbFiledsRuleEntityList.get(i2));
                                                }
                                            }
                                        }
                                        mdmModuleDbFiledsEntityList.get(i1).setMdmModuleDbFiledsRules(mdmModuleDbFiledsRules);
                                    }

                                }

                            }
                        }
                    }
                } else if ("2".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    //2、明细
                    sublistMdmModuleDb.add(mdmModuleDbEntityList.get(i));

                    //循环字段表
                    if (mdmModuleDbFiledsEntityList != null && mdmModuleDbFiledsEntityList.size() > 0) {
                        List<MdmModuleDbFiledsEntity> sublistMdmModuleDbFileds = new ArrayList<>();
                        for (int b = 0; b < mdmModuleViewDetailEntityList.size(); b++) {
                            for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                                if (mdmModuleDbEntityList.get(i).getId().equals(mdmModuleDbFiledsEntityList.get(i1).getDbId())) {

                                    if (mdmModuleViewDetailEntityList.get(b).getViewFiled().equals(mdmModuleDbFiledsEntityList.get(i1).getId())) {
                                        sublistMdmModuleDbFileds.add(mdmModuleDbFiledsEntityList.get(i1));
                                        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRules = new ArrayList<>();
                                        //循环字段属性
                                        if (mdmModuleDbFiledsRuleEntityList != null && mdmModuleDbFiledsRuleEntityList.size() > 0) {
                                            for (int i2 = 0; i2 < mdmModuleDbFiledsRuleEntityList.size(); i2++) {
                                                if (mdmModuleDbFiledsEntityList.get(i1).getDbId().equals(mdmModuleDbFiledsRuleEntityList.get(i2).getDbId())
                                                        && mdmModuleDbFiledsEntityList.get(i1).getId().equals(mdmModuleDbFiledsRuleEntityList.get(i2).getFiledId())) {
                                                    mdmModuleDbFiledsRules.add(mdmModuleDbFiledsRuleEntityList.get(i2));
                                                }
                                            }
                                        }
                                        mdmModuleDbFiledsEntityList.get(i1).setMdmModuleDbFiledsRules(mdmModuleDbFiledsRules);
                                    }
                                }
                            }
                        }
                        mdmModuleDbEntityList.get(i).setSublistMdmModuleDbFileds(sublistMdmModuleDbFileds);
                    }
                }
            }
            mainMdmModuleDb.setSublistMdmModuleDbFileds(mainMdmModuleDbFileds);
            mdmViewVo.setMainMdmModuleDb(mainMdmModuleDb);
            //主数据子表
            List<MdmModuleDbEntity> mdmModuleDb = new ArrayList<>();
            if (sublistMdmModuleDb != null && sublistMdmModuleDb.size() > 0) {
                for (int i = 0; i < sublistMdmModuleDb.size(); i++) {
                    if (sublistMdmModuleDb.get(i).getSublistMdmModuleDbFileds() != null && sublistMdmModuleDb.get(i).getSublistMdmModuleDbFileds().size() > 0) {
                        mdmModuleDb.add(sublistMdmModuleDb.get(i));
                    }
                }
            }
            //if ("5".equals(showType)) {
            //    MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
            //    mdmModuleDbEntity.setDbName(mainMdmModuleDb.getDbName() + "_distribute");
            //    mdmModuleDbEntity.setRemark(mainMdmModuleDb.getRemark() + "分发表");
            //    mdmModuleDbEntity.setDbType("2");
            //    List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList1 = getDistributeRole();
            //    mdmModuleDbEntity.setSublistMdmModuleDbFileds(mdmModuleDbFiledsEntityList1);
            //    mdmModuleDb.add(mdmModuleDbEntity);
            //}
            mdmViewVo.setSublistMdmModuleDb(mdmModuleDb);

        } else {
            mdmViewVo.setMainMdmModuleDb(mainMdmModuleDb);
            mdmViewVo.setSublistMdmModuleDb(sublistMdmModuleDb);
        }
    }
    //
    //private List<MdmModuleDbFiledsEntity> getDistributeRole() {
    //    List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = new ArrayList<>();
    //    //app
    //    MdmModuleDbFiledsEntity appFiled = new MdmModuleDbFiledsEntity();
    //    appFiled.setChName("应用");
    //    appFiled.setEnName("app_id");
    //    appFiled.setFiledLength("50");
    //    appFiled.setFiledType("3");
    //    List<MdmModuleDbFiledsRuleEntity> appRule = new ArrayList<>();
    //    MdmModuleDbFiledsRuleEntity appRuleTitie = new MdmModuleDbFiledsRuleEntity();
    //    appRuleTitie.setFormName("optionRuleForm");
    //    appRuleTitie.setRuleCode("title");
    //    appRuleTitie.setRuleName("显示名");
    //    appRuleTitie.setRuleType("2");
    //    appRuleTitie.setRuleValue("应用");
    //    MdmModuleDbFiledsRuleEntity appRulerow = new MdmModuleDbFiledsRuleEntity();
    //    appRulerow.setFormName("optionRuleForm");
    //    appRulerow.setRuleCode("row");
    //    appRulerow.setRuleName("宽度");
    //    appRulerow.setRuleType("2");
    //    appRulerow.setRuleValue("6");
    //    MdmModuleDbFiledsRuleEntity appRuletype = new MdmModuleDbFiledsRuleEntity();
    //    appRuletype.setFormName("optionRuleForm");
    //    appRuletype.setRuleCode("type");
    //    appRuletype.setRuleName("数据类型");
    //    appRuletype.setRuleType("2");
    //    appRuletype.setRuleValue("select");
    //    MdmModuleDbFiledsRuleEntity appRulerequired = new MdmModuleDbFiledsRuleEntity();
    //    appRulerequired.setFormName("optionRuleForm");
    //    appRulerequired.setRuleCode("required");
    //    appRulerequired.setRuleName("必填");
    //    appRulerequired.setRuleType("2");
    //    MdmModuleDbFiledsRuleEntity appRuleservice = new MdmModuleDbFiledsRuleEntity();
    //    appRuleservice.setFormName("typeOptionRuleForm");
    //    appRuleservice.setRuleCode("service");
    //    appRuleservice.setRuleName("服务名称");
    //    appRuleservice.setRuleType("2");
    //    appRuleservice.setRuleValue("sys_application");
    //    MdmModuleDbFiledsRuleEntity appRulelabel = new MdmModuleDbFiledsRuleEntity();
    //    appRulelabel.setFormName("typeOptionRuleForm");
    //    appRulelabel.setRuleCode("label");
    //    appRulelabel.setRuleName("label");
    //    appRulelabel.setRuleType("2");
    //    appRulelabel.setRuleValue("name");
    //    MdmModuleDbFiledsRuleEntity appRulevalue = new MdmModuleDbFiledsRuleEntity();
    //    appRulevalue.setFormName("typeOptionRuleForm");
    //    appRulevalue.setRuleCode("value");
    //    appRulevalue.setRuleName("value");
    //    appRulevalue.setRuleType("2");
    //    appRulevalue.setRuleValue("id");
    //    appRule.add(appRulevalue);
    //    appRule.add(appRulelabel);
    //    appRule.add(appRuleservice);
    //    appRule.add(appRulerequired);
    //    appRule.add(appRuletype);
    //    appRule.add(appRulerow);
    //    appRule.add(appRuleTitie);
    //    appFiled.setMdmModuleDbFiledsRules(appRule);
    //    mdmModuleDbFiledsEntityList.add(appFiled);
    //
    //
    //    //status
    //    MdmModuleDbFiledsEntity statusFiled = new MdmModuleDbFiledsEntity();
    //    statusFiled.setChName("状态");
    //    statusFiled.setEnName("status");
    //    statusFiled.setFiledLength("50");
    //    statusFiled.setFiledType("3");
    //    List<MdmModuleDbFiledsRuleEntity> statusRule = new ArrayList<>();
    //    MdmModuleDbFiledsRuleEntity statusRuleTitie = new MdmModuleDbFiledsRuleEntity();
    //    statusRuleTitie.setFormName("optionRuleForm");
    //    statusRuleTitie.setRuleCode("title");
    //    statusRuleTitie.setRuleName("显示名");
    //    statusRuleTitie.setRuleType("2");
    //    statusRuleTitie.setRuleValue("状态");
    //    MdmModuleDbFiledsRuleEntity statusRulerow = new MdmModuleDbFiledsRuleEntity();
    //    statusRulerow.setFormName("optionRuleForm");
    //    statusRulerow.setRuleCode("row");
    //    statusRulerow.setRuleName("宽度");
    //    statusRulerow.setRuleType("2");
    //    statusRulerow.setRuleValue("6");
    //    MdmModuleDbFiledsRuleEntity statusRuletype = new MdmModuleDbFiledsRuleEntity();
    //    statusRuletype.setFormName("optionRuleForm");
    //    statusRuletype.setRuleCode("type");
    //    statusRuletype.setRuleName("数据类型");
    //    statusRuletype.setRuleType("2");
    //    statusRuletype.setRuleValue("input");
    //    MdmModuleDbFiledsRuleEntity statusRulerequired = new MdmModuleDbFiledsRuleEntity();
    //    statusRulerequired.setFormName("optionRuleForm");
    //    statusRulerequired.setRuleCode("required");
    //    statusRulerequired.setRuleName("必填");
    //    statusRulerequired.setRuleType("2");
    //    statusRule.add(statusRulerequired);
    //    statusRule.add(statusRuletype);
    //    statusRule.add(statusRulerow);
    //    statusRule.add(statusRuleTitie);
    //    statusFiled.setMdmModuleDbFiledsRules(statusRule);
    //    mdmModuleDbFiledsEntityList.add(statusFiled);
    //
    //
    //    //msg
    //    MdmModuleDbFiledsEntity msgFiled = new MdmModuleDbFiledsEntity();
    //    msgFiled.setChName("描述");
    //    msgFiled.setEnName("msg");
    //    msgFiled.setFiledLength("200");
    //    msgFiled.setFiledType("3");
    //    List<MdmModuleDbFiledsRuleEntity> msgRule = new ArrayList<>();
    //    MdmModuleDbFiledsRuleEntity msgRuleTitie = new MdmModuleDbFiledsRuleEntity();
    //    msgRuleTitie.setFormName("optionRuleForm");
    //    msgRuleTitie.setRuleCode("title");
    //    msgRuleTitie.setRuleName("显示名");
    //    msgRuleTitie.setRuleType("2");
    //    msgRuleTitie.setRuleValue("描述");
    //    MdmModuleDbFiledsRuleEntity msgRulerow = new MdmModuleDbFiledsRuleEntity();
    //    msgRulerow.setFormName("optionRuleForm");
    //    msgRulerow.setRuleCode("row");
    //    msgRulerow.setRuleName("宽度");
    //    msgRulerow.setRuleType("2");
    //    msgRulerow.setRuleValue("6");
    //    MdmModuleDbFiledsRuleEntity msgRuletype = new MdmModuleDbFiledsRuleEntity();
    //    msgRuletype.setFormName("optionRuleForm");
    //    msgRuletype.setRuleCode("type");
    //    msgRuletype.setRuleName("数据类型");
    //    msgRuletype.setRuleType("2");
    //    msgRuletype.setRuleValue("input");
    //    MdmModuleDbFiledsRuleEntity msgRulerequired = new MdmModuleDbFiledsRuleEntity();
    //    msgRulerequired.setFormName("optionRuleForm");
    //    msgRulerequired.setRuleCode("required");
    //    msgRulerequired.setRuleName("必填");
    //    msgRulerequired.setRuleType("2");
    //    msgRule.add(msgRulerequired);
    //    msgRule.add(msgRuletype);
    //    msgRule.add(msgRulerow);
    //    msgRule.add(msgRuleTitie);
    //    msgFiled.setMdmModuleDbFiledsRules(msgRule);
    //    mdmModuleDbFiledsEntityList.add(msgFiled);
    //    return mdmModuleDbFiledsEntityList;
    //}
    //
    ///**
    // * @param object
    // * @return com.hzya.frame.web.entity.JsonResultEntity
    // * @Author lvleigang
    // * @Description 查询数据（list 或者 分页）
    // * @Date 1:33 下午 2023/7/12
    // **/
    //@Override
    //public JsonResultEntity queryMdmOptionData(JSONObject object) {
    //    ModuleDto entity = getData("jsonStr", object, ModuleDto.class);
    //    //校验是否有servecr传参
    //    if (!checkStr(entity.getService())) {
    //        return BaseResult.getFailureMessageEntity("请先传递服务名");
    //    }
    //    if (!checkStr(entity.getValue())) {
    //        return BaseResult.getFailureMessageEntity("请先传递存储字段");
    //    }
    //    if (!checkStr(entity.getLabel())) {
    //        return BaseResult.getFailureMessageEntity("请先传递显示字段");
    //    }
    //    //查询数据源主表
    //    MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
    //    mdmModuleDbEntity.setId(entity.getId());
    //    mdmModuleDbEntity.setSts("Y");
    //    List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmModuleDbDao.queryBase(mdmModuleDbEntity);
    //    if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
    //        return BaseResult.getFailureMessageEntity("系统错误");
    //    }
    //    entity.setTableName(mdmModuleDbEntityList.get(0).getDbName());
    //    if (entity.getPageNum() != null && entity.getPageSize() != null) {
    //        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
    //        List<HashMap<String, Object>> mapList = mdmModuleDao.queryTemplateData(entity);
    //        PageInfo pageInfo = new PageInfo(mapList);
    //        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    //    } else {
    //        List<HashMap<String, Object>> mapList = mdmModuleDao.queryTemplateData(entity);
    //        return BaseResult.getSuccessMessageEntity("查询数据成功", mapList);
    //    }
    //}
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据详情数据 区分类型 新增、修改、查看
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShowDetailsData(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询模版

        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        if (mdmModuleEntity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询数据源主表
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //String tablename = null;
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType()) || "2".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                //查询数据
                Map<String, Object> queryData = new HashMap<>();
                queryData.put("tableName", mdmModuleDbEntityList.get(i).getDbName());//表名
                if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    queryData.put("detailFlag", false);//是否明细
                    queryData.put("id", entity.getId());//字段
                    HashMap<String, Object> datas = mdmModuleDbDao.getServiceDataById(queryData);
                    convertKeysToLowerCase(datas);

                    jsonObject.put(mdmModuleDbEntityList.get(i).getDbName(), datas);
                    //tablename = mdmModuleDbEntityList.get(i).getDbName() + "_distribute";
                } else {
                    queryData.put("detailFlag", true);//是否明细
                    queryData.put("id", entity.getId());//字段
                    List<HashMap<String, Object>> datas = mdmModuleDbDao.getServiceByFormmainId(queryData);
                    convertKeysToLowerCase(datas);
                    jsonObject.put(mdmModuleDbEntityList.get(i).getDbName(), datas);
                }
            }

        }



        return BaseResult.getSuccessMessageEntity("获取数据成功", jsonObject);
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据详情数据字典
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity queryMdmShowDetailsDictionary(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询模版

        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        if (mdmModuleEntity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询数据源主表
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbFiledsEntity queryFild = new MdmModuleDbFiledsEntity();
        queryFild.setMdmId(mdmModuleEntity.getId());
        queryFild.setSts("Y");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(queryFild);

        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbFiledsRuleEntity.setSts("Y");
        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntities = mdmModuleDbFiledsRuleDao.queryBase(mdmModuleDbFiledsRuleEntity);

        List<MdmFiledsRuleDto> mdmFiledsRuleDtos = new ArrayList<>();
        if(mdmModuleDbFiledsRuleEntities != null && mdmModuleDbFiledsRuleEntities.size() > 0){
            for (int i = 0; i < mdmModuleDbFiledsRuleEntities.size(); i++) {
                if("treeselect".equals(mdmModuleDbFiledsRuleEntities.get(i).getRuleValue()) || "select".equals(mdmModuleDbFiledsRuleEntities.get(i).getRuleValue())){
                    MdmFiledsRuleDto mdmFiledsRuleDto = new MdmFiledsRuleDto();
                    mdmFiledsRuleDto.setDbId(mdmModuleDbFiledsRuleEntities.get(i).getDbId());
                    mdmFiledsRuleDto.setFiledId(mdmModuleDbFiledsRuleEntities.get(i).getFiledId());
                    mdmFiledsRuleDto.setMdmId(mdmModuleDbFiledsRuleEntities.get(i).getMdmId());
                    mdmFiledsRuleDto.setFiledType(mdmModuleDbFiledsRuleEntities.get(i).getRuleValue());
                    mdmFiledsRuleDtos.add(mdmFiledsRuleDto);
                }
            }
        }

        if(mdmFiledsRuleDtos != null && mdmFiledsRuleDtos.size() > 0){
            for (int i = 0; i < mdmFiledsRuleDtos.size(); i++) {
                for (int i1 = 0; i1 < mdmModuleDbFiledsRuleEntities.size(); i1++) {
                    if(mdmFiledsRuleDtos.get(i).getFiledId().equals(mdmModuleDbFiledsRuleEntities.get(i1).getFiledId())
                            && "service".equals(mdmModuleDbFiledsRuleEntities.get(i1).getRuleCode())){
                        mdmFiledsRuleDtos.get(i).setTypeFiled(mdmModuleDbFiledsRuleEntities.get(i1).getRuleValue());
                    }
                }
            }
        }

        //String tablename = null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appName","数智中台");
        jsonObject.put("appCode","800004");
        jsonObject.put("mdmCode",entity.getMdmCode());
        jsonObject.put("optionName","admin");
        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType()) || "2".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                //查询数据
                Map<String, Object> queryData = new HashMap<>();
                queryData.put("tableName", mdmModuleDbEntityList.get(i).getDbName());//表名
                if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    queryData.put("detailFlag", false);//是否明细
                    queryData.put("id", entity.getId());//字段
                    HashMap<String, Object> datas = mdmModuleDbDao.getServiceDataById(queryData);
                    convertKeysToLowerCase(datas);
                    JSONObject zbdata = new JSONObject();
                    if(mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0){
                        for (int i2 = 0; i2 < mdmModuleDbFiledsEntities.size(); i2++) {
                            if(mdmModuleDbFiledsEntities.get(i2).getDbId().equals(mdmModuleDbEntityList.get(i).getId())){
                                if("1".equals(mdmModuleDbFiledsEntities.get(i2).getViewType())){
                                    //判断是否是下拉类型
                                    Object data = datas.get(mdmModuleDbFiledsEntities.get(i2).getEnName());
                                    if(mdmFiledsRuleDtos != null && mdmFiledsRuleDtos.size() > 0){
                                        for (int i1 = 0; i1 < mdmFiledsRuleDtos.size(); i1++) {
                                            if(mdmModuleDbFiledsEntities.get(i2).getId().equals(mdmFiledsRuleDtos.get(i1).getFiledId())){
                                                //查询对应的data_id
                                                String strData = String.valueOf(data);
                                                int index = strData.lastIndexOf(',');
                                                if (index!= -1) {
                                                    strData = strData.substring(index + 1);
                                                }
                                                mdmFiledsRuleDtos.get(i1).setId(strData);
                                                MdmFiledsRuleDto mdmFiledsRuleDto =  mdmModuleDao.queryDataId(mdmFiledsRuleDtos.get(i1));
                                                if(mdmFiledsRuleDto != null && mdmFiledsRuleDto.getDataId() != null &&  !"".equals(mdmFiledsRuleDto.getDataId())){
                                                    data = mdmFiledsRuleDto.getDataId();
                                                }else {
                                                    data = null;
                                                }
                                            }
                                        }
                                    }
                                    zbdata.put(mdmModuleDbFiledsEntities.get(i2).getEnName(),data);
                                }
                            }
                        }
                    }
                    jsonObject.put(mdmModuleDbEntityList.get(i).getDbName(), zbdata);
                } else {
                    queryData.put("detailFlag", true);//是否明细
                    queryData.put("id", entity.getId());//字段
                    List<HashMap<String, Object>> datas = mdmModuleDbDao.getServiceByFormmainId(queryData);
                    convertKeysToLowerCase(datas);
                    List<JSONObject> mxDataList = new ArrayList<>();
                    if(datas != null && datas.size() > 0){
                        for (int i1 = 0; i1 < datas.size(); i1++) {
                            JSONObject mxData = new JSONObject();
                            if(mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0){
                                for (int i2 = 0; i2 < mdmModuleDbFiledsEntities.size(); i2++) {
                                    if(mdmModuleDbFiledsEntities.get(i2).getDbId().equals(mdmModuleDbEntityList.get(i).getId())){
                                        if("1".equals(mdmModuleDbFiledsEntities.get(i2).getViewType())){

                                            //判断是否是下拉类型
                                            Object data = datas.get(i1).get(mdmModuleDbFiledsEntities.get(i2).getEnName());
                                            if(mdmFiledsRuleDtos != null && mdmFiledsRuleDtos.size() > 0){
                                                for (int b = 0; b < mdmFiledsRuleDtos.size(); b++) {
                                                    if(mdmModuleDbFiledsEntities.get(i2).getId().equals(mdmFiledsRuleDtos.get(b).getFiledId())){
                                                        //查询对应的data_id
                                                        String strData = String.valueOf(data);
                                                        int index = strData.lastIndexOf(',');
                                                        if (index!= -1) {
                                                            strData = strData.substring(index + 1);
                                                        }
                                                        mdmFiledsRuleDtos.get(b).setId(strData);
                                                        MdmFiledsRuleDto mdmFiledsRuleDto =  mdmModuleDao.queryDataId(mdmFiledsRuleDtos.get(b));
                                                        if(mdmFiledsRuleDto != null && mdmFiledsRuleDto.getDataId() != null &&  !"".equals(mdmFiledsRuleDto.getDataId())){
                                                            data = mdmFiledsRuleDto.getDataId();
                                                        }else {
                                                            data = null;
                                                        }
                                                    }
                                                }
                                            }
                                            mxData.put(mdmModuleDbFiledsEntities.get(i2).getEnName(),data);
                                        }
                                    }
                                }
                            }
                            mxDataList.add(mxData);
                        }
                    }else {
                        JSONObject mxData = new JSONObject();
                        if(mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0){
                            for (int i2 = 0; i2 < mdmModuleDbFiledsEntities.size(); i2++) {
                                if(mdmModuleDbFiledsEntities.get(i2).getDbId().equals(mdmModuleDbEntityList.get(i).getId())){
                                    if("1".equals(mdmModuleDbFiledsEntities.get(i2).getViewType())){
                                        mxData.put(mdmModuleDbFiledsEntities.get(i2).getEnName(),null);
                                    }
                                }
                            }
                        }
                        mxDataList.add(mxData);
                    }
                    jsonObject.put(mdmModuleDbEntityList.get(i).getDbName(), mxDataList);
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("获取数据成功", jsonObject);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据详情数据修改
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity updateMdmShowDetailsData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "mdmCode")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "data")) {
            return BaseResult.getFailureMessageEntity("请先传递要修改的数据");
        }
        JSONObject saveData = jsonObject.getJSONObject("data");
        //校验唯一
        MdmModuleEntity mdmModuleEntity = new MdmModuleEntity();
        mdmModuleEntity.setMdmCode(jsonObject.getLong("mdmCode"));
        mdmModuleEntity.setSts("Y");
        List<MdmModuleEntity> mdmModuleEntities = mdmModuleDao.queryBase(mdmModuleEntity);
        if (mdmModuleEntities == null || mdmModuleEntities.size() != 1) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        mdmModuleEntity = mdmModuleEntities.get(0);
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmModuleDbDao.queryBase(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        String res = checkDataOnly(jsonObject.getInteger("mdmCode"), saveData, true, mdmModuleEntity, mdmModuleDbEntityList, "界面修改");

        if (res == null || "".equals(res)) {
            return BaseResult.getSuccessMessageEntity("修改数据成功");
        } else {
            return BaseResult.getFailureMessageEntity(res);
        }
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据详情数据新增
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity saveMdmShowDetailsData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "mdmCode")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "data")) {
            return BaseResult.getFailureMessageEntity("请先传递要修改的数据");
        }
        JSONObject saveData = jsonObject.getJSONObject("data");
        //校验唯一
        MdmModuleEntity mdmModuleEntity = new MdmModuleEntity();
        mdmModuleEntity.setMdmCode(jsonObject.getLong("mdmCode"));
        mdmModuleEntity.setSts("Y");
        List<MdmModuleEntity> mdmModuleEntities = mdmModuleDao.queryBase(mdmModuleEntity);
        if (mdmModuleEntities == null || mdmModuleEntities.size() != 1) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        mdmModuleEntity = mdmModuleEntities.get(0);
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmModuleDbDao.queryBase(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }


        String res = checkDataOnly(jsonObject.getInteger("mdmCode"), saveData, false, mdmModuleEntity, mdmModuleDbEntityList, "界面新增");

        if (res == null || "".equals(res)) {
            return BaseResult.getSuccessMessageEntity("保存数据成功");
        } else {
            return BaseResult.getFailureMessageEntity(res);
        }
    }

    private void saveOption(String dbname, Long mdmCode, JSONObject jsonObject, String id, String msg) {
        MdmModuleOptionLogEntity mdmModuleOptionLogEntity = new MdmModuleOptionLogEntity();
        mdmModuleOptionLogEntity.setTableName(dbname + "_option_log");
        mdmModuleOptionLogEntity.setMdmCode(mdmCode);
        mdmModuleOptionLogEntity.setFormmainId(id);
        mdmModuleOptionLogEntity.setSourceName("数智中台");
        mdmModuleOptionLogEntity.setDataType("1");
        mdmModuleOptionLogEntity.setRemark(msg);
        mdmModuleOptionLogEntity.setCode("800004");
        mdmModuleOptionLogEntity.setSourceData(jsonObject.toJSONString());
        mdmModuleOptionLogEntity.setOptionType(msg);
        SysUserEntity sysUserEntity = sysUserDao.get(StpUtil.getLoginIdAsString());
        if (sysUserEntity != null) {
            mdmModuleOptionLogEntity.setOptionName(sysUserEntity.getPersonName());
        }
        mdmModuleOptionLogEntity.setCreate();
        mdmModuleOptionLogDao.save(mdmModuleOptionLogEntity);

    }
    //
    //

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据详情数据修改
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity deleteMdmShowDetailsData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "mdmCode")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "id")) {
            return BaseResult.getFailureMessageEntity("请先传递要删除的数据");
        }
        //校验唯一
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(jsonObject.getLong("mdmCode"));
        if (mdmModuleEntity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntityList == null || mdmModuleDbEntityList.size() == 0) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        String dbname = null;
        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                dbname = mdmModuleDbEntityList.get(i).getDbName();
                //查询视图，如果树形，删除下级及子集
                //查询展示类型
                MdmModuleViewEntity mdmModuleViewEntity = new MdmModuleViewEntity();
                mdmModuleViewEntity.setMdmId(mdmModuleEntity.getId());
                mdmModuleViewEntity.setSts("Y");
                List<MdmModuleViewEntity> moduleViewEntityList = mdmModuleViewDao.queryBase(mdmModuleViewEntity);
                if (moduleViewEntityList == null || moduleViewEntityList.size() != 1) {
                    return BaseResult.getFailureMessageEntity("系统错误");
                }
                mdmModuleViewEntity = moduleViewEntityList.get(0);


                //删除自己
                FormmainDeleteDto formmainDeleteDto = new FormmainDeleteDto();
                formmainDeleteDto.setId(jsonObject.getString("id"));
                formmainDeleteDto.setLoginId(StpUtil.getLoginIdAsString());
                formmainDeleteDto.setTableName(mdmModuleDbEntityList.get(i).getDbName());
                if (mdmModuleViewEntity != null && mdmModuleViewEntity.getViewName() != null && "1".equals(mdmModuleViewEntity.getViewName()) &&
                        mdmModuleViewEntity.getUpIdFiled() != null && !"".equals(mdmModuleViewEntity.getUpIdFiled())) {
                    formmainDeleteDto.setUpIdFiled(mdmModuleViewEntity.getUpIdFiled());

                    Map<String, Object> queryData = new HashMap<>();
                    queryData.put("tableName", mdmModuleDbEntityList.get(i).getDbName());//表名
                    queryData.put("detailFlag", false);//是否明细
                    queryData.put("id", jsonObject.getString("id"));//字段
                    HashMap<String, Object> datas = mdmModuleDbDao.getServiceDataById(queryData);
                    convertKeysToLowerCase(datas);

                    String upValue = null;
                    if (datas != null && datas.get(mdmModuleViewEntity.getUpIdFiled()) != null) {
                        upValue = datas.get(mdmModuleViewEntity.getUpIdFiled()).toString();
                    }
                    if (upValue != null) {
                        formmainDeleteDto.setUpIdFiledValue(upValue + "," + jsonObject.getString("id"));
                    } else {
                        formmainDeleteDto.setUpIdFiledValue(jsonObject.getString("id"));
                    }
                    mdmModuleDao.deleteChilder(formmainDeleteDto);
                }
                mdmModuleDao.deleteTemplateById(formmainDeleteDto);

            } else if ("2".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                //删除明细
                FormmainDeleteDto formmainDeleteDto = new FormmainDeleteDto();
                formmainDeleteDto.setFormmain_id(jsonObject.getString("id"));
                formmainDeleteDto.setLoginId(StpUtil.getLoginIdAsString());
                formmainDeleteDto.setTableName(mdmModuleDbEntityList.get(i).getDbName());
                mdmModuleDao.deleteTemplateById(formmainDeleteDto);
            }


        }
        saveOption(dbname, mdmModuleEntity.getMdmCode(), jsonObject, jsonObject.getString("id"), "界面删除");
        return BaseResult.getSuccessMessageEntity("删除数据成功");
    }

    public JSONObject toLowerCaseKeys(JSONObject jsonObject) {
        JSONObject lowerCaseJson = new JSONObject();
        if (jsonObject != null) {
            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                lowerCaseJson.put(key.toLowerCase(), value);
            }
        }
        return lowerCaseJson;
    }

    public List<JSONObject> toLowerCaseKeys(List<JSONObject> list) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                jsonObjects.add(toLowerCaseKeys(list.get(i)));
            }
        }
        return jsonObjects;
    }

    /**
     * @param jsonStr
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 处理分发数据
     * @Date 9:40 上午 2023/10/18
     **/
    @Override
    public JsonResultEntity doMdmDistribute(JSONObject jsonStr) throws Exception {
        JSONObject jsonObject = getstrObj("jsonStr", jsonStr);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "mdmCode")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "id")) {
            return BaseResult.getFailureMessageEntity("请先传递要下发数据");
        }
        if (!checkData(jsonObject, "distributeId")) {
            return BaseResult.getFailureMessageEntity("请先传递要下发的数据");
        }
        if (!checkData(jsonObject, "type")) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        Long mdmCode = jsonObject.getLong("mdmCode");//主数据编码
        String id = jsonObject.getString("id");//行数据的单据规则编码
        String distributeId = jsonObject.getString("distributeId");//发送表id
        String type = jsonObject.getString("type");//发送类型，1、新增2、修改3、删除

        String apiId = null;
        String scriptId = null;
        //查找对应数据，组装数据发送

        //获取主数据信息
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(mdmCode);
        if (mdmModuleEntity == null || mdmModuleEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("主数据不存在");
        }

        //获取主数据db
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntities == null || mdmModuleDbEntities.size() == 0) {
            return BaseResult.getFailureMessageEntity("主数据数据源不存在");
        }

        //获取分发表
        //获取分发的接口
        MdmModuleDistributeEntity mdmModuleDistributeEntity = mdmModuleDistributeDao.get(distributeId);
        if (mdmModuleDistributeEntity == null || mdmModuleDistributeEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("分发不存在");
        }
        // 启用停用 0、停用
        if ("0".equals(mdmModuleDistributeEntity.getEnabledState())) {
            return BaseResult.getFailureMessageEntity("分发已停用");
        }
        MdmModuleDistributeDetailEntity mdmModuleDistributeDetailEntity = new MdmModuleDistributeDetailEntity();
        mdmModuleDistributeDetailEntity.setDistributeId(distributeId);
        mdmModuleDistributeDetailEntity.setSts("Y");
        List<MdmModuleDistributeDetailEntity> mdmModuleDistributeDetailEntities = mdmModuleDistributeDetailDao.queryBase(mdmModuleDistributeDetailEntity);

        // 启用停用 0、停用
        if ("1".equals(mdmModuleDistributeEntity.getEnabledType())) {
            return BaseResult.getFailureMessageEntity("分发为自动下发，不允许手动下发");
        }
        //1、新增2、修改3、删除
        if ("1".equals(type)) {
            apiId = mdmModuleDistributeEntity.getAddApi();
            scriptId = mdmModuleDistributeEntity.getAddScript();
        } else if ("2".equals(type)) {
            apiId = mdmModuleDistributeEntity.getUpdateApi();
            scriptId = mdmModuleDistributeEntity.getUpdateScript();

        } else if ("3".equals(type)) {
            apiId = mdmModuleDistributeEntity.getDeleteApi();
            scriptId = mdmModuleDistributeEntity.getDeleteScript();
        }
        if (apiId == null || "".equals(apiId)) {
            return BaseResult.getFailureMessageEntity("分发接口未选择，请先选择");
        }
        SysApplicationScriptEntity scriptEntity = null;
        if (scriptId != null && !"".equals(scriptId)) {
            scriptEntity = sysApplicationScriptDao.get(scriptId);
            if (scriptEntity == null || scriptEntity.getId() == null) {
                return BaseResult.getFailureMessageEntity("分发接口脚本不存在");
            }
        }
        SysApplicationApiEntity apiEntity = sysApplicationApiDao.get(apiId);
        if (apiEntity == null || apiEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("分发接口不存在");

        }
        SysApplicationEntity sysApplicationEntity = sysApplicationDao.get(apiEntity.getAppId());
        if (sysApplicationEntity == null || sysApplicationEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("应用不存在");
        }

        //获取主数据db字段，以及字段的规则
        MdmModuleDbFiledsEntity queryFild = new MdmModuleDbFiledsEntity();
        queryFild.setMdmId(mdmModuleEntity.getId());
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(queryFild);
        MdmModuleDbFiledsRuleEntity queryFildRule = new MdmModuleDbFiledsRuleEntity();
        queryFildRule.setMdmId(mdmModuleEntity.getId());
        queryFildRule.setRuleCode("service");
        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntities = mdmModuleDbFiledsRuleDao.queryBase(queryFildRule);

        String dbname = null;
        //获取主数据db对应的实体表的数据
        JSONObject object = new JSONObject();
        for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
            if ("1".equals(mdmModuleDbEntities.get(i).getDbType())) {
                dbname = mdmModuleDbEntities.get(i).getDbName();
                Map<String, Object> map = new HashMap<>();
                map.put("tableName", mdmModuleDbEntities.get(i).getDbName());
                map.put("id", id);
                object = mdmModuleDao.queryMdmST(map);
                object = toLowerCaseKeys(object);
                if (object.getString("id") == null || "".equals(object.getString("id"))) {
                    return BaseResult.getFailureMessageEntity("当前数据不存在");
                }
                if (mdmModuleDistributeDetailEntities != null && mdmModuleDistributeDetailEntities.size() > 0) {
                    Map<String, Object> checkData = new HashMap<>();
                    checkData.put("tableName", mdmModuleDbEntities.get(i).getDbName());
                    checkData.put("id", id);
                    checkData.put("mdmModuleDistributeDetailEntities", mdmModuleDistributeDetailEntities);
                    JSONObject object1 = mdmModuleDao.queryMdmST(checkData);
                    object1 = toLowerCaseKeys(object1);
                    if (object1 == null || object1.getString("id") == null || "".equals(object1.getString("id"))) {
                        return BaseResult.getFailureMessageEntity("当前数据不符合规则不发送");
                    }
                }
                //查看主表是否有字段是关联的
                if (mdmModuleDbFiledsRuleEntities != null && mdmModuleDbFiledsRuleEntities.size() > 0) {
                    for (int i1 = 0; i1 < mdmModuleDbFiledsRuleEntities.size(); i1++) {
                        if (mdmModuleDbFiledsRuleEntities.get(i1).getDbId().equals(mdmModuleDbEntities.get(i).getId())) {
                            if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
                                for (int i2 = 0; i2 < mdmModuleDbFiledsEntities.size(); i2++) {
                                    if (mdmModuleDbFiledsRuleEntities.get(i1).getFiledId().equals(mdmModuleDbFiledsEntities.get(i2).getId())) {
                                        if (object.getString(mdmModuleDbFiledsEntities.get(i2).getEnName()) != null) {
                                            Map<String, Object> mapDetail = new HashMap<>();
                                            mapDetail.put("tableName", mdmModuleDbFiledsRuleEntities.get(i1).getRuleValue());
                                            String ids = object.getString(mdmModuleDbFiledsEntities.get(i2).getEnName());
                                            String[] idss = ids.split(",");
                                            mapDetail.put("id", idss[idss.length - 1]);
                                            JSONObject objectDetail = mdmModuleDao.queryMdmST(mapDetail);
                                            objectDetail = toLowerCaseKeys(objectDetail);
                                            object.put(mdmModuleDbFiledsEntities.get(i2).getEnName(), objectDetail);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
            if ("2".equals(mdmModuleDbEntities.get(i).getDbType())) {
                Map<String, Object> map = new HashMap<>();
                map.put("tableName", mdmModuleDbEntities.get(i).getDbName());
                map.put("formmainId", object.getString("id"));
                List<JSONObject> detail = mdmModuleDao.queryMdmSTDetail(map);
                //查看主表是否有字段是关联的
                if (detail != null && detail.size() > 0) {
                    detail = toLowerCaseKeys(detail);
                    if (mdmModuleDbFiledsRuleEntities != null && mdmModuleDbFiledsRuleEntities.size() > 0) {
                        for (int i1 = 0; i1 < mdmModuleDbFiledsRuleEntities.size(); i1++) {
                            if (mdmModuleDbFiledsRuleEntities.get(i1).getDbId().equals(mdmModuleDbEntities.get(i).getId())) {
                                if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
                                    for (int i2 = 0; i2 < mdmModuleDbFiledsEntities.size(); i2++) {
                                        if (mdmModuleDbFiledsRuleEntities.get(i1).getFiledId().equals(mdmModuleDbFiledsEntities.get(i2).getId())) {
                                            for (int i3 = 0; i3 < detail.size(); i3++) {
                                                if (detail.get(i3).getString(mdmModuleDbFiledsEntities.get(i2).getEnName()) != null) {
                                                    Map<String, Object> mapDetail = new HashMap<>();
                                                    mapDetail.put("tableName", mdmModuleDbFiledsRuleEntities.get(i1).getRuleValue());
                                                    String ids = detail.get(i3).getString(mdmModuleDbFiledsEntities.get(i2).getEnName());
                                                    String[] idss = ids.split(",");
                                                    mapDetail.put("id", idss[idss.length - 1]);
                                                    JSONObject objectDetail = mdmModuleDao.queryMdmST(mapDetail);
                                                    objectDetail = toLowerCaseKeys(objectDetail);

                                                    detail.get(i3).put(mdmModuleDbFiledsEntities.get(i2).getEnName(), objectDetail);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                object.put(mdmModuleDbEntities.get(i).getDbName(), detail);
            }
        }

        //执行脚本
        JSONObject groovy = new JSONObject();
        JSONObject parameterJson = new JSONObject();
        parameterJson.put("header", apiEntity.getHeaderIn());
        parameterJson.put("body", apiEntity.getBodyIn());
        parameterJson.put("query", apiEntity.getQueryIn());
        parameterJson.put("data", object);

        if (!"1".equals(type)) {//删除或者修改情况,查询三方id，有就设置，没有就设置空
            //1、根据主数据id、数据id、应用ID默认查询主数据应用下发三方id表，只查询最新的一条数据
            MdmModuleDistributeTripartiteEntity mdmModuleDistributeTripartiteEntity = new MdmModuleDistributeTripartiteEntity();
            mdmModuleDistributeTripartiteEntity.setSts("Y");
            mdmModuleDistributeTripartiteEntity.setMdmId(mdmModuleEntity.getId());
            mdmModuleDistributeTripartiteEntity.setDataId(object.getString("id"));
            mdmModuleDistributeTripartiteEntity.setAppId(sysApplicationEntity.getId());
            mdmModuleDistributeTripartiteEntity = mdmModuleDistributeTripartiteDao.queryOneTripartite(mdmModuleDistributeTripartiteEntity);
            String tripartiteId = "";
            if (mdmModuleDistributeTripartiteEntity != null && mdmModuleDistributeTripartiteEntity.getTripartiteId() != null) {
                tripartiteId = mdmModuleDistributeTripartiteEntity.getTripartiteId();
            }
            parameterJson.put("tripartiteId", tripartiteId);
        }

        /**  head */
        Map<String, String> header = null;
        /**  query */
        String querys = null;
        /**  body */
        String bodys = object.toJSONString();
        if (scriptEntity != null && scriptEntity.getId() != null && scriptEntity.getScriptData() != null) {
            groovy.put("code", scriptEntity.getScriptCode());
            groovy.put("className", scriptEntity.getClassName());
            groovy.put("name", scriptEntity.getScriptName());
            groovy.put("methodStr", scriptEntity.getScriptData());
            groovy.put("parameterJson", parameterJson);
            JSONObject groovyStr = new JSONObject();
            groovyStr.put("jsonStr", groovy);
            try {
                Object str = groovyIntegrationService.groovyScriptExecution(groovyStr);
                JSONObject jsonResultEntity = JSONObject.parseObject(str.toString());
                header = (Map<String, String>) jsonResultEntity.get("header");
                querys = jsonResultEntity.getString("querys");
                bodys = jsonResultEntity.getString("bodys");
            } catch (Exception e) {
                return BaseResult.getFailureMessageEntity("分发脚本转换错误");
            }
        }
        Map<String, String> headers = new HashMap<>();
        headers.put("publicKey", "ZJYAWb7lhAUTYqekPkU+uHJv1/ObJxb7dT7sD8HPRDGAgyhCe7eDIk+3zDUT+v578prj");
        headers.put("secretKey", "fviZnLBsQUAGF8w8FSOdJi7XlIm/XAZclMxRagDLfTyJFlvnIBF3w66Hrpfzs8cYj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=");
        headers.put("appId", sysApplicationEntity.getAppId().toString());
        headers.put("apiCode", apiEntity.getApiCode().toString());
        if (header != null) {
            headers.putAll(header);
        }
        if (SysEnum.NEED_LOGIN.getValue().equals(apiEntity.getNeedLogin())) {
            //找到登陆接口
            SysApplicationApiEntity loginApi = sysApplicationApiDao.get(apiEntity.getAuthenticationPort());
            if (null == loginApi) {
                saveMdmModuleSendLogEntity(mdmModuleEntity.getMdmCode(), mdmModuleDistributeEntity.getId(), "2", "转发失败,认证接口不存在", dbname, object.getString("id"), sysApplicationEntity.getName(), sysApplicationEntity.getId(), apiEntity.getApiName(), apiEntity.getId(), object.toJSONString(), type);
                return BaseResult.getFailureMessageEntity("发送错误，认证接口不存在");

            }
            String rzquerys = getQuery(loginApi, null, null);
            Map<String, String> headersa = new HashMap<>();
            headersa.put("publicKey", "ZJYAWb7lhAUTYqekPkU+uHJv1/ObJxb7dT7sD8HPRDGAgyhCe7eDIk+3zDUT+v578prj");
            headersa.put("secretKey", "fviZnLBsQUAGF8w8FSOdJi7XlIm/XAZclMxRagDLfTyJFlvnIBF3w66Hrpfzs8cYj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=");
            headersa.put("appId", sysApplicationEntity.getAppId().toString());
            headersa.put("apiCode", loginApi.getApiCode().toString());
            Map<String, String> rzheaders = getHeaders(loginApi, headersa, null);
            String rzbodys = getBodys(loginApi, null, null);
            JsonResultEntity rzjsonResultEntity = sendData(loginApi, rzheaders, rzbodys, rzquerys);
            if (!rzjsonResultEntity.isFlag()) {
                saveMdmModuleSendLogEntity(mdmModuleEntity.getMdmCode(), mdmModuleDistributeEntity.getId(), "2", "转发失败,认证接口调用失败", dbname, object.getString("id"), sysApplicationEntity.getName(), sysApplicationEntity.getId(), apiEntity.getApiName(), apiEntity.getId(), object.toJSONString(), type);
                return BaseResult.getFailureMessageEntity("发送错误:转发失败,认证接口调用失败");
            }
            JSONObject attritube = JSONObject.parseObject(rzjsonResultEntity.getAttribute().toString());
            //logger.info("认证接口返回信息："+ attritube.toJSONString());
            querys = getQuery(apiEntity, querys, attritube);
            //logger.info("认证接口拼接querys信息："+ querys);

            headers = getHeaders(apiEntity, headers, attritube);
            if (headers != null && headers.size() > 0) {
                StringBuffer stringBuffer = new StringBuffer();
                for (String key : headers.keySet()) {
                    stringBuffer.append(key).append("=").append(headers.get(key)).append("&");
                }
                //logger.info("认证接口拼接header信息："+ stringBuffer);
            }
            bodys = getBodys(apiEntity, bodys, attritube);
            //logger.info("认证接口拼接bodys信息："+ bodys);
        }


        //组装数据发送
        JsonResultEntity jsonResultEntity = sendData(apiEntity, headers, bodys, querys);
        if (jsonResultEntity.isFlag()) {

            if ("1".equals(type)) {//保存三方返回id
                if (scriptEntity != null && scriptEntity.getId() != null && scriptEntity.getBackScriptData() != null) {
                    JSONObject backScriptJson = new JSONObject();
                    backScriptJson.put("data", jsonResultEntity.getAttribute());
                    groovy.put("code", scriptEntity.getScriptCode() + "back");
                    groovy.put("className", scriptEntity.getClassName() + "back");
                    groovy.put("name", scriptEntity.getScriptName() + "back");
                    groovy.put("methodStr", scriptEntity.getBackScriptData());
                    groovy.put("parameterJson", backScriptJson);
                    JSONObject groovyStr = new JSONObject();
                    groovyStr.put("jsonStr", groovy);
                    try {
                        Object str = groovyIntegrationService.groovyScriptExecution(groovyStr);
                        if (str != null) {
                            JSONObject backJsonResultEntity = JSONObject.parseObject(str.toString());
                            if (backJsonResultEntity != null && backJsonResultEntity.getString("success") != null && "true".equals(backJsonResultEntity.getString("success"))) {
                                MdmModuleDistributeTripartiteEntity mdmModuleDistributeTripartiteEntity = new MdmModuleDistributeTripartiteEntity();
                                mdmModuleDistributeTripartiteEntity.setCreate();
                                mdmModuleDistributeTripartiteEntity.setMdmId(mdmModuleEntity.getId());
                                mdmModuleDistributeTripartiteEntity.setDataId(object.getString("id"));
                                mdmModuleDistributeTripartiteEntity.setAppId(sysApplicationEntity.getId());
                                mdmModuleDistributeTripartiteEntity.setTripartiteId(backJsonResultEntity.getString("tripartiteId"));
                                mdmModuleDistributeTripartiteDao.save(mdmModuleDistributeTripartiteEntity);
                            }
                        }
                    } catch (Exception e) {
                        logger.error("新增返回脚本解析保存三方id错误" + jsonResultEntity.getAttribute());
                        saveMdmModuleSendLogEntity(mdmModuleEntity.getMdmCode(), mdmModuleDistributeEntity.getId(), "2", "转发失败:"+jsonResultEntity.getAttribute(), dbname, object.getString("id"), sysApplicationEntity.getName(), sysApplicationEntity.getId(), apiEntity.getApiName(), apiEntity.getId(), object.toJSONString(), type);
                        return BaseResult.getFailureMessageEntity("发送错误:新增返回脚本解析保存三方id错误");
                    }
                }
            }
            saveMdmModuleSendLogEntity(mdmModuleEntity.getMdmCode(), mdmModuleDistributeEntity.getId(), "1", "发送成功", dbname, object.getString("id"), sysApplicationEntity.getName(), sysApplicationEntity.getId(), apiEntity.getApiName(), apiEntity.getId(), object.toJSONString(), type);
            return BaseResult.getSuccessMessageEntity("发送成功");
        } else {
            saveMdmModuleSendLogEntity(mdmModuleEntity.getMdmCode(), mdmModuleDistributeEntity.getId(), "2", "转发失败:"+jsonResultEntity.getAttribute(), dbname, object.getString("id"), sysApplicationEntity.getName(), sysApplicationEntity.getId(), apiEntity.getApiName(), apiEntity.getId(), object.toJSONString(), type);
            JSONObject object1 = JSONObject.parseObject(jsonResultEntity.getAttribute().toString());
            return BaseResult.getFailureMessageEntity("发送错误:" +object1.getString("msg"));
        }
    }

    private Map<String, String> getHeaders(SysApplicationApiEntity loginApi, Map<String, String> map, JSONObject loginData) {
        if (loginData == null) {
            loginData = new JSONObject();
        }
        if (loginApi.getHeaderIn() != null && !"".equals(loginApi.getHeaderIn())) {
            if (JSONUtil.isTypeJSONArray(loginApi.getHeaderIn())) {
                JSONArray headerArray = JSON.parseArray(loginApi.getHeaderIn());
                for (int i = 0; i < headerArray.size(); i++) {
                    JSONObject querys = headerArray.getJSONObject(i);
                    //query 只有基本类型，不用循环判断下级
                    //判断参数是否有值
                    //获取对象下面的层级数据
                    if (SysEnum.AUTHPORT.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型
                        String query = querys.getString(SysEnum.EXAMPLE.getValue());
                        if (query != null && !"".equals(query)) {
                            JSONArray example = JSONArray.parseArray(query);
                            String logValue = getObjectValue(loginData, example);
                            map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), logValue);
                        }
                    } else {
                        if (querys.getString(SysEnum.EXAMPLE.getValue()) != null && !"".equals(querys.getString(SysEnum.EXAMPLE.getValue()))) {//入参没有值用实例值，如果没有不添加
                            if (map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null) {
                                map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), querys.getString(SysEnum.EXAMPLE.getValue()));
                            }
                        } else {//没有值直接拼接
                            if (map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null) {
                                map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), "");
                            }
                        }
                    }
                }
            }
        }
        return map;


    }


    private String getQuery(SysApplicationApiEntity loginApi, String sendDatastr, JSONObject loginData) {
        Map<String, String> map = new HashMap<>();
        if (sendDatastr != null) {
            String[] parts = sendDatastr.split("&");
            if (parts != null && parts.length > 0) {
                for (int i = 0; i < parts.length; i++) {
                    String[] part = parts[i].split("=");
                    if (part != null && part.length >= 2) {
                        for (int a = 0; a < part.length; a++) {
                            map.put(part[0], part[1]);
                        }
                    }
                }
            }
        }
        if (loginData == null) {
            loginData = new JSONObject();
        }
        if (loginApi.getQueryIn() != null && !"".equals(loginApi.getQueryIn())) {
            if (JSONUtil.isTypeJSONArray(loginApi.getQueryIn())) {
                JSONArray headerArray = JSON.parseArray(loginApi.getQueryIn());
                for (int i = 0; i < headerArray.size(); i++) {
                    JSONObject querys = headerArray.getJSONObject(i);
                    //query 只有基本类型，不用循环判断下级
                    //判断参数是否有值
                    //获取对象下面的层级数据
                    if (SysEnum.AUTHPORT.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型
                        String query = querys.getString(SysEnum.EXAMPLE.getValue());
                        if (query != null && !"".equals(query)) {
                            JSONArray example = JSONArray.parseArray(query);
                            String logValue = getObjectValue(loginData, example);
                            map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), logValue);
                        }
                    } else {
                        //不是认证类型直接取值
                        if (querys.getString(SysEnum.EXAMPLE.getValue()) != null && !"".equals(querys.getString(SysEnum.EXAMPLE.getValue()))) {//入参没有值用实例值，如果没有不添加
                            if (map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null) {
                                map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), querys.getString(SysEnum.EXAMPLE.getValue()));
                            }
                        } else {//没有值直接拼接
                            if (map.get(querys.getString(SysEnum.PARAMETERNAME.getValue())) == null) {
                                map.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), "");
                            }
                        }
                    }
                }
            }
        }
        StringBuffer returnStr = new StringBuffer();
        if (map != null && map.size() > 0) {
            for (String key : map.keySet()) {
                if ("".equals(returnStr)) {
                    returnStr.append(key).append("=").append(map.get(key));
                } else {
                    returnStr.append("&").append(key).append("=").append(map.get(key));
                }
            }
        }
        return returnStr.toString();
    }

    /**
     * @param loginData
     * @param example
     * @return java.lang.String
     * @Author lvleigang
     * @Description 根据jsonArray 获取jsonobject中的值
     * @Date 11:47 上午 2023/8/31
     **/
    private String getObjectValue(JSONObject loginData, JSONArray example) {
        String values = "";
        if (example != null && example.size() > 0) {
            for (int i = 0; i < example.size(); i++) {
                if (loginData.getString(example.getString(i)) != null && !"".equals(loginData.getString(example.getString(i)))) {
                    if (i == (example.size() - 1)) {
                        values = loginData.getString(example.getString(i));
                    } else {
                        loginData = JSONObject.parseObject(loginData.getString(example.getString(i)));
                    }
                } else {
                    return values;
                }
            }
        }
        return values;
    }

    private String getBodys(SysApplicationApiEntity loginApi, String sendDatastr, JSONObject loginData) {
        JSONObject sendData = new JSONObject();
        if (sendDatastr != null) {
            sendData = JSONObject.parseObject(sendDatastr);
        }
        if (loginData == null) {
            loginData = new JSONObject();
        }
        if (loginApi.getBodyIn() != null && !"".equals(loginApi.getBodyIn())) {
            if (JSONUtil.isTypeJSONArray(loginApi.getBodyIn())) {
                JSONArray headerArray = JSON.parseArray(loginApi.getBodyIn());
                for (int i = 0; i < headerArray.size(); i++) {
                    //获取到第一个数据
                    JSONObject querys = headerArray.getJSONObject(i);
                    if (SysEnum.AUTHPORT.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型
                        String query = querys.getString(SysEnum.EXAMPLE.getValue());
                        if (query != null && !"".equals(query)) {
                            JSONArray example = JSONArray.parseArray(query);
                            String logValue = getObjectValue(loginData, example);
                            sendData.put(querys.getString(SysEnum.PARAMETERNAME.getValue()), logValue);
                        }
                    }
                }
            }
        }
        return sendData.toString();
    }

    /**
     * @param querys    设置的字段属性
     * @param sendData  发送数据
     * @param loginData 认证信息
     * @return java.lang.Object
     * @Author lvleigang
     * @Description 设置值
     * @Date 4:14 下午 2023/8/31
     **/
    private Object getUpdata(JSONObject querys, JSONObject sendData, JSONObject loginData) {
        if (SysEnum.AUTHPORT.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//认证类型 可以直接设置值
            String query = querys.getString(SysEnum.EXAMPLE.getValue());
            if (query != null && !"".equals(query)) {
                JSONArray example = JSONArray.parseArray(query);
                String logValue = getObjectValue(loginData, example);
                return logValue;
            }
        } else if (SysEnum.FUNDAMENTAL.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//基本类型
            if (sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())) != null
                    && !"".equals(sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())))) {//先判断入参是否有值
                return sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue()));
            } else if (querys.getString(SysEnum.EXAMPLE.getValue()) != null && !"".equals(querys.getString(SysEnum.EXAMPLE.getValue()))) {//入参没有值用实例值，如果没有不添加
                return querys.getString(SysEnum.EXAMPLE.getValue());
            } else {//没有值直接拼接
                return "";
            }
        } else if (SysEnum.COMPLEX.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//复杂类型
            if (sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())) != null
                    && !"".equals(sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())))) {//先判断入参是否有值
                //获取子级
                String childers = querys.getString(SysEnum.CHILDREN.getValue());
                if (childers != null && !"".equals(childers)) {
                    JSONArray childersObj = JSONArray.parseArray(childers);
                    if (childersObj != null && childersObj.size() > 0) {
                        JSONObject obj = new JSONObject();
                        for (int i = 0; i < childersObj.size(); i++) {
                            JSONObject a = childersObj.getJSONObject(i);
                            obj.put(a.getString(SysEnum.PARAMETERNAME.getValue()), getUpdata(a, sendData.getJSONObject(querys.getString(SysEnum.PARAMETERNAME.getValue())), loginData));
                        }
                        return obj;
                    }
                } else {
                    JSONObject obj = new JSONObject();
                    return obj;
                }
            } else {//入参没有值用实例值，如果没有不添加
                //获取子级
                String childers = querys.getString(SysEnum.CHILDREN.getValue());
                if (childers != null && !"".equals(childers)) {
                    JSONArray childersObj = JSONArray.parseArray(childers);
                    if (childersObj != null && childersObj.size() > 0) {
                        JSONObject obj = new JSONObject();
                        for (int i = 0; i < childersObj.size(); i++) {
                            JSONObject a = childersObj.getJSONObject(i);
                            obj.put(a.getString(SysEnum.PARAMETERNAME.getValue()), getUpdata(a, new JSONObject(), loginData));
                        }
                        return obj;
                    } else { //没有值直接拼接
                        JSONObject obj = new JSONObject();
                        return obj;
                    }
                } else { //没有值直接拼接
                    JSONObject obj = new JSONObject();
                    return obj;
                }
            }
        } else if (SysEnum.FUNDAMENTALLIST.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//基本列表
            if (sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())) != null
                    && !"".equals(sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())))) {//先判断入参是否有值
                JSONArray jsonArray = JSONArray.parseArray(sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())));
                return jsonArray;
            } else if (querys.getString(SysEnum.EXAMPLE.getValue()) != null && !"".equals(querys.getString(SysEnum.EXAMPLE.getValue()))) {//入参没有值用实例值，如果没有不添加
                if ("String".equals(querys.getString(SysEnum.CONCRETETYPE.getValue()))) {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getString(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                } else if ("Int".equals(querys.getString(SysEnum.CONCRETETYPE.getValue()))) {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getIntValue(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                } else if ("Long".equals(querys.getString(SysEnum.CONCRETETYPE.getValue()))) {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getLongValue(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                } else if ("Float".equals(querys.getString(SysEnum.CONCRETETYPE.getValue()))) {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getFloatValue(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                } else if ("Double".equals(querys.getString(SysEnum.CONCRETETYPE.getValue()))) {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getDoubleValue(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                } else if ("Boolean".equals(querys.getString(SysEnum.CONCRETETYPE.getValue()))) {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getBooleanValue(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                } else {
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(querys.getString(SysEnum.EXAMPLE.getValue()));
                    return jsonArray;
                }
            } else {//没有值直接拼接
                JSONArray jsonArray = new JSONArray();
                return jsonArray;
            }
        } else if (SysEnum.COMPLEXLIST.getValue().equals(querys.getString(SysEnum.PARAMETERTYPE.getValue()))) {//复杂列表
            if (sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())) != null
                    && !"".equals(sendData.getString(querys.getString(SysEnum.PARAMETERNAME.getValue())))) {//先判断入参是否有值
                //获取入参的数据
                //循环入参
                //循环api配置的参数
                //设置api的参数
                //返回数据
                JSONArray sendArray = sendData.getJSONArray(querys.getString(SysEnum.PARAMETERNAME.getValue()));
                if (sendArray == null || sendArray.size() == 0) {
                    return new JSONArray();
                }
                String childers = querys.getString(SysEnum.CHILDREN.getValue());
                if (childers == null || "".equals(childers)) {
                    return new JSONArray();
                }
                JSONArray childersObj = JSONArray.parseArray(childers);
                if (childersObj == null || childersObj.size() == 0) {
                    return new JSONArray();
                }
                JSONArray obj = new JSONArray();
                for (int i = 0; i < sendArray.size(); i++) {
                    JSONObject sl = new JSONObject();
                    for (int a = 0; a < childersObj.size(); a++) {
                        JSONObject b = childersObj.getJSONObject(a);
                        //入参没有只能取实例，实例只会有一个
                        sl.put(b.getString(SysEnum.PARAMETERNAME.getValue()), getUpdata(b, sendArray.getJSONObject(i), loginData));
                    }
                    obj.add(sl);
                }
                return obj;
            } else {//入参没有值用实例值，如果没有不添加
                String childers = querys.getString(SysEnum.CHILDREN.getValue());
                if (childers != null && !"".equals(childers)) {
                    JSONArray childersObj = JSONArray.parseArray(childers);
                    if (childersObj != null && childersObj.size() > 0) {
                        JSONArray obj = new JSONArray();
                        JSONObject sl = new JSONObject();
                        for (int i = 0; i < childersObj.size(); i++) {
                            JSONObject a = childersObj.getJSONObject(i);
                            //入参没有只能取实例，实例只会有一个
                            sl.put(a.getString(SysEnum.PARAMETERNAME.getValue()), getUpdata(a, new JSONObject(), loginData));
                        }
                        obj.add(sl);
                        return obj;
                    }
                } else {
                    JSONArray obj = new JSONArray();
                    return obj;
                }
            }
        }
        return null;
    }

    private JsonResultEntity sendData(SysApplicationApiEntity applicationApiEntity, Map<String, String> headers, String bodys, String querys) {
        StringBuffer urls = new StringBuffer(url);
        if (querys != null && !"".equals(querys)) {
            urls.append("?");
            urls.append(querys);
        }
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.disableCookieManagement().build();
        HttpPost post = new HttpPost(urls.toString());
        CloseableHttpResponse response = null;

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Integer.valueOf(applicationApiEntity.getTimeoutPeriod())).build();
        post.setConfig(requestConfig);//设置请求参数【超时时间】

        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
        }
        StringBuilder body = new StringBuilder();
        boolean flag = true;
        try {
            if (bodys != null && !"".equals(bodys)) {
                ByteArrayEntity entity = new ByteArrayEntity(bodys.getBytes("UTF-8"));
                entity.setContentType("application/json");
                post.setEntity(entity);
            }

            logger.info("接口拼接querys信息：" + querys);
            if (headers != null && headers.size() > 0) {
                StringBuffer stringBuffer = new StringBuffer();
                for (String key : headers.keySet()) {
                    stringBuffer.append(key).append("=").append(headers.get(key)).append("&");
                }
                logger.info("接口拼接header信息：" + stringBuffer);
            }
            logger.info("接口拼接bodys信息：" + bodys);

            response = closeableHttpClient.execute(post);

            HttpEntity entity = response.getEntity();
            synchronized (lock) {
                body.append(EntityUtils.toString(entity, "UTF-8"));
            }
            flag = true;
            logger.info("返回结果：" + body);
        } catch (Exception e) {
            logger.error("请求错误：" + e.getMessage());
            body.append(e.getMessage());
            flag = false;
        } finally {
            try {
                // 关闭响应对象
                if (response != null) {
                    response.close();
                }
                // 关闭响应对象
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (flag) {
            if (JSONUtil.isTypeJSON(body.toString())) {
                JsonResultEntity jsonResultEntity = JSONObject.parseObject(body.toString(), JsonResultEntity.class);
                if (jsonResultEntity.isFlag()) {
                    return BaseResult.getSuccessMessageEntity("转发成功", body);
                } else {
                    return BaseResult.getFailureMessageEntity("转发失败", body);
                }
            } else {
                return BaseResult.getFailureMessageEntity("转发失败", body);
            }
        } else {
            return BaseResult.getFailureMessageEntity("转发失败", body);
        }
    }

    private void saveMdmModuleSendLogEntity(Long mdmCode, String distributeId, String dataType, String remark, String dbname, String formmain_id, String target_app, String appID, String target_api, String apiId, String source_data, String option_type) throws Exception {
        MdmModuleSendLogEntity mdmModuleSendLogEntity = new MdmModuleSendLogEntity();
        mdmModuleSendLogEntity.setTableName(dbname + "_send_log");
        mdmModuleSendLogEntity.setId(UUIDUtils.getUUID());
        mdmModuleSendLogEntity.setDistributeId(distributeId);
        mdmModuleSendLogEntity.setAppId(appID);
        mdmModuleSendLogEntity.setApiId(apiId);
        mdmModuleSendLogEntity.setSts("Y");
        mdmModuleSendLogEntity.setCreate_user_id("1");
        mdmModuleSendLogEntity.setModify_user_id("1");
        mdmModuleSendLogEntity.setCreate_time(new Date());
        mdmModuleSendLogEntity.setModify_time(new Date());
        mdmModuleSendLogEntity.setOrg_id("0");
        mdmModuleSendLogEntity.setCompanyId("0");
        mdmModuleSendLogEntity.setFormmainId(formmain_id);
        mdmModuleSendLogEntity.setTargetApp(target_app);
        mdmModuleSendLogEntity.setTargetApi(target_api);
        mdmModuleSendLogEntity.setSourceData(source_data);
        mdmModuleSendLogEntity.setOptionType(option_type);
        mdmModuleSendLogEntity.setDataType(dataType);
        mdmModuleSendLogEntity.setRemark(remark);
        mdmModuleSendLogDao.save(mdmModuleSendLogEntity);


        JSONObject jsonObject = JSONObject.parseObject(source_data);
        IntegrationTaskLivingDetailsEntity integrationTaskLivingDetailsEntity = new IntegrationTaskLivingDetailsEntity();
        integrationTaskLivingDetailsEntity.setId(UUIDUtils.getUUID());
        integrationTaskLivingDetailsEntity.setSts("Y");
        integrationTaskLivingDetailsEntity.setCreate_user_id("1");
        integrationTaskLivingDetailsEntity.setModify_user_id("1");
        integrationTaskLivingDetailsEntity.setCreate_time(new Date());
        integrationTaskLivingDetailsEntity.setModify_time(new Date());
        integrationTaskLivingDetailsEntity.setOrg_id("0");
        integrationTaskLivingDetailsEntity.setCompanyId("0");
        JSONObject aa = new JSONObject();
        aa.put("mdmCode", mdmCode);//主数据编码
        aa.put("documentRule", jsonObject.getString("document_rule"));//行数据的单据规则编码
        aa.put("distributeId", distributeId);//发送表id
        aa.put("type", option_type);//发送类型，1、新增2、修改3、删除
        integrationTaskLivingDetailsEntity.setRootAppPk(aa.toJSONString());
        integrationTaskLivingDetailsEntity.setRootAppBill(jsonObject.getString("document_rule"));
        integrationTaskLivingDetailsEntity.setPluginId("MdmModulePlugin");
        integrationTaskLivingDetailsEntity.setRootAppNewData(source_data);
        integrationTaskLivingDetailsEntity.setNewTransmitInfo(remark);
        if ("1".equals(dataType)) {
            taskLivingDetailsService.saveLogToSuccess(integrationTaskLivingDetailsEntity);
        } else {
            taskLivingDetailsService.saveLogToFail(integrationTaskLivingDetailsEntity);

        }

    }


    /**
     * @param mdmCode
     * @param saveData
     * @param mdmModuleEntity
     * @param mdmModuleDbEntityList
     * @param flag                  修改 新增
     * @return java.lang.String
     * @Author lvleigang
     * @Description 校验数据
     * @Date 11:20 上午 2023/11/6
     **/
    private String checkDataOnly(long mdmCode, JSONObject saveData, boolean flag, MdmModuleEntity mdmModuleEntity, List<MdmModuleDbEntity> mdmModuleDbEntityList, String msg) {
        StringBuffer str = new StringBuffer();
        String dbname = null;
        for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                dbname = mdmModuleDbEntityList.get(i).getDbName();
            }
        }
        //查询字段
        //查询字段下的必填规则
        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbFiledsEntity.setSts("Y");
        mdmModuleDbFiledsEntity.setRoletype("required");
        mdmModuleDbFiledsEntity.setRoleValue("true");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmModuleDbFiledsDao.queryBase(mdmModuleDbFiledsEntity);

        if (mdmModuleDbFiledsEntityList != null && mdmModuleDbFiledsEntityList.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    JSONObject jsonObject = saveData.getJSONObject(mdmModuleDbEntityList.get(i).getDbName());
                    for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                        if (mdmModuleDbFiledsEntityList.get(i1).getDbId().equals(mdmModuleDbEntityList.get(i).getId())) {
                            if (jsonObject.get(mdmModuleDbFiledsEntityList.get(i1).getEnName()) == null || "".equals(jsonObject.getString(mdmModuleDbFiledsEntityList.get(i1).getEnName()))) {
                                if (str.toString() != null && !"".equals(str.toString())) {
                                    str.append("；");
                                }
                                str.append(mdmModuleDbEntityList.get(i).getRemark() + "的" + mdmModuleDbFiledsEntityList.get(i1).getChName() + "不为空");
                            }
                        }
                    }
                } else {
                    JSONArray jsonArray = saveData.getJSONArray(mdmModuleDbEntityList.get(i).getDbName());
                    if (jsonArray != null && jsonArray.size() > 0) {
                        for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                            if (mdmModuleDbFiledsEntityList.get(i1).getDbId().equals(mdmModuleDbEntityList.get(i).getId())) {
                                for (int i2 = 0; i2 < jsonArray.size(); i2++) {
                                    JSONObject jsonObject = jsonArray.getJSONObject(i2);
                                    if (jsonObject.get(mdmModuleDbFiledsEntityList.get(i1).getEnName()) == null || "".equals(jsonObject.getString(mdmModuleDbFiledsEntityList.get(i1).getEnName()))) {
                                        if (str.toString() != null && !"".equals(str.toString())) {
                                            str.append("；");
                                        }
                                        str.append(mdmModuleDbEntityList.get(i).getRemark() + "的" + mdmModuleDbFiledsEntityList.get(i1).getChName() + "不为空");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (str == null || "".equals(str.toString())) {
            //校验数据
            MdmModuleDbFiledsEntity onlyFiledsEntity = new MdmModuleDbFiledsEntity();
            onlyFiledsEntity.setMdmId(mdmModuleEntity.getId());
            onlyFiledsEntity.setSts("Y");
            onlyFiledsEntity.setRoletype("fieldsSole");
            onlyFiledsEntity.setRoleValue("true");

            List<MdmModuleDbFiledsEntity> onlyEntityList = mdmModuleDbFiledsDao.queryBase(onlyFiledsEntity);

            if (onlyEntityList != null && onlyEntityList.size() > 0) {
                for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                    if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                        JSONObject jsonObject = saveData.getJSONObject(mdmModuleDbEntityList.get(i).getDbName());
                        for (int i1 = 0; i1 < onlyEntityList.size(); i1++) {
                            if (onlyEntityList.get(i1).getDbId().equals(mdmModuleDbEntityList.get(i).getId())) {
                                //校验唯一
                                Map<String, String> map = new HashMap<>();
                                map.put("tableName", mdmModuleDbEntityList.get(i).getDbName());
                                if (flag) {
                                    map.put("id", jsonObject.getString("id"));
                                }
                                map.put("filedName", onlyEntityList.get(i1).getEnName());
                                map.put("filedValue", jsonObject.getString(onlyEntityList.get(i1).getEnName()));
                                Integer count = mdmModuleDao.checkData(map);
                                if (count > 0) {
                                    if (str.toString() != null && !"".equals(str.toString())) {
                                        str.append("；");
                                    }
                                    str.append(mdmModuleDbEntityList.get(i).getRemark() + "的" + mdmModuleDbFiledsEntityList.get(i1).getChName() + "已存在");
                                }
                            }
                        }
                    } else {
                        JSONArray jsonArray = saveData.getJSONArray(mdmModuleDbEntityList.get(i).getDbName());
                        if (jsonArray != null && jsonArray.size() > 0) {
                            for (int i1 = 0; i1 < onlyEntityList.size(); i1++) {
                                if (onlyEntityList.get(i1).getDbId().equals(mdmModuleDbEntityList.get(i).getId())) {
                                    for (int i2 = 0; i2 < jsonArray.size(); i2++) {
                                        JSONObject jsonObject = jsonArray.getJSONObject(i2);

                                        //校验唯一
                                        Map<String, String> map = new HashMap<>();
                                        map.put("tableName", mdmModuleDbEntityList.get(i).getDbName());
                                        if (flag) {
                                            map.put("id", jsonObject.getString("id"));
                                        }
                                        map.put("filedName", onlyEntityList.get(i1).getEnName());
                                        map.put("filedValue", jsonObject.getString(onlyEntityList.get(i1).getEnName()));
                                        Integer count = mdmModuleDao.checkData(map);
                                        if (count > 0) {
                                            if (str.toString() != null && !"".equals(str.toString())) {
                                                str.append("；");
                                            }
                                            str.append(mdmModuleDbEntityList.get(i).getRemark() + "的" + mdmModuleDbFiledsEntityList.get(i1).getChName() + "已存在");
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        //保存数据
        if (str == null || "".equals(str.toString())) {
            String id = UUIDUtils.getUUID();
            for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    JSONObject jsonObject = saveData.getJSONObject(mdmModuleDbEntityList.get(i).getDbName());
                    //保存数据
                    MdmDataDto mdmDataDto = new MdmDataDto();
                    mdmDataDto.setMdmId(mdmModuleEntity.getId());
                    mdmDataDto.setTableName(mdmModuleDbEntityList.get(i).getDbName());
                    MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity1 = new MdmModuleDbFiledsEntity();
                    mdmModuleDbFiledsEntity1.setMdmId(mdmModuleEntity.getId());
                    mdmModuleDbFiledsEntity1.setDbId(mdmModuleDbEntityList.get(i).getId());
                    mdmModuleDbFiledsEntity1.setSts("Y");
                    List<MdmModuleDbFiledsEntity> fileds = mdmModuleDbFiledsDao.queryBase(mdmModuleDbFiledsEntity1);
                    //查询单据规则
                    MdmTableCodeRuleEntity mdmTableCodeRuleEntity = new MdmTableCodeRuleEntity();
                    mdmTableCodeRuleEntity.setMdmId(mdmModuleEntity.getId());
                    //mdmTableCodeRuleEntity.setDbId(mdmModuleDbEntityList.get(i).getId());
                    mdmTableCodeRuleEntity.setSts("Y");
                    List<MdmTableCodeRuleEntity> allMdmTableCodeRuleEntities = mdmTableCodeRuleDao.queryBase(mdmTableCodeRuleEntity);
                    List<MdmTableCodeRuleEntity> mdmTableCodeRuleEntities = new ArrayList<>();
                    if (allMdmTableCodeRuleEntities != null && allMdmTableCodeRuleEntities.size() > 0) {
                        mdmTableCodeRuleEntities = allMdmTableCodeRuleEntities;
                        //for (int i1 = 0; i1 < allMdmTableCodeRuleEntities.size(); i1++) {
                        //    if(mdmModuleDbEntityList.get(i).getId().equals(allMdmTableCodeRuleEntities.get(i1).getDbId())){
                        //        mdmTableCodeRuleEntities.add(allMdmTableCodeRuleEntities.get(i1));
                        //    }
                        //}
                    }
                    StringBuffer document_rule = new StringBuffer();
                    Integer document_rule_num = 0;
                    if (mdmTableCodeRuleEntities != null && mdmTableCodeRuleEntities.size() > 0) {
                        for (int i1 = 0; i1 < mdmTableCodeRuleEntities.size(); i1++) {
                            //1、连接符号 2、字符串 3、日期 4、流水号
                            if ("1".equals(mdmTableCodeRuleEntities.get(i1).getDbType())) {
                                document_rule.append("-");
                            } else if ("2".equals(mdmTableCodeRuleEntities.get(i1).getDbType())) {
                                document_rule.append(mdmTableCodeRuleEntities.get(i1).getDbValue());
                            } else if ("3".equals(mdmTableCodeRuleEntities.get(i1).getDbType())) {
                                SimpleDateFormat sdf = new SimpleDateFormat(mdmTableCodeRuleEntities.get(i1).getDbValue());//要转换的时间格式
                                String stra = sdf.format(new Date());
                                document_rule.append(stra);
                            } else if ("4".equals(mdmTableCodeRuleEntities.get(i1).getDbType())) {
                                document_rule_num = mdmTableCodeRuleEntities.get(i1).getDbValue().length();
                            }
                        }
                    }
                    List<MdmDataFiledDto> mdmDataFiledDtos = new ArrayList<>();
                    for (int i1 = 0; i1 < fileds.size(); i1++) {
                        if (jsonObject.get(fileds.get(i1).getEnName()) != null) {
                            if (flag && "update_status".equals(fileds.get(i1).getEnName())) {
                                MdmDataFiledDto update_status = new MdmDataFiledDto();
                                update_status.setFiledsName("update_status");
                                update_status.setFiledsValue("0");
                                mdmDataFiledDtos.add(update_status);
                            } else if (flag && "data_status".equals(fileds.get(i1).getEnName())) {
                                MdmDataFiledDto data_status = new MdmDataFiledDto();
                                data_status.setFiledsName("data_status");
                                data_status.setFiledsValue("F");
                                mdmDataFiledDtos.add(data_status);
                            } else if (flag && "modify_user_id".equals(fileds.get(i1).getEnName())) {
                                MdmDataFiledDto data_status = new MdmDataFiledDto();
                                data_status.setFiledsName("modify_user_id");
                                data_status.setFiledsValue("F");
                                //mdmDataFiledDtos.add(data_status);
                            } else {
                                MdmDataFiledDto mdmDataFiledDto = new MdmDataFiledDto();
                                mdmDataFiledDto.setFiledsName(fileds.get(i1).getEnName());
                                mdmDataFiledDto.setFiledsValue(jsonObject.getString(fileds.get(i1).getEnName()));
                                mdmDataFiledDtos.add(mdmDataFiledDto);
                            }
                        }
                    }
                    MdmDataFiledDto mdmDataFiledDto = new MdmDataFiledDto();
                    mdmDataFiledDto.setFiledsName("logid");
                    mdmDataFiledDto.setFiledsValue(StpUtil.getLoginIdAsString());
                    mdmDataFiledDtos.add(mdmDataFiledDto);
                    mdmDataDto.setMdmDataFiledDtos(mdmDataFiledDtos);
                    if (flag) {
                        mdmDataDto.setId(jsonObject.getString("id"));
                        id = jsonObject.getString("id");


                        updateTreeUseData(mdmModuleEntity.getId(),mdmDataDto.getTableName(),id,mdmDataFiledDtos);



                        int a = mdmModuleDao.updateForm(mdmDataDto);
                    } else {

                        mdmDataDto.setDbType(mdmModuleDbEntityList.get(i).getDbType());
                        mdmDataDto.setDocumentRule(document_rule.toString());
                        mdmDataDto.setDocumentRuleNum(document_rule_num);
                        MdmDataFiledDto ids = new MdmDataFiledDto();
                        ids.setFiledsName("id");
                        ids.setFiledsValue(id);
                        mdmDataFiledDtos.add(ids);
                        MdmDataFiledDto data_status = new MdmDataFiledDto();
                        data_status.setFiledsName("data_status");
                        data_status.setFiledsValue("Y");
                        ;
                        mdmDataFiledDtos.add(data_status);
                        MdmDataFiledDto org_id = new MdmDataFiledDto();
                        org_id.setFiledsName("org_id");
                        org_id.setFiledsValue("Y");
                        ;
                        mdmDataFiledDtos.add(org_id);
                        MdmDataFiledDto company_id = new MdmDataFiledDto();
                        company_id.setFiledsName("company_id");
                        company_id.setFiledsValue("Y");
                        ;
                        mdmDataFiledDtos.add(company_id);

                        MdmDataFiledDto add_status = new MdmDataFiledDto();
                        add_status.setFiledsName("add_status");
                        add_status.setFiledsValue("0");
                        mdmDataFiledDtos.add(add_status);
                        MdmDataFiledDto update_status = new MdmDataFiledDto();
                        update_status.setFiledsName("update_status");
                        update_status.setFiledsValue("1");
                        mdmDataFiledDtos.add(update_status);
                        MdmDataFiledDto delete_status = new MdmDataFiledDto();
                        delete_status.setFiledsName("delete_status");
                        delete_status.setFiledsValue("1");
                        mdmDataFiledDtos.add(delete_status);
                        mdmDataDto.setMdmDataFiledDtos(mdmDataFiledDtos);
                        int a = mdmModuleDao.saveForm(mdmDataDto);
                    }
                }
            }

            for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                if ("2".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    //删除明细
                    FormmainDeleteDto formmainDeleteDto = new FormmainDeleteDto();
                    formmainDeleteDto.setFormmain_id(id);
                    formmainDeleteDto.setLoginId(StpUtil.getLoginIdAsString());
                    formmainDeleteDto.setTableName(mdmModuleDbEntityList.get(i).getDbName());
                    mdmModuleDao.deleteTemplateById(formmainDeleteDto);

                    JSONArray jsonArray = saveData.getJSONArray(mdmModuleDbEntityList.get(i).getDbName());
                    if (jsonArray != null && jsonArray.size() > 0) {
                        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity1 = new MdmModuleDbFiledsEntity();
                        mdmModuleDbFiledsEntity1.setMdmId(mdmModuleEntity.getId());
                        mdmModuleDbFiledsEntity1.setSts("Y");
                        List<MdmModuleDbFiledsEntity> allfileds = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity1);
                        List<MdmModuleDbFiledsEntity> fileds = new ArrayList<>();
                        if (allfileds != null && allfileds.size() > 0) {
                            for (int i1 = 0; i1 < allfileds.size(); i1++) {
                                if (mdmModuleDbEntityList.get(i).getId().equals(allfileds.get(i1).getDbId())) {
                                    fileds.add(allfileds.get(i1));
                                }
                            }
                        }

                        for (int i2 = 0; i2 < jsonArray.size(); i2++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i2);
                            //保存数据
                            MdmDataDto mdmDataDto = new MdmDataDto();
                            mdmDataDto.setTableName(mdmModuleDbEntityList.get(i).getDbName());
                            List<MdmDataFiledDto> mdmDataFiledDtos = new ArrayList<>();
                            for (int i1 = 0; i1 < fileds.size(); i1++) {
                                if ("id".equals(fileds.get(i1).getEnName())) {
                                    if (jsonObject.getString("id") != null) {
                                        MdmDataFiledDto ids = new MdmDataFiledDto();
                                        ids.setFiledsName("id");
                                        ids.setFiledsValue(jsonObject.getString("id"));
                                        mdmDataFiledDtos.add(ids);
                                    } else {
                                        MdmDataFiledDto ids = new MdmDataFiledDto();
                                        ids.setFiledsName("id");
                                        ids.setFiledsValue(UUIDUtils.getUUID());
                                        mdmDataFiledDtos.add(ids);
                                    }
                                } else if ("formmain_id".equals(fileds.get(i1).getEnName())) {
                                    MdmDataFiledDto formain_id = new MdmDataFiledDto();
                                    formain_id.setFiledsName("formmain_id");
                                    formain_id.setFiledsValue(id);
                                    mdmDataFiledDtos.add(formain_id);
                                } else if ("data_status".equals(fileds.get(i1).getEnName())) {
                                    if (jsonObject.getString("id") != null) {
                                        MdmDataFiledDto ids = new MdmDataFiledDto();
                                        ids.setFiledsName("data_status");
                                        ids.setFiledsValue("F");
                                        mdmDataFiledDtos.add(ids);
                                    } else {
                                        MdmDataFiledDto ids = new MdmDataFiledDto();
                                        ids.setFiledsName("data_status");
                                        ids.setFiledsValue("Y");
                                        mdmDataFiledDtos.add(ids);
                                    }
                                } else if ("sorts".equals(fileds.get(i1).getEnName())) {

                                } else if ("create_user_id".equals(fileds.get(i1).getEnName())) {
                                    //if (jsonObject.getString("id") != null) {
                                    //
                                    //} else {
                                    //    MdmDataFiledDto ids = new MdmDataFiledDto();
                                    //    ids.setFiledsName("create_user_id");
                                    //    ids.setFiledsValue(UUIDUtils.getUUID());
                                    //    mdmDataFiledDtos.add(ids);
                                    //}
                                } else if ("create_time".equals(fileds.get(i1).getEnName())) {

                                } else if ("modify_user_id".equals(fileds.get(i1).getEnName())) {
                                    //MdmDataFiledDto ids = new MdmDataFiledDto();
                                    //ids.setFiledsName("modify_user_id");
                                    //ids.setFiledsValue(UUIDUtils.getUUID());
                                    //mdmDataFiledDtos.add(ids);
                                } else if ("company_id".equals(fileds.get(i1).getEnName())) {
                                    MdmDataFiledDto ids = new MdmDataFiledDto();
                                    ids.setFiledsName("company_id");
                                    ids.setFiledsValue("0");
                                    mdmDataFiledDtos.add(ids);
                                } else if ("org_id".equals(fileds.get(i1).getEnName())) {
                                    MdmDataFiledDto ids = new MdmDataFiledDto();
                                    ids.setFiledsName("org_id");
                                    ids.setFiledsValue("0");
                                    mdmDataFiledDtos.add(ids);
                                } else if ("modify_time".equals(fileds.get(i1).getEnName())) {

                                } else if ("sts".equals(fileds.get(i1).getEnName())) {

                                } else {
                                    MdmDataFiledDto mdmDataFiledDto = new MdmDataFiledDto();
                                    mdmDataFiledDto.setFiledsName(fileds.get(i1).getEnName());
                                    mdmDataFiledDto.setFiledsValue(jsonObject.getString(fileds.get(i1).getEnName()));
                                    mdmDataFiledDtos.add(mdmDataFiledDto);
                                }
                            }
                            MdmDataFiledDto mdmDataFiledDto = new MdmDataFiledDto();
                            mdmDataFiledDto.setFiledsName("logid");
                            mdmDataFiledDto.setFiledsValue(StpUtil.getLoginIdAsString());
                            mdmDataFiledDtos.add(mdmDataFiledDto);

                            mdmDataDto.setMdmDataFiledDtos(mdmDataFiledDtos);
                            if (jsonObject.getString("id") != null) {
                                mdmDataDto.setId(jsonObject.getString("id"));

                                int a = mdmModuleDao.updateForm(mdmDataDto);
                            } else {
                                mdmDataDto.setDbType(mdmModuleDbEntityList.get(i).getDbType());
                                int a = mdmModuleDao.saveForm(mdmDataDto);
                            }
                        }
                    }
                }
            }
            saveOption(dbname, mdmCode, saveData, id, msg);
        }
        return str.toString();
    }

    /**
     * @Author lvleigang
     * @Description  修改树结构的下级以及其他使用地方数据
     * @Date 9:12 上午 2024/8/2
     * @param mdmId 主数据mdmId
     * @param tableName 主数据的表名
     * @param id 数据id
     * @param mdmDataFiledDtos 数据
     * @return void
     **/
    public void updateTreeUseData(String mdmId, String tableName, String id, List<MdmDataFiledDto> mdmDataFiledDtos) {
        //查询是否设置的树形
        MdmModuleViewEntity mdmModuleViewEntity = new MdmModuleViewEntity();
        mdmModuleViewEntity.setMdmId(mdmId);
        mdmModuleViewEntity.setViewName("1");
        mdmModuleViewEntity.setSts("Y");
        List<MdmModuleViewEntity> moduleViewEntityList = mdmModuleViewDao.queryBase(mdmModuleViewEntity);
        if (moduleViewEntityList != null && moduleViewEntityList.size() == 1) {
            mdmModuleViewEntity = moduleViewEntityList.get(0);
            //查询数据
            Map<String, Object> queryData = new HashMap<>();
            queryData.put("tableName", tableName);//表名
            queryData.put("detailFlag", false);//是否明细
            queryData.put("id", id);//字段
            HashMap<String, Object> datas = mdmModuleDbDao.getServiceDataById(queryData);
            convertKeysToLowerCase(datas);
            //对比值是否有修改
            boolean upFlag = false;
            String oldData = "";

            String newData = "";

            for (int i1 = 0; i1 < mdmDataFiledDtos.size(); i1++) {
                if (mdmDataFiledDtos.get(i1).getFiledsName().equals(mdmModuleViewEntity.getUpIdFiled())) {
                    newData = mdmDataFiledDtos.get(i1).getFiledsValue();
                    oldData = datas.get(mdmDataFiledDtos.get(i1).getFiledsName()).toString();

                    if (mdmDataFiledDtos.get(i1).getFiledsValue().equals(datas.get(mdmDataFiledDtos.get(i1).getFiledsName()).toString())) {
                        upFlag = true;
                        break;
                    }
                }
            }

            if (!"".equals(oldData)) {
                oldData = oldData + "," + id;
            } else {
                oldData = id;
            }
            if (!"".equals(newData)) {
                newData = newData + "," + id;
            } else {
                newData = id;
            }
            if (!upFlag) {
                //查询引用地方
                Map<String, String> maps = new HashMap<>();
                maps.put("service", tableName);
                maps.put("upId", mdmModuleViewEntity.getUpIdFiled());
                //maps.put("mdmId",mdmModuleEntity.getId());
                List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntities = mdmModuleDbFiledsRuleDao.queryServiceQuote(maps);
                if (mdmModuleDbFiledsRuleEntities != null && mdmModuleDbFiledsRuleEntities.size() > 0) {
                    for (int i1 = 0; i1 < mdmModuleDbFiledsRuleEntities.size(); i1++) {
                        //查询字段
                        String upFiled = "";

                        MdmModuleDbFiledsEntity moduleDbFiledsEntity = new MdmModuleDbFiledsEntity();
                        moduleDbFiledsEntity.setMdmId(mdmModuleDbFiledsRuleEntities.get(i1).getMdmId());
                        moduleDbFiledsEntity.setSts("Y");
                        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(moduleDbFiledsEntity);
                        if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
                            for (int i2 = 0; i2 < mdmModuleDbFiledsEntities.size(); i2++) {
                                if (mdmModuleDbFiledsRuleEntities.get(i1).getFiledId().equals(mdmModuleDbFiledsEntities.get(i2).getId())) {
                                    upFiled = mdmModuleDbFiledsEntities.get(i2).getEnName();
                                }
                            }
                        }
                        //修改引用数据
                        //查看是否主表
                        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
                        mdmModuleDbEntity.setMdmId(mdmModuleDbFiledsRuleEntities.get(i1).getMdmId());
                        mdmModuleDbEntity.setSts("Y");
                        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
                        //设置主表
                        String mainDBTable = "";
                        String dataDBTable = "";
                        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
                            for (int i2 = 0; i2 < mdmModuleDbEntities.size(); i2++) {
                                //主表
                                if ("1".equals(mdmModuleDbEntities.get(i2).getDbType())) {
                                    mainDBTable = mdmModuleDbEntities.get(i2).getDbName();
                                }
                                if (mdmModuleDbFiledsRuleEntities.get(i1).getDbId().equals(mdmModuleDbEntities.get(i2).getId())) {
                                    dataDBTable = mdmModuleDbEntities.get(i2).getDbName();
                                }
                            }
                        }

                        if (mainDBTable.equals(dataDBTable)) {//主表上的字段
                            Map<String, String> updateMaps = new HashMap<>();
                            updateMaps.put("tableName", dataDBTable);
                            updateMaps.put("upFiled", upFiled);
                            updateMaps.put("oldData", oldData);
                            updateMaps.put("newData", newData);
                            updateMaps.put("modify_user_id", StpUtil.getLoginIdAsString());
                            mdmModuleDao.updataTreeUpData(updateMaps);
                        } else {//明细表上的字段
                            Map<String, String> updateMaps = new HashMap<>();
                            updateMaps.put("mainDBTable", mainDBTable);
                            updateMaps.put("tableName", dataDBTable);
                            updateMaps.put("upFiled", upFiled);
                            updateMaps.put("oldData", oldData);
                            updateMaps.put("newData", newData);
                            updateMaps.put("modify_user_id", StpUtil.getLoginIdAsString());
                            mdmModuleDao.updataTreeUpDataDetail(updateMaps);


                        }

                    }
                }
            }

        }
    }


    /**
     * @param jsonObject
     * @return boolean
     * @Author lvleigang
     * @Description 校验是否有值
     * @Date 2:20 下午 2023/7/10
     **/
    private boolean checkData(JSONObject jsonObject, String key) {
        if (jsonObject != null
                && jsonObject.getString(key) != null
                && !"".equals(jsonObject.getString(key))) {
            return true;
        }
        return false;
    }

    protected <T> T getData(String key, JSONObject jsonObject, Class<T> clz) {
        if (checkStr(jsonObject.getString(key))) {
            return jsonObject.getJSONObject(key).toJavaObject(clz);
        }
        return null;
    }

    /**
     * @param key
     * @param object
     * @return com.alibaba.fastjson.JSONObject
     * @Author lvleigang
     * @Description 获取对象转换成jsonobj
     * @Date 11:51 下午 2023/7/10
     **/
    protected JSONObject getstrObj(String key, JSONObject object) {
        if (checkStr(object.getString(key))) {
            return object.getJSONObject(key);
        }
        return new JSONObject();
    }

    /**
     * @param str
     * @return void
     * @Author lvleigang
     * @Description 校验字符串
     * @Date 11:41 上午 2022/12/7
     **/
    protected Boolean checkStr(String str) {
        Boolean flag = true;
        if (str == null || "".equals(str)) {
            flag = false;
        }
        return flag;
    }
    ///***
    // * 查询所有的字段根据表名称
    // * @content:
    // * @author 👻👻👻👻👻👻👻👻 gjh
    // * @date  2024-06-27 16:29
    // * @param
    // * @return java.util.List<com.alibaba.fastjson.JSONObject>
    // **/
    //private List<String> queryMdMFields(Map<String, Object> maps){
    //    return  mdmModuleDao.queryMdMFields(maps);
    //}
}
