package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.enums.LabelFactoryEnum;
import cn.cecep.talroad.mapper.*;
import cn.cecep.talroad.service.IPcLabelFactoryService;
import cn.cecep.talroad.vo.*;
import cn.cecep.talroad.vo.query.PcLabelFactoryQuery;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


/**
 * 特征标签-与企业关系中间Service业务层处理
 *
 * @author szhpt
 * @date 2023-02-02*/
@Slf4j
@Service
public class PcLabelFactoryServiceImpl extends ServiceImpl<PcLabelFactoryMapper,PcLabelFactory> implements IPcLabelFactoryService
{
    @Autowired
    private PcLabelFactoryMapper pcLabelFactoryMapper;
    @Autowired
    private PcFactoryMapper pcFactoryMapper;
    @Autowired
    private PcLabelMapper pcLabelMapper;
    @Autowired
    private PcLabelValueMapper pcLabelValueMapper;
    @Autowired
    private BMainAllowablePollutionAirMapper bMainAllowablePollutionAirMapper;
    @Autowired
    private PcControlRequirementsMapper pcControlRequirementsMapper;
    @Autowired
    private AmDoorMonitorAccountFiMapper amDoorMonitorAccountFiMapper;
    @Autowired
    private BMainAllowablePollutionWaterMapper bMainAllowablePollutionWaterMapper;
    @Autowired
    private PcOutletMapper pcOutletMapper;

    @Autowired
    private BMainPollDisMapper bMainPollDisMapper;
    @Autowired
    private PcAdslFactoryMapper pcAdslFactoryMapper;
    @Autowired
    private MAmVideoDevFiMapper mAmVideoDevFiMapper;
    @Autowired
    private BaseControlConcentStandardsMapper baseControlConcentStandardsMapper;
    @Autowired
    private EarlyWarningMapper earlyWarningMapper;
    /**
     * 查询特征标签-与企业关系中间列表
     *
     * @param pcLabelFactory 特征标签-与企业关系中间
     * @return 特征标签-与企业关系中间
     */
    @Override
    public List<PcLabelFactory> selectPcLabelFactoryList(PcLabelFactory pcLabelFactory)
    {
        return pcLabelFactoryMapper.selectPcLabelFactoryList(pcLabelFactory);
    }

    @Override
    public void updateLabelStatus() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            //生产类型
            if (StringUtils.isNotBlank(item.getProductionStatus())) {
                pcLabelFactories.add(LabelFactoryEnum.BASE003.pcLabelFactory(item.getId(), pcLabelsMap, item.getProductionStatus(), "0", pcLabelFactory));
            }
            //是否重点排污单位
            if (StringUtils.isNotBlank(item.getWipollunit())) {
                pcLabelFactories.add(LabelFactoryEnum.BASE004.pcLabelFactory(item.getId(), pcLabelsMap, item.getWipollunit(), "1", pcLabelFactory));
            }
            //重点排污单位名录类别
            pcLabelFactories.add(LabelFactoryEnum.BASE005.pcLabelFactory(item.getId(),pcLabelsMap,item.getWipollunitType(),"1",pcLabelFactory));
           //许可管理类别
            if (StringUtils.isNotBlank(item.getLicenseManType())){
            pcLabelFactories.add(LabelFactoryEnum.BASE006.pcLabelFactory(item.getId(),pcLabelsMap,item.getLicenseManType(),"1",pcLabelFactory));
            }
            //许可是否整改
            if (StringUtils.isNotBlank(item.getLicenseWabarbeitung())){
            pcLabelFactories.add(LabelFactoryEnum.BASE007.pcLabelFactory(item.getId(),pcLabelsMap,item.getLicenseWabarbeitung(),"1",pcLabelFactory));
            }
           //管控级别
            if (StringUtils.isNotBlank(item.getPreConLevel())){
            pcLabelFactories.add(LabelFactoryEnum.BASE008.pcLabelFactory(item.getId(),pcLabelsMap,item.getPreConLevel(),"1",pcLabelFactory));
            }
            //生态环境信用级别
            if (StringUtils.isNotBlank(item.getEcoEnvCreditLevel())){
            pcLabelFactories.add(LabelFactoryEnum.BASE009.pcLabelFactory(item.getId(),pcLabelsMap,item.getEcoEnvCreditLevel(),"0",pcLabelFactory));
            }
            //所在地是否属于大气重点控制区
            if (StringUtils.isNotBlank(item.getWhetherLocalKeyAri())){
            pcLabelFactories.add(LabelFactoryEnum.BASE011.pcLabelFactory(item.getId(),pcLabelsMap,item.getWhetherLocalKeyAri().equals("是")?"AIR_CONTROL":"NON_AIR_CONTROL","0",pcLabelFactory));
            }
            //所在地是否属于总磷控制区
            if (StringUtils.isNotBlank(item.getWhetherLocalKeyPho())){
            pcLabelFactories.add(LabelFactoryEnum.BASE012.pcLabelFactory(item.getId(),pcLabelsMap,item.getWhetherLocalKeyPho().equals("是")?"P_CONTROL":"NON_P_CONTROL","0",pcLabelFactory));
            }
            //所在地是否属于总氮控制区
            if (StringUtils.isNotBlank(item.getWhetherLocalKeyNitrogen())){
            pcLabelFactories.add(LabelFactoryEnum.BASE013.pcLabelFactory(item.getId(),pcLabelsMap,item.getWhetherLocalKeyNitrogen().equals("是")?"N_CONTROL":"NON_P_CONTROL","0",pcLabelFactory));
            }
            //所在地是否属于重金属污染物特别排放限值实施区域
            if (StringUtils.isNotBlank(item.getWhetherLocalKeyPollArea())){
            pcLabelFactories.add(LabelFactoryEnum.BASE014.pcLabelFactory(item.getId(),pcLabelsMap,item.getWhetherLocalKeyPollArea().equals("是")?"METAL":"NON_METAL","0",pcLabelFactory));
            }
            //是否位于工业园区
            if (StringUtils.isNotBlank(item.getWhetherLocalPark())){
            pcLabelFactories.add(LabelFactoryEnum.BASE015.pcLabelFactory(item.getId(),pcLabelsMap,item.getWhetherLocalPark().equals("是")?"IN_PARK":"NOT_IN_PARK","0",pcLabelFactory));
            }
            //涉源类型
            if (StringUtils.isNotBlank(item.getMajorPollutantCategories())) {
                pcLabelFactories.add(LabelFactoryEnum.PF007.pcLabelFactory(item.getId(), pcLabelsMap, majorCategories(item.getMajorPollutantCategories()), "0", pcLabelFactory));
            }
        });
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();

        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(StringUtils.isNotBlank(stringListMap.get(item.getLabelId()).get(0).getLabelValue())){
                        if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                            //修改
                            item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                            updateLabelFactory.add(item);
                        }
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }

    }
    private  String majorCategories(String value){
        List<String> list=new ArrayList<>();
        if(value.contains("fq")){
            list.add("1020");
        }
        if(value.contains("fs")){
            list.add("1019");
        }
        if(value.contains("gf")){
            list.add("1021");
        }
        if(value.contains("wf")){
            list.add("1022");
        }
        return String.join(",", list);
    }

    @Override
    public void statisticsAirWastewater() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE020.getEnumId()));
        PcLabel waterMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE021.getEnumId()));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        HashSet<String> airMain = new HashSet<String>();
        HashSet<String> waterMain = new HashSet<String>();

        pcFactories.forEach(item->{
            //大气主要污染物种类
            if (StringUtils.isNotBlank(item.getAirMainPollutants())) {
                pcLabelFactories.add(LabelFactoryEnum.BASE020.pcLabelFactory(item.getId(), pcLabelsMap, item.getAirMainPollutants().replace("，", ","), "0", pcLabelFactory));
                airMain.addAll(Arrays.asList(item.getAirMainPollutants().replace("，", ",").split(",")));
            }
            //废水主要污染物种类
            if (StringUtils.isNotBlank(item.getWaterMainPollutants())) {
                pcLabelFactories.add(LabelFactoryEnum.BASE021.pcLabelFactory(item.getId(), pcLabelsMap, item.getWaterMainPollutants().replace("，", ","), "0", pcLabelFactory));
                waterMain.addAll(Arrays.asList(item.getWaterMainPollutants().replace("，", ",").split(",")));
            }
        });
        //标签值动态绑定---------------------------------------------------------------------------
        //去重
