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.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.domain.analysis.SRaOverWaterPollutionTraceability;
import cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline;
import cn.cecep.talroad.enums.OperationTypeEnum;
import cn.cecep.talroad.enums.OutletTypesEnum;
import cn.cecep.talroad.enums.PollutantTypeEnum;
import cn.cecep.talroad.enums.WzzStatusEnum;
import cn.cecep.talroad.mapper.BMainMonitorPointOperationLogMapper;
import cn.cecep.talroad.mapper.PcLabelFactoryMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.service.IPcOutletCoordinateOperationLogService;
import cn.cecep.talroad.service.IPcOutletService;
import cn.cecep.talroad.vo.*;
import cn.cecep.talroad.vo.analysis.SRaRealTimeAnalysicComsitMonitorVo;
import cn.cecep.talroad.vo.query.*;
import cn.cecep.talroad.vo.query.analysis.SRaOverWaterPollutionTraceabilityQuery;
import cn.cecep.talroad.vo.query.analysis.SRaWaterPermittedOnlineQuery;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 废气有组织、无组织排口Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-11
 */
@Service
public class PcOutletServiceImpl extends ServiceImpl<PcOutletMapper, PcOutlet> implements IPcOutletService {
    @Autowired
    private PcOutletMapper pcOutletMapper;

    @Autowired
    private BMainMonitorPointOperationLogMapper bMainMonitorPointOperationLogMapper;
    @Autowired
    private IPcOutletCoordinateOperationLogService pcOutletCoordinateOperationLogService;

    @Autowired
    private IPcFactoryService factoryService;

    /**
     * 查询废气有组织、无组织排口列表
     *
     * @param pcOutlet 废气有组织、无组织排口
     * @return 废气有组织、无组织排口
     */
    @Override
    public List<PcOutlet> selectPcOutletList(PcOutlet pcOutlet) {
        return pcOutletMapper.selectPcOutletList(pcOutlet);
    }

    @Override
    public List<PcOutlet> selectPointList(PcOutlet pcOutlet) {
        return pcOutletMapper.selectPointList(pcOutlet);
    }

    @Override
    public List<PcOutlet> regulatoryEnterprises(String name) {
        /*//查询大气管控监管企业 id 固定
        QueryWrapper<PcLabelFactory> wrapper = new QueryWrapper<PcLabelFactory>().eq("label_id", "502");
        List<PcLabelFactory> pcLabelFactories = pcLabelFactoryMapper.selectList(wrapper);
        List<List<PcLabelFactory>> partition = ListUtil.partition(pcLabelFactories, 2000);*/

        List<PcOutlet> pcOutlets = pcOutletMapper.selectListByOnline(name);

        return pcOutlets;
    }

    @Override
    public List<DropDownVO> queryMonitoringPointName(String factoryId, String pollutantType, String pollutantOutletType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("factory_id", factoryId);
        queryWrapper.eq("pollutant_type", pollutantType);
        queryWrapper.eq("is_status", "1");
        if (pollutantType.equals(PollutantTypeEnum.ONLINE_MONITORING.getCode())) {
            queryWrapper.eq("pollutant_outlet_type", pollutantOutletType);
        }
        List<DropDownVO> pointList = new ArrayList<>();
        List<PcOutlet> pcOutletList = pcOutletMapper.selectList(queryWrapper);
        pcOutletList.forEach(e -> {
            DropDownVO dropDownVO = new DropDownVO();
            dropDownVO.setId(e.getId());
            dropDownVO.setLabel(e.getMonitoringPointName());
            pointList.add(dropDownVO);
        });
        return pointList;
    }

