package com.lantone.daqe.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.exception.Asserts;
import com.lantone.common.util.BeanUtil;
import com.lantone.common.util.ListUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.common.util.SysUserUtils;
import com.lantone.daqe.dto.GetByIdColumnVerifyDTO;
import com.lantone.daqe.dto.GetColumnDTO;
import com.lantone.daqe.dto.GetColumnInfoDTO;
import com.lantone.daqe.dto.GetColumnNameDTO;
import com.lantone.daqe.dto.GetColumnVerifyDTO;
import com.lantone.daqe.dto.GetColumnVerifyPageDTO;
import com.lantone.daqe.dto.GetRegularDTO;
import com.lantone.daqe.dto.GetTableColumnNameDTO;
import com.lantone.daqe.dto.GetTableNameDTO;
import com.lantone.daqe.entity.ColumnInfo;
import com.lantone.daqe.entity.ColumnResult;
import com.lantone.daqe.entity.ColumnVerify;
import com.lantone.daqe.entity.NonnullInfo;
import com.lantone.daqe.entity.NonnullResult;
import com.lantone.daqe.entity.RegularInfo;
import com.lantone.daqe.entity.RegularMapping;
import com.lantone.daqe.entity.RegularResult;
import com.lantone.daqe.entity.StandardvalueInfo;
import com.lantone.daqe.entity.StandardvalueResult;
import com.lantone.daqe.enums.ColumnResultTypeEnum;
import com.lantone.daqe.enums.ColumnVerifyTypeEnum;
import com.lantone.daqe.facade.base.ColumnInfoFacade;
import com.lantone.daqe.facade.base.ColumnResultFacade;
import com.lantone.daqe.facade.base.ColumnVerifyFacade;
import com.lantone.daqe.facade.base.NonnullInfoFacade;
import com.lantone.daqe.facade.base.NonnullResultFacade;
import com.lantone.daqe.facade.base.RegularInfoFacade;
import com.lantone.daqe.facade.base.RegularMappingFacade;
import com.lantone.daqe.facade.base.RegularResultFacade;
import com.lantone.daqe.facade.base.StandardvalueInfoFacade;
import com.lantone.daqe.facade.base.StandardvalueResultFacade;
import com.lantone.daqe.vo.AddColumnVO;
import com.lantone.daqe.vo.AddColumnVerifyVO;
import com.lantone.daqe.vo.ColumnInfoVO;
import com.lantone.daqe.vo.DelColumnVerifyVO;
import com.lantone.daqe.vo.GetByIdColumnVerifyVO;
import com.lantone.daqe.vo.GetColumnNameVO;
import com.lantone.daqe.vo.GetColumnVerifyPageVO;
import com.lantone.daqe.vo.GetRegularVO;
import com.lantone.daqe.vo.GetTableColumnNameVO;
import com.lantone.daqe.vo.GetTableNameVO;
import com.lantone.daqe.vo.UpdateColumnVerifyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 字段校验规则维护API-业务处理类
 * @author： zhanghang
 * @time: 2022/3/1 10:14
 */

@Component
public class ColumnVerifyManagementFacade {

    @Autowired
    ColumnVerifyFacade columnVerifyFacade;
    @Autowired
    ColumnInfoFacade columnInfoFacade;
    @Autowired
    RegularInfoFacade regularInfoFacade;
    @Autowired
    RegularMappingFacade regularMappingFacade;
    @Autowired
    NonnullInfoFacade nonnullInfoFacade;
    @Autowired
    StandardvalueInfoFacade standardvalueInfoFacade;
    @Autowired
    ColumnResultFacade columnResultFacade;
    @Autowired
    StandardvalueResultFacade standardvalueResultFacade;
    @Autowired
    NonnullResultFacade nonnullResultFacade;
    @Autowired
    RegularResultFacade regularResultFacade;


    /**
     * 获取字段校验规则分页列表数据
     *
     * @param getColumnVerifyPageVO
     * @return: 字段校验规则分页数据
     */
    public IPage<GetColumnVerifyPageDTO> getColumnVerifyPage(GetColumnVerifyPageVO getColumnVerifyPageVO) {
        Long currentHospitalId = SysUserUtils.getCurrentHospitalId();
        getColumnVerifyPageVO.setHospitalId(currentHospitalId);
        return columnVerifyFacade.getBaseMapper().getColumnVerifyPage(getColumnVerifyPageVO);
    }

