package jnpf.personnels.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.cultivate.utils.CultivatePerUtils;
import jnpf.model.enums.FtbPersonnelsAuditTaskEnum;
import jnpf.model.personnels.dto.config.FtbPersionnelsSendCranbonCopy;
import jnpf.model.personnels.dto.config.FtbPersonnelsAuditConfigDTO;
import jnpf.model.personnels.dto.config.FtbPersonnelsAuditConfigInfoDTO;
import jnpf.model.personnels.dto.config.MasterConfigUserBoudDto;
import jnpf.model.personnels.po.*;
import jnpf.model.personnels.vo.config.FtbPersonnelsAuditConfigVO;
import jnpf.permission.OrganizeApi;
import jnpf.permission.PositionApi;
import jnpf.permission.dto.SynUserBoundDTO;
import jnpf.permission.entity.BasePositionGradesEntity;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.entity.PositionEntity;
import jnpf.personnels.mapper.FtbPersonnelsAuditMasterConfigMapper;
import jnpf.personnels.mapper.FtbPersonnelsAuditRunTaskMapper;
import jnpf.personnels.service.FtbPersonnelsAuditCarbonRecipientService;
import jnpf.personnels.service.FtbPersonnelsAuditMasterConfigService;
import jnpf.personnels.service.FtbPersonnelsAuditSubConfigService;
import jnpf.personnels.service.FtbPersonnelsStaffRosterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FtbPersonnelsAuditMasterConfigServiceImpl extends ServiceImpl<FtbPersonnelsAuditMasterConfigMapper, FtbPersonnelsAuditMasterConfig> implements FtbPersonnelsAuditMasterConfigService {

    @Resource
    FtbPersonnelsAuditRunTaskMapper runTaskMapper;

    @Resource
    FtbPersonnelsAuditSubConfigService subConfigService;

    @Resource
    FtbPersonnelsAuditCarbonRecipientService carbonRecipientService;

    @Autowired
    OrganizeApi organizeApi;

    @Autowired
    PositionApi positionApi;

    @Autowired
    CultivatePerUtils cultivatePerUtils;

    @Autowired
    FtbPersonnelsStaffRosterService staffRosterService;


    class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }
    }

    /**
     * 便利数据
     * @param dataList
     * @return
     */
    private  List<Pair<String, String>> initData(List<String> dataList ){
        List<Pair<String, String>> resultDataList = new ArrayList<>();
        // 遍历原始列表
        for (int i = 0; i < dataList.size(); i++) {
            // 对于第一个元素，其"前一个"数据不存在，可以设置为null或其他默认值
            if (dataList.size()  == 1) {
                resultDataList.add(new Pair<>("0", "0"));
            }else if (i == 0 && dataList.size() > 1) {
                resultDataList.add(new Pair<>("0", dataList.get(i+1)));
            }
            // 对于最后一个元素，其"后一个"数据不存在，可以设置为null或其他默认值
            else if (i == dataList.size() - 1) {
                resultDataList.add(new Pair<>(dataList.get(i - 1), "0"));
            }
            // 其他元素既有前一个也有后一个数据
            else {
                // 前一个数据 后一个数据
                resultDataList.add(new Pair<>(dataList.get(i - 1), dataList.get(i+1)));
            }
        }
        return resultDataList;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Deprecated
    public void createPersonnelApprovalProcessConfiguration(FtbPersonnelsAuditConfigDTO dto) {
        FtbPersonnelsAuditMasterConfig ftbPersonnelsAuditMasterConfig = FtbPersonnelsAuditConfigDTO.coverFtbPersonnelsAuditMasterConfig(dto);
        // 修改时需要校验是否存在正在执行的任务
        if (ObjectUtil.isNotEmpty(ftbPersonnelsAuditMasterConfig.getId())){
            LambdaQueryWrapper<FtbPersonnelsAuditRunTask> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(FtbPersonnelsAuditRunTask::getMasterAuditConfigId, ftbPersonnelsAuditMasterConfig.getId());
            List<Integer> list = new ArrayList<>();
            list.add(FtbPersonnelsAuditTaskEnum.PENDING.getCode());
            list.add(FtbPersonnelsAuditTaskEnum.UNDER_REVIEW.getCode());
            wrapper.in(FtbPersonnelsAuditRunTask::getExcuseStatus,list);
            Long l = runTaskMapper.selectCount(wrapper);
            if (l > 0){
                throw new RuntimeException("当前存在正在审核的任务,不允许修改!");
            }
            // 直接删除之前的数据重新新增
            LambdaUpdateWrapper<FtbPersonnelsAuditCarbonRecipient> updateRecipient = Wrappers.lambdaUpdate();
            // 删除之前的抄送数据
            updateRecipient.eq(FtbPersonnelsAuditCarbonRecipient::getMasterConfig, ftbPersonnelsAuditMasterConfig.getId());
            carbonRecipientService.remove(updateRecipient);
            baseMapper.updateById(ftbPersonnelsAuditMasterConfig);
            // 处理子配置先将子配置删除
            LambdaUpdateWrapper<FtbPersonnelsAuditSubConfig> updateSubConfig = Wrappers.lambdaUpdate();
            updateSubConfig.eq(FtbPersonnelsAuditSubConfig::getMasterId,ftbPersonnelsAuditMasterConfig.getId());
            subConfigService.remove(updateSubConfig);
        }else {
            LambdaQueryWrapper<FtbPersonnelsAuditMasterConfig> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(FtbPersonnelsAuditMasterConfig::getOrgId, dto.getOrgId());
            queryWrapper.eq(FtbPersonnelsAuditMasterConfig::getConfigType, dto.getConfigType());
            FtbPersonnelsAuditMasterConfig db = baseMapper.selectOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(db)){
                throw new RuntimeException("当前组织已存在相同配置项,请勿重复新增!");
            }
            baseMapper.insert(ftbPersonnelsAuditMasterConfig);
        }
        String masterConfigId = ftbPersonnelsAuditMasterConfig.getId();
        // 处理抄送人
        List<FtbPersionnelsSendCranbonCopy> copyRecipient = dto.getCopyRecipient();
        List<FtbPersonnelsAuditCarbonRecipient> carbonRecipientList = copyRecipient.stream().map(item -> {
            FtbPersonnelsAuditCarbonRecipient recipient = FtbPersionnelsSendCranbonCopy.coverFtbPersonnelsAuditCarbonRecipient(item);
            recipient.setMasterConfig(masterConfigId);
            return recipient;
        }).collect(Collectors.toList());
        carbonRecipientService.saveBatch(carbonRecipientList);
        // 处理审批人
        List<FtbPersonnelsAuditConfigInfoDTO> configInfo = dto.getConfigInfo();
        List<FtbPersonnelsAuditSubConfig> personnelsAuditSubConfigs = configInfo.stream().map(item -> {
            FtbPersonnelsAuditSubConfig ftbPersonnelsAuditSubConfig = FtbPersonnelsAuditConfigInfoDTO.coverFtbPersonnelsAuditSubConfig(item);
            String Id =IdWorker.getIdStr(item);
            ftbPersonnelsAuditSubConfig.setId(Id);
            ftbPersonnelsAuditSubConfig.setMasterId(masterConfigId);
            return ftbPersonnelsAuditSubConfig;
        }).sorted(Comparator.comparingInt(FtbPersonnelsAuditSubConfig::getAuditProcessLevel)).collect(Collectors.toList());
        // 根据id进行数据处理
        List<String> stringList = personnelsAuditSubConfigs.stream().map(SuperBaseEntity.SuperIBaseEntity::getId).collect(Collectors.toList());
        // 处理为前后的数据

        List<Pair<String, String>> pairList = initData(stringList);
        // 数据进行插入
        for (int i = 0; i < personnelsAuditSubConfigs.size(); i++){
            FtbPersonnelsAuditSubConfig ftbPersonnelsAuditSubConfig = personnelsAuditSubConfigs.get(i);
            Pair<String, String> pair = pairList.get(i);
            ftbPersonnelsAuditSubConfig.setPreReview(pair.key);
            ftbPersonnelsAuditSubConfig.setNextReview(pair.value);
        }
        subConfigService.saveBatch(personnelsAuditSubConfigs);
    }

    @Override
    public FtbPersonnelsAuditConfigVO getAuditList(String orgId, Integer configType) {
        LambdaQueryWrapper<FtbPersonnelsAuditMasterConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnelsAuditMasterConfig::getConfigType,configType);
        wrapper.eq(FtbPersonnelsAuditMasterConfig::getOrgId,orgId);
        wrapper.eq(FtbPersonnelsAuditMasterConfig::getEnableMark,"0");
        wrapper.last("limit 1");
        FtbPersonnelsAuditMasterConfig vo = baseMapper.selectOne(wrapper);
        FtbPersonnelsAuditConfigVO configVO = FtbPersonnelsAuditMasterConfig.coverFtbPersonnelsAuditConfigVO(vo);
        if (ObjectUtil.isEmpty(configVO)){
            return new FtbPersonnelsAuditConfigVO();
        }
        List<FtbPersonnelsAuditSubConfig> auditSubConfigs = subConfigService.lambdaQuery().eq(FtbPersonnelsAuditSubConfig::getMasterId, vo.getId())
                .orderByAsc(FtbPersonnelsAuditSubConfig::getAuditProcessLevel)
                .list();
        if (CollUtil.isNotEmpty(auditSubConfigs)){
            LambdaQueryWrapper<FtbPersonnelsStaffRoster> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.in(FtbPersonnelsStaffRoster::getUserId,auditSubConfigs.stream()
                    .map(FtbPersonnelsAuditSubConfig::getAuditorId).distinct()
                    .collect(Collectors.toList()));
            List<FtbPersonnelsStaffRoster> list = staffRosterService.list(lambdaQuery);
            Map<String, String> map = list.stream().collect(Collectors.toMap(FtbPersonnelsStaffRoster::getUserId, FtbPersonnelsStaffRoster::getName,(a,b)->a));
            auditSubConfigs.forEach(item->{
                if (ObjectUtil.isNotEmpty(item.getAuditorId()) && map.containsKey(item.getAuditorId())){
                    item.setAuditorName(map.get(item.getAuditorId()));
                }
            });
        }
        configVO.setConfigInfo(auditSubConfigs.stream().map(FtbPersonnelsAuditSubConfig::coverFtbPersonnelsAuditConfigInfoDTO).collect(Collectors.toList()));
        List<FtbPersonnelsAuditCarbonRecipient> list = carbonRecipientService.lambdaQuery()
                .eq(FtbPersonnelsAuditCarbonRecipient::getMasterConfig, vo.getId())
                .eq(FtbPersonnelsAuditCarbonRecipient::getEnableMark,0).list();
        List<FtbPersionnelsSendCranbonCopy> sendCranbonCopies = list.stream()
                .map(FtbPersonnelsAuditCarbonRecipient::coverFtbPersionnelsSendCranbonCopy).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(sendCranbonCopies)) {
            LambdaQueryWrapper<FtbPersonnelsStaffRoster> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.in(FtbPersonnelsStaffRoster::getUserId,sendCranbonCopies.stream()
                    .map(FtbPersionnelsSendCranbonCopy::getCcId).distinct()
                    .collect(Collectors.toList()));
            List<FtbPersonnelsStaffRoster> staffRosters = staffRosterService.list(lambdaQuery);
            Map<String, String> map = staffRosters.stream().collect(Collectors.toMap(FtbPersonnelsStaffRoster::getUserId, FtbPersonnelsStaffRoster::getName,(a,b)->a));
            sendCranbonCopies.forEach(item->{
                if (map.containsKey(item.getCcId())) item.setCcName(map.get(item.getCcId()));
            });
            configVO.setCopyRecipient(sendCranbonCopies);
        }
        return configVO;
    }

    @Override
    @Transactional
    public void createApprovalConfiguration(FtbPersonnelsAuditConfigDTO dto) {
        FtbPersonnelsAuditMasterConfig ftbPersonnelsAuditMasterConfig = FtbPersonnelsAuditConfigDTO.coverFtbPersonnelsAuditMasterConfig(dto);
        LambdaQueryWrapper<FtbPersonnelsStaffRoster> lambdaed = Wrappers.lambdaQuery();
        lambdaed.in(FtbPersonnelsStaffRoster::getUserId,dto.getConfigInfo().stream().map(FtbPersonnelsAuditConfigInfoDTO::getAuditorId).collect(Collectors.toList()));
        List<FtbPersonnelsStaffRoster> staffRosters = staffRosterService.list(lambdaed);
        List<FtbPersonnelsStaffRoster> personnelsStaffRosters = staffRosters.stream().filter(item -> item.getWorkerStatus().equals("306")).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(personnelsStaffRosters))
            throw new RuntimeException("试岗员工:"+personnelsStaffRosters.stream()
                    .map(FtbPersonnelsStaffRoster::getName)
                    .collect(Collectors.joining(","))+"不能作为审批人!");
        if (ObjectUtil.isNotEmpty(ftbPersonnelsAuditMasterConfig.getId())){
            LambdaQueryWrapper<FtbPersonnelsAuditRunTask> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(FtbPersonnelsAuditRunTask::getMasterAuditConfigId, ftbPersonnelsAuditMasterConfig.getId());
            List<Integer> list = new ArrayList<>();
            list.add(FtbPersonnelsAuditTaskEnum.PENDING.getCode());
            list.add(FtbPersonnelsAuditTaskEnum.UNDER_REVIEW.getCode());
            wrapper.in(FtbPersonnelsAuditRunTask::getExcuseStatus,list);
            Long l = runTaskMapper.selectCount(wrapper);
            if (l > 0){
                throw new RuntimeException("当前存在正在审核的任务,不允许修改!");
            }
            // 将之前的数据移除为 不可用保留 查询历史审核配置
            LambdaUpdateWrapper<FtbPersonnelsAuditMasterConfig>  updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(FtbPersonnelsAuditMasterConfig::getEnableMark,"1");
            updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId,ftbPersonnelsAuditMasterConfig.getId());
            baseMapper.update(null,updateWrapper);
            // 移除之前的抄送人信息
            LambdaUpdateWrapper<FtbPersonnelsAuditCarbonRecipient> update = Wrappers.lambdaUpdate();
            update.set(FtbPersonnelsAuditCarbonRecipient::getEnableMark,1);
            update.eq(FtbPersonnelsAuditCarbonRecipient::getMasterConfig,ftbPersonnelsAuditMasterConfig.getId());
            carbonRecipientService.update(update);
            // 将修改数据的id设置为null
            ftbPersonnelsAuditMasterConfig.setId(null);

        }
        // 新增审批配置
        LambdaQueryWrapper<FtbPersonnelsAuditMasterConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsAuditMasterConfig::getOrgId, dto.getOrgId());
        queryWrapper.eq(FtbPersonnelsAuditMasterConfig::getConfigType, dto.getConfigType());
        queryWrapper.eq(FtbPersonnelsAuditMasterConfig::getEnableMark,"0");
        FtbPersonnelsAuditMasterConfig db = baseMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(db)){
            throw new RuntimeException("当前组织已存在相同配置项,请勿重复新增!");
        }
        baseMapper.insert(ftbPersonnelsAuditMasterConfig);
        String masterConfigId = ftbPersonnelsAuditMasterConfig.getId();
        // 处理抄送人
        List<FtbPersionnelsSendCranbonCopy> copyRecipient = dto.getCopyRecipient();
        if (CollUtil.isNotEmpty(copyRecipient)) {
            List<FtbPersonnelsAuditCarbonRecipient> carbonRecipientList = copyRecipient.stream().map(item -> {
                FtbPersonnelsAuditCarbonRecipient recipient = FtbPersionnelsSendCranbonCopy.coverFtbPersonnelsAuditCarbonRecipient(item);
                recipient.setMasterConfig(masterConfigId);
                return recipient;
            }).collect(Collectors.toList());
            carbonRecipientService.saveBatch(carbonRecipientList);
        }
        // 处理审批人
        List<FtbPersonnelsAuditConfigInfoDTO> configInfo = dto.getConfigInfo();
        List<FtbPersonnelsAuditSubConfig> personnelsAuditSubConfigs = configInfo.stream().map(item -> {
            FtbPersonnelsAuditSubConfig ftbPersonnelsAuditSubConfig = FtbPersonnelsAuditConfigInfoDTO.coverFtbPersonnelsAuditSubConfig(item);
            String Id =IdWorker.getIdStr(item);
            ftbPersonnelsAuditSubConfig.setId(Id);
            ftbPersonnelsAuditSubConfig.setMasterId(masterConfigId);
            return ftbPersonnelsAuditSubConfig;
        }).sorted(Comparator.comparingInt(FtbPersonnelsAuditSubConfig::getAuditProcessLevel))
                .collect(Collectors.toCollection(LinkedList::new));
        // 根据id进行数据处理
        List<String> stringList = personnelsAuditSubConfigs.stream().map(SuperBaseEntity.SuperIBaseEntity::getId)
                .collect(Collectors.toCollection(LinkedList::new));
        // 处理为前后的数据
        List<Pair<String, String>> pairList = initData(stringList);
        // 数据进行插入
        for (int i = 0; i < personnelsAuditSubConfigs.size(); i++){
            FtbPersonnelsAuditSubConfig ftbPersonnelsAuditSubConfig = personnelsAuditSubConfigs.get(i);
            Pair<String, String> pair = pairList.get(i);
            ftbPersonnelsAuditSubConfig.setPreReview(pair.key);
            ftbPersonnelsAuditSubConfig.setNextReview(pair.value);
        }
        subConfigService.saveBatch(personnelsAuditSubConfigs);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void approverDataVerification(String userId, List<SynUserBoundDTO> OldDtos, List<SynUserBoundDTO> newDtos) {
        log.error("approverDataVerification userId={},OldDtos={},newDtos={}",userId, JSONUtil.toJsonStr(OldDtos),JSONUtil.toJsonStr(newDtos));
        List<MasterConfigUserBoudDto> masterConfigUserBoudDtos = subConfigService.queryUserBound(userId);
        if (CollUtil.isEmpty(masterConfigUserBoudDtos)){
            return;
        }
        String orgName = masterConfigUserBoudDtos.stream().map(MasterConfigUserBoudDto::getOrgName).distinct().collect(Collectors.joining(","));
        Map<String, List<MasterConfigUserBoudDto>>  masterMap = masterConfigUserBoudDtos.stream()
                .collect(Collectors.groupingBy(item->item.getOrgId()+"_"+item.getPositionId()+"_"+item.getPositionGradesId()));
        Map<String, List<MasterConfigUserBoudDto>>  masterOrgMap = masterConfigUserBoudDtos.stream()
                .collect(Collectors.groupingBy(MasterConfigUserBoudDto::getOrgId));
        // 匹配数据
        Map<String, List<SynUserBoundDTO>> oldOrgList = OldDtos.stream()
                .collect(Collectors.groupingBy(item -> item.getOrgId() + "_" + item.getPositionId() + "_" + item.getPositionGradesId()));
        // 之前数据的key集合
        Set<String> keySet = masterMap.keySet();
        // 数据标记
        int i = 0;
        for (String str : keySet) {
            if (oldOrgList.containsKey(str)) {
                i++;
            }
        }
        if (i == 0){
            String collect = masterConfigUserBoudDtos.stream().map(MasterConfigUserBoudDto::getOrgName).collect(Collectors.joining(","));
            throw new RuntimeException("当前用户组织信息与审批配置"+collect+"组织信息不符合请核对后重试!");
        }
        Map<String, List<SynUserBoundDTO>> newOrgMap = newDtos.stream()
                .collect(Collectors.groupingBy(item -> item.getOrgId() + "_" + item.getPositionId() + "_" + item.getPositionGradesId()));
        Set<String> orgKey = oldOrgList.keySet();
        int k =0;
        for (String str : orgKey){
            if (newOrgMap.containsKey(str)){
                newOrgMap.remove(str);
                masterMap.remove(str);
                k++;
            }
        }
        // 无任何修改
        if (k == orgKey.size()){
            return;
        }
        // 新数据先校验组织是否相同
        Map<String, List<SynUserBoundDTO>> newKeyOrgMap = newDtos.stream()
                .collect(Collectors.groupingBy(SynUserBoundDTO::getOrgId));
        int j = 0;
        Set<String> orgKeySet = masterOrgMap.keySet();
        for (String str : orgKeySet) {
            if (newKeyOrgMap.containsKey(str)) {
                j++;
            }
        }
        if (j == 0){
            throw new RuntimeException("修改失败！该人员在"+orgName+"组织中为审批人，请将其审批人身份变更为其他人后再进行编辑。!");
        }

        // 匹配数据
        // 数据库数据包含所有的组织信息
        Set<String> keySet1 = masterMap.keySet();
        Map<String, SynUserBoundDTO> dbSaveNewMap = new HashMap<>();
        for (String str : keySet1){
            String[] split = str.split("_");
            String orgId = split[0];
            String postId = split[1];
            String gradeId = split[2];
            Collection<List<SynUserBoundDTO>> values = newOrgMap.values();
            // 可能会有多个
            for (List<SynUserBoundDTO> value : values){
                for (SynUserBoundDTO synUserBoundDTO : value){

                    if (synUserBoundDTO.getOrgId().equals(orgId)
                            && synUserBoundDTO.getPositionId().equals(postId)
                            && synUserBoundDTO.getPositionGradesId().equals(gradeId)){

                    }else if (synUserBoundDTO.getOrgId().equals(orgId)&&
                            synUserBoundDTO.getPositionId().equals(postId)){
                        dbSaveNewMap.put(str,synUserBoundDTO);
                    }else if (synUserBoundDTO.getOrgId().equals(orgId)){
                        dbSaveNewMap.put(str,synUserBoundDTO);
                    }
                }
            }
        }
        Set<String> dbNewSet = dbSaveNewMap.keySet();
        List<FtbPersonnelsAuditConfigDTO> newDbData=new ArrayList<>();
        List<SynUserBoundDTO> boundDTOS = new ArrayList<>(dbSaveNewMap.values());
        List<String> gradesIds = boundDTOS.stream().map(SynUserBoundDTO::getPositionGradesId).collect(Collectors.toList());
        List<BasePositionGradesEntity>  gradesEntities =  positionApi.getGradesEntityList(gradesIds);
        Map<String, String> gradesMaps = Maps.newHashMap();
        if (CollUtil.isNotEmpty(gradesEntities)) gradesMaps = gradesEntities.stream().collect(Collectors.toMap(BasePositionGradesEntity::getId, BasePositionGradesEntity::getFullName, (k1, k2) -> k1));
        for (String str : dbNewSet){
            List<MasterConfigUserBoudDto> masterConfigUserBoudDtos1 = masterMap.get(str);
            SynUserBoundDTO synUserBoundDTO = dbSaveNewMap.get(str);
            for (MasterConfigUserBoudDto masterConfigUserBoudDto : masterConfigUserBoudDtos1){
                FtbPersonnelsAuditConfigVO auditList = getAuditList(masterConfigUserBoudDto.getOrgId(), masterConfigUserBoudDto.getConfigType());
                // 组装数据
                FtbPersonnelsAuditConfigDTO ftbPersonnelsAuditConfigDTO = new FtbPersonnelsAuditConfigDTO();
                ftbPersonnelsAuditConfigDTO.setId(auditList.getId());
                ftbPersonnelsAuditConfigDTO.setConfigType(auditList.getConfigType());
                ftbPersonnelsAuditConfigDTO.setOrgId(auditList.getOrgId());
                ftbPersonnelsAuditConfigDTO.setCopyRecipient(auditList.getCopyRecipient());
                ftbPersonnelsAuditConfigDTO.setIsOnePersonPasses(auditList.getIsOnePersonPasses());
                ftbPersonnelsAuditConfigDTO.setOrgName(auditList.getOrgName());
                ftbPersonnelsAuditConfigDTO.setIsApprovalComments(auditList.getIsApprovalComments());
                List<FtbPersonnelsAuditConfigInfoDTO> configInfo = auditList.getConfigInfo();
                List<FtbPersonnelsAuditConfigInfoDTO> newConfigInfo = new ArrayList<>();
                for (FtbPersonnelsAuditConfigInfoDTO configInfoDTO : configInfo){
                    //  封装
                    FtbPersonnelsAuditConfigInfoDTO ftbPersonnelsAuditConfigInfoDTO = new FtbPersonnelsAuditConfigInfoDTO();
                    ftbPersonnelsAuditConfigInfoDTO.setAuditProcessLevel(configInfoDTO.getAuditProcessLevel());
                    ftbPersonnelsAuditConfigInfoDTO.setAuditorId(configInfoDTO.getAuditorId());
                    ftbPersonnelsAuditConfigInfoDTO.setAuditorName(configInfoDTO.getAuditorName());
                    ftbPersonnelsAuditConfigInfoDTO.setPayrollAudit(configInfoDTO.getPayrollAudit());
                    ftbPersonnelsAuditConfigInfoDTO.setPayrollEntry(configInfoDTO.getPayrollEntry());
                    if (configInfoDTO.getAuditorId().equals(masterConfigUserBoudDto.getUserId())){
                        ftbPersonnelsAuditConfigInfoDTO.setOrgId(synUserBoundDTO.getOrgId());
                        OrganizeEntity infoById = organizeApi.getInfoById(synUserBoundDTO.getOrgId());
                        if (infoById != null)ftbPersonnelsAuditConfigInfoDTO.setOrgName(infoById.getFullName());
                        ftbPersonnelsAuditConfigInfoDTO.setPostId(synUserBoundDTO.getPositionId());
                        PositionEntity positionEntity = positionApi.queryInfoById(synUserBoundDTO.getPositionId());
                        if (positionEntity != null) ftbPersonnelsAuditConfigInfoDTO.setPostName(positionEntity.getFullName());
                        ftbPersonnelsAuditConfigInfoDTO.setGradeId(synUserBoundDTO.getPositionGradesId());
                        ftbPersonnelsAuditConfigInfoDTO.setGradeName(gradesMaps.get(synUserBoundDTO.getPositionGradesId()));
                    }else {
                        ftbPersonnelsAuditConfigInfoDTO.setOrgId(configInfoDTO.getOrgId());
                        ftbPersonnelsAuditConfigInfoDTO.setOrgName(configInfoDTO.getOrgName());
                        ftbPersonnelsAuditConfigInfoDTO.setPostId(configInfoDTO.getPostId());
                        ftbPersonnelsAuditConfigInfoDTO.setPostName(configInfoDTO.getPostName());
                        ftbPersonnelsAuditConfigInfoDTO.setGradeId(configInfoDTO.getGradeId());
                        ftbPersonnelsAuditConfigInfoDTO.setGradeName(configInfoDTO.getGradeName());

                    }
                    newConfigInfo.add(ftbPersonnelsAuditConfigInfoDTO);
                }
                ftbPersonnelsAuditConfigDTO.setConfigInfo(newConfigInfo);
                newDbData.add(ftbPersonnelsAuditConfigDTO);
            }
        }
        //   创建新的审批流程
        newDbData.forEach(this::createApprovalConfiguration);
    }


}
