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

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.AmProblemFi;
import cn.cecep.talroad.domain.BasControlValue;
import cn.cecep.talroad.domain.BasDeviceSalAnalysis;
import cn.cecep.talroad.domain.MonDevReal;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisCo;
import cn.cecep.talroad.enums.ProblemTypeEnum;
import cn.cecep.talroad.enums.ReleaseStatusEnum;
import cn.cecep.talroad.service.IBasControlValueService;
import cn.cecep.talroad.service.IBasDeviceService;
import cn.cecep.talroad.vo.FactoryDeviceVo;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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-06-27 17:42
 * @Class: GtGlmqgAdapter
 * @Deseription:
 * @Version V1.0
 */
@Slf4j
@Service("GTGLMQG")
public class GtGlmqgAdapter extends GtStatusStrategyAdapter{

    @Autowired
    private IBasControlValueService basControlValueService;
    @Autowired
    private IBasDeviceService basDeviceService;

    @Override
    protected void doProcessGTStatus(String industry, String deviceType, List<MonDevReal> list, Date lastDate, Date curr) {
        List<BasDeviceSalAnalysis> deviceStatusList = new ArrayList<>();
        List<SRaRealTimeAnalysisCo> coList = new ArrayList<>();
        Map<String,BasDeviceSalAnalysis> salAnalysisMap = new HashMap<>();
        List<AmProblemFi> alarmList = new ArrayList<>();
        // 监测数据不为空
        if (list != null && list.size() > 0) {
            list.forEach(real -> {
                AtomicInteger m = new AtomicInteger();
                AtomicInteger n = new AtomicInteger();
                // 根据设备ID，查询企业名称、设备名称等信息
                FactoryDeviceVo vo = basDeviceService .selectFactoryDeviceVoByDeviceId(real.getDeviceId());
                if (StringUtils.isNotEmpty(real.getB806())) {
                    m.getAndIncrement();
                    BasControlValue basControlValue = basControlValueService.selectByFactoryId(real.getFactoryId(),industry,deviceType);
                    if(basControlValue!=null){
                        if("2".equals(basControlValue.getNormalValue())) {
                            // 放散调节阀状态,开关量
                            if (Double.valueOf(real.getB806())==Double.valueOf(1)) {
                                n.getAndIncrement();
                            }
                        }else{
                            // 放散调节阀状态,开度
                            if (Double.valueOf(real.getB806())>Double.valueOf(10)) {
                                n.getAndIncrement();
                            }
                        }
                    }
                }

                if (m.get() == 0) {
                    //数据缺失，无法判断
                    StringBuffer sb = new StringBuffer();
                    sb.append("数据缺失，无法判断");
                    AmProblemFi problemFi = doProcessAlaram(vo.getFactoryId(), vo.getDeviceId(), vo.getDeviceName(), lastDate, real.getDataTime(), ProblemTypeEnum.SJQS, sb.toString(),"放散调节阀状态","mq1");
                    alarmList.add(problemFi);
                } else {
                    // 处理放散状态
                    //满足两个条件就是放散，否则为未放散
                    if (n.get() > 0) {
                        BasDeviceSalAnalysis basStatus = super.doProcessDeviceAnalysisStatus(vo.getFactoryId(), vo.getDeviceId(), "", ReleaseStatusEnum.NORMAL.getName());
                        deviceStatusList.add(basStatus);
                        SRaRealTimeAnalysisCo co = super.doProcessAnalysisCoData(vo, real.getDataTime(), real.getDataTime(), ReleaseStatusEnum.NORMAL);
                        coList.add(co);
                    } else {
                        SRaRealTimeAnalysisCo co = super.doProcessAnalysisCoData(vo, real.getDataTime(), real.getDataTime(), ReleaseStatusEnum.UNREAL);
                        coList.add(co);
                        BasDeviceSalAnalysis oldSal = salAnalysisMap.get(vo.getDeviceId());
                        if(oldSal==null) {
                            BasDeviceSalAnalysis basStatus = super.doProcessDeviceAnalysisStatus(vo.getFactoryId(),vo.getDeviceId(), "", ReleaseStatusEnum.UNREAL.getName());
                            salAnalysisMap.put(vo.getDeviceId(),basStatus);
                            deviceStatusList.add(basStatus);
                        }
                    }
                }
            });

            // 批量处理
            super.doSaveBatchSRaRealTimeAnalysisCo(coList);
            super.doSaveOrUpdateBatchDeviceStatus(deviceStatusList);
            super.doSaveBatchAlarm(alarmList);
        }
    }
}