    /**
     * 根据ID获取字段校验规则明细数据
     *
     * @param getByIdColumnVerifyVO
     * @return: 字段校验规则明细数据
     */
    public GetByIdColumnVerifyDTO getByIdColumnVerify(GetByIdColumnVerifyVO getByIdColumnVerifyVO) {
        RegularMapping mappingFacadeOne = regularMappingFacade.getOne(new QueryWrapper<RegularMapping>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("column_id", getByIdColumnVerifyVO.getColumnId())
        );
        //获取规则总表数据
        List<GetColumnVerifyDTO> columnVerifyDTOList = columnVerifyFacade.getBaseMapper().getByIdColumnVerify(getByIdColumnVerifyVO);
        GetByIdColumnVerifyDTO getByIdColumnVerifyDTO = new GetByIdColumnVerifyDTO();
        List<String> standardValueList = new ArrayList<>();
        if (ListUtil.isEmpty(columnVerifyDTOList)) {
            Asserts.fail("不存在该字段关联规则 !");
        }
        GetColumnVerifyDTO columnVerifyDTO = columnVerifyDTOList.get(0);
        getByIdColumnVerifyDTO.setColumnId(columnVerifyDTO.getColumnId());
        getByIdColumnVerifyDTO.setColumnCname(columnVerifyDTO.getColumnCname());
        getByIdColumnVerifyDTO.setColumnEname(columnVerifyDTO.getColumnEname());
        getByIdColumnVerifyDTO.setTableCname(columnVerifyDTO.getTableCname());
        getByIdColumnVerifyDTO.setTableEname(columnVerifyDTO.getTableEname());
        getByIdColumnVerifyDTO.setDescription(columnVerifyDTO.getDescription());
        //处理关键值信息及非空信息
        for (GetColumnVerifyDTO getColumnVerifyDTO : columnVerifyDTOList) {
            if (StringUtil.isNotEmpty(getColumnVerifyDTO.getStandardValue())) {
                standardValueList.add(getColumnVerifyDTO.getStandardValue());
            }
            if (StringUtil.isNotEmpty(getColumnVerifyDTO.getIsRequired())) {
                getByIdColumnVerifyDTO.setIsRequired(getColumnVerifyDTO.getIsRequired());
            }
        }

        if (mappingFacadeOne != null) {
            RegularInfo one = regularInfoFacade.getOne(new QueryWrapper<RegularInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .eq("id", mappingFacadeOne.getRegularId())
            );
            getByIdColumnVerifyDTO.setRegularId(mappingFacadeOne.getRegularId());
            getByIdColumnVerifyDTO.setRegular(one.getName());
        }
        String[] strings = new String[standardValueList.size()];
        if (ListUtil.isNotEmpty(standardValueList)) {
            getByIdColumnVerifyDTO.setStandardValueList(standardValueList.toArray(strings));
        }
        return getByIdColumnVerifyDTO;
    }

    /**
     * 查询下拉框获取数据库表中列数据表名称信息
     *
     * @param getTableNameVO
     * @return: 数据库表中列数据表名称信息
     */
    public List<GetTableNameDTO> getTableName(GetTableNameVO getTableNameVO) {
        return columnInfoFacade.getBaseMapper().getVerifyTableName(getTableNameVO);
    }

    /**
     * 查询下拉框获取数据库表中列数据字段名称信息
     *
     * @param getColumnNameVO
     * @return: 数据库表中列数据字段名称信息
     */
    public List<GetColumnNameDTO> getColumnName(GetColumnNameVO getColumnNameVO) {
        List<ColumnInfo> columnInfoList = columnInfoFacade.getBaseMapper().selectList(new QueryWrapper<ColumnInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq(StringUtil.isNotBlank(getColumnNameVO.getTableEname()), "table_ename", getColumnNameVO.getTableEname())
                .eq(StringUtil.isNotBlank(getColumnNameVO.getTableCname()), "table_cname", getColumnNameVO.getTableCname())
                .like(StringUtil.isNotBlank(getColumnNameVO.getColumnCname()), "column_cname", getColumnNameVO.getColumnCname())
                .like(StringUtil.isNotBlank(getColumnNameVO.getColumnEname()), "column_ename", getColumnNameVO.getColumnEname())
                .groupBy("column_cname", "column_ename")
        );
        List<GetColumnNameDTO> getColumnDTOList = BeanUtil.listCopyTo(columnInfoList, GetColumnNameDTO.class);
        return getColumnDTOList;
    }

