package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.annotation.Excel;
import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.bean.BeanUtils;
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.domain.BMainEmissionPollutionAir;
import cn.cecep.talroad.domain.BMainEmissionPollutionWater;
import cn.cecep.talroad.domain.BMainEmissionSourceAir;
import cn.cecep.talroad.domain.BMainEmissionSourceWater;
import cn.cecep.talroad.domain.analysis.SRaMultiSource;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRun;
import cn.cecep.talroad.service.IBMainEmissionPollutionAirService;
import cn.cecep.talroad.service.IBMainEmissionPollutionWaterService;
import cn.cecep.talroad.service.IBMainEmissionSourceAirService;
import cn.cecep.talroad.service.IBMainEmissionSourceWaterService;
import cn.cecep.talroad.util.CustomDateUtil;
import cn.cecep.talroad.vo.BMainEmissionSourceAirVo;
import cn.cecep.talroad.vo.ChartVo;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.query.BMainEmissionPollutionAirQuery;
import cn.cecep.talroad.vo.query.BMainEmissionPollutionWaterQuery;
import cn.cecep.talroad.vo.query.BMainEmissionSourceAirQuery;
import cn.cecep.talroad.vo.query.BMainEmissionSourceWaterQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业排放信息-多来源排放量对比-废气Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-31*/
@Api(tags = "企业排放信息-多来源排放量对比-废气")
@RestController
@RequestMapping("/bmainEmissionSourceAir")
public class BMainEmissionSourceAirController extends BasesController
{
    @Autowired
    private IBMainEmissionSourceAirService bMainEmissionSourceAirService;

    @Autowired
    private IBMainEmissionSourceWaterService bMainEmissionSourceWaterService;

    @Autowired
    private IBMainEmissionPollutionAirService bMainEmissionPollutionAirService;

    @Autowired
    private IBMainEmissionPollutionWaterService bMainEmissionPollutionWaterService;


    /**
     * 查询企业排放信息-多来源排放量对比-废气列表
     */
    @ApiOperation(value = "污染类别查询污染物类别")
    @GetMapping("/pullPollutants")
    public AjaxResults<List<Map<String, String>>> pullPollutants(BMainEmissionSourceAirQuery bMainEmissionSourceAir)
    {
        List<Map<String, String>> maps = Lists.newArrayList();
//        List<String> polls = Lists.newArrayList();
//        if(bMainEmissionSourceAir.getFacts().equals("废气")){
//            List<BMainEmissionSourceAir> list = bMainEmissionSourceAirService.list(new QueryWrapper<BMainEmissionSourceAir>().eq("factory_id", bMainEmissionSourceAir.getFactoryId()).isNotNull("pollution_category"));
//            if(!CollectionUtils.isEmpty(list)){
//                polls.addAll(list.stream().map(BMainEmissionSourceAir::getPollutionCategory).distinct().collect(Collectors.toList()));
//            }
//        } else if (bMainEmissionSourceAir.getFacts().equals("废水")){
//            List<BMainEmissionSourceWater> list = bMainEmissionSourceWaterService.list(new QueryWrapper<BMainEmissionSourceWater>().eq("factory_id", bMainEmissionSourceAir.getFactoryId()).isNotNull("pollution_category"));
//            if(!CollectionUtils.isEmpty(list)){
//                polls.addAll(list.stream().map(BMainEmissionSourceWater::getPollutionCategory).distinct().collect(Collectors.toList()));
//            }
//        }
//        if(!CollectionUtils.isEmpty(polls)){
//            polls.forEach(p->{
//                Map<String, String> map = Maps.newHashMap();
//                map.put("label", p);
//                map.put("value", p);
//                maps.add(map);
//            });
//        }

        String[] strings;
        // SO₂,颗粒物,COD,总氮,NOₓ,VOCs,氨氮,总磷'
        if (StrUtil.equals(bMainEmissionSourceAir.getFacts(), "废气")) {
            strings = new String[]{"SO₂", "颗粒物", "NOₓ", "VOCs"};

        } else if (StrUtil.equals(bMainEmissionSourceAir.getFacts(), "废水")) {
            strings = new String[]{"COD", "总氮", "总磷", "氨氮"};

        } else  {
            throw new InvalidParameterException("参数错误");
        }

        for (String string : strings) {
            maps.add(MapBuilder.<String, String>create()
                    .put("label", string)
                    .put("value", string)
                    .build());
        }

        return AjaxResults.success(maps);
    }

