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.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.ColumnVerify;
import com.lantone.daqe.entity.NonnullInfo;
import com.lantone.daqe.entity.RegularInfo;
import com.lantone.daqe.entity.RegularMapping;
import com.lantone.daqe.entity.StandardvalueInfo;
import com.lantone.daqe.enums.ColumnVerifyTypeEnum;
import com.lantone.daqe.facade.base.ColumnInfoFacade;
import com.lantone.daqe.facade.base.ColumnVerifyFacade;
import com.lantone.daqe.facade.base.NonnullInfoFacade;
import com.lantone.daqe.facade.base.RegularInfoFacade;
import com.lantone.daqe.facade.base.RegularMappingFacade;
import com.lantone.daqe.facade.base.StandardvalueInfoFacade;
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;

    /**
     * 获取字段校验规则分页列表数据
     *
     * @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())
        );
        GetByIdColumnVerifyDTO byIdColumnVerify = columnVerifyFacade.getBaseMapper().getByIdColumnVerify(getByIdColumnVerifyVO);
        if (mappingFacadeOne != null) {
            RegularInfo one = regularInfoFacade.getOne(new QueryWrapper<RegularInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .eq("id", mappingFacadeOne.getRegularId())
            );
            byIdColumnVerify.setRegularId(mappingFacadeOne.getRegularId());
            byIdColumnVerify.setRegular(one.getName());
        }
        return byIdColumnVerify;
    }

    /**
     * 查询下拉框获取数据库表中列数据表名称信息
     *
     * @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);
        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);
                }
            }
            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())
        );
        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("表字段信息不能重复 !");
        }
        //查出表列信息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) {
                //插入正则维护表
                //先判断是否该字段信息是否已关联正则
                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.getVerifyId());
            }
            //关键值
            if (ColumnVerifyTypeEnum.STANDARD_TYPE.getKey().equals(type)) {
                standardValueId.add(columnVerify.getVerifyId());
            }
            //正则
            if (ColumnVerifyTypeEnum.REGULAR_TYPE.getKey().equals(type)) {
                regularId.add(columnVerify.getVerifyId());
            }
        }
        //非空
        if (ListUtil.isNotEmpty(nonnullId)) {
            nonnullInfo = nonnullInfoFacade.remove(new UpdateWrapper<NonnullInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("id", nonnullId)
            );
        }
        //关键值
        if (ListUtil.isNotEmpty(standardValueId)) {
            tandardvalueInfo = standardvalueInfoFacade.remove(new UpdateWrapper<StandardvalueInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("id", standardValueId)
            );
        }
        //正则
        if (ListUtil.isNotEmpty(regularId)) {
            regular = regularMappingFacade.remove(new UpdateWrapper<RegularMapping>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("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) {
                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("表字段信息不能重复 !");
        }
        AddColumnVerifyVO addColumnVerifyVO = new AddColumnVerifyVO();
        List<ColumnInfoVO> columnInfoAdd = new ArrayList<>();
        List<ColumnInfoVO> columnInfoUpdate = new ArrayList<>();
        //查出表列信息ID
        List<Long> columnInfoIdAdd = new ArrayList<>();
        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 {
                //库中暂无维护
                columnInfoIdAdd.add(columnId);
                columnInfoAdd.add(columnInfoVO);
            }
        }
        if (ListUtil.isNotEmpty(columnInfoAdd)) {
            addColumnVerifyVO.setHospitalId(updateColumnVerifyVO.getHospitalId());
            addColumnVerifyVO.setColumnList(columnInfoAdd);
            addColumnVerifyVO.setIsRequired(updateColumnVerifyVO.getIsRequired());
            addColumnVerifyVO.setRegularId(updateColumnVerifyVO.getRegularId());
            addColumnVerifyVO.setRegularName(updateColumnVerifyVO.getRegularName());
            addColumnVerifyVO.setStandardValueList(updateColumnVerifyVO.getStandardValueList());
            addColumnVerifyVO.setDescription(updateColumnVerifyVO.getDescription());
            //库中暂无走新增流程
            addColumnVerify(addColumnVerifyVO);
        }
        //库中已有走修改流程
        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) {
                        //已关联正则走修改
                        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 {
                        //未关联正则走新增
                        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();
                        //修改总表
                        columnVerifyFacade.remove(new UpdateWrapper<ColumnVerify>()
                                .eq("column_id", columnId)
                                .eq("type", ColumnVerifyTypeEnum.REGULAR_TYPE.getKey())
                                .eq("verify_id", mappingId)
                        );
                    }
                    //未关联则不处理
                }
                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 {
                    //为空则存在的关联给删掉
                    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 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;
    }
}