    /**
     * 新增修改下拉框获取数据库表中列数据字段名称信息
     *
     * @param
     * @return: 数据库表中列数据字段名称信息
     */
    public List<GetTableColumnNameDTO> getTableColumnName(GetTableColumnNameVO getTableColumnNameVO) {
        List<GetColumnInfoDTO> columnInfoList = columnInfoFacade.getBaseMapper().getNoVerifyColumnName(getTableColumnNameVO);
        ColumnInfo columnById = columnInfoFacade.getById(getTableColumnNameVO.getColumnId());
        List<String> columnString = new ArrayList<>();
        List<GetTableColumnNameDTO> getTableColumnNameDTOS = new ArrayList<>();
        for (GetColumnInfoDTO columnInfo : columnInfoList) {
            GetTableColumnNameDTO getTableColumnNameDTO = new GetTableColumnNameDTO();
            GetTableNameDTO getTableNameDTO = new GetTableNameDTO();
            List<GetColumnNameDTO> getColumnNameDTOS = new ArrayList<GetColumnNameDTO>();
            getTableNameDTO.setTableCname(columnInfo.getTableCname());
            getTableNameDTO.setTableEname(columnInfo.getTableEname());
            if (columnString.contains(columnInfo.getTableCname())) {
                continue;
            }
            columnString.add(columnInfo.getTableCname());
            for (GetColumnInfoDTO info : columnInfoList) {
                if (info.getTableCname().equals(columnInfo.getTableCname()) && info.getTableEname().equals(columnInfo.getTableEname())) {
                    GetColumnNameDTO getColumnNameDTO = new GetColumnNameDTO();
                    getColumnNameDTO.setId(info.getId());
                    getColumnNameDTO.setColumnCname(info.getColumnCname());
                    getColumnNameDTO.setColumnEname(info.getColumnEname());
                    getColumnNameDTOS.add(getColumnNameDTO);
                }
            }
            //加入查看时的字段
            if (columnById != null) {
                if (columnInfo.getTableEname().equals(columnById.getTableEname())) {
                    GetColumnNameDTO getColumnNameDTO = new GetColumnNameDTO();
                    getColumnNameDTO.setId(columnById.getId());
                    getColumnNameDTO.setColumnCname(columnById.getColumnCname());
                    getColumnNameDTO.setColumnEname(columnById.getColumnEname());
                    getColumnNameDTOS.add(getColumnNameDTO);
                }
            }
            getTableColumnNameDTO.setGetTableNameDTO(getTableNameDTO);
            getTableColumnNameDTO.setGetColumnNameDTOList(getColumnNameDTOS);
            getTableColumnNameDTOS.add(getTableColumnNameDTO);
        }
        return getTableColumnNameDTOS;
    }

    /**
     * 下拉框获取正则式名称
     *
     * @param getRegularVO
     * @return: 正则式名称
     */
    public List<GetRegularDTO> getRegular(GetRegularVO getRegularVO) {
        List<RegularInfo> regularInfoList = regularInfoFacade.getBaseMapper().selectList(new QueryWrapper<RegularInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .like(StringUtil.isNotBlank(getRegularVO.getName()), "name", getRegularVO.getName())
                .orderByDesc("gmt_create")
        );
        List<GetRegularDTO> regularDTOList = BeanUtil.listCopyTo(regularInfoList, GetRegularDTO.class);
        return regularDTOList;
    }

