package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.data.analyse.task.real.concentration.GasConcentrationEnum;
import cn.cecep.talroad.domain.BMainEmissionPollutionAir;
import cn.cecep.talroad.domain.BMainEmissionSourceAir;
import cn.cecep.talroad.mapper.BMainEmissionPollutionAirMapper;
import cn.cecep.talroad.service.IBMainEmissionPollutionAirService;
import cn.cecep.talroad.service.IBMainEmissionSourceAirService;
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.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 企业排放信息-不同污染物年度排放趋势-废气Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-31*/
@Service
public class BMainEmissionPollutionAirServiceImpl extends ServiceImpl<BMainEmissionPollutionAirMapper,BMainEmissionPollutionAir> implements IBMainEmissionPollutionAirService
{
    @Autowired
    private BMainEmissionPollutionAirMapper bMainEmissionPollutionAirMapper;
    @Autowired
    private IBMainEmissionSourceAirService ibMainEmissionSourceAirService;

    /**
     * 查询企业排放信息-不同污染物年度排放趋势-废气列表
     *
     * @param bMainEmissionPollutionAir 企业排放信息-不同污染物年度排放趋势-废气
     * @return 企业排放信息-不同污染物年度排放趋势-废气
     */
    @Override
    public List<BMainEmissionPollutionAir> selectBMainEmissionPollutionAirList(BMainEmissionPollutionAir bMainEmissionPollutionAir)
    {
        return bMainEmissionPollutionAirMapper.selectBMainEmissionPollutionAirList(bMainEmissionPollutionAir);
    }

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

    @Override
    public void doProcessEmissionDataByYear(String lastYear) {
        List<BMainEmissionSourceAir> resultList = new ArrayList<>();
        List<BMainEmissionPollutionAir>  list = bMainEmissionPollutionAirMapper.selectEmissioinDataByYear(lastYear);
        if(list!=null&&list.size()>0){
            list.forEach(p->{
                BMainEmissionSourceAir airSo2 = new BMainEmissionSourceAir();
                airSo2.setId(UUID.randomUUID().toString());
                airSo2.setFactoryId(p.getFactoryId());
                airSo2.setPollutionCategory("SO₂");
                airSo2.setPollYear(lastYear);
                airSo2.setSystematicAccountingSo2(p.getActuallyEmittingSo2());
                airSo2.setPermissionSo2(p.getAllowableEmissionSo2());
                resultList.add(airSo2);
                BMainEmissionSourceAir airNox = new BMainEmissionSourceAir();
                airNox.setId(UUID.randomUUID().toString());
                airNox.setFactoryId(p.getFactoryId());
                airNox.setPollutionCategory("NOₓ");
                airNox.setPollYear(lastYear);
                airNox.setSystematicAccountingSo2(p.getActuallyEmittingNox());
                airNox.setPermissionSo2(p.getAllowableEmissionNox());
                resultList.add(airNox);
                BMainEmissionSourceAir airPm = new BMainEmissionSourceAir();
                airPm.setId(UUID.randomUUID().toString());
                airPm.setFactoryId(p.getFactoryId());
                airPm.setPollutionCategory("颗粒物");
                airPm.setPollYear(lastYear);
                airPm.setSystematicAccountingSo2(p.getActuallyEmittingPm());
                airPm.setPermissionSo2(p.getAllowableEmissionPm());
                resultList.add(airPm);
                BMainEmissionSourceAir airVocs = new BMainEmissionSourceAir();
                airVocs.setId(UUID.randomUUID().toString());
                airVocs.setFactoryId(p.getFactoryId());
                airVocs.setPollutionCategory("VOCs");
                airVocs.setPollYear(lastYear);
                airVocs.setSystematicAccountingSo2(p.getActuallyEmittingVocs());
                airVocs.setPermissionSo2(p.getAllowableEmissionVocs());
                resultList.add(airVocs);
            });
        }
        QueryWrapper<BMainEmissionSourceAir> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("poll_year",lastYear);
        int count = ibMainEmissionSourceAirService.count(queryWrapper);
        if(count==0){
            List<List<BMainEmissionSourceAir>> part = ListUtil.partition(resultList,2000);
            part.forEach(i->{
                ibMainEmissionSourceAirService.saveBatch(i);
            });
        }else{
            resultList.forEach(data->{
                UpdateWrapper<BMainEmissionSourceAir> 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_so2",data.getSystematicAccountingSo2());
                updateWrapper.set("permission_so2",data.getPermissionSo2());
                ibMainEmissionSourceAirService.update(updateWrapper);
            });
        }
    }
}
