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

import cn.cecep.talroad.data.analyse.task.limestone.BaseAnomalyLimestoneService;
import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.enums.EnIndustry;
import cn.cecep.talroad.enums.ProblemTypeEnum;
import cn.cecep.talroad.mapper.MonDevDlRealMapper;
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 DlRealWetAnomalyLimestoneService extends BaseAnomalyLimestoneService<MonDevDlReal> {

    @Autowired
    private MonDevDlRealMapper monDevDlRealMapper;


    @Override
    protected List<BasDevice> queryDevice() {
        return queryDevice(EnIndustry.DL);
    }

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

        LambdaQueryWrapper<MonDevDlReal> monDevDlRealLambdaQueryWrapper = new QueryWrapper<MonDevDlReal>().lambda()
                .in(MonDevDlReal::getDeviceId, deviceIds)
                .ge(MonDevDlReal::getDataTime, startTime)
                .le(MonDevDlReal::getDataTime, endTime)
                .isNotNull(MonDevDlReal::getG119)
                .isNotNull(MonDevDlReal::getG127)
                .isNotNull(MonDevDlReal::getG115);

        return monDevDlRealMapper.selectList(monDevDlRealLambdaQueryWrapper);
    }

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

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

        // 石灰石消耗量
        BigDecimal limestone = getLimestone(
                deviceRealData.stream().map(MonDevDlReal::getG119).map(num -> {
                    try {
                        return new BigDecimal(num);
                    } catch (Exception e) {
                        return new BigDecimal(0);
                    }
                }).collect(Collectors.toList()),
                deviceRealData.stream().map(MonDevDlReal::getG127).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(MonDevDlReal::getG115).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;
    }

}
