package org.well.well.manager;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.well.well.core.hibernate.HibernateEntityDao;
import org.well.well.core.util.DateUtils;
import org.well.well.core.util.StringUtils;
import org.well.well.domain.AlarmRecord;
import org.well.well.domain.Device;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by test203 on 2017/11/30.
 */
@Service
public class AlarmRecordManager extends HibernateEntityDao<AlarmRecord> {

    private static final String[] alarmTypeArr = {"1", "2"};//1:阈值报警；2：设备本身报警；
    private static final String[] alarmStatusArr = {"0", "1", "2", "3"};//告警状态( 0：因新报警而自动取消，1：正在报警，2：因工单结束而消警，3：因数据正常而消警)

    public Map<String, Object> saveData(Long jobId, String alarmType,Integer alarmLevel,
                                        String alarmContent, String alarmValue,
                                        Device device, String devCode,
                                        String wellCode, String alarmTime,String alarmMsg) throws IOException {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            AlarmRecord alarmRecord = new AlarmRecord(jobId, alarmType,alarmLevel, alarmContent, alarmValue,
                    device, devCode, wellCode, DateUtils.sdf4.parse(DateUtils.DateFormat(alarmTime)),alarmMsg);

            save(alarmRecord);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    public void clearAlarm(String devCode, String alarmType, String alarmValue) {//清除报警

        try {
            String status = alarmStatusArr[3];
            String hql = "update AlarmRecord set STATUS=:status " +
                    "where STATUS = " + alarmStatusArr[1] +
                    " and deviceCode=:deviceCode ";
            status = StringUtils.isNotBlank(alarmType) ?
                    alarmStatusArr[0] : status;
            if (alarmTypeArr[0].equals(alarmType)) {//阈值报警
                hql += "and  alarmType = " + alarmTypeArr[0];
            } else if (alarmTypeArr[1].equals(alarmType)) {//设备本身报警
                hql += StringUtils.isNotBlank(alarmValue) ? "and alarmType =" + alarmTypeArr[1] +
                        " and alarmValue =" + alarmValue : "and alarmType =" + alarmTypeArr[1];
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", status);
            map.put("deviceCode", devCode);
            batchUpdate(hql, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void clearAlarmByNormalData(String devCode, String alarmType) {//根据正常数据清除报警

        try {
            String status = alarmStatusArr[3];
            String hql = "update AlarmRecord set STATUS=:status " +
                    "where STATUS = " + alarmStatusArr[1] +
                    " and deviceCode=:deviceCode ";
            if (alarmTypeArr[0].equals(alarmType)) {//阈值报警
                hql += "and  alarmType = " + alarmTypeArr[0];
            } else if (alarmTypeArr[1].equals(alarmType)) {//设备本身报警
                hql += "and alarmType =" + alarmTypeArr[1];
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", status);
            map.put("deviceCode", devCode);
            batchUpdate(hql, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public AlarmRecord getThresholdAlarm(String devCode) {//根据告警判断是否有工单

        Criteria criteria = getSession().createCriteria(AlarmRecord.class);
        criteria.add(Restrictions.eq("deviceCode", devCode));
        criteria.add(Restrictions.eq("alarmType", alarmTypeArr[0]));
        criteria.add(Restrictions.eq("status", alarmStatusArr[1]));
        List<AlarmRecord> alarmRecordList = criteria.list();
        return alarmRecordList != null && alarmRecordList.size() > 0 ?
                alarmRecordList.get(0) : null;

    }

    public AlarmRecord getDeviceAlarm(String devCode, String alarmValue) {//获取设备本身报警消息（正在告警）

        Criteria criteria = getSession().createCriteria(AlarmRecord.class);
        criteria.add(Restrictions.eq("deviceCode", devCode));
        criteria.add(Restrictions.eq("alarmType", alarmTypeArr[1]));
        criteria.add(Restrictions.eq("alarmValue", alarmValue));
        criteria.add(Restrictions.eq("status", alarmStatusArr[1]));
        List<AlarmRecord> alarmRecordList = criteria.list();
        return alarmRecordList != null && alarmRecordList.size() > 0 ?
                alarmRecordList.get(0) : null;

    }
}
