package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.BMainMonitorPointOperationLog;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.enums.OutletTypesEnum;
import cn.cecep.talroad.enums.PollutantTypeEnum;
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.hutool.core.bean.BeanUtil;
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 io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


/**
 * * 开发公司：中节能天融科技有限公司
 * 废气有组织、无组织排口Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-11
 */
@Api(tags = "基本档案--废气有组织、无组织排口")
@RestController
@RequestMapping("/pcOutlet")
public class PcOutletController extends BasesController {

    @Autowired
    private IPcOutletService pcOutletService;

    /**
     * 查询废气有组织、无组织排口列表
     */
    @ApiOperation(value = "查询废气有组织、无组织排口")
    @GetMapping("/list")
    public AjaxResults<IPage<PcOutlet>> list(PcOutletQuery pcOutlet) {
        Page<PcOutlet> page = new Page<>(pcOutlet.getPageNum(), pcOutlet.getPageSize());
        IPage<PcOutlet> list = pcOutletService.page(page, PcOutletQuery.createLambdaQueryWrapper(pcOutlet));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "排污许可-废气排口基本信息")
    @PostMapping("/pwxk/list")
    public AjaxResults<IPage<PcOutletVo.PwxkGas>> pwxkList(@RequestBody PcOutletQuery.PwxkGas query) {
        Page<PcOutletVo.PwxkGas> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcOutletVo.PwxkGas> list = pcOutletService.selectPwxkList(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废气排口基本信息导出")
    @PostMapping("/export/pwxk/list")
    public void exportPwxkList(@RequestBody PcOutletQuery.PwxkGas query) {
        Page<PcOutletVo.PwxkGas> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcOutletVo.PwxkGas> list = pcOutletService.selectPwxkList(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcOutletVo.PwxkGas.class);
        ExcelUtil.export(exportMap, "废气排口基本信息");
    }

    @ApiOperation(value = "排污许可-废水直接排口基本信息")
    @PostMapping("/pwxk/waterDirect/list")
    public AjaxResults<IPage<PcOutletVo.PwxkWaterDirect>> waterDirect(@RequestBody PcOutletQuery.PwxkWaterDirect query) {
        Page<PcOutletVo.PwxkWaterDirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcOutletVo.PwxkWaterDirect> list = pcOutletService.selectPwxkWaterDirect(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废水直接排口基本信息导出")
    @PostMapping("/export/pwxk/waterDirect/list")
    public void exportWaterDirect(@RequestBody PcOutletQuery.PwxkWaterDirect query) {
        Page<PcOutletVo.PwxkWaterDirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcOutletVo.PwxkWaterDirect> list = pcOutletService.selectPwxkWaterDirect(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcOutletVo.PwxkWaterDirect.class);
        ExcelUtil.export(exportMap, "废水直接排口基本信息");
    }

    @ApiOperation(value = "排污许可-废水间接排口基本信息")
    @PostMapping("/pwxk/waterIndirect/list")
    public AjaxResults<IPage<PcOutletVo.PwxkWaterIndirect>> waterIndirect(@RequestBody PcOutletQuery.PwxkWaterIndirect query) {
        Page<PcOutletVo.PwxkWaterIndirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcOutletVo.PwxkWaterIndirect> list = pcOutletService.selectPwxkWaterIndirect(page, query);
        page.setRecords(list);
        return AjaxResults.success(page);
    }

    @ApiOperation(value = "排污许可-废水直接排口基本信息导出")
    @PostMapping("/export/pwxk/waterIndirect/list")
    public void exportWaterIndirect(@RequestBody PcOutletQuery.PwxkWaterIndirect query) {
        Page<PcOutletVo.PwxkWaterIndirect> page = new Page<>(query.getSearchVo().getPageNum(), query.getSearchVo().getPageSize());
        List<PcOutletVo.PwxkWaterIndirect> list = pcOutletService.selectPwxkWaterIndirect(page, query);

        Map<String, Object> exportMap = ExcelUtil.getExportMap(list, PcOutletVo.PwxkWaterIndirect.class);
        ExcelUtil.export(exportMap, "废水间接排口基本信息");
    }

    @ApiOperation(value = "排水去向下拉选")
    @GetMapping("/directionDrainage/options")
    public AjaxResults<List<String>> directionDrainageOptions() {
        List<PcOutlet> list = pcOutletService.list(new QueryWrapper<PcOutlet>()
                .select("direction_drainage")
                .isNotNull("direction_drainage")
                .groupBy("direction_drainage")
                .orderByAsc("direction_drainage")
        );
        List<String> collect = list.stream().map(PcOutlet::getDirectionDrainage).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }

    @ApiOperation(value = "排放规律下拉选")
    @GetMapping("/dischargeLaw/options")
    public AjaxResults<List<String>> dischargeLawOptions() {
        List<PcOutlet> list = pcOutletService.list(new QueryWrapper<PcOutlet>()
                .select("discharge_law")
                .isNotNull("discharge_law")
                .groupBy("discharge_law")
                .orderByAsc("discharge_law")
        );
        List<String> collect = list.stream().map(PcOutlet::getDischargeLaw).collect(Collectors.toList());
        return AjaxResults.success(collect);
    }

    /**
     * 查询废气有组织、无组织排口列表
     */
    @ApiOperation(value = "排口下拉框列表")
    @GetMapping("/selectList")
    public AjaxResults<List<PcOutlet>> selectList(PcOutlet pcOutlet) {
        return AjaxResults.success(pcOutletService.selectPcOutletList(pcOutlet));
    }

    /**
     * 查询废气有组织、无组织排口列表
     */
    @ApiOperation(value = "监测点位下拉框列表")
    @GetMapping("/selectPointList")
    public AjaxResults<List<PcOutlet>> selectPointList(PcOutlet pcOutlet) {
        return AjaxResults.success(pcOutletService.selectPointList(pcOutlet));
    }

    @ApiOperation(value = "查询企业废气排口，按照排口分类（有组织、无组织）分组返回")
    @GetMapping("/gas/groupByType")
    public AjaxResults<Map<String, List<PcOutlet>>> getGasOutletGroupByType(String factoryId) {
        List<PcOutlet> list = pcOutletService.list(new QueryWrapper<PcOutlet>()
                .eq("factory_id", factoryId)
                .eq("pollutant_type", "1")
                .orderByAsc("outlet_code", "id")
        );
        Map<String, List<PcOutlet>> data = list.stream().collect(Collectors.groupingBy(PcOutlet::getPollutantOutletType));
        return AjaxResults.success(data);
    }

    /**
     * 导出废气有组织、无组织排口列表
     */
    @ApiOperation(value = "导出废气有组织、无组织排口")
    @Log(title = "废气有组织、无组织排口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcOutlet pcOutlet) {
        List<PcOutlet> list = pcOutletService.selectPcOutletList(pcOutlet);
        ExcelUtil<PcOutlet> util = new ExcelUtil<PcOutlet>(PcOutlet.class);
        util.exportExcel(response, list, "废气有组织、无组织排口数据");
    }

    /**
     * 获取废气有组织、无组织排口详细信息
     */
    @ApiOperation(value = "获取废气有组织、无组织排口详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResults<PcOutlet> getInfo(@PathVariable("id") String id) {
        return AjaxResults.success(pcOutletService.getInfo(id));
    }

    /**
     * 新增废气有组织、无组织排口
     */
    @ApiOperation(value = "新增废气有组织、无组织排口")
    @Log(title = "废气有组织、无组织排口", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody PcOutlet pcOutlet) {
        return toAjax(pcOutletService.save(pcOutlet));
    }

    /**
     * 修改废气有组织、无组织排口
     */
    @ApiOperation(value = "修改废气有组织、无组织排口")
    @Log(title = "废气有组织、无组织排口", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody PcOutletVo pcOutletVo) {
        if (ObjectUtil.isEmpty(pcOutletVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(pcOutletVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        PcOutlet pcOutlet = pcOutletService.getById(pcOutletVo.getId());
        if (ObjectUtil.isEmpty(pcOutlet)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", pcOutletVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(pcOutletVo, pcOutlet);
        return toAjax(pcOutletService.saveOrUpdate(pcOutlet));
    }

    /**
     * 删除废气有组织、无组织排口
     */
    @ApiOperation(value = "删除废气有组织、无组织排口")
    @RequiresPermissions("talroad:outlet:remove")
    @Log(title = "废气有组织、无组织排口", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(pcOutletService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 大气监管企业点位下拉框
     */
    @ApiOperation(value = "大气监管企业点位下拉框")
    @GetMapping("/regulatoryEnterprises")
    public AjaxResults<List<PcOutlet>> regulatoryEnterprises(String name) {
        return AjaxResults.success(pcOutletService.regulatoryEnterprises(name));
    }

    @ApiOperation("企业端-废气有组织/废水/废气无组织-在线监测点位名称下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织在线监测点：4｜废水在线监测点：4｜废气无组织监测点：3）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pollutantOutletType", value = "排口分类 （废气有组织在线监测点：1｜废水在线监测点：2）", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "factoryId", value = "企业id", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/queryMonitoringPointName")
    public AjaxResults<List<DropDownVO>> queryMonitoringPointName(
            @RequestParam(value = "pollutantType") String pollutantType,
            @RequestParam(value = "pollutantOutletType", required = false) String pollutantOutletType,
            @RequestParam(value = "factoryId", required = false) String factoryId
    ) {
        if (pollutantType.equals(PollutantTypeEnum.ONLINE_MONITORING.getCode())) {
            if (StringUtils.isEmpty(pollutantOutletType)) {
                return AjaxResults.error("污染物类型为4时，排口分类不允许为空！");
            }
        }
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        List<DropDownVO> point = pcOutletService.queryMonitoringPointName(factoryId, pollutantType, pollutantOutletType);
        return AjaxResults.success(point);
    }

    @ApiOperation("企业端-废气无组织-在线监测点位编号下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气无组织监测点：3）", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping("/queryMonitoringPointCode")
    public AjaxResults<List<DropDownVO>> queryMonitoringPointCode(
            @RequestParam(value = "pollutantType") String pollutantType,
            @RequestParam(value = "factoryId", required = false) String factoryId
    ) {
        List<DropDownVO> point = pcOutletService.queryMonitoringPointCode(pollutantType,factoryId);
        return AjaxResults.success(point);
    }

    @ApiOperation("企业端-许可证排口名称下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pollutantOutletType", value = "排口分类 （废气有组织排口：1）", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "factoryId", value = "企业id", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/queryOutletName")
    public AjaxResults queryOutletName(
            @RequestParam(value = "pollutantType") String pollutantType,
            @RequestParam(value = "pollutantOutletType", required = false) String pollutantOutletType,
            @RequestParam(value = "factoryId", required = false) String factoryId
    ) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
             factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            if (pollutantType.equals(PollutantTypeEnum.GAS.getCode())) {
                if (StringUtils.isEmpty(pollutantOutletType)) {
                    return AjaxResults.error("污染物类型为1时查询废气有组织排口，排口分类不允许为空");
                }
            }
        }
        List<DropDownVO> point = pcOutletService.queryOutletName(factoryId, pollutantType, pollutantOutletType);
        return AjaxResults.success(point);
    }

    @ApiOperation("企业端-许可证排口编号下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pollutantOutletType", value = "排口分类 （废气有组织排口：1）", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "factoryId", value = "企业id", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/queryOutletCode")
    public AjaxResults queryOutletCode(
            @RequestParam(value = "pollutantType") String pollutantType,
            @RequestParam(value = "pollutantOutletType", required = false) String pollutantOutletType,
            @RequestParam(value = "factoryId", required = false) String factoryId
    ) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            if (pollutantType.equals(PollutantTypeEnum.GAS.getCode())) {
                if (StringUtils.isEmpty(pollutantOutletType)) {
                    return AjaxResults.error("污染物类型为1时查询废气有组织排口，排口分类不允许为空");
                }
            }
        }
        List<DropDownVO> point = pcOutletService.queryOutletCode(factoryId, pollutantType, pollutantOutletType);
        return AjaxResults.success(point);

    }

    @ApiOperation("企业端-排口类型下拉列表")
    @GetMapping("/queryOutletTypes")
    public AjaxResults queryOutletTypes() {
        return AjaxResults.success(OutletTypesEnum.list);
    }

    @ApiOperation(value = "企业端-废气有组织在线监测点位匹配列表查询/废水在线监测点位匹配")
    @PostMapping("/queryPcOutletGasOrWasList")
    public AjaxResults<IPage<PcOutletGasVO>> queryPcOutletGasOrWasList(@RequestBody PcOutletGasQuery pcOutletGasQuery) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        String factoryId =pcOutletGasQuery.getFactoryId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        if (StringUtils.isEmpty(pcOutletGasQuery.getPollutantType())) {
            return AjaxResults.error("污染物类型不允许为空！");
        }
        if (StringUtils.isEmpty(pcOutletGasQuery.getPollutantOutletType())) {
            return AjaxResults.error("排口分类不允许为空！");
        }
        IPage<PcOutletGasVO> pcOutletGasList = pcOutletService.queryPcOutletGasList(pcOutletGasQuery, factoryId);
        return AjaxResults.success(pcOutletGasList);

    }

    @ApiOperation(value = "企业端-导出/废气有组织在线监测点位匹配列表/废水在线监测点位匹配")
    @PostMapping("/exportOutletGasOrWas")
    public void exportOutletGasOrWas(HttpServletResponse response, @RequestBody PcOutletGasQuery pcOutletGasQuery) {
        pcOutletGasQuery.setPageNum(1);
        pcOutletGasQuery.setPageSize(999999);
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        String factoryId =pcOutletGasQuery.getFactoryId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        IPage<PcOutletGasVO> pcOutletGasList = pcOutletService.queryPcOutletGasList(pcOutletGasQuery, factoryId);
        ExcelUtil<PcOutletGasVO> util = new ExcelUtil<PcOutletGasVO>(PcOutletGasVO.class);
        String sheetName = "";
        if (pcOutletGasQuery.getPollutantOutletType().equals("1")) {
            sheetName = "企业端-导出废气有组织在线监测点位匹配列表";
        }
        if (pcOutletGasQuery.getPollutantOutletType().equals("2")) {
            sheetName = "企业端-导出废水在线监测点位匹配列表";
        }
        util.exportExcel(response, pcOutletGasList.getRecords(), sheetName);
    }

    @ApiOperation("企业端-对应许可证排口名称下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pollutantOutletType", value = "排口分类 （废气有组织排口：1）", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/outletName")
    public AjaxResults outletName(
            @RequestParam(value = "pollutantType") String pollutantType,
            @RequestParam(value = "pollutantOutletType", required = false) String pollutantOutletType
    ) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        String factoryId ="";
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        if (pollutantType.equals(PollutantTypeEnum.GAS.getCode())) {
            if (StringUtils.isEmpty(pollutantOutletType)) {
                return AjaxResults.error("污染物类型为1时查询废气有组织排口，排口分类不允许为空");
            }
        }
        List<OutletNameVO> point = pcOutletService.outletName(factoryId, pollutantType, pollutantOutletType);
        return AjaxResults.success(point);
    }

    @ApiOperation(value = "企业端-编辑/废气有组织在线监测点位/废水在线监测点位")
    @PostMapping("/updateOutletGas")
    public AjaxResults updateOutletGas(@RequestBody List<PcOutletGasUpdateQuery> pcOutletGasUpdateQuery) {
        if (pcOutletGasUpdateQuery.size() == 0) {
            return AjaxResults.error("参数不允许为空！");
        }
        AjaxResults ajaxResults = pcOutletService.updateOutletGas(pcOutletGasUpdateQuery);
        return ajaxResults;
    }

    @ApiOperation(value = "企业端-取消对应关系/废气有组织在线监测点位/废水在线监测点位")
    @PostMapping("/cancelRelationship")
    public AjaxResults cancelRelationship(@RequestBody List<PcOutletGasUpdateQuery> pcOutletGasUpdateQueryList) {
        AjaxResults ajaxResults = pcOutletService.cancelRelationship(pcOutletGasUpdateQueryList);
        return ajaxResults;
    }

    @ApiOperation(value = "企业端-查询操作日志/1-废气有组织在线监测点位/2-废水在线监测点位/3-废气无组织监测点位")
    @PostMapping("/queryMonitoringPointLogList")
    public AjaxResults<IPage<BMainMonitorPointOperationLog>> queryMonitoringPointLogList(@RequestBody MonitoringPointLogQuery monitoringPointLogQuery) throws ParseException {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        String factoryId =monitoringPointLogQuery.getFactoryId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        if (StringUtils.isEmpty(monitoringPointLogQuery.getMonitoringPointType())) {
            return AjaxResults.error("类型不能为空！");
        }
        IPage<BMainMonitorPointOperationLog> bMainMonitorPointOperationLogIPage = pcOutletService.queryMonitoringPointLog(monitoringPointLogQuery, factoryId);
        return AjaxResults.success(bMainMonitorPointOperationLogIPage);
    }


    @ApiOperation(value = "企业端-废气无组织监测点位填报-列表")
    @PostMapping("/queryGasPEMonitoringPointList")
    public AjaxResults<IPage<GasPEMonitoringPointVO>> queryGasPEMonitoringPointList(@RequestBody GasPEMonitoringPointQuery gasPEMonitoringPointQuery) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        String factoryId =gasPEMonitoringPointQuery.getFactoryId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        IPage<GasPEMonitoringPointVO> bMainMonitorPointOperationLogIPage = pcOutletService.queryGasPEMonitoringPointList(factoryId, gasPEMonitoringPointQuery);
        return AjaxResults.success(bMainMonitorPointOperationLogIPage);
    }

    @ApiOperation(value = "企业端-废气无组织监测点位填报-新增/编辑")
    @PostMapping("/insertOrUpdateGasPEMonitoringPoint")
    public AjaxResults insertOrUpdateGasPEMonitoringPoint(@RequestBody List<GasPEMonitoringPointVO> gasPEMonitoringPointVO) {
        if (gasPEMonitoringPointVO.size() == 0) {
            return AjaxResults.error("参数不允许为空！");
        }
        AjaxResults ajaxResults = pcOutletService.insertOrUpdateGasPEMonitoringPoint(gasPEMonitoringPointVO);
        return ajaxResults;
    }

    @ApiOperation(value = "企业端-废气无组织监测点位填报-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitoringPointId", value = "监测点主键标识", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/deleteGasPEMonitoringPoint")
    public AjaxResults deleteGasPEMonitoringPoint(@RequestBody List<GasPEMonitoringPointVO> gasPEMonitoringPointVOList) {
        AjaxResults ajaxResults = pcOutletService.deleteGasPEMonitoringPoint(gasPEMonitoringPointVOList);
        return ajaxResults;
    }

    @ApiOperation(value = "企业端-废气无组织监测点位填报列表-导出")
    @PostMapping("/exportGasPEMonitoringPointList")
    public void exportGasPEMonitoringPointList(HttpServletResponse response, @RequestBody GasPEMonitoringPointQuery gasPEMonitoringPointQuery) {
        gasPEMonitoringPointQuery.setPageNum(1);
        gasPEMonitoringPointQuery.setPageSize(999999);
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        String factoryId =gasPEMonitoringPointQuery.getFactoryId();
        if(ObjectUtil.equal(roleId,10L)) {
            factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
        }
        IPage<GasPEMonitoringPointVO> bMainMonitorPointOperationLogIPage = pcOutletService.queryGasPEMonitoringPointList(factoryId, gasPEMonitoringPointQuery);
        ExcelUtil<GasPEMonitoringPointVO> util = new ExcelUtil<GasPEMonitoringPointVO>(GasPEMonitoringPointVO.class);
        util.exportExcel(response, bMainMonitorPointOperationLogIPage.getRecords(), "企业端-导出废气无组织在线监测点位匹配列表");
    }
    /**
     * 手工监测排放点位下拉框
     */
    @ApiOperation(value = "手工监测排放点位下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气有组织排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping("/manualMonitoring")
    public AjaxResults<List<PcOutlet>> manualMonitoring( @RequestParam(value = "pollutantType") String pollutantType
    ) {
        return AjaxResults.success(pcOutletService.manualMonitoring(pollutantType));
    }
    /**
     * 手工监测排口编号下拉框
     */
    @ApiOperation(value = "手工监测排口编号下拉框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantType", value = "污染物类型 （废气排口：1｜废水排口：2）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "排口编号", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/manualMonitoringCode")
    public AjaxResults<List<PcOutlet>> manualMonitoringCode(
            @RequestParam(value = "pollutantType") String pollutantType,
            @RequestParam(value = "code",required = false) String code
    ) {
        return AjaxResults.success(pcOutletService.manualMonitoringCode(pollutantType,code));
    }

    /**
     * 企业下废气废水排口列表（包含超标信息）
     */
    @ApiOperation(value = "企业下废气废水排口列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "factoryId", value = "企业编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "排口编号", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "monitorPointName", value = "监测点位名称", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/factoryOutlets")
    public AjaxResults<List<HashMap<String, Object>>> factoryOutlets(
            @RequestParam(value = "factoryId") String factoryId,
            @RequestParam(value = "type") Integer type,
            @RequestParam(value = "monitorPointName", required = false) String monitorPointName
    ) {
        return AjaxResults.success(pcOutletService.queryPcOutletList(factoryId,type, monitorPointName));
    }
    /**
     * 企业下废气废水排口列表（包含超标信息）
     */
    @ApiOperation(value = "多源排放量监测点名称列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "factoryName", value = "企业编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "1在线，2许可", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pollutantOutletType", value = "1废气，2废水", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "monitorPointName", value = "监测点位名称", required = false, paramType = "query", dataType = "String")
    })
    @GetMapping("/sourceOutlets")
    public AjaxResults<List<HashMap<String, Object>>> sourceOutlets(
            @RequestParam(value = "factoryName") String factoryName,
            @RequestParam(value = "type") String type,
            @RequestParam(value = "pollutantOutletType") String pollutantOutletType,
            @RequestParam(value = "monitorPointName", required = false) String monitorPointName
    ) {
        return AjaxResults.success(pcOutletService.sourceOutlets(factoryName,type, pollutantOutletType,monitorPointName));
    }

    @ApiOperation(value = "监测点位对比分析下拉框列表")
    @GetMapping("/selectContrastPointList")
    public AjaxResults<IPage<SRaRealTimeAnalysicComsitMonitorVo>> selectContrastPointList(SRaRealTimeAnalysicComsitMonitorVo pcOutlet) {
        return AjaxResults.success(pcOutletService.selectContrastPointList(pcOutlet));
    }

    /**
     * 查询在线监测,无组织,排口坐标列表
     */
    @ApiOperation(value = "查询在线监测,无组织,排口坐标列表")
    @GetMapping("/coordinateList")
    public AjaxResults<IPage<PcOutlet>> coordinateList(PcOutletCoordinateQuery pcOutletCoordinateQuery) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            pcOutletCoordinateQuery.setFactoryId(factoryId);
        }
        Page<PcOutlet> page = new Page<>(pcOutletCoordinateQuery.getPageNum(), pcOutletCoordinateQuery.getPageSize());
        IPage<PcOutlet> list = pcOutletService.page(page, PcOutletCoordinateQuery.createLambdaQueryWrapper(pcOutletCoordinateQuery));
        return AjaxResults.success(list);
    }

    /**
     * 查询在线监测,无组织,排口坐标下拉框
     */
    @ApiOperation(value = "查询在线监测,无组织,排口坐标下拉框")
    @GetMapping("/coordinateSelect")
    public AjaxResults<List<PcOutlet>> coordinateSelect(PcOutletCoordinateQuery pcOutletCoordinateQuery) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            pcOutletCoordinateQuery.setFactoryId(factoryId);
        }
        List<PcOutlet> list = pcOutletService.list(PcOutletCoordinateQuery.createLambdaQueryWrapper(pcOutletCoordinateQuery));
        return AjaxResults.success(list);
    }

    /**
     * 导出在线监测,无组织,排口坐标列表
     */
    @ApiOperation(value = "导出在线监测,无组织,排口坐标列表")
    @PostMapping("/exportCoordinateList")
    public void exportCoordinateList(HttpServletResponse response, @RequestBody PcOutletCoordinateQuery pcOutletCoordinateQuery) {
        String pollutantType = pcOutletCoordinateQuery.getPollutantType();
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            pcOutletCoordinateQuery.setFactoryId(factoryId);
        }
        List<PcOutlet> list = pcOutletService.list(PcOutletCoordinateQuery.createLambdaQueryWrapper(pcOutletCoordinateQuery));

        List<PcOutletCoordinateVo> pcOutletCoordinateList = new ArrayList<>();
        PrimitiveIterator.OfInt iterator = IntStream.iterate(1, k -> k + 1).iterator();
        list.forEach(e -> {
            PcOutletCoordinateVo pcOutletCoordinateVo = new PcOutletCoordinateVo();
            BeanUtil.copyProperties(e, pcOutletCoordinateVo);
            pcOutletCoordinateVo.setSerialnumber(iterator.next());
            pcOutletCoordinateVo.setInorganizationPointName(e.getMonitoringPointName());
            pcOutletCoordinateList.add(pcOutletCoordinateVo);
        });
        ExcelUtil<PcOutletCoordinateVo> util = new ExcelUtil<>(PcOutletCoordinateVo.class);
        if("1".equals(pollutantType)){
            util.hideColumnNative(1,2);
        }else if("3".equals(pollutantType)){
            util.hideColumnNative(1,3);
        }else{
            util.hideColumnNative(2,3);
        }
        util.exportExcel(response, pcOutletCoordinateList, "坐标信息");
    }

    /**
     * 批量修改在线监测,无组织,排口坐标
     */
    @ApiOperation(value = "批量修改在线监测,无组织,排口坐标")
    @Log(title = "在线监测,无组织,排口坐标信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editBatchCoordinate")
    public AjaxResults<Boolean> editBatchCoordinate(@RequestBody PcOutlet[] pcOutletArr) {
        if (pcOutletArr ==null || pcOutletArr.length < 1) {
            throw new InvalidParameterException("参数为空");
        }
        pcOutletService.editBatchCoordinate(pcOutletArr);
        return toAjax(true);
    }
}