    /**
     * 废气-废水-固废-导出企业排放信息-多来源排放量对比
     */
    @ApiOperation(value = "废气-废水-固废-导出企业排放信息-多来源排放量对比")
    @Log(title = "废气-废水-导出企业排放信息-多来源排放量对比", businessType = BusinessType.EXPORT)
    @PostMapping("/chart/export")
    public void chartExport(HttpServletResponse response, @Valid @RequestBody BMainEmissionSourceAirQuery bMainEmissionSourceAir)
    {
        switch(bMainEmissionSourceAir.getFacts()){
            case "废气":
                List<BMainEmissionSourceAir> airTables = bMainEmissionSourceAirService.list(BMainEmissionSourceAirQuery.createLambdaQueryWrapper(bMainEmissionSourceAir));
                Class clazz =configClass(BMainEmissionSourceAir.class, bMainEmissionSourceAir.getPollutionCategory()+"系统核算排放量","systematicAccountingSo2",bMainEmissionSourceAir.getPollutionCategory()+"执行报告排放量","actionReportsSo2",bMainEmissionSourceAir.getPollutionCategory()+"许可排放量","permissionSo2");
                ExcelUtil<BMainEmissionSourceAir> airUtil = new ExcelUtil<BMainEmissionSourceAir>(clazz);
                airUtil.exportExcel(response, airTables, bMainEmissionSourceAir.getPollutionCategory()+"-多来源排放量对比");
                break;
            case "废水":
                List<BMainEmissionSourceWater> waterTables = bMainEmissionSourceWaterService.list(BMainEmissionSourceWaterQuery.createLambdaQueryWrapper(new BMainEmissionSourceWaterQuery(bMainEmissionSourceAir.getPollutionCategory(), bMainEmissionSourceAir.getSearchBeginYear(), bMainEmissionSourceAir.getSearchEndYear(),bMainEmissionSourceAir.getFactoryId())));
                Class clazz1 =configClass(BMainEmissionSourceWater.class, bMainEmissionSourceAir.getPollutionCategory()+"系统核算排放量","systematicAccountingCod",bMainEmissionSourceAir.getPollutionCategory()+"执行报告排放量","actionReportsCod",bMainEmissionSourceAir.getPollutionCategory()+"许可排放量","permissionCod");
                ExcelUtil<BMainEmissionSourceWater> waterUtil = new ExcelUtil<BMainEmissionSourceWater>(clazz1);
                waterUtil.exportExcel(response, waterTables, bMainEmissionSourceAir.getPollutionCategory()+"-多来源排放量对比");
                break;
        }
    }
    /**
     * 注解之运行时修改字段的注解值操作
     * @param clazz
     * @param val
     * @return
     */
    private Class configClass(Class clazz , String val,String filed,String val1,String filed1,String val2,String filed2) {

        Field[] fields = clazz.getDeclaredFields();
        try {
            for(int i = 0;i < fields.length;i++){
                Field f = fields[i];
                if(f.getName().equals(filed)){
                    Excel excelAn = f.getAnnotation(Excel.class);//Excel是注解类型
                    if(excelAn == null){
                        continue;
                    }
                    InvocationHandler h = Proxy.getInvocationHandler(excelAn);
                    Field hField = h.getClass().getDeclaredField("memberValues");
                    // 因为这个字段事 private final 修饰，所以要打开权限
                    hField.setAccessible(true);
                    // 获取 memberValues
                    Map memberValues = (Map) hField.get(h);
                    // 修改 value 属性值 这里修改的是@Excel(name = "姓名")
                    //name是key
                    memberValues.put("name", val);
                }
                if(f.getName().equals(filed1)){
                    Excel excelAn = f.getAnnotation(Excel.class);//Excel是注解类型
                    if(excelAn == null){
                        continue;
                    }
                    InvocationHandler h = Proxy.getInvocationHandler(excelAn);
                    Field hField = h.getClass().getDeclaredField("memberValues");
                    // 因为这个字段事 private final 修饰，所以要打开权限
                    hField.setAccessible(true);
                    // 获取 memberValues
                    Map memberValues = (Map) hField.get(h);
                    // 修改 value 属性值 这里修改的是@Excel(name = "姓名")
                    //name是key
                    memberValues.put("name", val1);
                }
                if(f.getName().equals(filed2)){
                    Excel excelAn = f.getAnnotation(Excel.class);//Excel是注解类型
                    if(excelAn == null){
                        continue;
                    }
                    InvocationHandler h = Proxy.getInvocationHandler(excelAn);
                    Field hField = h.getClass().getDeclaredField("memberValues");
                    // 因为这个字段事 private final 修饰，所以要打开权限
                    hField.setAccessible(true);
                    // 获取 memberValues
                    Map memberValues = (Map) hField.get(h);
                    // 修改 value 属性值 这里修改的是@Excel(name = "姓名")
                    //name是key
                    memberValues.put("name", val2);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clazz;
    }
    @ApiOperation(value = "废气-废水-固废-查询企业排放信息-多来源排放量对比")
    @Log(title = "废气-废水-查询企业排放信息-多来源排放量对比", businessType = BusinessType.EXPORT)
    @PostMapping("/chart/list")
    public AjaxResults<List<BMainEmissionSourceAir>> chartList( @RequestBody BMainEmissionSourceAirQuery bMainEmissionSourceAir)
    {
        List<BMainEmissionSourceAir> list=new ArrayList<>();
        if(bMainEmissionSourceAir.getFacts().equals("废气")){
            list= bMainEmissionSourceAirService.list(BMainEmissionSourceAirQuery.createLambdaQueryWrapper(bMainEmissionSourceAir));
        }
        if(bMainEmissionSourceAir.getFacts().equals("废水")){
            List<BMainEmissionSourceWater> waterTables = bMainEmissionSourceWaterService.list(BMainEmissionSourceWaterQuery.createLambdaQueryWrapper(new BMainEmissionSourceWaterQuery(bMainEmissionSourceAir.getPollutionCategory(), bMainEmissionSourceAir.getSearchBeginYear(), bMainEmissionSourceAir.getSearchEndYear(),bMainEmissionSourceAir.getFactoryId())));
            List<BMainEmissionSourceAir> listq=new ArrayList<>();
            waterTables.forEach(item->{
                BMainEmissionSourceAir bMainEmissionSourceAir1=new BMainEmissionSourceAir();
                BeanUtils.copyProperties(item, bMainEmissionSourceAir1);
                bMainEmissionSourceAir1.setActionReportsSo2(item.getActionReportsCod());
                bMainEmissionSourceAir1.setSystematicAccountingSo2(item.getSystematicAccountingCod());
                bMainEmissionSourceAir1.setPermissionSo2(item.getPermissionCod());
                listq.add(bMainEmissionSourceAir1);
            });
            list=listq;
        }

        return AjaxResults.success(list);
    }
    @ApiOperation(value = "废气-废水-固废-查询企业排放信息-多来源排放量对比")
    @PostMapping("/chartListBy")
    public AjaxResults<EChartsVO> chartListBy(@RequestBody BMainEmissionSourceAirQuery bMainEmissionSourceAir)
    {
        List<BMainEmissionSourceAir> list=new ArrayList<>();
        if(bMainEmissionSourceAir.getFacts().equals("废气")){
            list= bMainEmissionSourceAirService.list(BMainEmissionSourceAirQuery.createLambdaQueryWrapper(bMainEmissionSourceAir));
        }
        if(bMainEmissionSourceAir.getFacts().equals("废水")){
            List<BMainEmissionSourceWater> waterTables = bMainEmissionSourceWaterService.list(BMainEmissionSourceWaterQuery.createLambdaQueryWrapper(new BMainEmissionSourceWaterQuery(bMainEmissionSourceAir.getPollutionCategory(), bMainEmissionSourceAir.getSearchBeginYear(), bMainEmissionSourceAir.getSearchEndYear(),bMainEmissionSourceAir.getFactoryId())));
            List<BMainEmissionSourceAir> listq=new ArrayList<>();
            waterTables.forEach(item->{
                BMainEmissionSourceAir bMainEmissionSourceAir1=new BMainEmissionSourceAir();
                BeanUtils.copyProperties(item, bMainEmissionSourceAir1);
                bMainEmissionSourceAir1.setActionReportsSo2(item.getActionReportsCod());
                bMainEmissionSourceAir1.setSystematicAccountingSo2(item.getSystematicAccountingCod());
                bMainEmissionSourceAir1.setPermissionSo2(item.getPermissionCod());
                listq.add(bMainEmissionSourceAir1);
            });
            list=listq;
        }
        List<String> xdata=new ArrayList<>();
        if(ObjectUtil.isNotNull(bMainEmissionSourceAir.getSearchBeginYear())&&ObjectUtil.isNotNull(bMainEmissionSourceAir.getSearchEndYear())){
             xdata = CustomDateUtil.getAllYears(DateUtil.parse(bMainEmissionSourceAir.getSearchBeginYear()+"", CustomDateUtil.DATE_PATTERN_YEAR), DateUtil.parse(bMainEmissionSourceAir.getSearchEndYear()+"", CustomDateUtil.DATE_PATTERN_YEAR), CustomDateUtil.DATE_PATTERN_YEAR);
        }else {
            List<String> listData=new ArrayList<>();
            list.forEach(item->{
                listData.add(item.getPollYear());
            });
            xdata=listData;
        }
        // x轴数据
            EChartsVO eChartsVO=new EChartsVO();
            List<EChartsVO.SeriesObj> chartsVO= new ArrayList<>();
            EChartsVO.SeriesObj ser = new EChartsVO.SeriesObj();
            ser.setName("系统核算排放量");
            ser.setProp("systematicAccountingSo2");
            ser.setUnit("(千瓦)");
            ser.setValues(new ArrayList<>());
            chartsVO.add(ser);
            EChartsVO.SeriesObj ser1 = new EChartsVO.SeriesObj();
            ser1.setName("执行报告排放量");
            ser1.setProp("actionReportsSo2");
            ser1.setUnit("(千瓦)");
            ser1.setValues(new ArrayList<>());
            chartsVO.add(ser1);
            EChartsVO.SeriesObj ser2 = new EChartsVO.SeriesObj();
            ser2.setName("许可排放量");
            ser2.setProp("permissionSo2");
            ser2.setUnit("(千瓦)");
            ser2.setValues(new ArrayList<>());
            chartsVO.add(ser2);
                /*
                    填充系列值
                 */
            Map<String, BMainEmissionSourceAir> timeStatusMap = list.stream().collect(Collectors.toMap(o ->  o.getPollYear(), o -> o, (o1, o2) -> o1));
            xdata.forEach(time->{
                if(timeStatusMap.containsKey(time)){
                    BMainEmissionSourceAir sRaRealTimeAnalysisRun = timeStatusMap.get(time);
                    chartsVO.get(0).getValues().add(sRaRealTimeAnalysisRun.getSystematicAccountingSo2());
                    chartsVO.get(1).getValues().add(sRaRealTimeAnalysisRun.getActionReportsSo2());
                    chartsVO.get(2).getValues().add(sRaRealTimeAnalysisRun.getPermissionSo2());
                }else {
                    chartsVO.get(0).getValues().add(null);
                    chartsVO.get(1).getValues().add(null);
                    chartsVO.get(2).getValues().add(null);
                }
            });
            eChartsVO.setXData(xdata);
            eChartsVO.setSeries(chartsVO);
        return AjaxResults.success(eChartsVO);
    }

    /**
     * 查询企业排放信息-多来源排放量对比-废气列表
     */
    @ApiOperation(value = "废气-废水-(图表+列表)查询企业排放信息-多来源排放量对比")
    @GetMapping("/chart")
    public AjaxResults<Map<String, Object>> chart(@Valid BMainEmissionSourceAirQuery bMainEmissionSourceAir)
    {
        Map<String, Object> res = Maps.newHashMap();
        ChartVo chartVo = new ChartVo();
        switch(bMainEmissionSourceAir.getFacts()){
            case "废气":
                BMainEmissionPollutionAirQuery bMainEmissionPollutionAirQuery = new BMainEmissionPollutionAirQuery();
                bMainEmissionPollutionAirQuery.setFactoryId(bMainEmissionSourceAir.getFactoryId());
                bMainEmissionPollutionAirQuery.setDataSource("系统核算");
                List<BMainEmissionPollutionAir> airTables = bMainEmissionPollutionAirService.list(BMainEmissionPollutionAirQuery.createLambdaQueryWrapper(bMainEmissionPollutionAirQuery));
//                List<BMainEmissionSourceAir> airTables = bMainEmissionSourceAirService.list(BMainEmissionSourceAirQuery.createLambdaQueryWrapper(new BMainEmissionSourceAirQuery(bMainEmissionSourceAir.getPollutionCategory(), bMainEmissionSourceAir.getSearchBeginYear(), bMainEmissionSourceAir.getSearchEndYear())));
                if(!CollectionUtils.isEmpty(airTables)){
                    chartVo.setLegendData(new Object[]{"系统核算","执行报告"});
                    chartVo.setXsxisData(airTables.stream().map(BMainEmissionPollutionAir::getPollYear).distinct().collect(Collectors.toList()).toArray(new Object[0]));
                    List<Map<Object, Object>> seriesData = Lists.newArrayList();
                    for (Object legendDatum : chartVo.getLegendData()) {
                        Map<Object, Object> map = Maps.newHashMap();
                        if (legendDatum.equals("系统核算")) {
                            map.put("name", legendDatum);
                            switch (bMainEmissionSourceAir.getPollutionCategory()) {
                                case "SO₂":
                                    map.put("data", airTables.stream().map(BMainEmissionPollutionAir::getActuallyEmittingSo2).collect(Collectors.toList()));
                                    res.put("table", airTables.stream().map(x -> new BMainEmissionSourceAir(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingSo2(), null, x.getAllowableEmissionSo2())).collect(Collectors.toList()));
                                    break;
                                case "颗粒物":
                                    map.put("data", airTables.stream().map(BMainEmissionPollutionAir::getActuallyEmittingPm).collect(Collectors.toList()));
                                    res.put("table", airTables.stream().map(x -> new BMainEmissionSourceAir(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingPm(), null, x.getAllowableEmissionPm())).collect(Collectors.toList()));
                                    break;
                                case "NOₓ":
                                    map.put("data", airTables.stream().map(BMainEmissionPollutionAir::getActuallyEmittingNox).collect(Collectors.toList()));
                                    res.put("table", airTables.stream().map(x -> new BMainEmissionSourceAir(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingNox(), null, x.getAllowableEmissionNox())).collect(Collectors.toList()));
                                    break;
                                case "VOCs":
                                    map.put("data", airTables.stream().map(BMainEmissionPollutionAir::getActuallyEmittingVocs).collect(Collectors.toList()));
                                    res.put("table", airTables.stream().map(x -> new BMainEmissionSourceAir(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingVocs(), null, x.getAllowableEmissionVocs())).collect(Collectors.toList()));
                                    break;
                            }
                            seriesData.add(map);
                        }
//                        else if (legendDatum.equals("执行报告")) {
//                            map.put("name", legendDatum);
//                            map.put("data", new ArrayList<>(airTables.size()));
//                            seriesData.add(map);
//                        }

                    }
                    chartVo.setSeriesData(seriesData);
                } else {
                    chartVo.setSeriesData(Lists.newArrayList());
                    chartVo.setXsxisData(new Object[0]);
                }

//                res.put("table", airTables);
                break;
            case "废水":
                BMainEmissionPollutionWaterQuery bMainEmissionPollutionWaterQuery = new BMainEmissionPollutionWaterQuery();
                bMainEmissionPollutionWaterQuery.setFactoryId(bMainEmissionSourceAir.getFactoryId());
                bMainEmissionPollutionWaterQuery.setDataSource("系统核算");
                List<BMainEmissionPollutionWater> waterTables = bMainEmissionPollutionWaterService.list(BMainEmissionPollutionWaterQuery.createLambdaQueryWrapper(bMainEmissionPollutionWaterQuery));
//                List<BMainEmissionSourceWater> waterTables = bMainEmissionPollutionWaterService.list(BMainEmissionSourceWaterQuery.createLambdaQueryWrapper(new BMainEmissionSourceWaterQuery(bMainEmissionSourceAir.getPollutionCategory(), bMainEmissionSourceAir.getSearchBeginYear(), bMainEmissionSourceAir.getSearchEndYear())));
                if(!CollectionUtils.isEmpty(waterTables)){
                    chartVo.setLegendData(new Object[]{"系统核算","执行报告"});
                    chartVo.setXsxisData(waterTables.stream().map(BMainEmissionPollutionWater::getPollYear).distinct().collect(Collectors.toList()).toArray(new Object[0]));
                    List<Map<Object, Object>> seriesData = Lists.newArrayList();
                    for (Object legendDatum : chartVo.getLegendData()) {
                        Map<Object, Object> map = Maps.newHashMap();
                        if (legendDatum.equals("系统核算")) {
                            map.put("name", legendDatum);

                            switch (bMainEmissionSourceAir.getPollutionCategory()) {
                                case "COD":
                                    map.put("data", waterTables.stream().map(BMainEmissionPollutionWater::getActuallyEmittingCod).collect(Collectors.toList()));
                                    res.put("table", waterTables.stream().map(x -> new BMainEmissionSourceWater(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingCod(), null, x.getAllowableEmissionCod())).collect(Collectors.toList()));
                                    break;
                                case "总氮":
                                    map.put("data", waterTables.stream().map(BMainEmissionPollutionWater::getActuallyEmittingTn).collect(Collectors.toList()));
                                    res.put("table", waterTables.stream().map(x -> new BMainEmissionSourceWater(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingTn(), null, x.getAllowableEmissionTn())).collect(Collectors.toList()));
                                    break;
                                case "总磷":
                                    map.put("data", waterTables.stream().map(BMainEmissionPollutionWater::getActuallyEmittingTp).collect(Collectors.toList()));
                                    res.put("table", waterTables.stream().map(x -> new BMainEmissionSourceWater(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingTp(), null, x.getAllowableEmissionTp())).collect(Collectors.toList()));
                                    break;
                                case "氨氮":
                                    map.put("data", waterTables.stream().map(BMainEmissionPollutionWater::getActuallyEmittingNhn).collect(Collectors.toList()));
                                    res.put("table", waterTables.stream().map(x -> new BMainEmissionSourceWater(x.getFactoryId(), x.getPollYear(), x.getActuallyEmittingNhn(), null, x.getAllowableEmissionNhn())).collect(Collectors.toList()));
                                    break;
                            }
                            seriesData.add(map);
                        }
//                        else if (legendDatum.equals("执行报告")) {
//                            map.put("name", legendDatum);
//                            map.put("data", Arrays.asList(new Object[waterTables.size()]));
//                        }
                    }
                    chartVo.setSeriesData(seriesData);
                } else {
                    chartVo.setSeriesData(Lists.newArrayList());
                    chartVo.setXsxisData(new Object[0]);
                    res.put("table", new ArrayList<>());
                }
//                res.put("table", waterTables);
                break;
        }
        if(bMainEmissionSourceAir.getSearchBeginYear() != null && bMainEmissionSourceAir.getSearchEndYear() != null){
            List<String> xsxisDatas = CustomDateUtil.getAllYears(DateUtil.parse(bMainEmissionSourceAir.getSearchBeginYear()+"", CustomDateUtil.DATE_PATTERN_YEAR), DateUtil.parse(bMainEmissionSourceAir.getSearchEndYear()+"", CustomDateUtil.DATE_PATTERN_YEAR), CustomDateUtil.DATE_PATTERN_YEAR);
            // 无数据默认补全x轴
            if(chartVo.getXsxisData().length == 0){
                chartVo.setXsxisData(xsxisDatas.stream().toArray());
            }
            // 数据缺失, 补全x轴及对应数据
            if(chartVo.getXsxisData().length != xsxisDatas.size()){
                CustomDateUtil.packEcharts(chartVo, xsxisDatas);
            }
        }
        res.put("chart", chartVo);
        return AjaxResults.success(res);
    }


    /**
     * 查询企业排放信息-多来源排放量对比-废气列表
     */
    @ApiOperation(value = "查询企业排放信息-多来源排放量对比-废气")
    @GetMapping("/list")
    public AjaxResults<IPage<BMainEmissionSourceAir>> list(BMainEmissionSourceAirQuery bMainEmissionSourceAir)
    {
        Page<BMainEmissionSourceAir> page = new Page<>(bMainEmissionSourceAir.getPageNum(), bMainEmissionSourceAir.getPageSize());
        IPage<BMainEmissionSourceAir> list = bMainEmissionSourceAirService.page(page, BMainEmissionSourceAirQuery.createLambdaQueryWrapper(bMainEmissionSourceAir));
        return AjaxResults.success(list);
    }


    /**
     * 导出企业排放信息-多来源排放量对比-废气列表
     */
    @ApiOperation(value = "导出企业排放信息-多来源排放量对比-废气")
    @Log(title = "企业排放信息-多来源排放量对比-废气", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BMainEmissionSourceAir bMainEmissionSourceAir)
    {
        List<BMainEmissionSourceAir> list = bMainEmissionSourceAirService.selectBMainEmissionSourceAirList(bMainEmissionSourceAir);
        ExcelUtil<BMainEmissionSourceAir> util = new ExcelUtil<BMainEmissionSourceAir>(BMainEmissionSourceAir.class);
        util.exportExcel(response, list, "企业排放信息-多来源排放量对比-废气数据");
    }

    /**
     * 获取企业排放信息-多来源排放量对比-废气详细信息
     */
    @ApiOperation(value = "获取企业排放信息-多来源排放量对比-废气详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<BMainEmissionSourceAir> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(bMainEmissionSourceAirService.getById(id));
    }
    /**
     * 新增企业排放信息-多来源排放量对比-废气
     */
    @ApiOperation(value = "新增企业排放信息-多来源排放量对比-废气")
    @Log(title = "企业排放信息-多来源排放量对比-废气", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BMainEmissionSourceAir bMainEmissionSourceAir) {
        return toAjax(bMainEmissionSourceAirService.save(bMainEmissionSourceAir));
    }
    /**
     * 修改企业排放信息-多来源排放量对比-废气
     */
    @ApiOperation(value = "修改企业排放信息-多来源排放量对比-废气")
    @Log(title = "企业排放信息-多来源排放量对比-废气", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BMainEmissionSourceAirVo bMainEmissionSourceAirVo) {
        if(ObjectUtil.isEmpty(bMainEmissionSourceAirVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(bMainEmissionSourceAirVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        BMainEmissionSourceAir bMainEmissionSourceAir = bMainEmissionSourceAirService.getById(bMainEmissionSourceAirVo.getId());
        if(ObjectUtil.isEmpty(bMainEmissionSourceAir)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", bMainEmissionSourceAirVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(bMainEmissionSourceAirVo,bMainEmissionSourceAir);
        return toAjax(bMainEmissionSourceAirService.saveOrUpdate(bMainEmissionSourceAir));
    }

    /**
     * 删除企业排放信息-多来源排放量对比-废气
     */
    @ApiOperation(value = "删除企业排放信息-多来源排放量对比-废气")
    @RequiresPermissions("talroad:bmainEmissionSourceAir:remove")
    @Log(title = "企业排放信息-多来源排放量对比-废气", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(bMainEmissionSourceAirService.removeByIds(Arrays.asList(ids)));
    }
}
