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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.Method;
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.dateutil.DateUtil;
import com.hzya.frame.sys.contrastTask.dao.ISysContrastTaskDao;
import com.hzya.frame.sys.contrastTask.entity.SysContrastTaskEntity;
import com.hzya.frame.sys.contrastTask.service.ISysContrastTaskService;
import com.hzya.frame.sys.databaseFields.dao.IDatabaseFieldsMapper;
import com.hzya.frame.sys.databaseFields.entity.DatabaseFields;
import com.hzya.frame.sys.databaseNames.dao.IDatabaseNamesMapper;
import com.hzya.frame.sys.databaseNames.entity.DatabaseNames;
import com.hzya.frame.sys.entity.DebugApiDto;
import com.hzya.frame.sys.entity.FormmainCheckDto;
import com.hzya.frame.sys.entity.FormmainDeleteDto;
import com.hzya.frame.sys.entity.FormmainDto;
import com.hzya.frame.sys.entity.FormmainFiledsDto;
import com.hzya.frame.sys.entity.ModuleDto;
import com.hzya.frame.sys.entity.TemplateKeyValue;
import com.hzya.frame.sys.entity.TemplateQuery;
import com.hzya.frame.sys.entity.TemplateSave;
import com.hzya.frame.sys.entity.TemplateSaveFields;
import com.hzya.frame.sys.entity.TemplateSaveFieldsOption;
import com.hzya.frame.sys.service.IOptionService;
import com.hzya.frame.sys.template.dao.ITemplateMapper;
import com.hzya.frame.sys.template.entity.Template;
import com.hzya.frame.sys.templateButton.dao.ISysTemplateButtonDao;
import com.hzya.frame.sys.templateButton.entity.SysTemplateButtonEntity;
import com.hzya.frame.sys.templateButton.entity.SysTemplateButtonVo;
import com.hzya.frame.sys.templateFields.dao.ITemplateFieldsMapper;
import com.hzya.frame.sys.templateFields.entity.TemplateDto;
import com.hzya.frame.sys.templateFields.entity.TemplateFields;
import com.hzya.frame.sys.templateRelation.dao.ITemplateRelationMapper;
import com.hzya.frame.sys.templateRelation.entity.TemplateRelation;
import com.hzya.frame.sys.templateView.dao.ITemplateViewMapper;
import com.hzya.frame.sys.templateView.entity.TemplateView;
import com.hzya.frame.sys.templateViewAccredit.dao.ITemplateViewAccreditMapper;
import com.hzya.frame.sys.templateViewAccredit.entity.TemplateViewAccredit;
import com.hzya.frame.sys.testReturn.entity.TestReturn;
import com.hzya.frame.sys.testReturn.service.ITestReturnService;
import com.hzya.frame.sys.userTemplateButton.dao.ISysUserTemplateButtonDao;
import com.hzya.frame.sys.userTemplateButton.entity.SysUserTemplateButtonEntity;
import com.hzya.frame.sys.userTemplateView.dao.IUserTemplateViewMapper;
import com.hzya.frame.sys.userTemplateView.entity.UserTemplateView;
import com.hzya.frame.uuid.UUIDUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import com.hzya.frame.serviceUtil.ServiceUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description: 系统主要接口
 */