//        List<String> airMainCollect = airMain.stream().distinct().collect(Collectors.toList());
        AtomicInteger orderNum= new AtomicInteger();
        //先删除大气污染物
        pcLabelValueMapper.delete(new QueryWrapper<PcLabelValue>().eq("label_id",airMainCode.getId()));
        //每次进行新增
        List<PcLabelValue> pcLabelValueAir=new ArrayList<>();
        airMain.forEach(item->{
            PcLabelValue pcLabelValue=new PcLabelValue();
            pcLabelValue.setLabelId(airMainCode.getId());
            pcLabelValue.setArgumentsCode(item);
            pcLabelValue.setArgumentsName(item);
            pcLabelValue.setOrderNum(orderNum.getAndIncrement());
            pcLabelValue.setId(UUID.randomUUID().toString().replace("-", ""));
            pcLabelValueAir.add(pcLabelValue);
        });
        if(CollectionUtil.isNotEmpty(pcLabelValueAir)){
            List<List<PcLabelValue>> partition = ListUtil.partition(pcLabelValueAir, 2000);
            partition.forEach(i->{
                pcLabelValueMapper.batchSave(i);
            });
            log.info("新增大气污染物的标签值{}",pcLabelValueAir.size());
        }
        //去重
//        List<String> waterMainCollect = waterMain.stream().distinct().collect(Collectors.toList());
        //先删除废水污染物
        pcLabelValueMapper.delete(new QueryWrapper<PcLabelValue>().eq("label_id",waterMainCode.getId()));
        AtomicInteger waterOrderNum= new AtomicInteger();
        //每次进行新增
        List<PcLabelValue> pcLabelValueWater=new ArrayList<>();
        waterMain.forEach(item->{
            PcLabelValue pcLabelValue=new PcLabelValue();
            pcLabelValue.setLabelId(waterMainCode.getId());
            pcLabelValue.setArgumentsCode(item);
            pcLabelValue.setArgumentsName(item);
            pcLabelValue.setOrderNum(waterOrderNum.getAndIncrement());
            pcLabelValue.setId(UUID.randomUUID().toString().replace("-", ""));
            pcLabelValueWater.add(pcLabelValue);
        });
        if(CollectionUtil.isNotEmpty(pcLabelValueWater)){
            List<List<PcLabelValue>> partition = ListUtil.partition(pcLabelValueWater, 2000);
            partition.forEach(i->{
                pcLabelValueMapper.batchSave(i);
            });
            log.info("新增废水污染物的标签值{}",pcLabelValueWater.size());
        }
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
        //先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",waterMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void statisticsLicensed() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        List<BMainAllowablePollutionAir> bMainAllowablePollutionAirs = bMainAllowablePollutionAirMapper.selectLabelBMainAllowablePollutionAir();
        List<BMainAllowablePollutionAir> bMainAllowablePollutionWater = bMainAllowablePollutionWaterMapper.selectLabelWater();
        bMainAllowablePollutionAirs.addAll(bMainAllowablePollutionWater);
        Map<String, List<BMainAllowablePollutionAir>> pollutionAir = bMainAllowablePollutionAirs.stream().collect(Collectors.groupingBy( BMainAllowablePollutionAir::getFactoryId));
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE022.getEnumId()));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        HashSet<Map<String,String>> airMain = new HashSet<>();
        pcFactories.forEach(item->{
            //有许可总量要求的污染物
            if(pollutionAir.containsKey(item.getId())){
                List<BMainAllowablePollutionAir> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                HashSet<String> chilePollutant = new HashSet<>();
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    Map<String,String> stringMap=new HashMap<>();
                    stringMap.put("code",childItem.getPollutionCategory());
                    stringMap.put("name",childItem.getPollName());
                    airMain.add(stringMap);
                    if(StringUtils.isNotBlank(childItem.getTotalFrist())&&!childItem.getTotalFrist().equals("/")){
                        chilePollutant.add(childItem.getPollutionCategory());
                    }
                });
//                if(CollectionUtil.isNotEmpty(chilePollutant)){
                    pcLabelFactories.add(LabelFactoryEnum.BASE022.pcLabelFactory(item.getId(), pcLabelsMap,String.join(",", chilePollutant) , "0", pcLabelFactory));
//                }

            }
        });
        //标签值动态绑定---------------------------------------------------------------------------
        AtomicInteger orderNum= new AtomicInteger();
        //先删除有许可总量要求的污染物
        pcLabelValueMapper.delete(new QueryWrapper<PcLabelValue>().eq("label_id",airMainCode.getId()));
        //每次进行新增
        List<PcLabelValue> pcLabelValueList=new ArrayList<>();
        airMain.forEach(item->{
            PcLabelValue pcLabelValue=new PcLabelValue();
            pcLabelValue.setLabelId(airMainCode.getId());
            pcLabelValue.setArgumentsCode(item.get("code"));
            pcLabelValue.setArgumentsName(item.get("name"));
            pcLabelValue.setOrderNum(orderNum.getAndIncrement());
            pcLabelValue.setId(UUID.randomUUID().toString().replace("-", ""));
            pcLabelValueList.add(pcLabelValue);
        });
        if(CollectionUtil.isNotEmpty(pcLabelValueList)){
            List<List<PcLabelValue>> partition = ListUtil.partition(pcLabelValueList, 2000);
            partition.forEach(i->{
                pcLabelValueMapper.batchSave(i);
            });
            log.info("新增有许可总量要求的污染物的标签值{}",pcLabelValueList.size());
        }
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void statisticseMissions() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.PF027.getEnumId()));
        List<PcControlRequirementsVo> bMainAllowablePollutionAirs = pcControlRequirementsMapper.selectLabelRequirements();
        Map<String, List<PcControlRequirementsVo>> pollutionAir = bMainAllowablePollutionAirs.stream().collect(Collectors.groupingBy( PcControlRequirementsVo::getFactoryId));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            AtomicBoolean bol= new AtomicBoolean(false);
            if(pollutionAir.containsKey(item.getId())){
                List<PcControlRequirementsVo> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    if(StringUtils.isNotBlank(childItem.getCommitmentEmissionLim())&&!childItem.getCommitmentEmissionLim().equals("/")){
                        bol.set(true);
                    }
                });
            }
            pcLabelFactories.add(LabelFactoryEnum.PF027.pcLabelFactory(item.getId(), pcLabelsMap,bol.get()?"P_CNPFXZ":"NON_P_CNPFXZ" , "0", pcLabelFactory));
        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void transportVehicleType() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.JG010.getEnumId()));
        LocalDateTime now = LocalDateTime.now();
        String startTime=now.format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        );
        String endTime= now.minusWeeks(1).format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        );
        List<AmDoorMonitorAccountFiVo> amDoorMonitorAccountFis = amDoorMonitorAccountFiMapper.selectLabel(endTime,startTime);
        Map<String, List<AmDoorMonitorAccountFiVo>> pollutionAir = amDoorMonitorAccountFis.stream().collect(Collectors.groupingBy( AmDoorMonitorAccountFiVo::getFactoryId));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            if(pollutionAir.containsKey(item.getId())){
                List<AmDoorMonitorAccountFiVo> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    pcLabelFactories.add(LabelFactoryEnum.JG010.pcLabelFactory(item.getId(), pcLabelsMap,childItem.getDischargeStage() , "0", pcLabelFactory));
                });
            }

        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void wastewaterDischarge() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.PF001.getEnumId()));
        List<PcOutlet> pcOutlets = pcOutletMapper.selectLabelRequirements();
        Map<String, List<PcOutlet>> pollutionAir = pcOutlets.stream().collect(Collectors.groupingBy( PcOutlet::getFactoryId));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            List<String> stringList=new ArrayList<>();
            if(pollutionAir.containsKey(item.getId())){
                List<PcOutlet> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    if(StringUtils.isNotBlank(childItem.getEmissionsForm())){
                        stringList.add(childItem.getEmissionsForm().equals("直接排放")?"1001":"1002");
                    }
                });
            }
            pcLabelFactories.add(LabelFactoryEnum.PF001.pcLabelFactory(item.getId(), pcLabelsMap,String.join(",",stringList) , "0", pcLabelFactory));
        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void receivingWaterBody() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.PF003.getEnumId()));
        List<PcOutlet> pcOutlets = pcOutletMapper.selectLabelReceivingWaterObjective();
        Map<String, List<PcOutlet>> pollutionAir = pcOutlets.stream().collect(Collectors.groupingBy( PcOutlet::getFactoryId));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            HashSet<String> stringList = new HashSet<String>();
            if(pollutionAir.containsKey(item.getId())){
                List<PcOutlet> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    if(StringUtils.isNotBlank(childItem.getReceivingWaterObjective())){
                        stringList.add(receivingWaterObjective(childItem.getReceivingWaterObjective()));
                    }
                });
            }
            pcLabelFactories.add(LabelFactoryEnum.PF003.pcLabelFactory(item.getId(), pcLabelsMap,String.join(",",stringList) , "0", pcLabelFactory));
        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }
    private  String receivingWaterObjective(String value){
        String str="";
        if(value.contains("Ⅲ类")){
            str="1008";
        }
        if(value.contains("III类")){
            str="1008";
        }
        if(value.contains("Ⅱ类")){
            str="1007";
        }
        if(value.contains("II类")){
            str="1007";
        }
        if(value.contains("劣Ⅴ类")){
            str="1011";
        }
        if(value.contains("劣V类")){
            str="1011";
        }
        if(value.contains("Ⅰ类")){
            str="1006";
        }
        if(value.contains("I类")){
            str="1006";
        }
        if(value.contains("Ⅴ类")){
            str="1010";
        }
        if(value.contains("V类")){
            str="1010";
        }
        if(value.contains("Ⅳ类")){
            str= "1009";
        }
        if(value.contains("IV类")){
            str= "1009";
        }
        return str;
    }

    /**
     * @Author: Mr.Yanghongtao
     * @data: 2023/4/9 11:13 PM
     * @Deseription: 排污许可证是否在有效期内
     * @Params: type = 1 (排污许可证是否在有效期内) type = 2 (许可证有效期不足1个月)
     * @Return:
     * @Throws:
     */
    @Override
    public void efficientlyDischargePollutants(Integer type) {
        // 查询所有企业
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<PcFactory>().ne("factory_source",3));
        // 查询所有企业排污许可证办理所对应的时间（不区分业务类型，只取办结时间最新的那一条数据）
        List<BMainPollDis> bMainPollDis = bMainPollDisMapper.selectBMainPollDisListByFactory();
        Date nowDate = new Date();
        // key为企业ID，过滤出在有效期内的企业
        Map<String, Boolean> collect = bMainPollDis.stream()
                .filter(x->StringUtils.isNotBlank(x.getPollDisLicenseDeadline()) && x.getPollDisLicenseDeadline().contains("至") && x.getPollDisLicenseDeadline().length() >= 21)
                .collect(Collectors.toMap(BMainPollDis::getFactoryId, x->{
                    if (1 == type) {
                        // 截取开始日期
                        Date startTime = DateUtils.parseDate(x.getPollDisLicenseDeadline().split("至")[0] + " 00:00:00");
                        Date endTime = DateUtils.parseDate(x.getPollDisLicenseDeadline().split("至")[1] + " 23:59:59");
                        // 判断当前日期是否在有效期内
                        if (nowDate.getTime() >= startTime.getTime() && nowDate.getTime() <= endTime.getTime()) {
                            return true;
                        }
                    } else {
                        // 截取结束日期
                        Date endTime = DateUtils.parseDate(x.getPollDisLicenseDeadline().split("至")[1] + " 23:59:59");
                        // 获取结束时间前一个月的日期
                        Date startTime = DateUtils.addDateMonths(endTime,-1);
                        // 判断当前日期是否在有效期内
                        if (nowDate.getTime() >= startTime.getTime() && nowDate.getTime() <= endTime.getTime()) {
                            return true;
                        }
                    }
                    return false;
                }, (oldValue, newValue) -> newValue));


        List<PcLabelFactory> pcLabelFactories = new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory = null;
        pcFactories.forEach(item->{
            if(collect.containsKey(item.getId())){
                if (1 == type) {
                    pcLabelFactories.add(LabelFactoryEnum.JG001.pcLabelFactory(item.getId(), pcLabelsMap, collect.get(item.getId()) ? "1001" : "", "0", pcLabelFactory));
                } else {
                    pcLabelFactories.add(LabelFactoryEnum.JG002.pcLabelFactory(item.getId(), pcLabelsMap, collect.get(item.getId()) ? "1001" : "", "0", pcLabelFactory));
                }
            }else{
                if (1 == type) {
                    pcLabelFactories.add(LabelFactoryEnum.JG001.pcLabelFactory(item.getId(), pcLabelsMap, "1002", "0", pcLabelFactory));
                }
            }
        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory = new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory = new ArrayList<>();
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void acceptNature() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        List<PcOutlet> pcOutlets = pcOutletMapper.selectLabeRreceivingWaterName();
        Map<String, List<PcOutlet>> pollutionAir = pcOutlets.stream().collect(Collectors.groupingBy( PcOutlet::getFactoryId));
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.PF002.getEnumId()));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        HashSet<Map<String,String>> airMain = new HashSet<>();
        pcFactories.forEach(item->{
            if(pollutionAir.containsKey(item.getId())){
                List<PcOutlet> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                HashSet<String> chilePollutant = new HashSet<>();
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    if(StringUtils.isNotBlank(childItem.getReceivingWaterName())&&!childItem.getReceivingWaterName().equals("/")){
                        Map<String,String> stringMap=new HashMap<>();
                        stringMap.put("code",childItem.getReceivingWaterName());
                        stringMap.put("name",childItem.getReceivingWaterName());
                        airMain.add(stringMap);
                        chilePollutant.add(childItem.getReceivingWaterName());
                    }
                });
//                if(CollectionUtil.isNotEmpty(chilePollutant)){
                pcLabelFactories.add(LabelFactoryEnum.PF002.pcLabelFactory(item.getId(), pcLabelsMap,String.join(",", chilePollutant) , "0", pcLabelFactory));
//                }

            }
        });
        //标签值动态绑定---------------------------------------------------------------------------
        AtomicInteger orderNum= new AtomicInteger();
        //先删除有许可总量要求的污染物
        pcLabelValueMapper.delete(new QueryWrapper<PcLabelValue>().eq("label_id",airMainCode.getId()));
        //每次进行新增
        List<PcLabelValue> pcLabelValueList=new ArrayList<>();
        airMain.forEach(item->{
            PcLabelValue pcLabelValue=new PcLabelValue();
            pcLabelValue.setLabelId(airMainCode.getId());
            pcLabelValue.setArgumentsCode(item.get("code"));
            pcLabelValue.setArgumentsName(item.get("name"));
            pcLabelValue.setOrderNum(orderNum.getAndIncrement());
            pcLabelValue.setId(UUID.randomUUID().toString().replace("-", ""));
            pcLabelValueList.add(pcLabelValue);
        });
        if(CollectionUtil.isNotEmpty(pcLabelValueList)){
            List<List<PcLabelValue>> partition = ListUtil.partition(pcLabelValueList, 2000);
            partition.forEach(i->{
                pcLabelValueMapper.batchSave(i);
            });
            log.info("新增有许可总量要求的污染物的标签值{}",pcLabelValueList.size());
        }
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    // 直排海企业
    private final String [] zpqy = {"唐山三友化工股份有限公司纯碱分公司","华润电力（唐山曹妃甸）有限公司","首钢京唐钢铁联合有限责任公司","厦门水务乐亭城建投资有限公司"
            ,"河北大唐国家王滩发电有限责任公司","曹妃甸中石油京唐液化天然气有限公司"};

    /**
     * @Author: Mr.Yanghongtao
     * @data: 2023/4/10 1:25 AM
     * @Deseription: 是否为直排海企业
     * @Params: null
     * @Return:
     * @Throws:
     */
    @Override
    public void isDirectDischargeEnterprises() {
        // 查询所有企业
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());

        List<PcLabelFactory> pcLabelFactories = new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory = null;
        pcFactories.forEach(item->{
            if (Arrays.asList(zpqy).contains(item.getFactoryName())) {
                pcLabelFactories.add(LabelFactoryEnum.PF004.pcLabelFactory(item.getId(), pcLabelsMap, "1012", "0", pcLabelFactory));
            } else {
                pcLabelFactories.add(LabelFactoryEnum.PF004.pcLabelFactory(item.getId(), pcLabelsMap, "1013", "0", pcLabelFactory));
            }
        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory = new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory = new ArrayList<>();
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void monitoringEquipment() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.JG011.getEnumId()));
        List<PcControlRequirementsVo> amDoorMonitorAccountFis = pcControlRequirementsMapper.selectLabelOnlineRequirements();
        List<PcControlRequirementsVo> onlineOutPoll = pcControlRequirementsMapper.selectLabelOnlineOutPoll();
        Map<String, List<PcControlRequirementsVo>> pollutionOut = onlineOutPoll.stream().collect(Collectors.groupingBy( v -> v.getId() + "_" + v.getPollutantId()));
        Map<String, List<PcControlRequirementsVo>> pollutionAir = amDoorMonitorAccountFis.stream().collect(Collectors.groupingBy( PcControlRequirementsVo::getFactoryId));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            if(pollutionAir.containsKey(item.getId())){
                AtomicBoolean bol= new AtomicBoolean(true);
                List<PcControlRequirementsVo> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    if(!pollutionOut.containsKey(childItem.getOutletId()+ "_" +childItem.getPollutantId())){
                        bol.set(false);
                    };
                });
                pcLabelFactories.add(LabelFactoryEnum.JG011.pcLabelFactory(item.getId(), pcLabelsMap,bol.get()?"1001":"1002" , "0", pcLabelFactory));
            }

        });

        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void selectAdslFactory() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE023.getEnumId()));
        List<PcAdslFactory> pcOutlets = pcAdslFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, List<PcAdslFactory>> pollutionAir = pcOutlets.stream().collect(Collectors.groupingBy( PcAdslFactory::getFactoryId));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        pcFactories.forEach(item->{
            if(pollutionAir.containsKey(item.getId())){
                List<PcAdslFactory> bMainAllowablePollutionAirs1 = pollutionAir.get(item.getId());
                HashSet<String> chilePollutant = new HashSet<>();
                bMainAllowablePollutionAirs1.forEach(childItem->{
                    if(StringUtils.isNotBlank(childItem.getAdslCode())){
                        chilePollutant.add(childItem.getAdslCode());
                    }
                });
                pcLabelFactories.add(LabelFactoryEnum.BASE023.pcLabelFactory(item.getId(), pcLabelsMap,String.join(",", chilePollutant) , "0", pcLabelFactory));
            }
        });
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
        //先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void totalVideo() {

