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

import cn.cecep.talroad.data.analyse.task.entity.MainProductDeviceVo;
import cn.cecep.talroad.data.analyse.task.real.RealService;
import cn.cecep.talroad.domain.OdsMeshingShStationHourI;
import cn.cecep.talroad.enums.PollutantTypeEnum;
import cn.cecep.talroad.mapper.BMainProductionInorgGasMapper;
import cn.cecep.talroad.mapper.OdsMeshingShStationHourIMapper;
import cn.cecep.talroad.util.DateUtil;
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 org.springframework.util.CollectionUtils;

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

@Service
public class OdsMeShingShRealService extends RealService<OdsMeshingShStationHourI> {

    @Autowired
    private BMainProductionInorgGasMapper bMainProductionInorgGasMapper;

    @Autowired
    private OdsMeshingShStationHourIMapper odsMeshingShStationHourIMapper;

    @Override
    public List<OdsMeshingShStationHourI> queryReal(List<String> deviceIds, Date startTime, Date endTime) {

        List<MainProductDeviceVo> mainProductDeviceVos = MainProductDeviceVo.parse(deviceIds);

        List<String> stationIds = mainProductDeviceVos.stream().map(MainProductDeviceVo::getOutletId).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(stationIds)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<OdsMeshingShStationHourI> sEnvGasRealLambdaQueryWrapper = new QueryWrapper<OdsMeshingShStationHourI>().lambda()
                .in(OdsMeshingShStationHourI::getStationId, stationIds)
                .ge(OdsMeshingShStationHourI::getReceiveTime, DateUtil.format(startTime,"yyyy-MM-dd HH:mm:ss"))
                .le(OdsMeshingShStationHourI::getReceiveTime, DateUtil.format(endTime,"yyyy-MM-dd HH:mm:ss"));

        List<OdsMeshingShStationHourI> sEnvGasMonReals = odsMeshingShStationHourIMapper.selectList(sEnvGasRealLambdaQueryWrapper);

        return sEnvGasMonReals;
    }

    @Override
    public List<String> queryDeviceIds() {
        List<MainProductDeviceVo> deviceIdsData = bMainProductionInorgGasMapper.selectDeviceIdByPollutantType(PollutantTypeEnum.ONLINE_MONITORING.getCode());

        deviceIdsData = MainProductDeviceVo.groupDevice(deviceIdsData);

        return MainProductDeviceVo.toJsonString(deviceIdsData);
    }

    @Override
    public Map<String, List<OdsMeshingShStationHourI>> groupDeviceRealList(List<OdsMeshingShStationHourI> dataReal,List<String> deviceIds) {
        List<MainProductDeviceVo> mainProductDeviceVos = MainProductDeviceVo.parse(deviceIds);

        Map<String, List<OdsMeshingShStationHourI>> groupData = new HashMap<>();

        for (OdsMeshingShStationHourI sEnvWaterMonReal : dataReal) {

            String outletId = sEnvWaterMonReal.getStationId();

            List<String> proDeviceIds = mainProductDeviceVos.stream()
                    .filter(mainProductDeviceVo -> mainProductDeviceVo.getOutletId().equals(outletId))
                    .map(MainProductDeviceVo::getProFacilityId).collect(Collectors.toList());

            for (String proDeviceId : proDeviceIds) {
                List<OdsMeshingShStationHourI> sEnvWaterMonReals = groupData.get(proDeviceId);
                if (sEnvWaterMonReals==null){
                    sEnvWaterMonReals = new ArrayList<>();
                    groupData.put(proDeviceId,sEnvWaterMonReals);
                }
                sEnvWaterMonReals.add(sEnvWaterMonReal);
            }

        }

        return groupData;
    }

    @Override
    public String getLogicExceptionsDate(OdsMeshingShStationHourI entity) {
        return entity.getReceiveTime();
    }
}
