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

import cn.cecep.talroad.data.analyse.task.anomaly.analysis.DataAnomalyAbstractService;
import cn.cecep.talroad.domain.BasDevice;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.enums.EnIndustry;
import cn.cecep.talroad.enums.PollutantTypeEnum;
import cn.cecep.talroad.mapper.BasDeviceMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.service.impl.SEnvGasMonRealServiceImpl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 站点信息
 * @param <T>
 */
@Slf4j
public abstract class OutletRealService<T> {

    @Autowired
    private PcOutletMapper pcOutletMapper;

    /**
     * 获取实时数据
     */
    public abstract List<T> queryReal(List<String> outletIds, Date startTime, Date endTime);

    /**
     * 获取设备信息  大多数返回都为 json数据  需注意
     */
    public abstract List<String> queryOutletIds();

    /**
     * 数据分组
     */
    public abstract Map<String, List<T>> groupOutletRealList(List<T> dataReal,List<String> outletIds);

    /**
     * 获取报警时间
     */
    public abstract String getLogicExceptionsDate(T entity);


    public Map<String,List<BigDecimal>> calculatePollData(List<T> dataList){
        if (CollectionUtil.isEmpty(dataList)){
            return new HashMap<>();
        }

        List<String> params = Arrays.stream(SEnvGasMonRealServiceImpl.PollutantEnum.values()).map(SEnvGasMonRealServiceImpl.PollutantEnum::getProp).collect(Collectors.toList());
        Map<String, List<Object>> columnParamValue = DataAnomalyAbstractService.getColumnParamValue(dataList, params);

        Map<String, List<BigDecimal>> map = new HashMap<>();

        for (Map.Entry<String, List<Object>> stringListEntry : columnParamValue.entrySet()) {

            List<Object> value = stringListEntry.getValue();
            List<BigDecimal> dataAll = value.stream().map(String::valueOf).filter(StringUtils::isNotEmpty).filter((data) -> {
                try {
                    new BigDecimal(data);
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }).map(BigDecimal::new).collect(Collectors.toList());
            map.put(stringListEntry.getKey(),dataAll);
        }
        return map;
    }

    protected List<PcOutlet> queryOutlet(PollutantTypeEnum pollutantTypeEnum){
        if (pollutantTypeEnum == null){
            throw new RuntimeException("pollutantTypeEnum is null!");
        }
        LambdaQueryWrapper<PcOutlet> wrapper = Wrappers.<PcOutlet>lambdaQuery()
                .eq(PcOutlet::getPollutantType, pollutantTypeEnum.getCode());
        return pcOutletMapper.selectList(wrapper);
    }

}