@Service(value = "optionService")
public class OptionServiceImpl extends ServiceUtil implements IOptionService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ITestReturnService testReturnService;

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 根据用户id查询菜单树
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryTreeById(JSONObject jsonObject) {
        TestReturn testReturn = new TestReturn();
        testReturn.setType("module");
        testReturn = testReturnService.getEntity(testReturn);
        if (testReturn != null && testReturn.getValue() != null) {
            JSONObject object = JSONObject.parseObject(testReturn.getValue());
            return BaseResult.getSuccessMessageEntity("获取菜单成功", object);
        } else {
            return BaseResult.getFailureMessageEntity("请先初始化数据");
        }
    }


    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询list表头
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryTableHeader(JSONObject jsonObject) {
        TestReturn testReturn = new TestReturn();
        testReturn.setType("tableHeader");
        testReturn = testReturnService.getEntity(testReturn);
        if (testReturn != null && testReturn.getValue() != null) {
            JSONArray object = JSONArray.parseArray(testReturn.getValue());
            return BaseResult.getSuccessMessageEntity("获取菜单成功", object);
        } else {
            return BaseResult.getFailureMessageEntity("请先初始化数据");
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询字段
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryColumn(JSONObject jsonObject) {
        TestReturn testReturn = new TestReturn();
        testReturn.setType("column");
        testReturn = testReturnService.getEntity(testReturn);
        if (testReturn != null && testReturn.getValue() != null) {
            String str = testReturn.getValue().trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                JSONObject object = JSONObject.parseObject(str);
                return BaseResult.getSuccessMessageEntity("获取字段成功", object);
            } else if (str.startsWith("[") && str.endsWith("]")) {
                JSONArray object = JSONArray.parseArray(str);
                return BaseResult.getSuccessMessageEntity("获取字段成功", object);
            }
            return BaseResult.getSuccessMessageEntity("数据类型转换错误");
        } else {
            return BaseResult.getFailureMessageEntity("请先初始化数据");
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询按钮
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryButton(JSONObject jsonObject) {
        TestReturn testReturn = new TestReturn();
        testReturn.setType("button");
        testReturn = testReturnService.getEntity(testReturn);
        if (testReturn != null && testReturn.getValue() != null) {
            String str = testReturn.getValue().trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                JSONObject object = JSONObject.parseObject(str);
                return BaseResult.getSuccessMessageEntity("获取按钮成功", object);
            } else if (str.startsWith("[") && str.endsWith("]")) {
                JSONArray object = JSONArray.parseArray(str);
                return BaseResult.getSuccessMessageEntity("获取按钮成功", object);
            }
            return BaseResult.getSuccessMessageEntity("数据类型转换错误");
        } else {
            return BaseResult.getFailureMessageEntity("请先初始化数据");
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询 查询条件
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryQueryCondition(JSONObject jsonObject) {
        TestReturn testReturn = new TestReturn();
        testReturn.setType("queryCondition");
        testReturn = testReturnService.getEntity(testReturn);
        if (testReturn != null && testReturn.getValue() != null) {
            JSONArray object = JSONArray.parseArray(testReturn.getValue());
            return BaseResult.getSuccessMessageEntity("获取菜单成功", object);
        } else {
            return BaseResult.getFailureMessageEntity("请先初始化数据");
        }
    }


    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询按钮
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryTableButton(JSONObject jsonObject) {
        return gettestdata(jsonObject, "tableButton");
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询按钮
     * @Date 9:48 上午 2023/6/29
     **/
    @Override
    public JsonResultEntity queryTableData(JSONObject jsonObject) {
        return gettestdata(jsonObject, "tableData");
    }


    private JsonResultEntity gettestdata(JSONObject jsonObject, String button) {
        TestReturn testReturn = new TestReturn();
        testReturn.setType(button);
        testReturn = testReturnService.getEntity(testReturn);
        if (testReturn != null && testReturn.getValue() != null) {
            String str = testReturn.getValue().trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                JSONObject object = JSONObject.parseObject(str);
                return BaseResult.getSuccessMessageEntity("获取成功", object);
            } else if (str.startsWith("[") && str.endsWith("]")) {
                JSONArray object = JSONArray.parseArray(str);
                return BaseResult.getSuccessMessageEntity("获取成功", object);
            }
            return BaseResult.getSuccessMessageEntity("数据类型转换错误");
        } else {
            return BaseResult.getFailureMessageEntity("请先初始化数据");
        }
    }


    //********************************************************

    // 正式接口

    @Resource
    protected ITemplateViewMapper templateviewMapper;//模板视图表
    @Resource
    protected IUserTemplateViewMapper usertemplateviewMapper;//用户模板视图展示表
    @Resource
    protected ITemplateViewAccreditMapper templateviewaccreditMapper;//模板视图授权
    @Resource
    protected ITemplateRelationMapper templaterelationMapper;//模版关系表
    @Resource
    protected ITemplateFieldsMapper templatefieldsMapper;//模版字段表
    @Resource
    protected IDatabaseNamesMapper databasenamesMapper;//数据库表名表
    @Resource
    protected ITemplateMapper templateMapper;//模版表
    @Resource
    protected IDatabaseFieldsMapper databaseFieldsMapper;//数据库字段表
    @Resource
    private ISysContrastTaskDao sysContrastTaskDao;
    @Resource
    private ISysTemplateButtonDao sysTemplateButtonDao;
    @Resource
    private ISysUserTemplateButtonDao sysUserTemplateButtonDao;
    @Resource
    private TaskExecutor taskExecutor;
    @Resource
    private ISysContrastTaskService sysContrastTaskService;
    //模版树的数据结构
    private String templateTree = "{\"id\":\"id\",\"label\":\"templateName\",\"service\":\"serviceName\",\"upid\":null,\"children\":null}";

    /**
     * @param object
     * @return
     * @Author lvleigang
     * @Description 获取用户模版数据
     * @Date 9:34 上午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryUserTemplate(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        if (!checkData(jsonObject, "templateTypes")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询字段类型");
        }
        //1、查询模板视图授权表，查询用户分配的模版
        TemplateViewAccredit templateViewAccredit = new TemplateViewAccredit();
        templateViewAccredit.setUserId(StpUtil.getLoginIdAsString());
        templateViewAccredit.setSts("Y");
        List<TemplateViewAccredit> templateViewAccreditList = templateviewaccreditMapper.entity_list_base(templateViewAccredit);

        //2、根据用户分配的模版查询模版表
        List<Template> templateList = null;
        if (templateViewAccreditList != null && templateViewAccreditList.size() > 0) {
            Template template = new Template();
            template.setSts("Y");
            JSONArray array = jsonObject.getJSONArray("templateTypes");
            template.setTemplateTypes(array.toJavaList(String.class));
            template.setTemplateViewAccreditList(templateViewAccreditList);
            templateList = templateMapper.entity_list_base(template);
        }
        //3、转换数据结构
        List<JSONObject> changeData = changeTreeData(templateList);

        return BaseResult.getSuccessMessageEntity("获取用户模版成功", changeData);
    }


    /**
     * @param
     * @return java.util.List<java.lang.Object>
     * @Author lvleigang
     * @Description 转换数据结构
     * @Date 10:39 上午 2023/7/10
     **/
    private List<JSONObject> changeTreeData(List<Template> templateList) {
        List<JSONObject> returnData = new ArrayList<>();

        if (templateList == null || templateList.size() == 0) {
            return returnData;
        }
        for (int i = 0; i < templateList.size(); i++) {
            JSONObject object = new JSONObject();
            object.put("id", templateList.get(i).getId());
            object.put("label", templateList.get(i).getTemplateName());
            object.put("service", templateList.get(i).getServiceName());
            object.put("upid", null);
            object.put("children", new ArrayList<>());
            returnData.add(object);
        }
        return returnData;
    }

    /**
     * @param jsonObject
     * @return
     * @Author lvleigang
     * @Description 获取所有模版数据
     * @Date 9:34 上午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryAllTemplate(JSONObject jsonObject) {
        List<Template> templateList = null;
        Template template = new Template();
        template.setSts("Y");
        templateList = templateMapper.entity_list_base(template);
        //转换数据结构
        List<JSONObject> changeData = changeTreeData(templateList);
        return BaseResult.getSuccessMessageEntity("获取模版成功", changeData);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取用户模版字段（查询、列表、新增、修改，查看）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryUserTemplateFields(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "templateViewType")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询字段类型");
        }
        //查询用户授权的字段
        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setUserId(StpUtil.getLoginIdAsString());
        userTemplateView.setSts("Y");
        userTemplateView.setTemplateViewType(jsonObject.getString("templateViewType"));
        userTemplateView.setServiceName(jsonObject.getString("service"));
        Integer filedCount = usertemplateviewMapper.entity_count(userTemplateView);

        List<JSONObject> jsonObjectList = null;
        if (filedCount > 0) {
            jsonObjectList = queryServiceFields(true, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"));
        } else {
            jsonObjectList = queryServiceFields(false, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"));
        }
        return BaseResult.getSuccessMessageEntity("获取模版字段成功", jsonObjectList);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取用户模版按钮
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryUserTemplateButtons(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        //查询用户授权的字段
        SysUserTemplateButtonEntity templateButtonEntity = new SysUserTemplateButtonEntity();
        templateButtonEntity.setUserId(StpUtil.getLoginIdAsString());
        templateButtonEntity.setSts("Y");
        templateButtonEntity.setServiceName(jsonObject.getString("service"));
        List<SysUserTemplateButtonEntity> sysUserTemplateButtonEntities = sysUserTemplateButtonDao.query(templateButtonEntity);
        SysTemplateButtonEntity sysTemplateButtonEntity = new SysTemplateButtonEntity();
        sysTemplateButtonEntity.setSts("Y");
        sysTemplateButtonEntity.setServiceName(jsonObject.getString("service"));
        sysTemplateButtonEntity.setSysUserTemplateButtonEntities(sysUserTemplateButtonEntities);
        sysTemplateButtonEntity.setSort("button_sort");
        sysTemplateButtonEntity.setOrder("asc");
        List<SysTemplateButtonEntity> sysTemplateButtonEntities = sysTemplateButtonDao.query(sysTemplateButtonEntity);
        List<SysTemplateButtonVo> sysTemplateButtonVos = setButtonView(sysTemplateButtonEntities);
        return BaseResult.getSuccessMessageEntity("获取模版按钮成功", sysTemplateButtonVos);
    }

    private List<SysTemplateButtonVo> setButtonView(List<SysTemplateButtonEntity> sysTemplateButtonEntities) {
        List<SysTemplateButtonVo> sysTemplateButtonVos = new ArrayList<>();
        if (sysTemplateButtonEntities != null && sysTemplateButtonEntities.size() > 0) {
            for (int i = 0; i < sysTemplateButtonEntities.size(); i++) {

                SysTemplateButtonVo sysTemplateButtonVo = new SysTemplateButtonVo();
                sysTemplateButtonVo.setType(sysTemplateButtonEntities.get(i).getButtonType());
                sysTemplateButtonVo.setTitle(sysTemplateButtonEntities.get(i).getButtonTitle());
                sysTemplateButtonVo.setStyle(sysTemplateButtonEntities.get(i).getButtonStyle());
                sysTemplateButtonVo.setSort(sysTemplateButtonEntities.get(i).getButtonSort());
                sysTemplateButtonVo.setIcon(sysTemplateButtonEntities.get(i).getButtonIcon());
                sysTemplateButtonVos.add(sysTemplateButtonVo);
            }
        }
        return sysTemplateButtonVos;
    }


    /**
     * @param userFlag
     * @param loginIdAsString
     * @param templateViewType
     * @param service
     * @return java.util.List<com.hzya.frame.sys.templateFields.entity.TemplateFields>
     * @Author lvleigang
     * @Description 查询字段
     * @Date 4:39 下午 2023/7/10
     **/
    private List<JSONObject> queryServiceFields(boolean userFlag, String loginIdAsString, String templateViewType, String service) {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<TemplateFields> templateFieldsList = null;
        if (userFlag) {
            //用户设置了字段，直接用用户字段以及用户的排序
            TemplateDto templateDto = new TemplateDto();
            templateDto.setUserId(loginIdAsString);
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryUserTemplateFields(templateDto);
        } else {
            //用户没有设置了字段采用模版的字段
            TemplateDto templateDto = new TemplateDto();
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        }
        Integer tableNum = 1;
        if (templateFieldsList != null && templateFieldsList.size() > 0) {
            for (int i = 0; i < templateFieldsList.size(); i++) {
                if ("5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                    if (!"1".equals(templateViewType) && !"2".equals(templateViewType)) {
                        JSONObject object = JSONObject.parseObject(templateFieldsList.get(i).getFieldsOption());
                        object.put("id", templateFieldsList.get(i).getDetailFields());//字段名称
                        object.put("title", templateFieldsList.get(i).getFieldsRemark());//字段名称
                        object.put("index", tableNum++);//字段名称
                        object.put("detailFields", queryServiceFields(userFlag, loginIdAsString, templateViewType, templateFieldsList.get(i).getDetailService()));//明细字段列表
                        jsonObjectList.add(object);
                    }
                } else {
                    JSONObject object = JSONObject.parseObject(templateFieldsList.get(i).getFieldsOption());
                    //object.put("fieldsName",templateFieldsList.get(i).getDetailFields());//字段名称
                    //object.put("detailFields",queryServiceFields(userFlag,loginIdAsString,templateViewType,templateFieldsList.get(i).getDetailService()));//明细字段列表
                    jsonObjectList.add(object);
                }
            }
        }
        return jsonObjectList;
    }

    /**
     * @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;
    }


    /**
     * @param
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取模版所有字段 （查询、列表、新增、修改，查看）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryAllTemplateFields(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "templateViewType")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询字段类型");
        }
        List<JSONObject> jsonObjectList = queryServiceFields(false, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"));
        return BaseResult.getSuccessMessageEntity("获取模版字段成功", jsonObjectList);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 添加模版以及模版类型数据结构（先删除后添加）（默认分配给管理员admin）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity addTemplateAll(JSONObject object) {
        TemplateSave entity = getData("jsonStr", object, TemplateSave.class);
        if (saveService(entity, false, null, null, null)) {
            return BaseResult.getSuccessMessageEntity("获取新增数据成功");
        } else {
            return BaseResult.getFailureMessageEntity("服务已经存在");
        }
    }

    /**
     * @param templateSaveList
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 添加模版以及模版类型数据结构（先删除后添加）（默认分配给管理员admin）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity testTemplateAll(List<TemplateSave> templateSaveList) {
        for (int i = 0; i < templateSaveList.size(); i++) {
            saveService(templateSaveList.get(i), false, null, null, null);
        }
        return BaseResult.getSuccessMessageEntity("获取新增数据成功");
    }

    @Override
    public void deleteData(String serviceName, boolean flag) {
        Template template = new Template();
        template.setSts("Y");
        template.setServiceName(serviceName);
        List<Template> templateList = templateMapper.entity_list_base(template);
        if (flag) {
            templateMapper.dropTable(serviceName);
        }
        DatabaseNames databaseNames = new DatabaseNames();
        databaseNames.setServiceName(serviceName);
        databaseNames.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        databasenamesMapper.entity_logicDelete_Multi_Condition(databaseNames);

        SysTemplateButtonEntity tem1 = new SysTemplateButtonEntity();
        tem1.setServiceName(serviceName);
        tem1.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        sysTemplateButtonDao.logicRemoveMultiCondition(tem1);

        SysUserTemplateButtonEntity u1 = new SysUserTemplateButtonEntity();
        u1.setServiceName(serviceName);
        u1.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        sysUserTemplateButtonDao.logicRemoveMultiCondition(u1);

        DatabaseFields databaseFields = new DatabaseFields();
        databaseFields.setServiceName(serviceName);
        databaseFields.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        databaseFieldsMapper.entity_logicDelete_Multi_Condition(databaseFields);

        Template template1 = new Template();
        template1.setServiceName(serviceName);
        template1.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        templateMapper.entity_logicDelete_Multi_Condition(template1);

        TemplateRelation templateRelation = new TemplateRelation();
        templateRelation.setParentServiceName(serviceName);
        templateRelation.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        templaterelationMapper.entity_logicDelete_Multi_Condition(templateRelation);

        TemplateFields templateFields = new TemplateFields();
        templateFields.setServiceName(serviceName);
        templateFields.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        templatefieldsMapper.entity_logicDelete_Multi_Condition(templateFields);

        TemplateView templateView = new TemplateView();
        templateView.setServiceName(serviceName);
        templateView.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        templateviewMapper.entity_logicDelete_Multi_Condition(templateView);

        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setServiceName(serviceName);
        userTemplateView.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
        usertemplateviewMapper.entity_logicDelete_Multi_Condition(userTemplateView);
        if (templateList != null && templateList.size() == 1) {
            TemplateViewAccredit templateViewAccredit = new TemplateViewAccredit();
            templateViewAccredit.setTemplateId(templateList.get(0).getId());
            templateViewAccredit.setModifyUserId("c796fd9ba4c9f5ff3cc2fa41a040e443");
            templateviewaccreditMapper.entity_logicDelete_Multi_Condition(templateViewAccredit);
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取模版对应service的某条数据（包括明细）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryTemplateServiceData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "templateViewType")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询字段类型");
        }
        if (!checkData(jsonObject, "id")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询数据id");
        }
        //查询用户授权的字段
        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setUserId(StpUtil.getLoginIdAsString());
        userTemplateView.setSts("Y");
        userTemplateView.setTemplateViewType(jsonObject.getString("templateViewType"));
        userTemplateView.setServiceName(jsonObject.getString("service"));
        Integer filedCount = usertemplateviewMapper.entity_count(userTemplateView);

        Object jsonObjectList = null;
        if (filedCount > 0) {
            jsonObjectList = queryServiceData(true, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"), jsonObject.getString("id"), false);
        } else {
            jsonObjectList = queryServiceData(false, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"), jsonObject.getString("id"), false);
        }
        return BaseResult.getSuccessMessageEntity("获取数据成功", jsonObjectList);
    }

    /**
     * @param userFlag
     * @param loginIdAsString
     * @param templateViewType
     * @param service
     * @return java.util.List<com.hzya.frame.sys.templateFields.entity.TemplateFields>
     * @Author lvleigang
     * @Description 查询字段
     * @Date 4:39 下午 2023/7/10
     **/
    private Object queryServiceDataDetail(boolean userFlag, String loginIdAsString, String templateViewType, String service, String id, boolean detailFlag) {

        List<TemplateFields> templateFieldsList = null;
        if (userFlag) {
            //用户设置了字段，直接用用户字段以及用户的排序
            TemplateDto templateDto = new TemplateDto();
            templateDto.setUserId(loginIdAsString);
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryUserTemplateFields(templateDto);
        } else {
            //用户没有设置了字段采用模版的字段
            TemplateDto templateDto = new TemplateDto();
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        }
        if (templateFieldsList != null && templateFieldsList.size() > 0) {
            //查询主表
            //根据service获取table
            String tableName = templateMapper.getTableName(service);
            if (tableName != null && !"".equals(tableName)) {
                //查询数据
                Map<String, Object> queryData = new HashMap<>();
                queryData.put("tableName", tableName);//表名
                queryData.put("templateFieldsList", templateFieldsList);//字段
                queryData.put("detailFlag", detailFlag);//是否明细
                queryData.put("id", id);//字段
                List<HashMap<String, Object>> datas = templateMapper.getServiceTemplateDataByFormmainId(queryData);
                //for (int i = 0; i < templateFieldsList.size(); i++) {
                //    if ("5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                //        datas.put(templateFieldsList.get(i).getFieldsName(),
                //                queryServiceDataDetail(userFlag,loginIdAsString, templateViewType,templateFieldsList.get(i).getDetailService(), datas.get("id").toString(),true));
                //    }
                //}
                return datas;
            }
        }
        return new Object();
    }

    /**
     * @param userFlag
     * @param loginIdAsString
     * @param templateViewType
     * @param service
     * @return java.util.List<com.hzya.frame.sys.templateFields.entity.TemplateFields>
     * @Author lvleigang
     * @Description 查询字段
     * @Date 4:39 下午 2023/7/10
     **/
    private Object queryServiceData(boolean userFlag, String loginIdAsString, String templateViewType, String service, String id, boolean detailFlag) {

        List<TemplateFields> templateFieldsList = null;
        if (userFlag) {
            //用户设置了字段，直接用用户字段以及用户的排序
            TemplateDto templateDto = new TemplateDto();
            templateDto.setUserId(loginIdAsString);
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryUserTemplateFields(templateDto);
        } else {
            //用户没有设置了字段采用模版的字段
            TemplateDto templateDto = new TemplateDto();
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        }
        if (templateFieldsList != null && templateFieldsList.size() > 0) {
            //查询主表
            //根据service获取table
            String tableName = templateMapper.getTableName(service);
            if (tableName != null && !"".equals(tableName)) {
                //查询数据
                Map<String, Object> queryData = new HashMap<>();
                queryData.put("tableName", tableName);//表名
                queryData.put("templateFieldsList", templateFieldsList);//字段
                queryData.put("detailFlag", detailFlag);//是否明细
                queryData.put("id", id);//字段
                HashMap<String, Object> datas = templateMapper.getServiceTemplateDataById(queryData);
                for (int i = 0; i < templateFieldsList.size(); i++) {
                    if ("5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                        datas.put(templateFieldsList.get(i).getFieldsName(),
                                queryServiceDataDetail(userFlag, loginIdAsString, templateViewType, templateFieldsList.get(i).getDetailService(), datas.get("id").toString(), true));
                    }
                }
                return datas;
            }
        }
        return new Object();
    }

    /**
     * @param entity
     * @param flag
     * @return void
     * @Author lvleigang
     * @Description 保存数据区分是否是明细表
     * @Date 2:26 下午 2023/7/11
     **/
    private boolean saveService(TemplateSave entity, boolean flag, String templateId, String parentServiceName, String parentTableName) {
        //设置数据库表名

        //校验Service
        DatabaseNames checkDatabaseNames = new DatabaseNames();
        checkDatabaseNames.setServiceName(entity.getServiceName());
        checkDatabaseNames.setSts("Y");
        Integer checkCount = databasenamesMapper.entity_count(checkDatabaseNames);
        if (checkCount != null && checkCount > 0) {
            return false;
        }
        DatabaseNames databaseNames = new DatabaseNames();
        databaseNames.setCreate();
        databaseNames.setSorts(getMaxSorts("sys_database_names", null));//Service服务名
        databaseNames.setServiceName(entity.getServiceName());//Service服务名
        databaseNames.setTableName(entity.getTableName());//表名（英文名称）
        databaseNames.setTableRemark(entity.getTableRemark());//表名（英文名称）
        databaseNames.setRemark(entity.getTableRemark());//表名（英文名称）
        databasenamesMapper.entity_insert(databaseNames);
        //设置数据库表字段
        List<DatabaseFields> allDatabaseFields = new ArrayList<>();

        List<DatabaseFields> userDatabaseFields = new ArrayList<>();
        for (int i = 0; i < entity.getTemplateSaveFieldsList().size(); i++) {
            TemplateSaveFields templateSaveFields = entity.getTemplateSaveFieldsList().get(i);
            DatabaseFields databaseFields = new DatabaseFields();
            databaseFields.setCreate();
            databaseFields.setServiceName(entity.getServiceName());//Service服务名
            databaseFields.setTableName(entity.getTableName());//表名（英文名称）
            databaseFields.setFieldsName(templateSaveFields.getFieldsName());//字段名称
            databaseFields.setFieldsRemark(templateSaveFields.getFieldsRemark());//字段备注
            databaseFields.setFieldsType(templateSaveFields.getFieldsType());//类型
            databaseFields.setFieldsLength(templateSaveFields.getFieldsLength());//长度
            databaseFields.setFieldsOption(templateSaveFields.getFieldsOption().getOptions());//属性
            databaseFields.setFieldsRequired(templateSaveFields.getFieldsRequired());//属性
            databaseFields.setFieldsSole(templateSaveFields.getFieldsSole());//属性
            databaseFields.setFieldsOption(templateSaveFields.getFieldsOption().getOptions());//属性
            databaseFields.setSorts(i + 2L);//排序
            userDatabaseFields.add(databaseFields);
        }
        createAllFields(allDatabaseFields, userDatabaseFields, entity, flag);
        for (int i = 0; i < allDatabaseFields.size(); i++) {
            databaseFieldsMapper.entity_insert(allDatabaseFields.get(i));
        }
        //创建语句
        createTable(databaseNames, allDatabaseFields);
        //设置模版表
        Template template = new Template();
        template.setCreate();
        template.setSorts(getMaxSorts("sys_template", null));//模版名称
        template.setTemplateName(entity.getTableRemark());//模版名称
        template.setTemplateCode(entity.getTableName());//模板编码
        template.setTemplateType(entity.getTemplateType());// 类型		1单据,2档案
        template.setServiceName(entity.getServiceName());// Service服务名
        template.setTableName(entity.getTableName());// 表名（英文名称）
        templateMapper.entity_insert(template);
        //模版关系表
        if (flag) {//正确说明是明细表需要添加关系表
            TemplateRelation templateRelation = new TemplateRelation();
            templateRelation.setCreate();
            templateRelation.setSorts(getMaxSorts("sys_template_relation", null));//模版名称
            templateRelation.setParentServiceName(parentServiceName);//Service服务名
            templateRelation.setParentTableName(parentTableName);//Service服务名
            templateRelation.setParentTableFields(entity.getServiceName() + "_Detail");//主表逻辑字段，返回前台字段
            templateRelation.setParentTableRemark(entity.getTableRemark());//主表逻辑字段，返回前台字段
            templateRelation.setChildServiceName(entity.getServiceName());//主表逻辑字段，返回前台字段
            templateRelation.setChildTableName(entity.getTableName());//主表逻辑字段，返回前台字段
            templaterelationMapper.entity_insert(templateRelation);
        }

        List<TemplateFields> templateFieldsList = new ArrayList<>();
        TemplateFields id = new TemplateFields();
        id.setCreate();
        id.setTemplateId(template.getId());//模版id
        id.setServiceName(entity.getServiceName());//Service服务名
        id.setTableName(entity.getTableName());//Table名称（英文名称）
        id.setTableName(entity.getTableName());//Table名称（英文名称）
        id.setFieldsId(allDatabaseFields.get(0).getId());//字段表id
        id.setDatabaseId(databaseNames.getId());//数据库表id
        id.setFieldsName("id");//字段名称
        id.setFieldsRemark("id");//字段备注
        id.setFieldsType("1");//类型 Vachar,number,decimal,datetime,array(如为array字段需设置明细数据)
        id.setFieldsLength("50");//长度
        id.setFieldsRequired("1");//是否必填
        id.setFieldsSole("1");//是否唯一性
        id.setFieldsOption("{\"id\":\"id\",\"type\":\"input\",\"title\":\"id\",\"hidden\":true,\"tableSort\":\"0\",\"disabled\":true,\"required\":true,\"row\":12}");//字段属性 设置字段的渲染属性，存json Todo
        id.setSorts(allDatabaseFields.get(0).getSorts());//排序
        templateFieldsList.add(id);
        if (flag) {
            TemplateFields formmain_id = new TemplateFields();
            formmain_id.setCreate();
            formmain_id.setTemplateId(templateId);//模版id
            formmain_id.setServiceName(parentServiceName);//Service服务名
            formmain_id.setTableName(parentTableName);//Table名称（英文名称）
            formmain_id.setFieldsRemark(parentTableName);//Table名称（英文名称）
            formmain_id.setFieldsId(null);//字段表id
            formmain_id.setDatabaseId(null);//数据库表id
            formmain_id.setFieldsName(entity.getServiceName() + "_Detail");//字段名称
            formmain_id.setFieldsRemark(entity.getTableRemark());//字段备注
            formmain_id.setFieldsType("5");//类型 Vachar,number,decimal,datetime,array(如为array字段需设置明细数据)
            formmain_id.setFieldsOption("{}");//字段属性 设置字段的渲染属性，存json Todo
            formmain_id.setSorts(getMaxSorts("sys_template_fields", parentServiceName));//模版名称
            formmain_id.setDetailFields(entity.getServiceName() + "_Detail");//明细字段 只有数据结构为array可设置
            formmain_id.setDetailService(entity.getServiceName());//明细字段 只有数据结构为array可设置
            formmain_id.setDetailTable(entity.getTableName());//明细表名
            templateFieldsList.add(formmain_id);
        }

        for (int i = 0; i < userDatabaseFields.size(); i++) {
            TemplateFields templateFields = new TemplateFields();
            templateFields.setCreate();
            templateFields.setTemplateId(template.getId());//模版id
            templateFields.setServiceName(entity.getServiceName());//Service服务名
            templateFields.setTableName(entity.getTableName());//Table名称（英文名称）
            //templateFields.setTableName(entity.getTableName());//Table名称（英文名称）
            templateFields.setFieldsId(userDatabaseFields.get(i).getId());//字段表id
            templateFields.setDatabaseId(databaseNames.getId());//数据库表id
            templateFields.setFieldsName(userDatabaseFields.get(i).getFieldsName());//字段名称
            templateFields.setFieldsRemark(userDatabaseFields.get(i).getFieldsRemark());//字段备注
            templateFields.setFieldsType(userDatabaseFields.get(i).getFieldsType());//类型 Vachar,number,decimal,datetime,array(如为array字段需设置明细数据)
            templateFields.setFieldsLength(userDatabaseFields.get(i).getFieldsLength());//长度
            templateFields.setFieldsRequired(userDatabaseFields.get(i).getFieldsRequired());//是否必填
            templateFields.setFieldsSole(userDatabaseFields.get(i).getFieldsSole());//是否唯一性
            templateFields.setFieldsOption(userDatabaseFields.get(i).getFieldsOption());//字段属性 设置字段的渲染属性，存json Todo
            templateFields.setSorts(userDatabaseFields.get(i).getSorts());//排序
            templateFieldsList.add(templateFields);
        }
        for (int i = 0; i < templateFieldsList.size(); i++) {
            templatefieldsMapper.entity_insert(templateFieldsList.get(i));
        }
        setButton(template.getId(), entity.getServiceName(), entity.getTableName());

        List<TemplateView> templateViews = new ArrayList<>();
        //模板视图表
        getTemplateViews(templateViews, templateFieldsList);
        for (int i = 0; i < templateViews.size(); i++) {
            templateviewMapper.entity_insert(templateViews.get(i));
        }
        if (!entity.getTableName().contains("formson")) {
            TemplateViewAccredit templateViewAccredit = new TemplateViewAccredit();
            templateViewAccredit.setCreate();
            templateViewAccredit.setSorts(getMaxSorts("sys_template_view_accredit", null));//模版名称
            templateViewAccredit.setTemplateId(template.getId());
            templateViewAccredit.setUserId(StpUtil.getLoginIdAsString());
            templateviewaccreditMapper.entity_insert(templateViewAccredit);
        }

        //判断是否有明细
        if (entity.getDetailTemplate() != null && entity.getDetailTemplate().size() > 0) {
            for (int i = 0; i < entity.getDetailTemplate().size(); i++) {
                saveService(entity.getDetailTemplate().get(i), true, template.getId(), template.getServiceName(), template.getTableName());
            }
        }

        return true;
    }

    private void setButton(String id, String serviceName, String tableName) {
        SysTemplateButtonEntity tem1 = new SysTemplateButtonEntity();
        tem1.setId(UUIDUtils.getUUID());
        tem1.setTemplateId(id);
        tem1.setServiceName(serviceName);
        tem1.setTableName(tableName);
        tem1.setButtonType("search");
        tem1.setButtonTitle("搜索");
        tem1.setButtonStyle("success");
        tem1.setButtonSort("1");
        tem1.setButtonIcon("el-icon-search");
        tem1.setCreateUserId(StpUtil.getLoginIdAsString());
        tem1.setModifyUserId(StpUtil.getLoginIdAsString());
        tem1.setCreateTime(new Date());
        tem1.setModifyTime(new Date());
        tem1.setSts("Y");
        sysTemplateButtonDao.save(tem1);

        SysUserTemplateButtonEntity u1 = new SysUserTemplateButtonEntity();
        u1.setId(UUIDUtils.getUUID());
        u1.setTemplateId(id);
        u1.setServiceName(serviceName);
        u1.setButtonId(tem1.getId());
        u1.setUserId(StpUtil.getLoginIdAsString());
        u1.setCreateUserId(StpUtil.getLoginIdAsString());
        u1.setModifyUserId(StpUtil.getLoginIdAsString());
        u1.setCreateTime(new Date());
        u1.setModifyTime(new Date());
        u1.setSts("Y");
        sysUserTemplateButtonDao.save(u1);

        SysTemplateButtonEntity tem2 = new SysTemplateButtonEntity();
        tem2.setId(UUIDUtils.getUUID());
        tem2.setTemplateId(id);
        tem2.setServiceName(serviceName);
        tem2.setTableName(tableName);
        tem2.setButtonType("resize");
        tem2.setButtonTitle("重置");
        tem2.setButtonStyle("primary");
        tem2.setButtonSort("2");
        tem2.setButtonIcon("el-icon-refresh");
        tem2.setCreateUserId(StpUtil.getLoginIdAsString());
        tem2.setModifyUserId(StpUtil.getLoginIdAsString());
        tem2.setCreateTime(new Date());
        tem2.setModifyTime(new Date());
        tem2.setSts("Y");
        sysTemplateButtonDao.save(tem2);

        SysUserTemplateButtonEntity u2 = new SysUserTemplateButtonEntity();
        u2.setId(UUIDUtils.getUUID());
        u2.setTemplateId(id);
        u2.setServiceName(serviceName);

        u2.setButtonId(tem2.getId());
        u2.setUserId(StpUtil.getLoginIdAsString());
        u2.setCreateUserId(StpUtil.getLoginIdAsString());
        u2.setModifyUserId(StpUtil.getLoginIdAsString());
        u2.setCreateTime(new Date());
        u2.setModifyTime(new Date());
        u2.setSts("Y");
        sysUserTemplateButtonDao.save(u2);


        SysTemplateButtonEntity tem3 = new SysTemplateButtonEntity();
        tem3.setId(UUIDUtils.getUUID());
        tem3.setTemplateId(id);
        tem3.setServiceName(serviceName);
        tem3.setTableName(tableName);
        tem3.setButtonType("new");
        tem3.setButtonTitle("新建");
        tem3.setButtonStyle("primary");
        tem3.setButtonSort("3");
        tem3.setButtonIcon("el-icon-edit");
        tem3.setCreateUserId(StpUtil.getLoginIdAsString());
        tem3.setModifyUserId(StpUtil.getLoginIdAsString());
        tem3.setCreateTime(new Date());
        tem3.setModifyTime(new Date());
        tem3.setSts("Y");

        sysTemplateButtonDao.save(tem3);

        SysUserTemplateButtonEntity u3 = new SysUserTemplateButtonEntity();
        u3.setId(UUIDUtils.getUUID());
        u3.setTemplateId(id);
        u3.setServiceName(serviceName);

        u3.setButtonId(tem3.getId());
        u3.setUserId(StpUtil.getLoginIdAsString());
        u3.setCreateUserId(StpUtil.getLoginIdAsString());
        u3.setModifyUserId(StpUtil.getLoginIdAsString());
        u3.setCreateTime(new Date());
        u3.setModifyTime(new Date());
        u3.setSts("Y");
        sysUserTemplateButtonDao.save(u3);
    }

    /**
     * @param databaseNames
     * @param databaseFields
     * @return void
     * @Author lvleigang
     * @Description 创建表
     * @Date 4:36 下午 2023/7/11
     **/
    private void createTable(DatabaseNames databaseNames, List<DatabaseFields> databaseFields) {
        //先判断表是否存在
        if (!checkTable(databaseNames.getTableName())) {
            //创建表
            Map<String, Object> maps = new HashMap<>();
            maps.put("databaseNames", databaseNames);
            maps.put("databaseFields", databaseFields);
            Integer i = templateMapper.createTable(maps);
        }
    }

    /**
     * @param tableName
     * @return java.lang.Long
     * @Author lvleigang
     * @Description 获取最大的排序
     * @Date 3:58 下午 2023/7/11
     **/
    private boolean checkTable(String tableName) {
        Map<String, String> maps = new HashMap<>();
        maps.put("tableName", tableName);
        Integer i = templateMapper.checkTable(maps);
        if (i != null && i > 0) {
            return true;
        }
        return false;
    }

    /**
     * @param tableName
     * @return java.lang.Long
     * @Author lvleigang
     * @Description 获取最大的排序
     * @Date 3:58 下午 2023/7/11
     **/
    private Long getMaxSorts(String tableName, String serviceName) {
        Map<String, String> maps = new HashMap<>();
        maps.put("tableName", tableName);
        maps.put("serviceName", serviceName);
        return templateMapper.getMaxSorts(maps);
    }


    private void getTemplateViews(List<TemplateView> templateViews, List<TemplateFields> templateFieldsList) {
        for (int i = 0; i < templateFieldsList.size(); i++) {
            TemplateView templateView1 = new TemplateView(templateFieldsList.get(i).getTemplateId(), templateFieldsList.get(i).getId(), "1", JSONObject.parseObject(templateFieldsList.get(i).getFieldsOption()).getString("searchSort") != null ? JSONObject.parseObject(templateFieldsList.get(i).getFieldsOption()).getLong("searchSort") : null, templateFieldsList.get(i).getServiceName());//查询
            templateView1.setCreate();
            TemplateView templateView2 = new TemplateView(templateFieldsList.get(i).getTemplateId(), templateFieldsList.get(i).getId(), "2", JSONObject.parseObject(templateFieldsList.get(i).getFieldsOption()).getString("tableSort") != null ? JSONObject.parseObject(templateFieldsList.get(i).getFieldsOption()).getLong("tableSort") : null, templateFieldsList.get(i).getServiceName());
            ;//列表
            templateView2.setCreate();
            TemplateView templateView3 = new TemplateView(templateFieldsList.get(i).getTemplateId(), templateFieldsList.get(i).getId(), "3", templateFieldsList.get(i).getSorts(), templateFieldsList.get(i).getServiceName());//新增
            templateView3.setCreate();
            TemplateView templateView4 = new TemplateView(templateFieldsList.get(i).getTemplateId(), templateFieldsList.get(i).getId(), "4", templateFieldsList.get(i).getSorts(), templateFieldsList.get(i).getServiceName());//编辑
            templateView4.setCreate();
            TemplateView templateView5 = new TemplateView(templateFieldsList.get(i).getTemplateId(), templateFieldsList.get(i).getId(), "5", templateFieldsList.get(i).getSorts(), templateFieldsList.get(i).getServiceName());//查看
            templateView5.setCreate();

            if ("id".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
                templateViews.add(templateView2);
                templateViews.add(templateView4);
                templateViews.add(templateView5);
            } else if ("formmain_id".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("sorts".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("create_user_id".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("create_time".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("modify_user_id".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("modify_time".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("sts".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else if ("org_id".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
            } else {
                if (templateFieldsList.get(i).getFieldsOption().contains("searchSort")) {
                    templateViews.add(templateView1);
                }
                if (templateFieldsList.get(i).getFieldsOption().contains("tableSort")) {
                    templateViews.add(templateView2);
                }
                templateViews.add(templateView3);
                templateViews.add(templateView4);
                templateViews.add(templateView5);
            }
        }
    }

    private void createAllFields(List<DatabaseFields> allDatabaseFields, List<DatabaseFields> userDatabaseFields, TemplateSave entity, boolean flag) {

        DatabaseFields id = new DatabaseFields();
        id.setCreate();
        id.setServiceName(entity.getServiceName());//Service服务名
        id.setTableName(entity.getTableName());//表名（英文名称）
        id.setFieldsName("id");//字段名称
        id.setFieldsRemark("id");//字段备注
        id.setFieldsType("1");//类型
        id.setFieldsLength("50");//长度
        id.setSorts(1L);//排序
        allDatabaseFields.add(id);
        if (flag) {
            DatabaseFields formmain_id = new DatabaseFields();
            formmain_id.setCreate();
            formmain_id.setServiceName(entity.getServiceName());//Service服务名
            formmain_id.setTableName(entity.getTableName());//表名（英文名称）
            formmain_id.setFieldsName("formmain_id");//字段名称
            formmain_id.setFieldsRemark("formmain_id");//字段备注
            formmain_id.setFieldsType("1");//类型
            formmain_id.setFieldsLength("50");//长度
            formmain_id.setSorts(2L);//排序
            allDatabaseFields.add(formmain_id);
        }

        allDatabaseFields.addAll(userDatabaseFields);

        DatabaseFields sorts = new DatabaseFields();
        sorts.setCreate();
        sorts.setServiceName(entity.getServiceName());//Service服务名
        sorts.setTableName(entity.getTableName());//表名（英文名称）
        sorts.setFieldsName("sorts");//字段名称
        sorts.setFieldsRemark("排序");//字段备注
        sorts.setFieldsType("2");//类型
        sorts.setFieldsLength("20");//长度
        sorts.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(sorts);

        DatabaseFields create_user_id = new DatabaseFields();
        create_user_id.setCreate();
        create_user_id.setServiceName(entity.getServiceName());//Service服务名
        create_user_id.setTableName(entity.getTableName());//表名（英文名称）
        create_user_id.setFieldsName("create_user_id");//字段名称
        create_user_id.setFieldsRemark("创建人id");//字段备注
        create_user_id.setFieldsType("1");//类型
        create_user_id.setFieldsLength("50");//长度
        create_user_id.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(create_user_id);

        DatabaseFields create_time = new DatabaseFields();
        create_time.setCreate();
        create_time.setServiceName(entity.getServiceName());//Service服务名
        create_time.setTableName(entity.getTableName());//表名（英文名称）
        create_time.setFieldsName("create_time");//字段名称
        create_time.setFieldsRemark("创建时间");//字段备注
        create_time.setFieldsType("4");//类型
        create_time.setFieldsLength(null);//长度
        create_time.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(create_time);

        DatabaseFields modify_user_id = new DatabaseFields();
        modify_user_id.setCreate();
        modify_user_id.setServiceName(entity.getServiceName());//Service服务名
        modify_user_id.setTableName(entity.getTableName());//表名（英文名称）
        modify_user_id.setFieldsName("modify_user_id");//字段名称
        modify_user_id.setFieldsRemark("修改人id");//字段备注
        modify_user_id.setFieldsType("1");//类型
        modify_user_id.setFieldsLength("50");//长度
        modify_user_id.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(modify_user_id);

        DatabaseFields modify_time = new DatabaseFields();
        modify_time.setCreate();
        modify_time.setServiceName(entity.getServiceName());//Service服务名
        modify_time.setTableName(entity.getTableName());//表名（英文名称）
        modify_time.setFieldsName("modify_time");//字段名称
        modify_time.setFieldsRemark("修改时间");//字段备注
        modify_time.setFieldsType("4");//类型
        modify_time.setFieldsLength(null);//长度
        modify_time.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(modify_time);

        DatabaseFields sts = new DatabaseFields();
        sts.setCreate();
        sts.setServiceName(entity.getServiceName());//Service服务名
        sts.setTableName(entity.getTableName());//表名（英文名称）
        sts.setFieldsName("sts");//字段名称
        sts.setFieldsRemark("状态(Y正常N删除)");//字段备注
        sts.setFieldsType("1");//类型
        sts.setFieldsLength("1");//长度
        sts.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(sts);

        DatabaseFields org_id = new DatabaseFields();
        org_id.setCreate();
        org_id.setServiceName(entity.getServiceName());//Service服务名
        org_id.setTableName(entity.getTableName());//表名（英文名称）
        org_id.setFieldsName("org_id");//字段名称
        org_id.setFieldsRemark("组织机构ID");//字段备注
        org_id.setFieldsType("1");//类型
        org_id.setFieldsLength("50");//长度
        org_id.setSorts(allDatabaseFields.size() + 1L);//排序
        allDatabaseFields.add(org_id);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取模版下的数据（分页）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryPageData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "pageNum")) {
            return BaseResult.getFailureMessageEntity("请先传递分页参数");
        }
        if (!checkData(jsonObject, "pageSize")) {
            return BaseResult.getFailureMessageEntity("请先传递分页参数");
        }
        //查询用户授权的字段
        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setUserId(StpUtil.getLoginIdAsString());
        userTemplateView.setSts("Y");
        userTemplateView.setTemplateViewType("2");
        userTemplateView.setServiceName(jsonObject.getString("service"));
        Integer filedCount = usertemplateviewMapper.entity_count(userTemplateView);

        List<String> stringList = null;
        if (filedCount > 0) {
            stringList = queryListFields(true, StpUtil.getLoginIdAsString(), "2", jsonObject.getString("service"));
        } else {
            stringList = queryListFields(false, StpUtil.getLoginIdAsString(), "2", jsonObject.getString("service"));
        }
        Template template = new Template();
        template = templateMapper.entity_get_by_service(jsonObject.getString("service"));
        //查询数据
        TemplateQuery templateQuery = new TemplateQuery();
        templateQuery.setTableName(template.getTableName());
        templateQuery.setReturnField(stringList);
        templateQuery.setQueryCondition(getListData("queryCondition", jsonObject, TemplateKeyValue.class));
        PageHelper.startPage(jsonObject.getIntValue("pageNum"), jsonObject.getIntValue("pageSize"));
        List<HashMap<String, Object>> businessResult = templateMapper.getServiceTemplateData(templateQuery);
        PageInfo pageInfo = new PageInfo(businessResult);
        return BaseResult.getSuccessMessageEntity("获取数据成功", pageInfo);
    }

    /**
     * @param userFlag
     * @param loginIdAsString
     * @param templateViewType
     * @param service
     * @return java.util.List<com.hzya.frame.sys.templateFields.entity.TemplateFields>
     * @Author lvleigang
     * @Description 查询字段
     * @Date 4:39 下午 2023/7/10
     **/
    private List<String> queryListFields(boolean userFlag, String loginIdAsString, String templateViewType, String service) {
        List<String> stringList = new ArrayList<>();
        List<TemplateFields> templateFieldsList = null;
        if (userFlag) {
            //用户设置了字段，直接用用户字段以及用户的排序
            TemplateDto templateDto = new TemplateDto();
            templateDto.setUserId(loginIdAsString);
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryUserTemplateFields(templateDto);
        } else {
            //用户没有设置了字段采用模版的字段
            TemplateDto templateDto = new TemplateDto();
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        }
        boolean flag = false;
        if (templateFieldsList != null && templateFieldsList.size() > 0) {
            for (int i = 0; i < templateFieldsList.size(); i++) {
                if (!"5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                    stringList.add(templateFieldsList.get(i).getFieldsName());
                }
                if ("id".equals(templateFieldsList.get(i).getFieldsName().toLowerCase())) {
                    flag = true;
                }
            }
        }
        if (!flag) {
            stringList.add("id");
        }
        return stringList;
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取模版下的数据（不分页）
     * @Date 4:59 下午 2023/7/10
     **/
    @Override
    public JsonResultEntity queryAllData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        //查询用户授权的字段
        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setUserId(StpUtil.getLoginIdAsString());
        userTemplateView.setSts("Y");
        userTemplateView.setTemplateViewType("2");
        userTemplateView.setServiceName(jsonObject.getString("service"));
        Integer filedCount = usertemplateviewMapper.entity_count(userTemplateView);

        List<String> stringList = null;
        if (filedCount > 0) {
            stringList = queryListFields(true, StpUtil.getLoginIdAsString(), "2", jsonObject.getString("service"));
        } else {
            stringList = queryListFields(false, StpUtil.getLoginIdAsString(), "2", jsonObject.getString("service"));
        }
        Template template = new Template();
        template = templateMapper.entity_get_by_service(jsonObject.getString("service"));
        //查询数据
        TemplateQuery templateQuery = new TemplateQuery();
        templateQuery.setTableName(template.getTableName());
        templateQuery.setReturnField(stringList);
        templateQuery.setQueryCondition(getListData("queryCondition", jsonObject, TemplateKeyValue.class));
        List<HashMap<String, Object>> businessResult = templateMapper.getServiceTemplateData(templateQuery);
        return BaseResult.getSuccessMessageEntity("获取数据成功", businessResult);

    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 新增对应service的数据（包括明细）（校验数据必填以及唯一性）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity addTemplateServiceData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "templateViewType")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询字段类型");
        }
        if (!checkData(jsonObject, "data")) {
            return BaseResult.getFailureMessageEntity("请先传递要保存的数据");
        }
        //查询用户授权的字段
        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setUserId(StpUtil.getLoginIdAsString());
        userTemplateView.setSts("Y");
        userTemplateView.setTemplateViewType(jsonObject.getString("templateViewType"));
        userTemplateView.setServiceName(jsonObject.getString("service"));
        Integer filedCount = usertemplateviewMapper.entity_count(userTemplateView);

        String res = null;
        if (filedCount > 0) {
            res = saveServiceData(true, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"), jsonObject.getString("data"), "1");
        } else {
            res = saveServiceData(false, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"), jsonObject.getString("data"), "1");
        }
        if (res == null) {
            return BaseResult.getSuccessMessageEntity("保存数据成功");
        } else {
            throw new BaseSystemException(res);
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改对应service的数据（包括明细）（校验数据必填以及唯一性，主表修改，明细先删除后新增）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity updateTemplateServiceData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "templateViewType")) {
            return BaseResult.getFailureMessageEntity("请先传递要查询字段类型");
        }
        if (!checkData(jsonObject, "data")) {
            return BaseResult.getFailureMessageEntity("请先传递要修改的数据");
        }
        //查询用户授权的字段
        UserTemplateView userTemplateView = new UserTemplateView();
        userTemplateView.setUserId(StpUtil.getLoginIdAsString());
        userTemplateView.setSts("Y");
        userTemplateView.setTemplateViewType(jsonObject.getString("templateViewType"));
        userTemplateView.setServiceName(jsonObject.getString("service"));
        Integer filedCount = usertemplateviewMapper.entity_count(userTemplateView);
        String res = null;

        if (filedCount > 0) {
            res = saveServiceData(true, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"), jsonObject.getString("data"), "2");
        } else {
            res = saveServiceData(false, StpUtil.getLoginIdAsString(), jsonObject.getString("templateViewType"), jsonObject.getString("service"), jsonObject.getString("data"), "2");
        }
        if (res == null) {
            return BaseResult.getSuccessMessageEntity("修改数据成功");
        } else {
            return BaseResult.getFailureMessageEntity(res);

        }
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除对应service的数据（包括明细）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity deleteTemplateServiceData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "id")) {
            return BaseResult.getFailureMessageEntity("请先传递要删除的数据");
        }
        String res = deleteServiceData(StpUtil.getLoginIdAsString(), jsonObject.getString("service"), jsonObject.getString("id"), false);
        if (res == null) {
            return BaseResult.getSuccessMessageEntity("删除数据成功");
        } else {
            return BaseResult.getFailureMessageEntity(res);
        }
    }


    /**
     * @param loginIdAsString
     * @param service
     * @return java.util.List<com.hzya.frame.sys.templateFields.entity.TemplateFields>
     * @Author lvleigang
     * @Description 删除数据
     * @Date 4:39 下午 2023/7/10
     **/
    private String deleteServiceData(String loginIdAsString, String service, String id, boolean detailflag) {

        String stringBuffer = null;
        List<TemplateFields> templateFieldsList = null;
        //用户没有设置了字段采用模版的字段
        TemplateDto templateDto = new TemplateDto();
        templateDto.setTemplateViewType("4");//当作编辑去做
        templateDto.setServiceName(service);
        templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        //获取数据删除
        List<String> ids = null;
        if (detailflag) {
            //明细时候查询所有明细循环删除
            FormmainDeleteDto formmainDeleteDto = new FormmainDeleteDto();
            formmainDeleteDto.setFormmain_id(id);
            formmainDeleteDto.setTableName(templateMapper.getTableName(service));
            ids = templateMapper.queryTemplateId(formmainDeleteDto);

        }
        if ((ids != null && ids.size() > 0) || !detailflag) {
            if (templateFieldsList != null && templateFieldsList.size() > 0) {
                for (int i = 0; i < templateFieldsList.size(); i++) {
                    if ("5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                        if (!detailflag) {
                            stringBuffer = deleteServiceData(loginIdAsString, templateFieldsList.get(i).getDetailService(), id, true);
                        } else {
                            for (int j = 0; j < ids.size(); j++) {
                                stringBuffer = deleteServiceData(loginIdAsString, templateFieldsList.get(i).getDetailService(), ids.get(j), false);
                            }
                        }

                    }
                }
            }
        }
        if (stringBuffer == null || "".equals(stringBuffer)) {
            if (!detailflag) {
                //删除自己
                FormmainDeleteDto formmainDeleteDto = new FormmainDeleteDto();
                formmainDeleteDto.setId(id);
                formmainDeleteDto.setLoginId(loginIdAsString);
                formmainDeleteDto.setTableName(templateMapper.getTableName(service));
                templateMapper.deleteTemplateById(formmainDeleteDto);
            } else {
                //删除明细
                //删除自己
                FormmainDeleteDto formmainDeleteDto = new FormmainDeleteDto();
                formmainDeleteDto.setFormmain_id(id);
                formmainDeleteDto.setLoginId(loginIdAsString);
                formmainDeleteDto.setTableName(templateMapper.getTableName(service));
                templateMapper.deleteTemplateById(formmainDeleteDto);
            }
        } else {
            return "删除数据失败，请联系管理员";
        }
        return null;
    }

    /**
     * @param userFlag
     * @param loginIdAsString
     * @param templateViewType
     * @param service
     * @return java.util.List<com.hzya.frame.sys.templateFields.entity.TemplateFields>
     * @Author lvleigang
     * @Description 保存修改
     * @Date 4:39 下午 2023/7/10
     **/
    private String saveServiceData(boolean userFlag, String loginIdAsString, String templateViewType, String service, String saveDatas, String doType) {
        JSONObject object = JSONObject.parseObject(saveDatas);
        //1、获取字段
        List<FormmainDto> formmainDtoList = new ArrayList<>();
        FormmainDto formmainDto = new FormmainDto();

        List<TemplateFields> templateFieldsList = null;
        if (userFlag) {
            //用户设置了字段，直接用用户字段以及用户的排序
            TemplateDto templateDto = new TemplateDto();
            templateDto.setUserId(loginIdAsString);
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryUserTemplateFields(templateDto);
        } else {
            //用户没有设置了字段采用模版的字段
            TemplateDto templateDto = new TemplateDto();
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        }
        String id = null;
        if (templateFieldsList != null && templateFieldsList.size() > 0) {
            formmainDto.setTableName(templateMapper.getTableName(service));
            formmainDto.setServiceName(service);
            List<FormmainFiledsDto> filedsDtos = new ArrayList<>();
            if ("1".equals(doType)) {
                id = UUIDUtils.getUUID();
                FormmainFiledsDto ids = new FormmainFiledsDto();
                ids.setFieldsRemark("id");//字段名称
                ids.setFiledsName("id");//字段名称
                ids.setFiledsType("1");//字段类型
                ids.setFieldsRequired("2");//是否必填
                ids.setFieldsSole("2");//是否唯一性
                ids.setFiledsValue(id);//值
                filedsDtos.add(ids);
            } else {
                id = object.getString("id");
            }
            FormmainFiledsDto logid = new FormmainFiledsDto();
            logid.setFieldsRemark("logid");//字段名称
            logid.setFiledsName("logid");//字段名称
            logid.setFiledsType("1");//字段类型
            logid.setFieldsRequired("2");//是否必填
            logid.setFieldsSole("2");//是否唯一性
            logid.setFiledsValue(loginIdAsString);//值
            filedsDtos.add(logid);
            formmainDto.setFiledsDtos(filedsDtos);
            for (int i = 0; i < templateFieldsList.size(); i++) {
                if ("5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                    formmainDtoList.addAll(queryAddServiceFields(userFlag, loginIdAsString, templateViewType, templateFieldsList.get(i).getDetailService(), object.getString(templateFieldsList.get(i).getDetailFields()), id, doType));
                } else {
                    FormmainFiledsDto formmainFiledsDto = new FormmainFiledsDto();
                    formmainFiledsDto.setFieldsRemark(templateFieldsList.get(i).getFieldsRemark());//字段名称
                    formmainFiledsDto.setFiledsName(templateFieldsList.get(i).getFieldsName());//字段名称
                    formmainFiledsDto.setFiledsType(templateFieldsList.get(i).getFieldsType());//字段类型
                    formmainFiledsDto.setFieldsRequired(templateFieldsList.get(i).getFieldsRequired());//是否必填
                    formmainFiledsDto.setFieldsSole(templateFieldsList.get(i).getFieldsSole());//是否唯一性
                    formmainFiledsDto.setFiledsValue(object.getString(templateFieldsList.get(i).getFieldsName()));//值
                    filedsDtos.add(formmainFiledsDto);
                }
            }
            //formmainDtoList.add(formmainDto);
        }
        String checkstr = checkSaveData(formmainDto, formmainDtoList, doType, id);
        if (checkstr != null) {
            return checkstr;
        }
        if ("1".equals(doType)) {
            //保存
            templateMapper.saveFram(formmainDto);
        } else {
            //修改
            formmainDto.setId(id);
            templateMapper.update(formmainDto);
        }
        if (formmainDtoList != null && formmainDtoList.size() > 0) {
            for (int i = 0; i < formmainDtoList.size(); i++) {
                templateMapper.saveFram(formmainDtoList.get(i));
            }
        }
        return null;
    }

    private String checkSaveData(FormmainDto formmainDto, List<FormmainDto> formmainDtoList, String doType, String id) {
        Map<String, String> filedsss = new HashMap<>();

        StringBuffer stringBuffer = new StringBuffer();
        Map<String, Map<String, List<String>>> map = new HashMap<>();
        if (formmainDto.getFiledsDtos() != null && formmainDto.getFiledsDtos().size() > 0) {
            for (int i = 0; i < formmainDto.getFiledsDtos().size(); i++) {
                if ("1".equals(formmainDto.getFiledsDtos().get(i).getFieldsRequired())) {
                    if (formmainDto.getFiledsDtos().get(i).getFiledsValue() == null || "".equals(formmainDto.getFiledsDtos().get(i).getFiledsValue())) {
                        if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
                            stringBuffer.append("，");
                        }
                        stringBuffer.append(formmainDto.getFiledsDtos().get(i).getFieldsRemark() + "不允许为空");
                    }
                    if ("1".equals(formmainDto.getFiledsDtos().get(i).getFieldsSole())) {
                        if (map.get(formmainDto.getTableName()) != null) {
                            if (map.get(formmainDto.getTableName()).get(formmainDto.getFiledsDtos().get(i).getFiledsName()) != null
                                    && map.get(formmainDto.getTableName()).get(formmainDto.getFiledsDtos().get(i).getFiledsName()).size() > 0) {
                                map.get(formmainDto.getTableName()).get(formmainDto.getFiledsDtos().get(i).getFiledsName()).add(formmainDto.getFiledsDtos().get(i).getFiledsValue());
                            } else {
                                Map<String, List<String>> m = new HashMap<>();
                                List<String> a = new ArrayList<>();
                                a.add(formmainDto.getFiledsDtos().get(i).getFiledsValue());
                                m.put(formmainDto.getFiledsDtos().get(i).getFiledsName(), a);
                                map.put(formmainDto.getTableName(), m);
                                filedsss.put(formmainDto.getTableName() + formmainDto.getFiledsDtos().get(i).getFiledsName(), formmainDto.getFiledsDtos().get(i).getFieldsRemark());
                            }
                        } else {
                            Map<String, List<String>> m = new HashMap<>();
                            List<String> a = new ArrayList<>();
                            a.add(formmainDto.getFiledsDtos().get(i).getFiledsValue());
                            m.put(formmainDto.getFiledsDtos().get(i).getFiledsName(), a);
                            map.put(formmainDto.getTableName(), m);
                            filedsss.put(formmainDto.getTableName() + formmainDto.getFiledsDtos().get(i).getFiledsName(), formmainDto.getFiledsDtos().get(i).getFieldsRemark());
                        }
                    }
                }
            }
        }

        //明细
        if (formmainDtoList != null && formmainDtoList.size() > 0) {
            for (int s = 0; s < formmainDtoList.size(); s++) {
                if (formmainDtoList.get(s).getFiledsDtos() != null && formmainDtoList.get(s).getFiledsDtos().size() > 0) {
                    for (int i = 0; i < formmainDtoList.get(s).getFiledsDtos().size(); i++) {
                        if ("1".equals(formmainDtoList.get(s).getFiledsDtos().get(i).getFieldsRequired())) {
                            if (formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsValue() == null || "".equals(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsValue())) {
                                if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
                                    stringBuffer.append("，");
                                }
                                stringBuffer.append("第" + (s + 1) + "行的" + formmainDtoList.get(s).getFiledsDtos().get(i).getFieldsRemark() + "不允许为空");
                            }
                            if ("1".equals(formmainDtoList.get(s).getFiledsDtos().get(i).getFieldsSole())) {
                                if (map.get(formmainDtoList.get(s).getTableName()) != null) {
                                    if (map.get(formmainDtoList.get(s).getTableName()).get(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsName()) != null
                                            && map.get(formmainDtoList.get(s).getTableName()).get(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsName()).size() > 0) {
                                        map.get(formmainDtoList.get(s).getTableName()).get(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsName()).add(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsValue());
                                    }
                                } else {
                                    Map<String, List<String>> m = new HashMap<>();
                                    List<String> a = new ArrayList<>();
                                    a.add(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsValue());
                                    m.put(formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsName(), a);
                                    map.put(formmainDtoList.get(s).getTableName(), m);
                                    filedsss.put(formmainDtoList.get(s).getTableName() + formmainDtoList.get(s).getFiledsDtos().get(i).getFiledsName(), "第" + (s + 1) + "行的" + formmainDtoList.get(s).getFiledsDtos().get(i).getFieldsRemark());

                                }
                            }
                        }
                    }
                }
            }
        }
        if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
            return stringBuffer.toString();
        }
        //判断list里面是否有一样的

        for (Map.Entry<String, Map<String, List<String>>> stringMapEntry : map.entrySet()) {
            for (Map.Entry<String, List<String>> stringListEntry : stringMapEntry.getValue().entrySet()) {
                Set<String> stringSet = new HashSet<>(stringListEntry.getValue());
                if (stringListEntry.getValue().size() != stringSet.size()) {
                    if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
                        stringBuffer.append("，");
                    }
                    stringBuffer.append(filedsss.get(stringMapEntry.getKey() + stringListEntry.getKey()) + "已存在");
                }
            }
        }
        if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
            return stringBuffer.toString();
        }


        //查询表判断是否重复
        for (Map.Entry<String, Map<String, List<String>>> stringMapEntry : map.entrySet()) {
            for (Map.Entry<String, List<String>> stringListEntry : stringMapEntry.getValue().entrySet()) {
                //新增
                FormmainCheckDto formmainCheckDto = new FormmainCheckDto();
                formmainCheckDto.setTableName(stringMapEntry.getKey());
                formmainCheckDto.setFiledsName(stringListEntry.getKey());
                formmainCheckDto.setStringList(stringListEntry.getValue());
                if (!"1".equals(doType)) {
                    //编辑
                    if (formmainDto.getTableName().equals(stringMapEntry.getKey())) {
                        formmainCheckDto.setId(id);
                    }
                }
                Integer count = templateMapper.checkData(formmainCheckDto);
                if (count != null && count > 0) {
                    if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
                        stringBuffer.append("，");
                    }
                    stringBuffer.append(filedsss.get(stringMapEntry.getKey() + stringListEntry.getKey()) + "已存在");
                }
            }
        }
        if (stringBuffer.toString() != null && !"".equals(stringBuffer.toString())) {
            return stringBuffer.toString();
        }
        return null;
    }


    /**
     * @param userFlag
     * @param loginIdAsString
     * @param templateViewType
     * @param service
     * @param str
     * @return com.hzya.frame.sys.entity.FormmainDto
     * @Author lvleigang
     * @Description 获取明细字段
     * @Date 2:22 下午 2023/7/12
     **/
    private List<FormmainDto> queryAddServiceFields(boolean userFlag, String loginIdAsString, String templateViewType, String service, String str, String formmain_id, String doType) {
        if ("2".equals(doType)) {     //修改
            //删除明细表的数据
            Map<String, String> map = new HashMap<>();
            map.put("tableName", templateMapper.getTableName(service));
            map.put("loginIdAsString", loginIdAsString);
            map.put("formmain_id", formmain_id);
            templateMapper.deleteFormmain(map);
        }
        List<FormmainDto> formmainDtoList = new ArrayList<>();
        if (str == null || "".equals(str)) {
            return formmainDtoList;
        }
        List<TemplateFields> templateFieldsList = null;
        if (userFlag) {
            //用户设置了字段，直接用用户字段以及用户的排序
            TemplateDto templateDto = new TemplateDto();
            templateDto.setUserId(loginIdAsString);
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryUserTemplateFields(templateDto);
        } else {
            //用户没有设置了字段采用模版的字段
            TemplateDto templateDto = new TemplateDto();
            templateDto.setTemplateViewType(templateViewType);
            templateDto.setServiceName(service);
            templateFieldsList = templatefieldsMapper.queryTemplateFields(templateDto);
        }
        if (templateFieldsList != null && templateFieldsList.size() > 0) {
            JSONArray jsonArray = JSONArray.parseArray(str);
            if (jsonArray != null && jsonArray.size() > 0) {
                for (int a = 0; a < jsonArray.size(); a++) {
                    JSONObject object = jsonArray.getJSONObject(a);
                    FormmainDto formmainDto = new FormmainDto();
                    formmainDto.setTableName(templateMapper.getTableName(service));
                    formmainDto.setServiceName(service);
                    List<FormmainFiledsDto> filedsDtos = new ArrayList<>();
                    formmainDto.setFiledsDtos(filedsDtos);

                    String id = UUIDUtils.getUUID();

                    FormmainFiledsDto ids = new FormmainFiledsDto();
                    ids.setFieldsRemark("id");//字段名称
                    ids.setFiledsName("id");//字段名称
                    ids.setFiledsType("1");//字段类型
                    ids.setFieldsRequired("2");//是否必填
                    ids.setFieldsSole("2");//是否唯一性
                    ids.setFiledsValue(id);//值
                    filedsDtos.add(ids);
                    FormmainFiledsDto formmain_ids = new FormmainFiledsDto();
                    formmain_ids.setFieldsRemark("formmain_id");//字段名称
                    formmain_ids.setFiledsName("formmain_id");//字段名称
                    formmain_ids.setFiledsType("1");//字段类型
                    formmain_ids.setFieldsRequired("2");//是否必填
                    formmain_ids.setFieldsSole("2");//是否唯一性
                    formmain_ids.setFiledsValue(formmain_id);//值
                    filedsDtos.add(formmain_ids);
                    FormmainFiledsDto logid = new FormmainFiledsDto();
                    logid.setFiledsName("logid");//字段名称
                    logid.setFiledsType("1");//字段类型
                    logid.setFieldsRequired("2");//是否必填
                    logid.setFieldsSole("2");//是否唯一性
                    logid.setFiledsValue(loginIdAsString);//值
                    filedsDtos.add(logid);
                    for (int i = 0; i < templateFieldsList.size(); i++) {
                        if ("5".equals(templateFieldsList.get(i).getFieldsType())) {//array 数组
                            formmainDtoList.addAll(queryAddServiceFields(userFlag, loginIdAsString, templateViewType, templateFieldsList.get(i).getDetailService(), object.getString(templateFieldsList.get(i).getDetailFields()), id, doType));
                        } else {
                            FormmainFiledsDto formmainFiledsDto = new FormmainFiledsDto();
                            formmainFiledsDto.setFieldsRemark(templateFieldsList.get(i).getFieldsRemark());//字段名称
                            formmainFiledsDto.setFiledsName(templateFieldsList.get(i).getFieldsName());//字段名称
                            formmainFiledsDto.setFiledsType(templateFieldsList.get(i).getFieldsType());//字段类型
                            formmainFiledsDto.setFieldsRequired(templateFieldsList.get(i).getFieldsRequired());//是否必填
                            formmainFiledsDto.setFieldsSole(templateFieldsList.get(i).getFieldsSole());//是否唯一性
                            formmainFiledsDto.setFiledsValue(object.getString(templateFieldsList.get(i).getFieldsName()));//值
                            filedsDtos.add(formmainFiledsDto);
                        }
                    }
                    formmainDtoList.add(formmainDto);
                }
            }
        }
        return formmainDtoList;
    }


    /**
     * @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) {
        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("请先传递显示字段");
        }
        entity.setTableName(templateMapper.getTableName(entity.getService()));
        if (entity.getPageNum() != null && entity.getPageSize() != null) {
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<HashMap<String, Object>> mapList = templateMapper.queryTemplateData(entity);
            PageInfo pageInfo = new PageInfo(mapList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        } else {
            List<HashMap<String, Object>> mapList = templateMapper.queryTemplateData(entity);
            return BaseResult.getSuccessMessageEntity("查询数据成功", mapList);
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询数据（树结构）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity queryTemplateTreeData(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("请先传递显示字段");
        }
        if (!checkStr(entity.getUpId())) {
            return BaseResult.getFailureMessageEntity("请先传递上级id");
        }
        String tableName = templateMapper.getTableName(entity.getService());
        entity.setTableName(tableName);

        //存在id只查询一条
        if (checkStr(entity.getId())) {
            HashMap<String, Object> mapList = templateMapper.queryTemplateDataOne(entity);
            if (mapList == null || mapList.size() == 0) {
                List<HashMap<String, Object>> returnList = new ArrayList<>();
                return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
            }
            if (mapList.get(entity.getUpId()) == null) {
                List<HashMap<String, Object>> returnList = new ArrayList<>();
                returnList.add(mapList);
                //mapList.put("children",new ArrayList<>());
                //mapList.put(entity.getUpId(),null);
                return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
            }
            List<HashMap<String, Object>> returnList = new ArrayList<>();
            List<HashMap<String, Object>> mapListAll = templateMapper.queryTemplateDataAll(entity);
            //默认为叶
            HashMap<String, Object> fathers = getFathers(mapListAll, mapList, entity);
            //fathers.put("children",mapList);
            returnList.add(fathers);
            return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
        } else {
            List<HashMap<String, Object>> returnList = new ArrayList<>();
            List<HashMap<String, Object>> mapList = templateMapper.queryTemplateDataAll(entity);
            if (mapList != null && mapList.size() > 0) {
                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), mapList, entity);
                        HashMap<String, Object> re = new HashMap<>();
                        re.put(entity.getValue(), mapList.get(i).get(entity.getValue()));
                        re.put(entity.getLabel(), mapList.get(i).get(entity.getLabel()));
                        re.put(entity.getUpId(), mapList.get(i).get(entity.getUpId()));
                        if (children != null && children.size() > 0) {
                            re.put("children", children);
                        }
                        returnList.add(re);
                    }
                }
            }
            return BaseResult.getSuccessMessageEntity("查询数据成功", returnList);
        }
    }

    private HashMap<String, Object> getFathers(List<HashMap<String, Object>> mapListAll, HashMap<String, Object> mapList, ModuleDto entity) {
        if (mapList.get(entity.getUpId()) == 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(entity.getUpId()))) {
                    retList.put(entity.getValue(), mapListAll.get(i).get(entity.getValue()));
                    retList.put(entity.getLabel(), mapListAll.get(i).get(entity.getLabel()));
                    retList.put(entity.getUpId(), 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(HashMap<String, Object> stringObjectHashMap, List<HashMap<String, Object>> mapList, ModuleDto 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.get("qsdfg").equals(mapList.get(i).get(entity.getUpId()))) {
                    List<HashMap<String, Object>> children = getchilder(mapList.get(i), mapList, entity);
                    HashMap<String, Object> re = new HashMap<>();
                    re.put(entity.getValue(), mapList.get(i).get(entity.getValue()));
                    re.put(entity.getLabel(), mapList.get(i).get(entity.getLabel()));
                    re.put(entity.getUpId(), mapList.get(i).get(entity.getUpId()));
                    if (children != null && children.size() > 0) {
                        re.put("children", children);
                    }
                    returnList.add(re);
                }
            }
        }
        return returnList;
    }

    /**
     * @param mapList
     * @param entity
     * @return void
     * @Author lvleigang
     * @Description 循环查询下级
     * @Date 2:21 下午 2023/7/13
     **/
    private void queryUpTemplate(List<HashMap<String, Object>> mapList, ModuleDto entity) {
        if (mapList != null && mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                entity.setId(mapList.get(i).get("qsdfg").toString());
                entity.setQueryUp(true);
                List<HashMap<String, Object>> children = templateMapper.queryTemplateDataUp(entity);
                queryUpTemplate(children, entity);
                mapList.get(i).put("children", children);
            }
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 新增对应service的数据（包括明细）（校验数据必填以及唯一性）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity addMasterData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "token")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "data")) {
            return BaseResult.getFailureMessageEntity("请先传递要保存的数据");
        }
        String loginId = StpUtil.getLoginIdByToken(jsonObject.getString("token")).toString();
        if (!checkStr(loginId)) {
            return BaseResult.getFailureMessageEntity("token错误");
        }
        String res = null;
        res = saveServiceData(false, loginId, "3", jsonObject.getString("service"), jsonObject.getString("data"), "1");
        if (res == null) {
            return BaseResult.getSuccessMessageEntity("保存数据成功");
        } else {
            throw new BaseSystemException(res);
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 新增对应service的数据（包括明细）（校验数据必填以及唯一性）
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity addMasterDataList(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "token")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "data")) {
            return BaseResult.getFailureMessageEntity("请先传递要保存的数据");
        }
        String loginId = StpUtil.getLoginIdByToken(jsonObject.getString("token")).toString();
        if (!checkStr(loginId)) {
            return BaseResult.getFailureMessageEntity("token错误");
        }
        String res = null;
        //循环处理
        List<String> stringList = getListData("data", jsonObject, String.class);
        if (stringList != null && stringList.size() > 0) {
            for (int i = 0; i < stringList.size(); i++) {
                res = saveServiceData(false, loginId, "3", jsonObject.getString("service"), stringList.get(i), "1");
                if (res != null && !"".equals(res)) {
                    throw new BaseSystemException(res);
                }
            }
        }
        if (res == null) {
            return BaseResult.getSuccessMessageEntity("保存数据成功");
        } else {
            throw new BaseSystemException(res);
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 对照
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity controlData(JSONObject object) {
        JSONObject jsonObject = getstrObj("jsonStr", object);
        //校验是否有servecr传参
        if (!checkData(jsonObject, "service")) {
            return BaseResult.getFailureMessageEntity("请先传递服务名");
        }
        if (!checkData(jsonObject, "leftAppId")) {
            return BaseResult.getFailureMessageEntity("请先传递左应用id");
        }
        if (!checkData(jsonObject, "leftApiId")) {
            return BaseResult.getFailureMessageEntity("请先传递左接口ID");
        }
        if (!checkData(jsonObject, "rightAppId")) {
            return BaseResult.getFailureMessageEntity("请先传递右应用id");
        }
        if (!checkData(jsonObject, "rightApiId")) {
            return BaseResult.getFailureMessageEntity("请先传递右接口ID");
        }
        if (!checkData(jsonObject, "matchMode")) {
            return BaseResult.getFailureMessageEntity("请先传递匹配方式");
        }
        //判断服务是否存在
        Template template = new Template();
        template.setServiceName(jsonObject.getString("service"));
        template.setSts("Y");
        List<Template> templateList = templateMapper.entity_list_base(template);
        if (templateList == null || templateList.size() != 1) {
            return BaseResult.getFailureMessageEntity("对照服务查询不到，请确认");

        }
        //保存数据到任务表执行任务
        SysContrastTaskEntity sysContrastTaskEntity = new SysContrastTaskEntity();
        sysContrastTaskEntity.setId(UUIDUtils.getUUID());
        sysContrastTaskEntity.setSts("Y");
        sysContrastTaskEntity.setCreateUserId(StpUtil.getLoginIdAsString());
        sysContrastTaskEntity.setModifyUserId(StpUtil.getLoginIdAsString());
        sysContrastTaskEntity.setCreateTime(new Date());
        sysContrastTaskEntity.setModifyTime(new Date());
        sysContrastTaskEntity.setMatchMode(jsonObject.getString("matchMode"));
        sysContrastTaskEntity.setServiceName(jsonObject.getString("service"));
        sysContrastTaskEntity.setLeftAppId(jsonObject.getString("leftAppId"));
        sysContrastTaskEntity.setLeftApiId(jsonObject.getString("leftApiId"));
        sysContrastTaskEntity.setRightAppId(jsonObject.getString("rightAppId"));
        sysContrastTaskEntity.setRightApiId(jsonObject.getString("rightApiId"));
        sysContrastTaskEntity.setTaskStatus("1");
        sysContrastTaskEntity.setTaskMsg("待执行");
        sysContrastTaskDao.save(sysContrastTaskEntity);
        //异步执行
        taskExecutor.execute(() -> sysContrastTaskService.doContrastTask(sysContrastTaskEntity));
        return BaseResult.getSuccessMessageEntity("正在执行对照任务，请稍后在日志中查看是否完成");
    }


}
