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

import cn.cecep.talroad.data.analyse.task.entity.DeviceSitVo;
import cn.cecep.talroad.data.analyse.task.real.device.DeviceRealService;
import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.mapper.BasDeviceSiteMapper;
import cn.cecep.talroad.mapper.MonElecRealMapper;
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 java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * mon_elec_real表里面没有设备id,只有device_site_id点位id,和设备怎么对应呢
 * bas_device_site 设备点位表，可以根据这个表关联
 * 会有一对多的情况，可以根据is_stat =1进行过滤
 * 多个点位的情况，is_stat=1的只有一条
 */
@Service
public class ElecDataDeviceRealService extends DeviceRealService<MonElecReal> {

    @Autowired
    private MonElecRealMapper monElecRealMapper;

    @Autowired
    private BasDeviceSiteMapper basDeviceSiteMapper;

    @Override
    public List<MonElecReal> queryReal(List<String> deviceIds, Date startTime, Date endTime) {
        List<DeviceSitVo> deviceSitVos = DeviceSitVo.parse(deviceIds);

        List<String> collect = deviceSitVos.stream().map(DeviceSitVo::getId).collect(Collectors.toList());

        LambdaQueryWrapper<MonElecReal> sEnvGasRealLambdaQueryWrapper = new QueryWrapper<MonElecReal>().lambda()
                .in(MonElecReal::getDeviceSiteId, collect)
                .ge(MonElecReal::getDataTime, startTime)
                .le(MonElecReal::getDataTime, endTime);

        List<MonElecReal> monElecReals = monElecRealMapper.selectList(sEnvGasRealLambdaQueryWrapper);

        return monElecReals;
    }

    @Override
    public List<String> queryDeviceIds() {
        LambdaQueryWrapper<BasDeviceSite> basDeviceSiteLambdaQueryWrapper = new QueryWrapper<BasDeviceSite>().lambda().eq(BasDeviceSite::getStatus,"正常").eq(BasDeviceSite::getIsStat, "1");
        List<BasDeviceSite> basDeviceSites = basDeviceSiteMapper.selectList(basDeviceSiteLambdaQueryWrapper);

        List<DeviceSitVo> deviceSitVos = DeviceSitVo.toDeviceSitVo(basDeviceSites);

        return DeviceSitVo.toJsonString(deviceSitVos);
    }

    @Override
    public Map<String, List<MonElecReal>> groupDeviceRealList(List<MonElecReal> dataReal,List<String> deviceIds) {

        Map<String, DeviceSitVo> deviceSitVoMap = DeviceSitVo.parse(deviceIds).stream().collect(Collectors.toMap(DeviceSitVo::getId, Function.identity()));

        Map<String, List<MonElecReal>> monSite = new HashMap<>();

        for (MonElecReal monElecReal : dataReal) {

            DeviceSitVo deviceSitVo = deviceSitVoMap.get(monElecReal.getDeviceSiteId());
            if (deviceSitVo != null){
                List<MonElecReal> monElecReals = monSite.get(deviceSitVo.getDeviceId());
                if (monElecReals == null){
                    monElecReals = new ArrayList<>();
                    monSite.put(deviceSitVo.getDeviceId(),monElecReals);
                }
                monElecReals.add(monElecReal);
            }

        }

        return dataReal.stream().collect(Collectors.groupingBy(MonElecReal::getDeviceSiteId));
    }

    @Override
    public String getLogicExceptionsDate(MonElecReal entity) {
        return DateUtil.getTime(entity.getDataTime());
    }
}