//        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
//      List<MAmVideoDevFi>  mAmVideoDevFis= mAmVideoDevFiMapper.selectList(new QueryWrapper<MAmVideoDevFi>().eq("status","9"));
//        Map<String, List<MAmVideoDevFi>> pollutionAir = mAmVideoDevFis.stream().collect(Collectors.groupingBy( MAmVideoDevFi::getFactoryId));
//        pcFactories.forEach(item->{
//            if(pollutionAir.containsKey(item.getFactoryName())){
//                List<MAmVideoDevFi> mal=pollutionAir.get(item.getFactoryName());
//                mal.forEach(iteml->{
//                    iteml.setFactoryId(item.getId());
//                    iteml.setOutletId("9");
//                    mAmVideoDevFiMapper.updateById(iteml);
//                });
//            }
//            });
        List<BigInteger> lis=new ArrayList<>();
        List<MAmVideoDevFi>  mAmVideoDevFis= mAmVideoDevFiMapper.selectListDel();
        mAmVideoDevFis.forEach(item->{
            List<MAmVideoDevFi> mal= mAmVideoDevFiMapper.selectList(new QueryWrapper<MAmVideoDevFi>().eq("channel_id",item.getDevId()));
//            mAmVideoDevFiMapper.deleteById(mal.get(0).getId());
            lis.add(mal.get(0).getId());
        });
        System.out.println(lis.toString());