    /**
     * 新增字段校验规则
     *
     * @param addColumnVerifyVO
     * @return: 是否成功
     */
    public Boolean addColumnVerify(AddColumnVerifyVO addColumnVerifyVO) {
        Long currentHospitalId = SysUserUtils.getCurrentHospitalId();
        Long principleId = SysUserUtils.getCurrentPrincipleId();
        addColumnVerifyVO.setHospitalId(currentHospitalId);
        List<ColumnInfoVO> columnList = addColumnVerifyVO.getColumnList();
        //判断是否有重复
        long distinctNum = columnList.stream().map(ColumnInfoVO::getId).distinct().count();
        if (distinctNum < columnList.size()) {
            Asserts.fail("表字段信息不能重复 !");
        }
        if(ListUtil.isNotEmpty(addColumnVerifyVO.getStandardValueList())) {
            long count = addColumnVerifyVO.getStandardValueList().stream().distinct().count();
            if (count < addColumnVerifyVO.getStandardValueList().size()){
                Asserts.fail("标准值不允许重复!");
            }
        }
        //查出表列信息ID
        for (ColumnInfoVO columnInfoVO : columnList) {
            Long columnId = columnInfoVO.getId();
            if (columnId == null) {
                Asserts.fail("不存在该字段信息！");
            }
            ColumnVerify columnVerify = new ColumnVerify();
            columnVerify.setColumnId(columnId);
            columnVerify.setColumnCname(columnInfoVO.getColumnCname());
            columnVerify.setColumnEname(columnInfoVO.getColumnEname());
            columnVerify.setTableCname(columnInfoVO.getTableCname());
            columnVerify.setTableEname(columnInfoVO.getTableEname());
            if (addColumnVerifyVO.getRegularId() != null) {
                //新增前判断是否存在该正则
                RegularInfo regularInfo = regularInfoFacade.getBaseMapper().selectById(addColumnVerifyVO.getRegularId());
                if (regularInfo == null) {
                    Asserts.fail("该正则不存在,已被删除，请重新关联正则!");
                }
                //插入正则维护表
                //先判断是否该字段信息是否已关联正则
                RegularMapping selectOneMapping = regularMappingFacade.getBaseMapper().selectOne(new QueryWrapper<RegularMapping>()
                        .eq("column_id", columnId)
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                );
                if (selectOneMapping != null) {
                    Asserts.fail("所选字段已关联正则！");
                }
                RegularMapping regularMapping = new RegularMapping();
                regularMapping.setColumnId(columnId);
                regularMapping.setGmtCreate(new Date());
                regularMapping.setGmtModified(new Date());
                if (principleId != null) {
                    regularMapping.setCreator(principleId.toString());
                }
                regularMapping.setRegularId(addColumnVerifyVO.getRegularId());
                try {
                    regularMappingFacade.getBaseMapper().insert(regularMapping);
                } catch (Exception e) {
                    Asserts.fail("关联正则失败,请重试！");
                }
                Long regularMappingId = regularMapping.getId();
                //插入总表
                columnVerify.setType(ColumnVerifyTypeEnum.REGULAR_TYPE.getKey());
                columnVerify.setVerifyId(regularMappingId);
                columnVerify.setVerifyVal(addColumnVerifyVO.getRegularName());
                columnVerify.setDescription(addColumnVerifyVO.getDescription());
                columnVerify.setGmtCreate(new Date());
                columnVerify.setGmtModified(new Date());
                if (principleId != null) {
                    columnVerify.setCreator(principleId.toString());
                }
                try {
                    columnVerifyFacade.getBaseMapper().insert(columnVerify);
                } catch (Exception e) {
                    Asserts.fail("保存失败");
                }
            }
            if (addColumnVerifyVO.getIsRequired() != null) {
                //插入表字段的非空校验维护表
                //先判断是否该字段信息是否已存在非空规则
                NonnullInfo selectOneInfo = nonnullInfoFacade.getBaseMapper().selectOne(new QueryWrapper<NonnullInfo>()
                        .eq("column_id", columnId)
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                );
                if (selectOneInfo != null) {
                    Asserts.fail("所选字段已存在是否非空维护规则");
                }
                NonnullInfo nonnullInfo = new NonnullInfo();
                nonnullInfo.setColumnId(columnId);
                nonnullInfo.setGmtCreate(new Date());
                nonnullInfo.setGmtModified(new Date());
                if (principleId != null) {
                    nonnullInfo.setCreator(principleId.toString());
                }
                nonnullInfo.setIsRequired(addColumnVerifyVO.getIsRequired());
                try {
                    nonnullInfoFacade.getBaseMapper().insert(nonnullInfo);
                } catch (Exception e) {
                    Asserts.fail("关联是否非空失败,请重试！");
                }
                Long nonnullInfoId = nonnullInfo.getId();
                //插入总表
                columnVerify.setType(ColumnVerifyTypeEnum.NONNULL_TYPE.getKey());
                columnVerify.setVerifyId(nonnullInfoId);
                columnVerify.setVerifyVal(addColumnVerifyVO.getIsRequired());
                columnVerify.setDescription(addColumnVerifyVO.getDescription());
                columnVerify.setGmtCreate(new Date());
                columnVerify.setGmtModified(new Date());
                if (principleId != null) {
                    columnVerify.setCreator(principleId.toString());
                }
                try {
                    columnVerifyFacade.getBaseMapper().insert(columnVerify);
                } catch (Exception e) {
                    Asserts.fail("保存失败");
                }
            }
            if (ListUtil.isNotEmpty(addColumnVerifyVO.getStandardValueList())) {
                //插入关键字信息维护表
                List<String> standardValueList = addColumnVerifyVO.getStandardValueList();
                for (String standardValue : standardValueList) {
                    StandardvalueInfo standardvalueInfo = new StandardvalueInfo();
                    standardvalueInfo.setColumnId(columnId);
                    standardvalueInfo.setGmtCreate(new Date());
                    standardvalueInfo.setGmtModified(new Date());
                    if (principleId != null) {
                        standardvalueInfo.setCreator(principleId.toString());
                    }
                    standardvalueInfo.setVal(standardValue);
                    try {
                        standardvalueInfoFacade.getBaseMapper().insert(standardvalueInfo);
                    } catch (Exception e) {
                        Asserts.fail("关联关键字失败,请重试！");
                    }
                    Long standardvalueInfoId = standardvalueInfo.getId();
                    //插入总表
                    columnVerify.setType(ColumnVerifyTypeEnum.STANDARD_TYPE.getKey());
                    columnVerify.setVerifyId(standardvalueInfoId);
                    columnVerify.setVerifyVal(standardValue);
                    columnVerify.setDescription(addColumnVerifyVO.getDescription());
                    columnVerify.setGmtCreate(new Date());
                    columnVerify.setGmtModified(new Date());
                    if (principleId != null) {
                        columnVerify.setCreator(principleId.toString());
                    }
                    try {
                        columnVerifyFacade.getBaseMapper().insert(columnVerify);
                    } catch (Exception e) {
                        Asserts.fail("保存失败");
                    }
                }
            }
        }
        return true;
    }

