package cn.cecep.talroad.service.strategy.support.fs;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.bean.BeanUtils;
import cn.cecep.talroad.domain.AmProblemFi;
import cn.cecep.talroad.domain.BasDeviceSalAnalysis;
import cn.cecep.talroad.domain.MonDevFsReal;
import cn.cecep.talroad.domain.SRaRealTimeAnalysisRunOne;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRun;
import cn.cecep.talroad.enums.DeviceStatusEnum;
import cn.cecep.talroad.enums.JudgeEnum;
import cn.cecep.talroad.enums.ProblemTypeEnum;
import cn.cecep.talroad.service.IBasDeviceService;
import cn.cecep.talroad.service.IMonDevFsRealService;
import cn.cecep.talroad.service.strategy.StatusStrategyAdapter;
import cn.cecep.talroad.util.MathUtils;
import cn.cecep.talroad.vo.FactoryDeviceVo;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: cjh
 * @Date: 2023-03-27 19:24
 * @Class: FsFslAdapter
 * @Deseription:
 * @Version V1.0
 */
@Slf4j
@Service("FSFSL")
public class FsFslAdapter extends StatusStrategyAdapter {

    @Autowired
    private IBasDeviceService basDeviceService;
    @Autowired
    private IMonDevFsRealService monDevFsRealService;

