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

import cn.cecep.talroad.data.analyse.task.real.outlet.OutletRealService;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.domain.SEnvGasMonHour;
import cn.cecep.talroad.domain.SEnvWaterMonHour;
import cn.cecep.talroad.enums.PollutantTypeEnum;
import cn.cecep.talroad.mapper.SEnvGasMonHourMapper;
import cn.cecep.talroad.mapper.SEnvWaterMonHourMapper;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *
 * @Title:
 * @Author: xuxin
 * @CreateTime: 2023-06-28 16:07
 * @Description
 *
 **/
@Service
public class WaterHourOutletService extends OutletRealService<SEnvWaterMonHour>{


    @Autowired
    private SEnvWaterMonHourMapper sEnvWaterMonHourMapper;


    //   去除 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<SEnvWaterMonHour> 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<SEnvWaterMonHour> sEnvGasMonHourLambdaQueryWrapper = new QueryWrapper<SEnvWaterMonHour>().lambda()
                .in(SEnvWaterMonHour::getOutletId, outletIds)
                .ge(SEnvWaterMonHour::getMonitoringTime, DateUtil.format(startTime,"yyyy-MM-dd HH:mm:ss"))
                .le(SEnvWaterMonHour::getMonitoringTime, DateUtil.format(endTime,"yyyy-MM-dd HH:mm:ss"))
                .notIn(SEnvWaterMonHour::getDataFlag,dataFlag);
        return sEnvWaterMonHourMapper.selectList(sEnvGasMonHourLambdaQueryWrapper);
    }

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

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

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

        for (PcOutlet pcOutlet : pcOutlets) {

            String outletId = pcOutlet.getId();

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

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

        }

        return groupData;
    }

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