//        mAmVideoDevFiMapper.updateById()
    }

    @Override
    public void industryCategory() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE001.getEnumId()));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        HashSet<String> airMain = new HashSet<String>();

        pcFactories.forEach(item->{
            if (StringUtils.isNotBlank(item.getIndustryPermissionName())) {
                pcLabelFactories.add(LabelFactoryEnum.BASE001.pcLabelFactory(item.getId(), pcLabelsMap, item.getIndustryPermissionName(), "1", pcLabelFactory));
                airMain.add(item.getIndustryPermissionName());
            }
        });
        //标签值动态绑定---------------------------------------------------------------------------
        //去重
//        List<String> airMainCollect = airMain.stream().distinct().collect(Collectors.toList());
        AtomicInteger orderNum= new AtomicInteger();
        //先删除
        pcLabelValueMapper.delete(new QueryWrapper<PcLabelValue>().eq("label_id",airMainCode.getId()));
        //每次进行新增
        List<PcLabelValue> pcLabelValueAir=new ArrayList<>();
        airMain.forEach(item->{
            PcLabelValue pcLabelValue=new PcLabelValue();
            pcLabelValue.setLabelId(airMainCode.getId());
            pcLabelValue.setArgumentsCode(item);
            pcLabelValue.setArgumentsName(item);
            pcLabelValue.setOrderNum(orderNum.getAndIncrement());
            pcLabelValue.setId(UUID.randomUUID().toString().replace("-", ""));
            pcLabelValueAir.add(pcLabelValue);
        });
        if(CollectionUtil.isNotEmpty(pcLabelValueAir)){
            List<List<PcLabelValue>> partition = ListUtil.partition(pcLabelValueAir, 2000);
            partition.forEach(i->{
                pcLabelValueMapper.batchSave(i);
            });
            log.info("新增行业类别的标签值{}",pcLabelValueAir.size());
        }
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
        //先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }
    @Override
    public void testindustryCategory() {
        List<PcFactory> pcFactories1 = pcFactoryMapper.selectList(new QueryWrapper<>());
        List<PcFactory> pcFactories = pcFactoryMapper.selectRegulateBusinesses();
        Map<String, PcFactory> pcLabelsMaps = pcFactories.stream().collect(Collectors.toMap(PcFactory::getId, a -> a,(k1, k2)->k1));
//        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE502.getEnumId()));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;

        pcFactories1.forEach(item->{
            if(pcLabelsMaps.containsKey(item.getId())){
                pcLabelFactories.add(LabelFactoryEnum.BASE502.pcLabelFactory(item.getId(), pcLabelsMap, "1", "0", pcLabelFactory));
            }else {
                pcLabelFactories.add(LabelFactoryEnum.BASE502.pcLabelFactory(item.getId(), pcLabelsMap, "0", "0", pcLabelFactory));
            }
        });
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();

        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }

    @Override
    public void testRegulateBusinesses() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectRegulateBusinesses();
        PcLabel airMainCode = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE501.getEnumId()));
        List<PcLabelFactory> pcLabelFactories=new ArrayList<>();
        List<PcLabel> pcLabels = pcLabelMapper.selectList(new QueryWrapper<PcLabel>().isNotNull("label_code"));
        Map<String, PcLabel> pcLabelsMap = pcLabels.stream().collect(Collectors.toMap(PcLabel::getLabelCode, a -> a,(k1, k2)->k1));
        PcLabelFactory pcLabelFactory=null;
        HashSet<Map<String,String>> airMain = new HashSet<Map<String,String>>();

        pcFactories.forEach(item->{
            //大气主要污染物种类
            if (StringUtils.isNotBlank(item.getIndustryCategory())) {
                pcLabelFactories.add(LabelFactoryEnum.BASE501.pcLabelFactory(item.getId(), pcLabelsMap, item.getIndustryCategory(), "0", pcLabelFactory));
                Map<String,String> map =new HashMap<>();
                map.put("name",item.getIndusName());
                map.put("code",item.getIndustryCategory());
                airMain.add(map);
            }
        });
        //标签值动态绑定---------------------------------------------------------------------------
        //去重