    @Override
    protected void doProcessStatus(String industry, String deviceType,Date curr) {
        List<BasDeviceSalAnalysis> deviceStatusList = new ArrayList<>();
        List<SRaRealTimeAnalysisRun> runList = new ArrayList<>();
        List<SRaRealTimeAnalysisRunOne> runOneList = new ArrayList<>();
        List<AmProblemFi> alarmList = new ArrayList<>();

        Date lastHour = DateUtils.addMinutes(curr, -15);
        Date lastHourBegin = DateUtils.addHours(curr, -1);
        List<MonDevFsReal> realList = monDevFsRealService.selectLatestByParams(industry,deviceType, lastHour,curr);
        if(realList!=null&&realList.size()>0){
            realList.forEach(real->{
                List<MonDevFsReal> lastHourData = monDevFsRealService.selectLatestHourDataByParams(real.getFactoryId(),real.getDeviceId(), lastHourBegin);
                // 所有条件
                AtomicInteger total = new AtomicInteger();
                // 停产为真的条件
                AtomicInteger n = new AtomicInteger();
                // 根据设备ID，查询企业名称、设备名称等信息
                FactoryDeviceVo vo = basDeviceService.selectFactoryDeviceVoByDeviceId(real.getDeviceId());
                SRaRealTimeAnalysisRunOne oldDeviceStatus = super.getOldDeviceStatus(vo.getFactoryId(),vo.getDeviceId());

                String fs100 = "";
                if (StringUtils.isNotEmpty(real.getFs100())) {
                    fs100 = real.getFs100();
                }
                String finalFs10 = fs100;
                boolean ljcll = lastHourData.stream().allMatch(r -> finalFs10.equals(r.getFs100()));
                total.getAndIncrement();
                // fs100所有值相等为恒指
                if (ljcll) {
                    n.getAndIncrement();
                }
                String gc19 = "";
                if (StringUtils.isNotEmpty(real.getGc19())) {
                    gc19 = real.getGc19();
                }
                String finalGc19 = gc19;
                // gc19所有值相等为恒指
                boolean tlqwy = lastHourData.stream().allMatch(r -> finalGc19.equals(r.getGc19()));

                if (StringUtils.isNotEmpty(real.getGc19())) {
                    //推料器位移行程
                    total.getAndIncrement();
                    //判断参数是否等于0
                    if ("0".equals(real.getGc19())|| tlqwy) {
                        n.getAndIncrement();
                    }
                }
                if (StringUtils.isNotEmpty(real.getGc21())) {
                    //焚烧工况_标记
                    total.getAndIncrement();
                    //判断参数是否等于0
                    if ("0".equals(real.getGc21())) {
                        n.getAndIncrement();
                    }
                }

                if (StringUtils.isNotEmpty(real.getFs101())) {
                    //焚烧炉炉膛温度
                    total.getAndIncrement();
                    if (Double.valueOf(real.getFs101()).compareTo(800.0) < 0) {
                        n.getAndIncrement();
                    }
                }
                if (StringUtils.isNotEmpty(real.getGc30())) {
                    //余热锅炉出口烟气温度
                    total.getAndIncrement();
                    if (Double.valueOf(real.getGc30()).compareTo(200.0) < 0) {
                        n.getAndIncrement();
                    }
                }

                if(total.get()<2){
                    //数据缺失，无法判断

                }else{
                    //停产条件真的比例超过66%
                    if(!MathUtils.divideSjj(n.get(),total.get())) {
                        //停产条件比例占比超过66%，设备停机
                        SRaRealTimeAnalysisRun run = super.doProcessDeviceRunStatus(vo,lastHour,curr,oldDeviceStatus, DeviceStatusEnum.STOP, JudgeEnum.GKSJ);
                        runList.add(run);
                        if(oldDeviceStatus!=null) {
                            String oneId = "";
                            if(StringUtils.isNotEmpty(oldDeviceStatus.getDataId())) {
                                oneId = oldDeviceStatus.getDataId();
                            }
                            BeanUtils.copyProperties(run, oldDeviceStatus);
                            oldDeviceStatus.setDataId(oneId);
                            oldDeviceStatus.setIndustryPermissionName(vo.getIndustryPermissionName());
                            runOneList.add(oldDeviceStatus);
                        }

                        BasDeviceSalAnalysis basStatus = super.doProcessDeviceAnalysisStatus(vo.getFactoryId(),vo.getDeviceId(),DeviceStatusEnum.STOP.getValue(),"");
                        deviceStatusList.add(basStatus);
                        if(DeviceStatusEnum.STOP_RUN.getCode().equals(run.getOperationStatus())||DeviceStatusEnum.RUN_STOP.getCode().equals(run.getOperationStatus())) {
                            String runEtc = super.getRunEtc(lastHour, curr, "");
                            StringBuffer sb = new StringBuffer();
                            sb.append(vo.getFactoryName()).append(vo.getDeviceName())
                                .append("于").append(runEtc).append("存在启停行为，请注意。");
                            AmProblemFi problemFi = doProcessAlaram(vo.getFactoryId(),vo.getDeviceId(),  vo.getDeviceName(), lastHour,curr, ProblemTypeEnum.STOP_RUN,sb.toString(),"垃圾处理量,推料器位移行程,焚烧工况标记,焚烧炉炉膛温度,余热锅炉出口烟气温度","fs100,gc19,gc21,fs101,gc30");
                            alarmList.add(problemFi);
                        }
                    }else{
                        //设备未停机
                        SRaRealTimeAnalysisRun run = super.doProcessDeviceRunStatus(vo,lastHour,curr,oldDeviceStatus, DeviceStatusEnum.RUN, JudgeEnum.GKSJ);
                        runList.add(run);
                        if(oldDeviceStatus!=null) {
                            String oneId = "";
                            if(StringUtils.isNotEmpty(oldDeviceStatus.getDataId())) {
                                oneId = oldDeviceStatus.getDataId();
                            }
                            BeanUtils.copyProperties(run, oldDeviceStatus);
                            oldDeviceStatus.setDataId(oneId);
                            oldDeviceStatus.setIndustryPermissionName(vo.getIndustryPermissionName());
                            runOneList.add(oldDeviceStatus);
                        }
                        BasDeviceSalAnalysis basStatus = super.doProcessDeviceAnalysisStatus(vo.getFactoryId(),vo.getDeviceId(),DeviceStatusEnum.RUN.getValue(),"");
                        deviceStatusList.add(basStatus);
                        if(DeviceStatusEnum.STOP_RUN.getCode().equals(run.getOperationStatus())||DeviceStatusEnum.RUN_STOP.getCode().equals(run.getOperationStatus())) {
                            String runEtc = super.getRunEtc(lastHour, curr, "");
                            StringBuffer sb = new StringBuffer();
                            sb.append(vo.getFactoryName()).append(vo.getDeviceName())
                                .append("于").append(runEtc).append("存在启停行为，请注意。");
                            AmProblemFi problemFi = doProcessAlaram(vo.getFactoryId(),vo.getDeviceId(),  vo.getDeviceName(), lastHour,curr, ProblemTypeEnum.STOP_RUN,sb.toString(),"垃圾处理量,推料器位移行程,焚烧工况标记,焚烧炉炉膛温度,余热锅炉出口烟气温度","fs100,gc19,gc21,fs101,gc30");
                            alarmList.add(problemFi);
                        }
                    }
                }
            });

            //批量保存
            super.doSaveBatchAnalysisRunStatus(runList);
            super.doSaveBatchAnalysisRunOneStatus(runOneList);
            super.doSaveOrUpdateBatchDeviceStatus(deviceStatusList);
            super.doSaveBatchAlarm(alarmList);
        }
    }

}
