package cn.cecep.talroad.data.analyse.task.real.outlet.impl;

import cn.cecep.talroad.data.analyse.task.entity.MainProductDeviceVo;
import cn.cecep.talroad.data.analyse.task.real.outlet.OutletRealService;
import cn.cecep.talroad.domain.OdsInorganizationWeiStationHourI;
import cn.cecep.talroad.domain.OdsMeshingShStationHourI;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.domain.SEnvGasMonHour;
import cn.cecep.talroad.enums.PollutantTypeEnum;
import cn.cecep.talroad.mapper.SEnvGasMonHourMapper;
import cn.cecep.talroad.util.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/***
 *
 * @Title:
 * @Author: xuxin
 * @CreateTime: 2023-06-28 13:50
 * @Description
 *
 **/
@Service
public class GasHourOutletService extends OutletRealService<SEnvGasMonHour> {

    @Autowired
    private SEnvGasMonHourMapper sEnvGasMonHourMapper;

    //   去除 F/C/M/D/MD/K/P/VGD/TD/ST/SD/B/STA/FA
    public static final String[] dataFlag = new String[]{"F","C","M","D","MD","K","P","VGD","TD","ST","SD","B","STA","FA"};

    @Override
    public List<SEnvGasMonHour> queryReal(List<String> outletIds, Date startTime, Date endTime) {

        outletIds = outletIds.stream().map(s -> JSONObject.parseObject(s, PcOutlet.class)).map(PcOutlet::getId).collect(Collectors.toList());


        LambdaQueryWrapper<SEnvGasMonHour> sEnvGasMonHourLambdaQueryWrapper = new QueryWrapper<SEnvGasMonHour>().lambda()
                .in(SEnvGasMonHour::getOutletId, outletIds)
                .ge(SEnvGasMonHour::getMonitoringTime, DateUtil.format(startTime,"yyyy-MM-dd HH:mm:ss"))
                .le(SEnvGasMonHour::getMonitoringTime, DateUtil.format(endTime,"yyyy-MM-dd HH:mm:ss"))
                .notIn(SEnvGasMonHour::getDataFlag,dataFlag);
        return sEnvGasMonHourMapper.selectList(sEnvGasMonHourLambdaQueryWrapper);
    }

    @Override
    public List<String> queryOutletIds() {
        return queryOutlet(PollutantTypeEnum.GAS).stream().map(JSONObject::toJSONString).collect(Collectors.toList());
    }

    @Override
    public Map<String, List<SEnvGasMonHour>> groupOutletRealList(List<SEnvGasMonHour> dataReal, List<String> outletIds) {

        List<PcOutlet> pcOutlets = outletIds.stream().map(s -> JSONObject.parseObject(s, PcOutlet.class)).collect(Collectors.toList());
        Map<String, List<SEnvGasMonHour>> groupData = new HashMap<>();

        for (PcOutlet pcOutlet : pcOutlets) {

            String outletId = pcOutlet.getId();

            List<SEnvGasMonHour> sEnvGasMonHours = dataReal.stream()
                    .filter(mainProductDeviceVo -> mainProductDeviceVo.getOutletId().equals(outletId))
                    .collect(Collectors.toList());

            groupData.put(JSONObject.toJSONString(pcOutlet),sEnvGasMonHours);

        }

        return groupData;
    }

    @Override
    public String getLogicExceptionsDate(SEnvGasMonHour entity) {
        return entity.getMonitoringTime();
    }
}