//        List<String> airMainCollect = airMain.stream().distinct().collect(Collectors.toList());
        AtomicInteger orderNum= new AtomicInteger();
        //先删除大气污染物
        pcLabelValueMapper.delete(new QueryWrapper<PcLabelValue>().eq("label_id",airMainCode.getId()));
        //每次进行新增
        List<PcLabelValue> pcLabelValueAir=new ArrayList<>();
        airMain.forEach(item->{
            PcLabelValue pcLabelValue=new PcLabelValue();
            pcLabelValue.setLabelId(airMainCode.getId());
            pcLabelValue.setArgumentsCode(item.get("code"));
            pcLabelValue.setArgumentsName(item.get("name"));
            pcLabelValue.setOrderNum(orderNum.getAndIncrement());
            pcLabelValue.setId(UUID.randomUUID().toString().replace("-", ""));
            pcLabelValueAir.add(pcLabelValue);
        });
        if(CollectionUtil.isNotEmpty(pcLabelValueAir)){
            List<List<PcLabelValue>> partition = ListUtil.partition(pcLabelValueAir, 2000);
            partition.forEach(i->{
                pcLabelValueMapper.batchSave(i);
            });
            log.info("新增大气污染物的标签值{}",pcLabelValueAir.size());
        }
        //企业标签绑定-----------------------------------------------------
        //新增集合
        List<PcLabelFactory> insertLabelFactory=new ArrayList<>();
        //修改集合
        List<PcLabelFactory> updateLabelFactory=new ArrayList<>();
