package org.well.well.manager;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.well.well.base.DeviceTypeEnum;
import org.well.well.core.hibernate.HibernateEntityDao;
import org.well.well.core.util.StringUtils;
import org.well.well.core.util.ThreadUtil;
import org.well.well.domain.*;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class MeterManager extends HibernateEntityDao<DataMeter> {
    @Resource
    private DeviceManager deviceManager;
    @Resource
    private AlarmRecordManager alarmRecordManager;
    @Resource
    BusWellManager busWellManager;
    @Resource
    AlarmJobManager alarmJobManager;

    @Transactional
    public void processMeterData(Map<String,String> meterMap){
        try {
            String devCode=meterMap.get("devCode");
            //1.清离线
            deviceManager.clearOffline(devCode);
            //2.存数据
            BusWell busWell = busWellManager.getWellByDevCode(devCode);
            save(new DataMeter(meterMap,null==busWell?"":busWell.getWellCode()));
            //3.有无超限
            String alarmContent = "", alarmValue = "";
            if(StringUtils.isNotBlank(meterMap.get("temp"))&&Float.valueOf(meterMap.get("temp"))>=60){
                alarmContent+="温度超限报警,";
                alarmValue+="温度:" + meterMap.get("temp") + "℃,";
            }
            if(StringUtils.isNotBlank(meterMap.get("PH"))&&(Float.valueOf(meterMap.get("PH"))>=10||
                    Float.valueOf(meterMap.get("PH"))<=6)){
                alarmContent+="PH值超限报警,";
                alarmValue+="PH:" + meterMap.get("PH") + ",";
            }
            if(StringUtils.isNotBlank(meterMap.get("conductivity"))&&
                    Float.valueOf(meterMap.get("conductivity"))>=5){
                alarmContent+="电导率超限报警,";
                alarmValue+="电导率:" + meterMap.get("conductivity") + "mS/cm,";
            }
            if(StringUtils.isNotBlank(meterMap.get("DO"))&&Float.valueOf(meterMap.get("DO"))>=20){
                alarmContent+="溶解氧超限报警,";
                alarmValue+="溶解氧:" + meterMap.get("DO") + "mg/L,";
            }
            if(StringUtils.isNotBlank(meterMap.get("turbidity"))&&Float.valueOf(meterMap.get("turbidity"))>=1000){
                alarmContent+="浊度超限报警,";
                alarmValue+="浊度:" + meterMap.get("turbidity") + "NTU,";
            }
            if(StringUtils.isNotBlank(meterMap.get("COD"))&&Float.valueOf(meterMap.get("COD"))>=500){
                alarmContent+="化学需氧量超限报警,";
                alarmValue+="化学需氧量:" + meterMap.get("COD") + "mg/L,";
            }
            if(StringUtils.isNotBlank(meterMap.get("AN"))&&Float.valueOf(meterMap.get("AN"))>=20){
                alarmContent+="氨氮超限报警,";
                alarmValue+="氨氮:" + meterMap.get("AN") + "mg/L,";
            }
            if(StringUtils.isNotBlank(meterMap.get("TP"))&&Float.valueOf(meterMap.get("TP"))>=15){
                alarmContent+="总磷超限报警,";
                alarmValue+="总磷:" + meterMap.get("TP") + "mg/L,";
            }
            if(StringUtils.isNotBlank(meterMap.get("TN"))&&Float.valueOf(meterMap.get("TN"))>=30){
                alarmContent+="总氮超限报警,";
                alarmValue+="总氮:" + meterMap.get("TN") + "mg/L,";
            }
            if (StringUtils.isNotBlank(alarmContent)) {
                AlarmRecord alarmRecord = alarmRecordManager.getThresholdAlarm(devCode);
                if (null != alarmRecord) {//已存在报警
                    //1.清除上一条告警记录
                    alarmRecordManager.clearAlarm(devCode, "1", "");
                    //2.写入新的告警记录,更新工单号为上一条告警记录的工单号
                    alarmRecordManager.saveData(alarmRecord.getJobId(), "1", null,
                            "水质超限告警",
                            alarmValue.substring(0, alarmValue.length() - 1), alarmRecord.getDevice(), devCode,
                            alarmRecord.getWellCode(), meterMap.get("DataTime"),
                            alarmContent.substring(0, alarmContent.length() - 1));
                } else {//不存在上一条报警时
                    Device device = deviceManager.getDeviceByDevCode(devCode);
                    if (device == null) return;//未注册设备舍弃
                    //1.生成新的工单
                    AlarmJob alarmJob = alarmJobManager.saveData(devCode, busWell != null ?
                            busWell.getWellCode() : "", DeviceTypeEnum.Meter.toString(),"1");
                    //2.写入新的告警
                    alarmRecordManager.saveData(alarmJob.getId(), "1", null,
                            "水质超限告警",
                            alarmValue.substring(0, alarmValue.length() - 1), device,
                            devCode, alarmJob.getWellCode(), meterMap.get("DataTime"),
                            alarmContent.substring(0, alarmContent.length() - 1));
//                        //3.toDo:向app推送报警消息
//                        List<String> cids = userManager.getClients(busWell);
//                        if (cids.size() > 0)
//                            PushList.pushToUser(cids, "液位超限", "报警啦，快来处理");
                    ThreadUtil.excuteMsg(alarmJob.getId(), "水质超限告警", busWell != null ?
                            busWell.getWellCode() : "");

                }
            } else {//未超限
                //1.清除之前的报警
                alarmRecordManager.clearAlarmByNormalData(devCode, "1");
                //2.结束之前的工单
                alarmJobManager.updateAlarmJobByDevCode(devCode, "数据正常，系统自动结束工单");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
