package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.domain.BMainEmissionPollutionWater;
import cn.cecep.talroad.domain.BMainEmissionSourceAir;
import cn.cecep.talroad.domain.BMainEmissionSourceWater;
import cn.cecep.talroad.mapper.BMainEmissionPollutionWaterMapper;
import cn.cecep.talroad.service.IBMainEmissionPollutionWaterService;
import cn.cecep.talroad.service.IBMainEmissionSourceWaterService;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




/**
 * 企业排放信息-不同污染物年度排放趋势-废水Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-31*/
@Service
public class BMainEmissionPollutionWaterServiceImpl extends ServiceImpl<BMainEmissionPollutionWaterMapper,BMainEmissionPollutionWater> implements IBMainEmissionPollutionWaterService
{
    @Autowired
    private BMainEmissionPollutionWaterMapper bMainEmissionPollutionWaterMapper;
    @Autowired
    private IBMainEmissionSourceWaterService ibMainEmissionSourceWaterService;
    /**
     * 查询企业排放信息-不同污染物年度排放趋势-废水列表
     *
     * @param bMainEmissionPollutionWater 企业排放信息-不同污染物年度排放趋势-废水
     * @return 企业排放信息-不同污染物年度排放趋势-废水
     */
    @Override
    public List<BMainEmissionPollutionWater> selectBMainEmissionPollutionWaterList(BMainEmissionPollutionWater bMainEmissionPollutionWater)
    {
        return bMainEmissionPollutionWaterMapper.selectBMainEmissionPollutionWaterList(bMainEmissionPollutionWater);
    }

    /**
     * 查询 废水 年统计排放趋势数据
     * @param dataSource 数据类型： 系统核算/执行报告
     * @param factoryId 企业id
     * @param year 年份
     * @param groupByField 对其他字段进行分组
     * @param groupByIndustry 是否对行业进行分组
     * @return 排放趋势数据列表
     */
    @Override
    public List<BMainEmissionPollutionWater> selectEmissionDataList(String dataSource, String factoryId, String industry, String year, String groupByField, boolean groupByIndustry) {

        return bMainEmissionPollutionWaterMapper.selectEmissionDataList(dataSource, factoryId, year, industry, groupByField, groupByIndustry, null);
    }

    @Override
    public void doProcessEmissionDataByYear(String lastYear) {
        List<BMainEmissionSourceWater> resultList = new ArrayList<>();
        List<BMainEmissionPollutionWater>  list = bMainEmissionPollutionWaterMapper.selectEmissioinDataByYear(lastYear);
        if(list!=null&&list.size()>0){
            list.forEach(p->{
                BMainEmissionSourceWater cod= new BMainEmissionSourceWater();
                cod.setId(UUID.randomUUID().toString());
                cod.setFactoryId(p.getFactoryId());
                cod.setPollutionCategory("COD");
                cod.setPollYear(lastYear);
                cod.setSystematicAccountingCod(p.getActuallyEmittingCod());
                cod.setPermissionCod(p.getAllowableEmissionCod());
                resultList.add(cod);
                BMainEmissionSourceWater zd = new BMainEmissionSourceWater();
                zd.setId(UUID.randomUUID().toString());
                zd.setFactoryId(p.getFactoryId());
                zd.setPollutionCategory("总氮");
                zd.setPollYear(lastYear);
                zd.setSystematicAccountingCod(p.getActuallyEmittingTn());
                zd.setPermissionCod(p.getAllowableEmissionTn());
                resultList.add(zd);
                BMainEmissionSourceWater zl = new BMainEmissionSourceWater();
                zl.setId(UUID.randomUUID().toString());
                zl.setFactoryId(p.getFactoryId());
                zl.setPollutionCategory("总磷");
                zl.setPollYear(lastYear);
                zl.setSystematicAccountingCod(p.getActuallyEmittingTp());
                zl.setPermissionCod(p.getAllowableEmissionTp());
                resultList.add(zl);
                BMainEmissionSourceWater nhn = new BMainEmissionSourceWater();
                nhn.setId(UUID.randomUUID().toString());
                nhn.setFactoryId(p.getFactoryId());
                nhn.setPollutionCategory("氨氮");
                nhn.setPollYear(lastYear);
                nhn.setSystematicAccountingCod(p.getActuallyEmittingNhn());
                nhn.setPermissionCod(p.getAllowableEmissionNhn());
                resultList.add(nhn);
            });
        }
        QueryWrapper<BMainEmissionSourceWater> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("poll_year",lastYear);
        int count = ibMainEmissionSourceWaterService.count(queryWrapper);
        if(count==0){
            List<List<BMainEmissionSourceWater>> part = ListUtil.partition(resultList,2000);
            part.forEach(i->{
                ibMainEmissionSourceWaterService.saveBatch(i);
            });
        }else{
            resultList.forEach(data->{
                UpdateWrapper<BMainEmissionSourceWater> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("factory_id",data.getFactoryId());
                updateWrapper.eq("poll_year",data.getPollYear());
                updateWrapper.eq("pollution_category",data.getPollutionCategory());
                updateWrapper.set("systematic_accounting_cod",data.getSystematicAccountingCod());
                updateWrapper.set("permission_cod",data.getPermissionCod());
                ibMainEmissionSourceWaterService.update(updateWrapper);
            });
        }
    }

}