    @Override
    public List<DropDownVO> queryOutletName(String factoryId, String pollutantType, String pollutantOutletType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("factory_id", factoryId);
        queryWrapper.eq("pollutant_type", pollutantType);
        queryWrapper.eq("is_status", "1");
        if (pollutantType.equals(PollutantTypeEnum.GAS.getCode())) {
            queryWrapper.eq("pollutant_outlet_type", pollutantOutletType);
        }
        List<DropDownVO> pointList = new ArrayList<>();
        List<PcOutlet> pcOutletList = pcOutletMapper.selectList(queryWrapper);
        pcOutletList.forEach(e -> {
            DropDownVO dropDownVO = new DropDownVO();
            dropDownVO.setId(e.getOutletCode());
            dropDownVO.setLabel(e.getOutletName());
            pointList.add(dropDownVO);
        });
        return pointList;
    }

    @Override
    public List<DropDownVO> queryOutletCode(String factoryId, String pollutantType, String pollutantOutletType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("factory_id", factoryId);
        queryWrapper.eq("pollutant_type", pollutantType);
        queryWrapper.eq("is_status", "1");
        if (pollutantType.equals(PollutantTypeEnum.GAS.getCode())) {
            queryWrapper.eq("pollutant_outlet_type", pollutantOutletType);
        }
        List<DropDownVO> pointList = new ArrayList<>();
        List<PcOutlet> pcOutletList = pcOutletMapper.selectList(queryWrapper);
        pcOutletList.forEach(e -> {
            DropDownVO dropDownVO = new DropDownVO();
            dropDownVO.setId(e.getOutletCode());
            dropDownVO.setLabel(e.getOutletCode());
            pointList.add(dropDownVO);
        });
        return pointList;
    }

    @Override
    public IPage<PcOutletGasVO> queryPcOutletGasList(PcOutletGasQuery pcOutletGasQuery, String factoryId) {
        Page<PcOutletGasVO> page = new Page<>(pcOutletGasQuery.getPageNum(), pcOutletGasQuery.getPageSize());
        List<String> pollutantOutletTypes = new ArrayList<>();
        if(pcOutletGasQuery.getPollutantOutletType().equals("1")){
            pollutantOutletTypes.add("1");
            pollutantOutletTypes.add("3");
        }else{
            pollutantOutletTypes.add(pcOutletGasQuery.getPollutantOutletType());
        }
        List<PcOutletGasVO> pcOutletGasVOList = pcOutletMapper.queryPcOutletGasList(page, pcOutletGasQuery, factoryId, pollutantOutletTypes);
        pcOutletGasVOList.forEach(e -> {
            if (StringUtils.isNotEmpty(e.getOutletTypes())) {
                e.setOutletTypes(OutletTypesEnum.getName(e.getOutletTypes()));
            }
        });
        page.setRecords(pcOutletGasVOList);
        return page;
    }