    /**
     * 删除字段校验规则
     *
     * @param delColumnVerifyVO
     * @return: 是否成功
     */
    public Boolean delColumnVerify(DelColumnVerifyVO delColumnVerifyVO) {
        Boolean nonnullInfo = true;
        Boolean tandardvalueInfo = true;
        Boolean regular = true;
        List<Long> ids = delColumnVerifyVO.getIds();
        List<ColumnVerify> columnVerifyList = columnVerifyFacade.getBaseMapper().selectList(new QueryWrapper<ColumnVerify>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .in("column_id", ids)
        );
        if (ListUtil.isEmpty(columnVerifyList)) {
            Asserts.fail("删除失败，不存在所需删除数据！");
        }
        //所删正则ID
        List<Long> regularId = new ArrayList<>();
        //非空判断ID
        List<Long> nonnullId = new ArrayList<>();
        //关键值ID
        List<Long> standardValueId = new ArrayList<>();
        for (ColumnVerify columnVerify : columnVerifyList) {
            String type = columnVerify.getType();
            //非空
            if (ColumnVerifyTypeEnum.NONNULL_TYPE.getKey().equals(type)) {
                nonnullId.add(columnVerify.getColumnId());
            }
            //关键值
            if (ColumnVerifyTypeEnum.STANDARD_TYPE.getKey().equals(type)) {
                standardValueId.add(columnVerify.getColumnId());
            }
            //正则
            if (ColumnVerifyTypeEnum.REGULAR_TYPE.getKey().equals(type)) {
                regularId.add(columnVerify.getColumnId());
            }
        }
        //非空
        if (ListUtil.isNotEmpty(nonnullId)) {
            nonnullInfo = nonnullInfoFacade.remove(new UpdateWrapper<NonnullInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("column_id", nonnullId)
            );
            if (nonnullInfo) {
                //删除结果表数据
                nonnullResultFacade.remove(new UpdateWrapper<NonnullResult>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("column_id", nonnullId)
                );
            }
        }
        //关键值
        if (ListUtil.isNotEmpty(standardValueId)) {
            tandardvalueInfo = standardvalueInfoFacade.remove(new UpdateWrapper<StandardvalueInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("column_id", standardValueId)
            );
            if (tandardvalueInfo) {
                //删除结果表数据
                standardvalueResultFacade.remove(new UpdateWrapper<StandardvalueResult>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("column_id", nonnullId)
                );
            }
        }
        //正则
        if (ListUtil.isNotEmpty(regularId)) {
            regular = regularMappingFacade.remove(new UpdateWrapper<RegularMapping>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("column_id", regularId)
            );
            if (regular) {
                //删除结果表数据
                regularResultFacade.remove(new UpdateWrapper<RegularResult>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("column_id", regularId)
                );
            }
        }
        if (nonnullInfo && tandardvalueInfo && regular) {
            boolean update = columnVerifyFacade.remove(new UpdateWrapper<ColumnVerify>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("column_id", ids)
            );
            if (update) {
                //删除结果表数据
                columnResultFacade.remove(new UpdateWrapper<ColumnResult>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("column_id",ids)
                );
                return true;
            }
        }
        Asserts.fail("删除失败");
        return false;
    }