//先删除
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",airMainCode.getId()));
        List<PcLabelFactory> pcLabelFactories1 = pcLabelFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, Map<String, List<PcLabelFactory>>> infoMap = pcLabelFactories1.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId, Collectors.groupingBy(PcLabelFactory::getLabelId)));
        pcLabelFactories.forEach(item->{
            if(infoMap.containsKey(item.getFactoryId())){
                Map<String, List<PcLabelFactory>> stringListMap = infoMap.get(item.getFactoryId());
                if(stringListMap.containsKey(item.getLabelId())){
                    if(!stringListMap.get(item.getLabelId()).get(0).getLabelValue().equals(item.getLabelValue())){
                        //修改
                        item.setId(stringListMap.get(item.getLabelId()).get(0).getId());
                        updateLabelFactory.add(item);
                    }
                }else {
                    //新增
                    item.setId(UUID.randomUUID().toString().replace("-", ""));
                    insertLabelFactory.add(item);
                }
            }else {
                //新增
                item.setId(UUID.randomUUID().toString().replace("-", ""));
                insertLabelFactory.add(item);
            }
        });
        if(CollectionUtil.isNotEmpty(updateLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(updateLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchUpdateValue(i);
            });
            log.info("更新企业标签的数量{}",updateLabelFactory.size());
        }
        if(CollectionUtil.isNotEmpty(insertLabelFactory)){
            List<List<PcLabelFactory>> partition = ListUtil.partition(insertLabelFactory, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
            log.info("新增企业标签的数量{}",insertLabelFactory.size());
        }
    }


    @Override
    public void earlyWarning() {
        //每十分钟对监测点位前十分钟的浓度进行一个均值分析 取折算值 数据缺少 动态因子
        List<EarlyWarning> earlyWarnings = baseControlConcentStandardsMapper.selectWarningStandards();
        Map<String, List<EarlyWarning>> pollutionAir = earlyWarnings.stream().collect(Collectors.groupingBy( EarlyWarning::getStationId));
        LocalDateTime now = LocalDateTime.now();
        String startTime=now.format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
        );
        String endTime= now.minusMinutes(10).format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
        );
        List<WarningEnvGasMonRealVo> list= pcLabelFactoryMapper.selectWarningReal(startTime,endTime);
        List<EarlyWarning> insertAll=new ArrayList<>();
        list.forEach(item->{
            if(pollutionAir.containsKey(item.getOutletId())){
                List<EarlyWarning> earlyWarnings1 = pollutionAir.get(item.getOutletId());
                earlyWarnings1.forEach(child->{
                    //氮氧化物
                    if(child.getPollCode().equals("A21002")){
                        addBatchWarning(child,insertAll,startTime,endTime,item.getNoxConvert());
                    }
                    //二氧化硫
                    if(child.getPollCode().equals("A21026")){
                        addBatchWarning(child,insertAll,startTime,endTime,item.getSo2Convert());
                    }
                    //烟尘
                    if(child.getPollCode().equals("A34013")){
                        addBatchWarning(child,insertAll,startTime,endTime,item.getSmokeDustConvert());
                    }
                });
            }
        });
        //报警数据入库
        if(CollectionUtil.isNotEmpty(insertAll)){
            List<List<EarlyWarning>> partition = ListUtil.partition(insertAll, 2000);
            partition.forEach(i->{
                earlyWarningMapper.batchSave(i);
            });
        }
        log.info("新增报警数据数量{}",insertAll.size());
    }
    public void addBatchWarning(EarlyWarning child, List<EarlyWarning> insertAll, String startTime, String endTime, BigDecimal convert ){
        if(ObjectUtil.isNotNull(child.getControlStandard())&&ObjectUtil.isNotNull(convert)){
            if(convert.compareTo(child.getControlStandard()) > 0){
                String content=String.format("\"%s\"\"%s至%s\"，\"%s\"\"%s\"浓度均值为%smg/m3(标准值为%smg/m3),超过管控浓度标准。",child.getFactoryName(),startTime,endTime,child.getStationName(),child.getPollName(),convert.stripTrailingZeros().toPlainString(),child.getControlStandard().stripTrailingZeros().toPlainString());
                child.setWarningContent(content);
                child.setWarningDate(DateUtil.parse(endTime+":00","yyyy-MM-dd HH:mm:ss"));
                child.setEwId(UUID.randomUUID().toString().replace("-", ""));
                child.setCreateTime(new Date());
                insertAll.add(child);
            }
        }
    }

    @Override
    public void batchDelete(String labelId){
       QueryWrapper queryWrapper = new QueryWrapper();
       queryWrapper.eq("label_id", labelId);
       List<PcLabelFactory> pcLabelFactories =  pcLabelFactoryMapper.selectList(queryWrapper);
       if(!pcLabelFactories.isEmpty()){
           pcLabelFactoryMapper.deleteBatchIds(pcLabelFactories.stream().map(PcLabelFactory::getId).collect(Collectors.toList()));
       }
    }


    @Override
    public void updateMajorPollutantCategory() {
        List<PcLabelFactory> pcLabelFactories = new ArrayList<>();

        PcLabel majorCate = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE017.getEnumId()));
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",majorCate.getId()));
        //List<PcLabelValue> values = pcLabelValueMapper.selectList(new QueryWrapper<PcLabelValue>().eq("label_id",majorCate.getId()));

        List<PcFactory> factoryList = pcFactoryMapper.selectMajorPollutantCategory();
        if(factoryList!=null&&factoryList.size()>0){
            factoryList.forEach(f ->{
                String codes = f.getMajorPollutantCategories();
                codes = codes.replace("1","AIR");
                codes = codes.replace("2","WATER");
                PcLabelFactory plf = new PcLabelFactory();
                plf.setLabelId(majorCate.getId());
                plf.setFactoryId(f.getId());
                plf.setLableUseing("0");
                plf.setLabelValue(codes);
                plf.setId(UUID.randomUUID().toString().replace("-", ""));
                pcLabelFactories.add(plf);
            });

            pcLabelFactoryMapper.batchSave(pcLabelFactories);
        }
    }

    @Override
    public void pollutantLicenseLabel() {
        List<PcLabelFactory> pcLabelFactories = new ArrayList<>();
        PcLabel majorCate = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.BASE019.getEnumId()));
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",majorCate.getId()));
        List<FactoryLicenseVo>  list = pcLabelFactoryMapper.selectPollutantLicenseLabel();
        if(list!=null&&list.size()>0){
            list.forEach(p->{
                PcLabelFactory plf = new PcLabelFactory();
                plf.setLabelId(majorCate.getId());
                plf.setFactoryId(p.getFactoryId());
                plf.setLableUseing("0");
                plf.setLabelValue(p.getLabelValue());
                plf.setId(UUID.randomUUID().toString().replace("-", ""));
                pcLabelFactories.add(plf);
            });
            List<List<PcLabelFactory>> partition = ListUtil.partition(pcLabelFactories, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
        }
    }

    @Override
    public void licenseTypeLabel() {
        List<PcLabelFactory> pcLabelFactories = new ArrayList<>();
        PcLabel majorCate = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.PF005.getEnumId()));
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",majorCate.getId()));
        List<FactoryLicenseVo>  list = pcLabelFactoryMapper.selectLicenseTypeLabel();
        if(list!=null&&list.size()>0){
            list.forEach(p->{
                PcLabelFactory plf = new PcLabelFactory();
                plf.setLabelId(majorCate.getId());
                plf.setFactoryId(p.getFactoryId());
                plf.setLableUseing("0");
                plf.setLabelValue(p.getLabelValue());
                plf.setId(UUID.randomUUID().toString().replace("-", ""));
                pcLabelFactories.add(plf);
            });
            List<List<PcLabelFactory>> partition = ListUtil.partition(pcLabelFactories, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
        }

    }

    @Override
    public void pollutantTypeLabel() {
        List<PcLabelFactory> pcLabelFactories = new ArrayList<>();
        PcLabel majorCate = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().eq("label_code", LabelFactoryEnum.PF006.getEnumId()));
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().eq("label_id",majorCate.getId()));
        List<FactoryLicenseVo>  list = pcLabelFactoryMapper.selectPollutantTypeLabel();
        if(list!=null&&list.size()>0){
            list.forEach(p->{
                PcLabelFactory plf = new PcLabelFactory();
                plf.setLabelId(majorCate.getId());
                plf.setFactoryId(p.getFactoryId());
                plf.setLableUseing("0");
                plf.setLabelValue(p.getLabelValue());
                plf.setId(UUID.randomUUID().toString().replace("-", ""));
                pcLabelFactories.add(plf);
            });
            List<List<PcLabelFactory>> partition = ListUtil.partition(pcLabelFactories, 2000);
            partition.forEach(i->{
                pcLabelFactoryMapper.batchSave(i);
            });
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void isInstallFbjdLabel() {
        final List<PcLabelFactory> data = baseMapper.isInstallFbjdLabel();
        saveLabelData(LabelFactoryEnum.JG006,"0", data);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void isInstallWzzjkdwLabel() {
        final List<PcLabelFactory> data = baseMapper.isInstallWzzjkdwLabel();
        saveLabelData(LabelFactoryEnum.JG007,"0",data);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void isZlssExceptionInMonthLabel() {
        final List<PcLabelFactory> data = baseMapper.isZlssExceptionInMonthLabel(DateUtil.offset(new Date(), DateField.YEAR,-1));
        saveLabelData(LabelFactoryEnum.JG003,"0",data);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void isWzzcbpfExceptionInMonthLabel() {
        final List<PcLabelFactory> data = baseMapper.isWzzcbpfExceptionInMonthLabel(DateUtil.offset(new Date(), DateField.YEAR,-1));
        saveLabelData(LabelFactoryEnum.PF023,"0",data);
    }

    @Override
    public List<PcLabelFactoryVo> selectFactoryLabelList(Page<PcLabelFactoryVo> voPage, PcLabelFactoryQuery query) {
        List<PcLabelFactoryVo> list = baseMapper.selectFactoryLabelList(voPage, query);
        return list;
    }

    @Override
    public void add(PcLabelFactory pcLabelFactory) {
        PcLabel pcLabel = pcLabelMapper.selectById(pcLabelFactory.getLabelId());
        if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
            throw new RuntimeException("不支持给企业新增非手工标签值");
        }

        List<PcLabelFactory> list = list(new QueryWrapper<PcLabelFactory>()
                .eq("factory_id", pcLabelFactory.getFactoryId())
                .eq("label_id", pcLabelFactory.getLabelId())
        );
        if (list.size() > 0) {
            throw new RuntimeException("企业已存在该标签");
        }
        if (StringUtils.isBlank(pcLabelFactory.getLabelValue())) {
            pcLabelFactory.setLableUseing("0");
        }
        pcLabelFactory.setId(UUID.randomUUID().toString());
        save(pcLabelFactory);
    }

    @Override
    public void edit(PcLabelFactory pcLabelFactory) {
        PcLabelFactory dbLabelFactory = getById(pcLabelFactory.getId());
        if (dbLabelFactory == null) {
            return;
        }

        // 若修改了标签值
        if (!Objects.equals(dbLabelFactory.getLabelValue(), pcLabelFactory.getLabelValue())) {
            PcLabel pcLabel = pcLabelMapper.selectById(dbLabelFactory.getLabelId());

            if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
                throw new RuntimeException("不支持修改企业非手工标签值");
            } else {
                if (StringUtils.isBlank(pcLabelFactory.getLabelValue())) {
                    throw new RuntimeException("标签值不能为空");
                }
            }
        }
        pcLabelFactory.setFactoryId(null);
        pcLabelFactory.setLabelId(null);
        updateById(pcLabelFactory);
    }

    @Override
    public void delete(String id) {
        PcLabelFactory dbLabelFactory = getById(id);
        if (dbLabelFactory == null) {
            return;
        }
        PcLabel pcLabel = pcLabelMapper.selectById(dbLabelFactory.getLabelId());
        if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
            throw new RuntimeException("不支持删除企业非手工标签值");
        }
        removeById(dbLabelFactory);
    }

    @Override
    public void batchAddFactoryLabel(PcLabelFactoryVo.FactoryLabelUpdateForm form) {
        PcLabel pcLabel = pcLabelMapper.selectById(form.getLabelId());
        if (StringUtils.isNotEmpty(pcLabel.getFactoryField()) || LabelFactoryEnum.getByCode(pcLabel.getLabelCode()) != null) {
            throw new RuntimeException("非手工标签，不允许分配企业");
        }
        List<PcLabelFactory> labelFactories = baseMapper.selectList(new QueryWrapper<PcLabelFactory>()
                .eq("label_id", form.getLabelId())
                .in("factory_id", form.getFactoryIds())
        );
        // 按照企业分组
        Map<String, List<PcLabelFactory>> factoryLabelGroup = labelFactories.stream()
                .collect(Collectors.groupingBy(PcLabelFactory::getFactoryId));

        List<PcLabelFactory> addList = new ArrayList<>(); // 新增的企业标签
        List<PcLabelFactory> updateList = new ArrayList<>(); // 修改的企业标签
        List<PcLabelFactory> deleteList = new ArrayList<>(); // 删除的企业标签

        // 表示需要给指定企业增加标签值
        if (form.getChecked()) {
            for (String factoryId : form.getFactoryIds()) {
                List<PcLabelFactory> list = factoryLabelGroup.get(factoryId);
                if (list == null) {
                    PcLabelFactory lf = new PcLabelFactory();
                    lf.setFactoryId(factoryId);
                    lf.setLabelId(pcLabel.getId());
                    lf.setLableUseing("0");
                    lf.setLabelValue(form.getLabelValue());
                    lf.setId(UUID.randomUUID().toString());
                    addList.add(lf);
                } else {
                    // 理论上只会有一个（若有重复数据，随便修改一个即可）
                    PcLabelFactory labelFactory = list.get(0);
                    if (StringUtils.isBlank(labelFactory.getLabelValue())) {
                        labelFactory.setLabelValue(form.getLabelValue());
                        updateList.add(labelFactory);
                    } else {
                        String[] split = labelFactory.getLabelValue().split(",");
                        if (!Arrays.asList(split).contains(form.getLabelValue())) {
                            labelFactory.setLabelValue(labelFactory.getLabelValue() + "," + form.getLabelValue());
                            updateList.add(labelFactory);
                        }
                    }
                }
            }
        } else {// 否则取消指定企业的标签值
            for (String factoryId : form.getFactoryIds()) {
                List<PcLabelFactory> list = factoryLabelGroup.get(factoryId);
                if (list == null) {
                    continue;
                }
                for (PcLabelFactory labelFactory : list) {
                    if (StringUtils.isBlank(labelFactory.getLabelValue())) {
                        deleteList.add(labelFactory); // 删除标签值为空的记录
                    } else {
                        String[] split = labelFactory.getLabelValue().split(",");
                        String value = Arrays.stream(split)
                                .filter(o -> StringUtils.isNotBlank(o) && !o.equals(form.getLabelValue()))
                                .collect(Collectors.joining(","));
                        if (StringUtils.isBlank(value)) {
                            deleteList.add(labelFactory); // 删除标签值为空的记录
                        } else {
                            labelFactory.setLabelValue(value);
                            updateList.add(labelFactory);
                        }
                    }
                }
            }
        }

        if (!addList.isEmpty()) {
            List<List<PcLabelFactory>> partition = ListUtils.partition(addList, 2000);
            for (List<PcLabelFactory> pcLabelFactories : partition) {
                baseMapper.batchSave(pcLabelFactories);
            }
        }
        if (!updateList.isEmpty()) {
            List<List<PcLabelFactory>> partition = ListUtils.partition(updateList, 2000);
            for (List<PcLabelFactory> pcLabelFactories : partition) {
                baseMapper.batchUpdateValue(pcLabelFactories);
            }
        }
        if (!deleteList.isEmpty()) {
            List<List<PcLabelFactory>> partition = ListUtils.partition(deleteList, 2000);
            for (List<PcLabelFactory> pcLabelFactories : partition) {
                baseMapper.deleteBatchIds(pcLabelFactories.stream().map(PcLabelFactory::getId).collect(Collectors.toList()));
            }
        }
    }

    private void saveLabelData(LabelFactoryEnum factoryEnum, String useing, List<PcLabelFactory> data) {
        PcLabel label = pcLabelMapper.selectOne(new QueryWrapper<PcLabel>().lambda().eq(PcLabel::getLabelCode, factoryEnum.getEnumId()));
        pcLabelFactoryMapper.delete(new QueryWrapper<PcLabelFactory>().lambda().eq(PcLabelFactory::getLabelId,label.getId()));
        if (CollectionUtils.isNotEmpty(data)) {
            for (PcLabelFactory labelFactory : data) {
                labelFactory.setId(UUID.randomUUID().toString().replace("-", ""));
                labelFactory.setLabelId(label.getId());
                labelFactory.setLableUseing(useing);
            }
            for (List<PcLabelFactory> factoryList : ListUtil.partition(data, 2000)) {
                pcLabelFactoryMapper.batchSave(factoryList);
            }
        }
    }

}