    @Override
    public List<OutletNameVO> outletName(String factoryId, String pollutantType, String pollutantOutletType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("factory_id", factoryId);
        queryWrapper.eq("pollutant_type", pollutantType);
        queryWrapper.eq("is_status", "1");
        if (pollutantType.equals(PollutantTypeEnum.GAS.getCode())) {
            queryWrapper.eq("pollutant_outlet_type", pollutantOutletType);
        }
        List<OutletNameVO> pcOutlets = new ArrayList<>();
        List<PcOutlet> pcOutletList = pcOutletMapper.selectList(queryWrapper);
        pcOutletList.forEach(e -> {
            OutletNameVO outletNameVO = new OutletNameVO();
            outletNameVO.setId(e.getId());
            outletNameVO.setOutletName(e.getOutletName());
            outletNameVO.setOutletCode(e.getOutletCode());
            outletNameVO.setOutletTypes(OutletTypesEnum.getName(e.getOutletTypes()));
            pcOutlets.add(outletNameVO);
        });
        return pcOutlets;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResults updateOutletGas(List<PcOutletGasUpdateQuery> pcOutletGasUpdateQuery) {
        try {
            pcOutletGasUpdateQuery.forEach(e -> {
                PcOutlet beforePcOutlet = new PcOutlet();
                beforePcOutlet.setId(e.getMonitoringPointId());
                PcOutlet beforePcOutletInfo = pcOutletMapper.selectById(beforePcOutlet);

                PcOutlet pcOutlet = new PcOutlet();
                pcOutlet.setId(e.getMonitoringPointId());
                if (StringUtils.isEmpty(e.getPcOutletGasId())) {
                    pcOutlet.setParentOutletId("");
                    //记录取消日志
                    insertOperationLog(beforePcOutletInfo, "", OperationTypeEnum.CANCEL_THE_CORRESPONDENCE.getName(), e.getMonitoringPointType());
                } else {
                    String operationType = "";
                    if (StringUtils.isEmpty(e.getParentOutletId())) {
                        operationType = OperationTypeEnum.INSERT_OPERATION.getName();
                    } else {
                        operationType = OperationTypeEnum.UPDATE_OPERATION.getName();
                    }
                    //记录编辑日志
                    insertOperationLog(beforePcOutletInfo, e.getPcOutletGasId(), operationType, e.getMonitoringPointType());
                    pcOutlet.setParentOutletId(e.getPcOutletGasId());
                }

                pcOutletMapper.updateById(pcOutlet);

            });
            return AjaxResults.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResults.error("更新失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResults cancelRelationship(List<PcOutletGasUpdateQuery> pcOutletGasUpdateQueryList) {
        try {
            pcOutletGasUpdateQueryList.forEach(e -> {
                PcOutlet beforePcOutlet = new PcOutlet();
                beforePcOutlet.setId(e.getMonitoringPointId());
                PcOutlet beforePcOutletInfo = pcOutletMapper.selectById(beforePcOutlet);

                PcOutlet pcOutlet = new PcOutlet();
                pcOutlet.setId(e.getMonitoringPointId());
                pcOutlet.setParentOutletId("");
                pcOutletMapper.updateById(pcOutlet);

                //记录日志
                insertOperationLog(beforePcOutletInfo, "", OperationTypeEnum.CANCEL_THE_CORRESPONDENCE.getName(), e.getMonitoringPointType());
            });
            return AjaxResults.success("取消对应关系成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResults.error("取消对应关系失败！");
        }
    }

    @Override
    public IPage<BMainMonitorPointOperationLog> queryMonitoringPointLog(MonitoringPointLogQuery monitoringPointLogQuery, String factoryId) throws ParseException {
        Page<BMainMonitorPointOperationLog> page = new Page<>(monitoringPointLogQuery.getPageNum(), monitoringPointLogQuery.getPageSize());
        QueryWrapper<BMainMonitorPointOperationLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(monitoringPointLogQuery.getMonitoringPointName())) {
            queryWrapper.eq("monitoring_point_name", monitoringPointLogQuery.getMonitoringPointName());
        }
        if (StringUtils.isNotEmpty(monitoringPointLogQuery.getOutletName())) {
            queryWrapper.and(
                    qw -> qw.eq("after_modify_name", monitoringPointLogQuery.getOutletName()).or().eq("before_modify_name", monitoringPointLogQuery.getOutletName())
            );
        }
        if (StringUtils.isNotEmpty(monitoringPointLogQuery.getOutletCode())) {
            queryWrapper.and(
                    qw -> qw.eq("after_modify_code", monitoringPointLogQuery.getOutletCode()).or().eq("before_modify_code", monitoringPointLogQuery.getOutletCode())
            );
        }
        if (StringUtils.isNotEmpty(factoryId)) {
            queryWrapper.eq("factory_id", factoryId);
        }
        if (StringUtils.isNotEmpty(monitoringPointLogQuery.getMonitoringPointType())) {
            queryWrapper.eq("monitoring_point_type", monitoringPointLogQuery.getMonitoringPointType());
        }
        if (StringUtils.isNotEmpty(monitoringPointLogQuery.getOperationTimeStart()) && StringUtils.isNotEmpty(monitoringPointLogQuery.getOperationTimeEnt())) {
            queryWrapper.between("operation_time", DateUtils.parseDate(monitoringPointLogQuery.getOperationTimeStart(), DateUtils.YYYY_MM_DD_HH_MM_SS),
                    DateUtils.parseDate(monitoringPointLogQuery.getOperationTimeEnt(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        queryWrapper.orderByDesc("operation_time");
        Page<BMainMonitorPointOperationLog> bMainMonitorPointOperationLogPage = bMainMonitorPointOperationLogMapper.selectPage(page, queryWrapper);
        return bMainMonitorPointOperationLogPage;

    }

    @Override
    public IPage<GasPEMonitoringPointVO> queryGasPEMonitoringPointList(String factoryId, GasPEMonitoringPointQuery gasPEMonitoringPointQuery) {
        Page<GasPEMonitoringPointVO> page = new Page<>(gasPEMonitoringPointQuery.getPageNum(), gasPEMonitoringPointQuery.getPageSize());
        Page<GasPEMonitoringPointVO> gasPEMonitoringPointVOPage = pcOutletMapper.queryGasPEMonitoringPointList(page, factoryId, gasPEMonitoringPointQuery);
        return gasPEMonitoringPointVOPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResults insertOrUpdateGasPEMonitoringPoint(List<GasPEMonitoringPointVO> gasPEMonitoringPointVO) {
        try {
            gasPEMonitoringPointVO.forEach(e -> {
                if (e.getIsManualEntry().equals("1")) {
                    PcOutlet pcOutlet = pcOutletMapper.selectById(e.getMonitoringPointId());
                    //执行删除
                    pcOutletMapper.deleteById(e.getMonitoringPointId());
                    //操作记录
                    insertPEOperationLog(pcOutlet.getMonitoringPointName(), pcOutlet.getOutletCode(), OperationTypeEnum.DELETE_OPERATION.getName(), PollutantTypeEnum.ORGANIZATION.getCode());
                } else {
                    if (StringUtils.isNotEmpty(e.getMonitoringPointId())) {
                        updateGasPEMonitoringPoint(e);
                    } else {
                        PcOutlet pcOutlet = new PcOutlet();
                        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                        pcOutlet.setId(uuid);
                        pcOutlet.setFactoryId(SecurityUtils.getLoginUser().getSysUser().getFactoryId());
                        pcOutlet.setPollutantType(PollutantTypeEnum.ORGANIZATION.getCode());
                        pcOutlet.setMonitoringPointName(e.getMonitoringPointName());
                        pcOutlet.setOutletCode(e.getOutletCode());
                        pcOutlet.setIsManualEntry("1");
                        pcOutlet.setIsStatus("1");
                        pcOutlet.setWzzStatus(WzzStatusEnum.OFFLINE.getName());
                        pcOutletMapper.insert(pcOutlet);
                        //操作记录
                        insertPEOperationLog(e.getMonitoringPointName(), e.getOutletCode(), OperationTypeEnum.INSERT_OPERATION.getName(), PollutantTypeEnum.ORGANIZATION.getCode());
                    }
                }
            });
            return AjaxResults.success("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResults.error("操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResults deleteGasPEMonitoringPoint(List<GasPEMonitoringPointVO> gasPEMonitoringPointVOList) {
        try {
            gasPEMonitoringPointVOList.forEach(e -> {
                PcOutlet pcOutlet = pcOutletMapper.selectById(e.getMonitoringPointId());
                //执行删除
                pcOutletMapper.deleteById(e.getMonitoringPointId());
                //操作记录
                insertPEOperationLog(pcOutlet.getMonitoringPointName(), pcOutlet.getOutletCode(), OperationTypeEnum.DELETE_OPERATION.getName(), "3");
            });
            return AjaxResults.success("删除成功！");
        } catch (Exception e) {
            return AjaxResults.error("删除失败！");
        }

    }

    @Override
    public List<DropDownVO> queryMonitoringPointCode(String pollutantType, String factoryId) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("factory_id", factoryId);
        queryWrapper.eq("pollutant_type", pollutantType);
        queryWrapper.eq("is_status", "1");
        List<DropDownVO> pointList = new ArrayList<>();
        List<PcOutlet> pcOutletList = pcOutletMapper.selectList(queryWrapper);
        pcOutletList.forEach(e -> {
            DropDownVO dropDownVO = new DropDownVO();
            dropDownVO.setId(e.getId());
            dropDownVO.setLabel(e.getOutletCode());
            pointList.add(dropDownVO);
        });
        return pointList;
    }

    public void updateGasPEMonitoringPoint(GasPEMonitoringPointVO gasPEMonitoringPointVO) {
        PcOutlet pcOutlet = new PcOutlet();
        pcOutlet.setId(gasPEMonitoringPointVO.getMonitoringPointId());
        pcOutlet.setMonitoringPointName(gasPEMonitoringPointVO.getMonitoringPointName());
        pcOutlet.setOutletCode(gasPEMonitoringPointVO.getOutletCode());
        pcOutletMapper.updateById(pcOutlet);
        //操作记录
        insertPEOperationLog(gasPEMonitoringPointVO.getMonitoringPointName(), gasPEMonitoringPointVO.getOutletCode(), OperationTypeEnum.UPDATE_OPERATION.getName(), "3");
    }

    public void insertOperationLog(PcOutlet beforePcOutletInfo, String pcOutletGasId, String operationStatus, String monitoringPointType) {
        BMainMonitorPointOperationLog bMainMonitorPointOperationLog = new BMainMonitorPointOperationLog();
        //修改前的数据
        if (StringUtils.isNotEmpty(beforePcOutletInfo.getParentOutletId())) {
            //根据父级ID 查询排口信息
            QueryWrapper queryWrapper = new QueryWrapper<>().eq("id", beforePcOutletInfo.getParentOutletId());
            PcOutlet newBeforePcOutletInfo = pcOutletMapper.selectOne(queryWrapper);
            bMainMonitorPointOperationLog.setBeforeModifyName(newBeforePcOutletInfo.getOutletName());
            bMainMonitorPointOperationLog.setBeforeModifyCode(newBeforePcOutletInfo.getOutletCode());
        }
        //修改后的数据 因为只传入ID 要交互数据库查询出 排口信息
        if (StringUtils.isNotEmpty(pcOutletGasId) || monitoringPointType.equals(PollutantTypeEnum.ORGANIZATION.getCode())) {
            PcOutlet afterPcOutlet = new PcOutlet();
            afterPcOutlet.setId(pcOutletGasId);
            PcOutlet afterPcOutletInfo = pcOutletMapper.selectById(afterPcOutlet);
            bMainMonitorPointOperationLog.setAfterModifyName(afterPcOutletInfo.getOutletName());
            bMainMonitorPointOperationLog.setAfterModifyCode(afterPcOutletInfo.getOutletCode());
        }
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        bMainMonitorPointOperationLog.setLogId(uuid);
        bMainMonitorPointOperationLog.setFactoryId(SecurityUtils.getLoginUser().getSysUser().getFactoryId());
        bMainMonitorPointOperationLog.setOperationTime(DateUtils.parseFormatDate("yyyy-MM-dd HH:mm:ss", new Date()));
        bMainMonitorPointOperationLog.setMonitoringPointName(beforePcOutletInfo.getMonitoringPointName());
        bMainMonitorPointOperationLog.setOperationStatus(operationStatus);
        bMainMonitorPointOperationLog.setOperator(SecurityUtils.getLoginUser().getSysUser().getUserName());
        bMainMonitorPointOperationLog.setMonitoringPointType(monitoringPointType);
        bMainMonitorPointOperationLogMapper.insert(bMainMonitorPointOperationLog);
    }

    public void insertPEOperationLog(String afterMonitoringPointName, String afterOutletCode, String operationStatus, String monitoringPointType) {
        BMainMonitorPointOperationLog bMainMonitorPointOperationLog = new BMainMonitorPointOperationLog();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        bMainMonitorPointOperationLog.setLogId(uuid);
        bMainMonitorPointOperationLog.setFactoryId(SecurityUtils.getLoginUser().getSysUser().getFactoryId());
        bMainMonitorPointOperationLog.setMonitoringPointName(afterMonitoringPointName);
        bMainMonitorPointOperationLog.setOperationTime(DateUtils.parseFormatDate("yyyy-MM-dd HH:mm:ss", new Date()));
        bMainMonitorPointOperationLog.setOperationStatus(operationStatus);
        bMainMonitorPointOperationLog.setOperator(SecurityUtils.getLoginUser().getSysUser().getUserName());
        bMainMonitorPointOperationLog.setMonitoringPointType(monitoringPointType);
        bMainMonitorPointOperationLog.setAfterModifyName(afterMonitoringPointName);
        bMainMonitorPointOperationLog.setAfterModifyCode(afterOutletCode);
        bMainMonitorPointOperationLogMapper.insert(bMainMonitorPointOperationLog);
    }

    @Override
    public List<PcOutlet> manualMonitoring(String pollutantType) {
        List<PcOutlet> pcOutlets = pcOutletMapper.selectList(new QueryWrapper<PcOutlet>().eq("is_manual_entry", "1").eq("pollutant_type", "4").eq("pollutant_outlet_type", pollutantType));
        return pcOutlets;
    }

    @Override
    public List<PcOutlet> manualMonitoringCode(String pollutantType, String code) {
        QueryWrapper<PcOutlet> eq = new QueryWrapper<PcOutlet>().eq("is_manual_entry", "1").eq("pollutant_type", pollutantType);
        if(StringUtils.isNotBlank(code)){
            eq.like("outlet_code",code);
        }
        List<PcOutlet> pcOutlets = pcOutletMapper.selectList(eq);
        return pcOutlets;
    }

    @Override
    public IPage<SRaOverWaterPollutionTraceability> pageAndFactory(Page<PcOutlet> page, SRaOverWaterPollutionTraceabilityQuery query) {
        return  pcOutletMapper.pageAndFactory(page, query);
    }

    @Override
    public List<HashMap<String, Object>> queryPcOutletList(String factoryId, Integer type, String monitorPointName) {
        List<HashMap<String, Object>> outletList = new ArrayList<>();
        if(StringUtils.isNotEmpty(factoryId)){
            if (type.equals(1)){
                //废气
                outletList = pcOutletMapper.queryPcOutletsByPcFactoryId(factoryId,"4","1", monitorPointName);
            }else{
                //废水
                outletList = pcOutletMapper.queryPcOutletsByPcFactoryId(factoryId,"4","2", monitorPointName);
            }
            //废水
        }
        return outletList;
    }

    @Override
    public List<PcOutlet> selectByDevice(String deviceId){
        return pcOutletMapper.selectByDevice(deviceId);
    }

    @Override
    public PcOutlet getInfo(String id) {
        PcOutlet outlet = getById(id);
        if (outlet != null) {
            List<PcOutlet> children = list(new QueryWrapper<PcOutlet>().eq("parent_outlet_id", id).orderByAsc("monitoring_point_name", "id"));
            String names = children.stream()
                    .map(PcOutlet::getMonitoringPointName)
                    .filter(Objects::nonNull).collect(Collectors.joining(","));
            outlet.setMonitoringPointName(names);
        }
        return outlet;
    }

    @Override
    public List<SRaWaterPermittedOnline> selectByOnlineAndSource(Page<SRaWaterPermittedOnline> page, SRaWaterPermittedOnlineQuery lambdaQueryWrapper) {
        return pcOutletMapper.selectByOnlineAndSource(page,lambdaQueryWrapper);
    }

    @Override
    public List<SRaWaterPermittedOnline> selectBySource(Page<SRaWaterPermittedOnline> page, SRaWaterPermittedOnlineQuery lambdaQueryWrapper) {
        return pcOutletMapper.selectBySource(page,lambdaQueryWrapper);
    }

    @Override
    public List<HashMap<String, Object>> sourceOutlets(String factoryId, String type, String pollutantOutletType, String monitorPointName) {

        return pcOutletMapper.sourceOutlets(factoryId,type,pollutantOutletType,monitorPointName);
    }

    @Override
    public IPage<SRaRealTimeAnalysicComsitMonitorVo> selectContrastPointList(SRaRealTimeAnalysicComsitMonitorVo pcOutlet) {
        Page<SRaRealTimeAnalysicComsitMonitorVo> page = new Page<>(pcOutlet.getPageNum(), pcOutlet.getPageSize());
        List<SRaRealTimeAnalysicComsitMonitorVo> list = pcOutletMapper.selectContrastPointList(page,pcOutlet);
        list.forEach(item->{item.setParameterName(pcOutlet.getParameterName());});
        page.setRecords(list);
        return page;
    }

    @Override
    public void editBatchCoordinate(PcOutlet[] pcOutletArr) {
        List<PcOutlet> pcOutletList = Arrays.asList(pcOutletArr);

        //记录操作日志
        List<PcOutletCoordinateOperationLog> logList = new ArrayList<>();
        pcOutletList.forEach(e -> {
            PcOutletCoordinateOperationLog log = new PcOutletCoordinateOperationLog();
            PcOutlet pcOutlet = baseMapper.selectById(e.getId());
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            log.setLogId(uuid);
            log.setFactoryId(pcOutlet.getFactoryId());
            log.setOperationTime(DateUtils.parseFormatDate("yyyy-MM-dd HH:mm:ss", new Date()));
            log.setOperationStatus(OperationTypeEnum.UPDATE_OPERATION.getName());
            log.setOperator(SecurityUtils.getLoginUser().getSysUser().getUserName());
            log.setOperatorId(SecurityUtils.getLoginUser().getSysUser().getUserId() + "");
            log.setPollutantType(pcOutlet.getPollutantType());
            log.setMonitoringPointName(pcOutlet.getMonitoringPointName());
            log.setBeforeModifyLag(StrUtil.toStringOrNull(pcOutlet.getOutletLat()));
            log.setBeforeModifyLng(StrUtil.toStringOrNull(pcOutlet.getOutletLon()));
            log.setOutletName(pcOutlet.getOutletName());
            log.setAfterModifyLag(StrUtil.toStringOrNull(e.getOutletLat()));
            log.setAfterModifyLng(StrUtil.toStringOrNull(e.getOutletLon()));
            logList.add(log);
        });
        baseMapper.updateBatchCoordinate(pcOutletList);
        pcOutletCoordinateOperationLogService.saveBatch(logList);
    }

    @Override
    public List<PcOutletVo.PwxkGas> selectPwxkList(Page<PcOutletVo.PwxkGas> page, PcOutletQuery.PwxkGas query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcOutletVo.PwxkGas> dataList = baseMapper.selectPwxkList(factoryIds, query);
        return dataList;
    }

    @Override
    public List<PcOutletVo.PwxkWaterDirect> selectPwxkWaterDirect(Page<PcOutletVo.PwxkWaterDirect> page, PcOutletQuery.PwxkWaterDirect query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcOutletVo.PwxkWaterDirect> dataList = baseMapper.selectPwxkWaterDirect(factoryIds, query);
        return dataList;
    }

    @Override
    public List<PcOutletVo.PwxkWaterIndirect> selectPwxkWaterIndirect(Page<PcOutletVo.PwxkWaterIndirect> page, PcOutletQuery.PwxkWaterIndirect query) {
        Page<PcFactoryVo> factoryPage = new Page<>(page.getCurrent(), page.getSize());
        List<PcFactoryVo> list = factoryService.search(factoryPage, query.getSearchVo(), "list", SecurityUtils.getLoginUser().getSysUser().getRegionCode());
        page.setTotal(factoryPage.getTotal());
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> factoryIds = list.stream().map(PcFactoryVo::getId).collect(Collectors.toList());
        List<PcOutletVo.PwxkWaterIndirect> dataList = baseMapper.selectPwxkWaterIndirect(factoryIds, query);
        return dataList;
    }
}