    /**
     * 修改字段校验规则
     *
     * @param updateColumnVerifyVO
     * @return: 是否成功
     */
    public Boolean updateColumnVerify(UpdateColumnVerifyVO updateColumnVerifyVO) {
        Long currentHospitalId = SysUserUtils.getCurrentHospitalId();
        Long principleId = SysUserUtils.getCurrentPrincipleId();
        updateColumnVerifyVO.setHospitalId(currentHospitalId);
        List<ColumnInfoVO> columnList = updateColumnVerifyVO.getColumnList();
        //判断是否有重复--去重
        long distinctNum = columnList.stream().map(ColumnInfoVO::getId).distinct().count();
        if (distinctNum < columnList.size()) {
            Asserts.fail("表字段信息不能重复 !");
        }
        if(ListUtil.isNotEmpty(updateColumnVerifyVO.getStandardValueList())) {
            long count = updateColumnVerifyVO.getStandardValueList().stream().distinct().count();
            if (count < updateColumnVerifyVO.getStandardValueList().size()){
                Asserts.fail("标准值不允许重复!");
            }
        }
        List<ColumnInfoVO> columnInfoUpdate = new ArrayList<>();
        //查出表列信息ID
        List<Long> columnInfoIdUpdate = new ArrayList<>();
        for (ColumnInfoVO columnInfoVO : columnList) {
            Long columnId = columnInfoVO.getId();
            List<ColumnVerify> columnVerifyList = columnVerifyFacade.getBaseMapper().selectList(new QueryWrapper<ColumnVerify>()
                    .eq("column_id", columnId)
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
            );
            if (ListUtil.isNotEmpty(columnVerifyList)) {
                //库中已有维护
                columnInfoIdUpdate.add(columnId);
                columnInfoUpdate.add(columnInfoVO);
            } else {
                //库中暂无维护
                Asserts.fail("该规则已被删除!");
            }
        }
        //库中已有走修改流程
        if (ListUtil.isNotEmpty(columnInfoIdUpdate)) {
            for (Long columnId : columnInfoIdUpdate) {
                ColumnInfo columnInfo = columnInfoFacade.getById(columnId);
                ColumnVerify columnVerify = new ColumnVerify();
                columnVerify.setColumnId(columnId);
                columnVerify.setColumnCname(columnInfo.getColumnCname());
                columnVerify.setColumnEname(columnInfo.getColumnEname());
                columnVerify.setTableCname(columnInfo.getTableCname());
                columnVerify.setTableEname(columnInfo.getTableEname());
                //先判断是否该字段信息是否已关联正则
                RegularMapping selectOneMapping = regularMappingFacade.getBaseMapper().selectOne(new QueryWrapper<RegularMapping>()
                        .eq("column_id", columnId)
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                );
                if (updateColumnVerifyVO.getRegularId() != null) {
                    //插入正则维护表
                    if (selectOneMapping != null) {
                        //修改前判断是否存在该正则
                        RegularInfo regularInfo = regularInfoFacade.getBaseMapper().selectById(updateColumnVerifyVO.getRegularId());
                        if (regularInfo == null) {
                            Asserts.fail("该正则不存在,已被删除，请重新关联正则!");
                        }
                        //已关联正则走修改
                        regularMappingFacade.update(new UpdateWrapper<RegularMapping>()
                                .eq("column_id", columnId)
                                .eq("is_deleted", IsDeleteEnum.N.getKey())
                                .set("regular_id", updateColumnVerifyVO.getRegularId())
                                .set("gmt_modified", new Date())
                                .set("modifier", principleId == null ? "0" : principleId)
                        );
                        Long mappingId = selectOneMapping.getId();
                        //修改总表
                        columnVerifyFacade.update(new UpdateWrapper<ColumnVerify>()
                                .eq("column_id", columnId)
                                .eq("type", ColumnVerifyTypeEnum.REGULAR_TYPE.getKey())
                                .eq("verify_id", mappingId)
                                .set("verify_val", updateColumnVerifyVO.getRegularName())
                                .set("description", updateColumnVerifyVO.getDescription())
                                .set("gmt_modified", new Date())
                                .set("modifier", principleId == null ? "0" : principleId)
                        );
                    } else {
                        //未关联正则走新增
                        //新增前判断是否存在该正则
                        RegularInfo regularInfo = regularInfoFacade.getBaseMapper().selectById(updateColumnVerifyVO.getRegularId());
                        if (regularInfo == null) {
                            Asserts.fail("该正则不存在,已被删除，请重新关联正则!");
                        }
                        RegularMapping regularMapping = new RegularMapping();
                        regularMapping.setColumnId(columnId);
                        regularMapping.setGmtCreate(new Date());
                        if (principleId != null) {
                            regularMapping.setCreator(principleId.toString());
                        }
                        regularMapping.setRegularId(updateColumnVerifyVO.getRegularId());
                        try {
                            regularMappingFacade.getBaseMapper().insert(regularMapping);
                        } catch (Exception e) {
                            Asserts.fail("关联正则失败,请重试！");
                        }
                        Long regularMappingId = regularMapping.getId();
                        //插入总表
                        columnVerify.setType(ColumnVerifyTypeEnum.REGULAR_TYPE.getKey());
                        columnVerify.setVerifyId(regularMappingId);
                        columnVerify.setVerifyVal(updateColumnVerifyVO.getRegularName());
                        columnVerify.setDescription(updateColumnVerifyVO.getDescription());
                        columnVerify.setGmtCreate(new Date());
                        if (principleId != null) {
                            columnVerify.setCreator(principleId.toString());
                        }
                        try {
                            columnVerifyFacade.getBaseMapper().insert(columnVerify);
                        } catch (Exception e) {
                            Asserts.fail("保存失败");
                        }
                    }
                } else {
                    //为空则存在的关联给删掉
                    if (selectOneMapping != null) {
                        //已关联正则删除
                        regularMappingFacade.remove(new UpdateWrapper<RegularMapping>()
                                .eq("column_id", columnId)
                                .eq("is_deleted", IsDeleteEnum.N.getKey())
                        );
                        Long mappingId = selectOneMapping.getId();
                        Long regularId = selectOneMapping.getRegularId();
                        //获取结果表id
                        List<Long> ids = new ArrayList<>();
                        List<RegularResult> regularResults = regularResultFacade.getBaseMapper().selectList(new QueryWrapper<RegularResult>()
                                .eq("is_deleted", IsDeleteEnum.N.getKey())
                                .eq("regular_id", regularId));
                        if (ListUtil.isNotEmpty(regularResults)) {
                            regularResults.stream().forEach(regularResult -> {
                                ids.add(regularResult.getId());
                            });
                        }
                        //删除结果表数据
                        regularResultFacade.remove(new UpdateWrapper<RegularResult>()
                                .eq("is_deleted", IsDeleteEnum.N.getKey())
                                .eq("regular_id", regularId)
                        );
                        //修改总表
                        columnVerifyFacade.remove(new UpdateWrapper<ColumnVerify>()
                                .eq("column_id", columnId)
                                .eq("type", ColumnVerifyTypeEnum.REGULAR_TYPE.getKey())
                                .eq("verify_id", mappingId)
                        );
                        //删除结果表数据
                        if(ListUtil.isNotEmpty(ids)) {
                            delColumnResult(ids, ColumnResultTypeEnum.REGULAR_TYPE.getKey());
                        }
                    }
                    //未关联则不处理
                }
                if (updateColumnVerifyVO.getIsRequired() != null) {
                    //插入表字段的非空校验维护表
                    //先判断是否该字段信息是否已存在非空规则
                    NonnullInfo selectOneInfo = nonnullInfoFacade.getBaseMapper().selectOne(new QueryWrapper<NonnullInfo>()
                            .eq("column_id", columnId)
                            .eq("is_deleted", IsDeleteEnum.N.getKey())
                    );
                    if (selectOneInfo != null) {
                        //已关联非空校验走修改
                        nonnullInfoFacade.update(new UpdateWrapper<NonnullInfo>()
                                .eq("column_id", columnId)
                                .eq("is_deleted", IsDeleteEnum.N.getKey())
                                .set("is_required", updateColumnVerifyVO.getIsRequired())
                                .set("gmt_modified", new Date())
                                .set("modifier", principleId == null ? "0" : principleId)
                        );
                        Long mappingId = selectOneInfo.getId();
                        //修改总表
                        columnVerifyFacade.update(new UpdateWrapper<ColumnVerify>()
                                .eq("column_id", columnId)
                                .eq("type", ColumnVerifyTypeEnum.NONNULL_TYPE.getKey())
                                .eq("verify_id", mappingId)
                                .set("verify_val", updateColumnVerifyVO.getIsRequired())
                                .set("description", updateColumnVerifyVO.getDescription())
                                .set("gmt_modified", new Date())
                                .set("modifier", principleId == null ? "0" : principleId)
                        );
                    } else {
                        NonnullInfo nonnullInfo = new NonnullInfo();
                        nonnullInfo.setColumnId(columnId);
                        nonnullInfo.setGmtCreate(new Date());
                        if (principleId != null) {
                            nonnullInfo.setCreator(principleId.toString());
                        }
                        nonnullInfo.setIsRequired(updateColumnVerifyVO.getIsRequired());
                        try {
                            nonnullInfoFacade.getBaseMapper().insert(nonnullInfo);
                        } catch (Exception e) {
                            Asserts.fail("关联是否非空失败,请重试！");
                        }
                        Long nonnullInfoId = nonnullInfo.getId();
                        //插入总表
                        columnVerify.setType(ColumnVerifyTypeEnum.NONNULL_TYPE.getKey());
                        columnVerify.setVerifyId(nonnullInfoId);
                        columnVerify.setVerifyVal(updateColumnVerifyVO.getIsRequired());
                        columnVerify.setDescription(updateColumnVerifyVO.getDescription());
                        columnVerify.setGmtCreate(new Date());
                        if (principleId != null) {
                            columnVerify.setCreator(principleId.toString());
                        }
                        try {
                            columnVerifyFacade.getBaseMapper().insert(columnVerify);
                        } catch (Exception e) {
                            Asserts.fail("保存失败");
                        }
                    }
                }
                if (ListUtil.isNotEmpty(updateColumnVerifyVO.getStandardValueList())) {
                    //插入关键字信息维护表
                    List<String> standardValueList = updateColumnVerifyVO.getStandardValueList();
                    //先删除之前的关联然后直接插入
                    standardvalueInfoFacade.remove(new UpdateWrapper<StandardvalueInfo>()
                            .eq("column_id", columnId)
                    );
                    columnVerifyFacade.remove(new UpdateWrapper<ColumnVerify>()
                            .eq("column_id", columnId)
                            .eq("type", ColumnVerifyTypeEnum.STANDARD_TYPE.getKey())
                    );
                    for (String standardValue : standardValueList) {
                        StandardvalueInfo standardvalueInfo = new StandardvalueInfo();
                        standardvalueInfo.setColumnId(columnId);
                        standardvalueInfo.setGmtCreate(new Date());
                        standardvalueInfo.setGmtModified(new Date());
                        standardvalueInfo.setVal(standardValue);
                        if (principleId != null) {
                            standardvalueInfo.setCreator(principleId.toString());
                        }
                        try {
                            standardvalueInfoFacade.getBaseMapper().insert(standardvalueInfo);
                        } catch (Exception e) {
                            Asserts.fail("关联关键字失败,请重试！");
                        }
                        Long standardvalueInfoId = standardvalueInfo.getId();
                        //插入总表
                        columnVerify.setType(ColumnVerifyTypeEnum.STANDARD_TYPE.getKey());
                        columnVerify.setVerifyId(standardvalueInfoId);
                        columnVerify.setVerifyVal(standardValue);
                        columnVerify.setDescription(updateColumnVerifyVO.getDescription());
                        columnVerify.setGmtCreate(new Date());
                        columnVerify.setGmtModified(new Date());
                        if (principleId != null) {
                            columnVerify.setCreator(principleId.toString());
                        }
                        try {
                            columnVerifyFacade.getBaseMapper().insert(columnVerify);
                        } catch (Exception e) {
                            Asserts.fail("保存失败");
                        }
                    }
                } else {
                    //获取结果表id
                    List<Long> ids = new ArrayList<>();
                    List<StandardvalueResult> standardvalueResultsList = standardvalueResultFacade.getBaseMapper().selectList(new QueryWrapper<StandardvalueResult>()
                            .eq("is_deleted", IsDeleteEnum.N.getKey())
                            .eq("column_id", columnId));
                    if (ListUtil.isNotEmpty(standardvalueResultsList)) {
                        standardvalueResultsList.stream().forEach(standardvalueResult -> {
                            ids.add(standardvalueResult.getId());
                        });
                    }
                    //删除结果表数据
                    standardvalueResultFacade.remove(new UpdateWrapper<StandardvalueResult>()
                            .eq("is_deleted", IsDeleteEnum.N.getKey())
                            .eq("column_id", columnId));
                    if(ListUtil.isNotEmpty(ids)) {
                        delColumnResult(ids, ColumnResultTypeEnum.STANDARD_TYPE.getKey());
                    }
                    //为空则存在的关联给删掉
                    standardvalueInfoFacade.remove(new UpdateWrapper<StandardvalueInfo>()
                            .eq("column_id", columnId)
                    );
                    columnVerifyFacade.remove(new UpdateWrapper<ColumnVerify>()
                            .eq("column_id", columnId)
                            .eq("type", ColumnVerifyTypeEnum.STANDARD_TYPE.getKey())
                    );
                    //未关联则不处理
                }
            }
        }
        return true;
    }
    /**
     * 删除结果总表数据
     *
     * @param ids
     * @param type
     * @return:
     */
    public Boolean delColumnResult(List<Long> ids , String type){
       return columnResultFacade.remove(new UpdateWrapper<ColumnResult>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("type",type)
                .in("result_id",ids)
        );
    }

    /**
     * 根据表名新增字段信息
     *
     * @param addColumnVO
     * @return:
     */
    public Boolean addColumn(AddColumnVO addColumnVO) {
        //根据表名查出所有字段及其注释信息
        List<GetColumnDTO> columnDTOS = columnVerifyFacade.getBaseMapper().getColumn(addColumnVO);
        ColumnInfo columnInfo = new ColumnInfo();
        columnInfo.setTableCname(addColumnVO.getTableCname());
        columnInfo.setTableEname(addColumnVO.getTableEname());
        columnInfo.setType("1");
        for (GetColumnDTO columnDTO : columnDTOS) {
            columnInfo.setGmtCreate(new Date());
            columnInfo.setColumnCname(columnDTO.getColumnComment());
            columnInfo.setColumnEname(columnDTO.getColumnName());
            columnInfoFacade.getBaseMapper().insert(columnInfo);
        }
        return true;
    }
}
