package cn.cecep.talroad.data.analyse.task.limestone.wet;

import cn.cecep.talroad.data.analyse.task.limestone.BaseAnomalyLimestoneService;
import cn.cecep.talroad.domain.AmProblemFi;
import cn.cecep.talroad.domain.BasDevice;
import cn.cecep.talroad.domain.MonDevReal;
import cn.cecep.talroad.enums.EnIndustry;
import cn.cecep.talroad.enums.GtDeviceTypeEnum;
import cn.cecep.talroad.enums.ProblemTypeEnum;
import cn.cecep.talroad.mapper.MonDevRealMapper;
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.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SteelRealWetAnomalyLimestoneService extends BaseAnomalyLimestoneService<MonDevReal> {

    @Autowired
    private MonDevRealMapper monDevRealMapper;


    @Override
    protected List<BasDevice> queryDevice() {
        return queryDevice(EnIndustry.GT, GtDeviceTypeEnum.SL.getCode(),GtDeviceTypeEnum.SJJ.getCode(),GtDeviceTypeEnum.JL.getCode(),GtDeviceTypeEnum.LY.getCode(),GtDeviceTypeEnum.FDJZ.getCode());
    }

    @Override
    protected List<MonDevReal> queryRealData(List<String> deviceIds , Date startTime , Date endTime) {

        LambdaQueryWrapper<MonDevReal> monDevDlRealLambdaQueryWrapper = new QueryWrapper<MonDevReal>().lambda()
                .in(MonDevReal::getDeviceId, deviceIds)
                .ge(MonDevReal::getDataTime, startTime)
                .le(MonDevReal::getDataTime, endTime)
                .isNotNull(MonDevReal::getSo2In)
                .isNotNull(MonDevReal::getSo2Out)
                .isNotNull(MonDevReal::getTljVol);

        return monDevRealMapper.selectList(monDevDlRealLambdaQueryWrapper);
    }

    @Override
    protected Map<String, List<MonDevReal>> groupRealData(List<MonDevReal> realData) {
        return realData.stream().collect(Collectors.groupingBy(MonDevReal::getDeviceId));
    }

    @Override
    protected AmProblemFi getRealAmProblemFi(BasDevice basDevice, List<MonDevReal> deviceRealData, BigDecimal gasData , BigDecimal limestoneConfig , BigDecimal consumeConfig) {

        // 石灰石消耗量
        BigDecimal limestone = getLimestone(
                deviceRealData.stream().map(MonDevReal::getSo2In).map(num -> {
                    try {
                        return new BigDecimal(num);
                    } catch (Exception e) {
                        return new BigDecimal(0);
                    }
                }).collect(Collectors.toList()),
                deviceRealData.stream().map(MonDevReal::getSo2Out).map(num -> {
                    try {
                        return new BigDecimal(num);
                    } catch (Exception e) {
                        return new BigDecimal(0);
                    }
                }).collect(Collectors.toList()),
                gasData,
                limestoneConfig
        );

        // 脱硫剂 消耗量
        BigDecimal realData = realAvg(deviceRealData.stream().map(MonDevReal::getTljVol).map(num -> {
            try {
                return new BigDecimal(num);
            } catch (Exception e) {
                return new BigDecimal(0);
            }
        }).collect(Collectors.toList()));
        // 计算是否 大于比例值
        boolean calculate = calculate(realData, limestone, consumeConfig);

        if (!calculate){

            return addProblem(basDevice.getName(),realData,limestone.subtract(consumeConfig),limestone,basDevice.getFactoryId(), ProblemTypeEnum.WET.getCode());

        }

        return null;
    }

}
