package cn.cecep.talroad.data.analyse.task.real.concentration.online;

import cn.cecep.talroad.data.analyse.task.entity.ConcentrationEntity;
import cn.cecep.talroad.data.analyse.task.entity.PollutantEntity;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 获取在线检测数据
 */
@Slf4j
public abstract class OnlineRealTaskService<T> {

    /**
     * 获取实时数据
     */
    public abstract List<T> queryRealData(List<String> outletId, Date findDate);

    /**
     * 获取污染物信息
     */
    public abstract List<PollutantEntity> getPollEntity();

    /**
     * 转为污染物数据实体
     */
    public abstract ConcentrationEntity getOnlineConcentration(T data , String pollCode , BigDecimal concentration);


    public List<ConcentrationEntity> getOnlineConcentration(List<String> outletId, Date findDate){
        return getOnlineConcentration(outletId , findDate);
    }

    public List<ConcentrationEntity> getOnlineConcentration(List<T> dataReal){

        List<PollutantEntity> pollEntity = getPollEntity();
        List<ConcentrationEntity> concentrationEntities = new ArrayList<>();

        for (T t : dataReal) {
            Class<?> aClass = t.getClass();
            for (PollutantEntity pollutantEntity : pollEntity) {
                String propName = pollutantEntity.getPropName();
                try {
                    Field declaredField = aClass.getDeclaredField(propName);
                    declaredField.setAccessible(true);
                    BigDecimal bigDecimal = new BigDecimal(declaredField.get(propName).toString());
                    ConcentrationEntity onlineConcentration = getOnlineConcentration(t , pollutantEntity.getPollutantsCode(), bigDecimal);
                    concentrationEntities.add(onlineConcentration);
                } catch (Exception e) {
                    log.error("获取在线数据异常 : {}",e.getMessage());
                    throw new RuntimeException(e);
                }

            }

        }

        return concentrationEntities;
    }




}
