package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.data.analyse.task.real.concentration.GasConcentrationEnum;
import cn.cecep.talroad.domain.SEnvGasSelfDay;
import cn.cecep.talroad.domain.SEnvWaterSelfDay;
import cn.cecep.talroad.domain.analysis.SRaSelfMonitoringData;
import cn.cecep.talroad.enums.WaterPollutantEnum;
import cn.cecep.talroad.mapper.analysis.SRaSelfMonitoringDataMapper;
import cn.cecep.talroad.service.ISEnvGasSelfDayService;
import cn.cecep.talroad.service.ISEnvWaterSelfDayService;
import cn.cecep.talroad.service.analysis.ISRaSelfMonitoringDataService;
import cn.cecep.talroad.vo.query.analysis.SRaSelfMonitoringDataQuery;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




/**
 * 关联分析系统-企业自行监测数据Service业务层处理
 *
 * @author szhpt
 * @date 2023-06-27*/
@Service
public class SRaSelfMonitoringDataServiceImpl extends ServiceImpl<SRaSelfMonitoringDataMapper, SRaSelfMonitoringData> implements ISRaSelfMonitoringDataService
{
    @Autowired
    private SRaSelfMonitoringDataMapper sRaSelfMonitoringDataMapper;
    @Autowired
    private ISEnvGasSelfDayService isEnvGasSelfDayService;
    @Autowired
    private ISEnvWaterSelfDayService isEnvWaterSelfDayService;

    /**
     * 查询关联分析系统-企业自行监测数据列表
     *
     * @param sRaSelfMonitoringData 关联分析系统-企业自行监测数据
     * @return 关联分析系统-企业自行监测数据
     */
    @Override
    public List<SRaSelfMonitoringData> selectSRaSelfMonitoringDataList(SRaSelfMonitoringDataQuery sRaSelfMonitoringData)
    {
        return sRaSelfMonitoringDataMapper.selectSRaSelfMonitoringDataList(sRaSelfMonitoringData);
    }

    @Override
    public List<Map<String, Object>> pullPollutantNameOptions(String name) {

        return sRaSelfMonitoringDataMapper.pullPollutantNameOptions(name);
    }

    @Override
    public void doProcessHandleData(DateTime lastDayBegin, DateTime lastDayEnd) {
        List<SEnvGasSelfDay> gasDatas = new ArrayList<>();
        List<SEnvWaterSelfDay> waterDatas = new ArrayList<>();
        List<SRaSelfMonitoringData> gasList = sRaSelfMonitoringDataMapper.selectAllLastDayGasData(lastDayBegin,lastDayEnd);
        if(gasList!=null&&gasList.size()>0){
            Map<String,List<SRaSelfMonitoringData>> map = gasList.stream().collect(Collectors.groupingBy(s->{return s.getFactoryId()+s.getMonitorId()+s.getMonitorTime();}));
            map.forEach((key,values)->{
                SEnvGasSelfDay selfDay = new SEnvGasSelfDay();
                selfDay.setDataType("D");
                values.forEach(value->{
                    selfDay.setFactoryId(value.getFactoryId());
                    selfDay.setOutletId(value.getMonitorId());
                    selfDay.setMonitoringTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",value.getMonitorTime()));
                    if(GasConcentrationEnum.so2.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setSo2(value.getActualConcentration());
                    }else if(GasConcentrationEnum.nox.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setNox(value.getActualConcentration());
                    }else if(GasConcentrationEnum.pm.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setPm(value.getActualConcentration());
                        selfDay.setSmokeDust(value.getActualConcentration());
                    }else if(GasConcentrationEnum.vocs.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setVocs(value.getActualConcentration());
                    }else if(GasConcentrationEnum.no.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setNo(value.getActualConcentration());
                    }else if(GasConcentrationEnum.no2.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setNo2(value.getActualConcentration());
                    }
                });
                gasDatas.add(selfDay);
            });
        }
        if(gasDatas!=null&&gasDatas.size()>0){
            List<List<SEnvGasSelfDay>> partion  = ListUtil.partition(gasDatas,2000);
            partion.forEach(i->{
                isEnvGasSelfDayService.saveBatch(i);
            });
            gasDatas.clear();
        }

        List<SRaSelfMonitoringData> waterList = sRaSelfMonitoringDataMapper.selectAllLastDayWaterData(lastDayBegin,lastDayEnd);
        if(waterList!=null&&waterList.size()>0){
            Map<String,List<SRaSelfMonitoringData>> map = waterList.stream().collect(Collectors.groupingBy(s->{return s.getFactoryId()+s.getMonitorId()+s.getMonitorTime();}));
            map.forEach((key,values)->{
                SEnvWaterSelfDay selfDay = new SEnvWaterSelfDay();
                selfDay.setDataType("D");
                values.forEach(value->{
                    selfDay.setFactoryId(value.getFactoryId());
                    selfDay.setOutletId(value.getMonitorId());
                    selfDay.setMonitoringTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",value.getMonitorTime()));
                    if(WaterPollutantEnum.COD.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setCod(value.getActualConcentration());
                    }else if(WaterPollutantEnum.TN.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setPhosphorusConcentration(value.getActualConcentration());
                    }else if(WaterPollutantEnum.TP.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setNitrogenConcentration(value.getActualConcentration());
                    }else if(WaterPollutantEnum.NHN.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setAmmoniaNitrogen(value.getActualConcentration());
                    }else if(WaterPollutantEnum.PH.getPollutantsCode().equals(value.getPollutantsCode())){
                        selfDay.setPhValue(value.getActualConcentration());
                    }
                });
                waterDatas.add(selfDay);
            });
        }

        if(waterDatas!=null&&waterDatas.size()>0){
            List<List<SEnvWaterSelfDay>> partion  = ListUtil.partition(waterDatas,2000);
            partion.forEach(i->{
                isEnvWaterSelfDayService.saveBatch(i);
            });
            waterDatas.clear();
        }
    }
}
