package org.well.well.manager;


import org.well.well.base.DeviceTypeEnum;
import org.well.well.core.hibernate.HibernateEntityDao;
import org.well.well.core.util.StringUtils;
import org.well.well.domain.AlarmRule;
import org.well.well.domain.SensorType;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.well.well.domain.SysDept;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlarmRuleManager extends HibernateEntityDao<AlarmRule> {


    @Resource
    private SensorTypeManager sensorTypeManager;

    @Resource
    private DeptManager deptManager;

    public Criteria getCriteria() {
        return getSession().createCriteria(AlarmRule.class);
    }


    public AlarmRule findRuleByDevAndSensor(String devCode, SensorType sensorType) {

        Criteria criteria = getCriteria();
//        criteria.createAlias("device", "device");
//        criteria.add(Restrictions.eq("device.devCode", devCode));
        criteria.add(Restrictions.eq("devCode", devCode));
        if (null != sensorType)
            criteria.add(Restrictions.eq("sensorType", sensorType));
        AlarmRule alarmRule = (AlarmRule) criteria.uniqueResult();
        return alarmRule;
    }

    public String getRuleStr(String devCode, String devTypeName) {
        String ruleStr = "";
        AlarmRule alarmRule = new AlarmRule();
        AlarmRule alarmRule2 = new AlarmRule();
        AlarmRule alarmRule3 = new AlarmRule();
        if (DeviceTypeEnum.Liquid.toString().equals(devTypeName)) {
//            SensorType sensorType = sensorTypeManager.get("000034");
            alarmRule = findRuleByDevAndSensor(devCode, null);
            if (null != alarmRule) {
                ruleStr = String.valueOf(alarmRule.getHighValue());
            }
        } else if (DeviceTypeEnum.Noise.toString().equals(devTypeName)) {
            SensorType sensorType = sensorTypeManager.get("000032");
            alarmRule = findRuleByDevAndSensor(devCode, sensorType);
            if (null != alarmRule) {
                ruleStr = String.valueOf(alarmRule.getHighValue());
            }
        } else if (DeviceTypeEnum.NoiseDig.toString().equals(devTypeName)) {
//            SensorType sensorType = sensorTypeManager.get("000064");
            alarmRule = findRuleByDevAndSensor(devCode, null);
            if (null != alarmRule) {
                ruleStr = String.valueOf(alarmRule.getHighValue());
            }
        } else if (DeviceTypeEnum.MultiLeak.toString().equals(devTypeName)) {
            SensorType sensorType = sensorTypeManager.get("000031");
            SensorType sensorType2 = sensorTypeManager.get("000032");
            SensorType sensorType3 = sensorTypeManager.get("000033");
            alarmRule = findRuleByDevAndSensor(devCode, sensorType);
            alarmRule2 = findRuleByDevAndSensor(devCode, sensorType2);
            alarmRule3 = findRuleByDevAndSensor(devCode, sensorType3);
            ruleStr += (null != alarmRule ? (String.valueOf(alarmRule.getHighValue()) + ",") : ",");
            ruleStr += (null != alarmRule2 ? (String.valueOf(alarmRule2.getHighValue()) + ",") : ",");
            ruleStr += (null != alarmRule3 ? (String.valueOf(alarmRule3.getHighValue()) +
                    "^" + String.valueOf(alarmRule.getLowValue())) : "");
        } else if (DeviceTypeEnum.Methane.toString().equals(devTypeName)) {
            ruleStr += "50,25";
        } else if (DeviceTypeEnum.WasteGas.toString().equals(devTypeName)) {
            ruleStr += "50,10,2,18:23";//CO,H2S,CH4,O2(18~23)
        } else if (DeviceTypeEnum.TempHumi.toString().equals(devTypeName)) {
            SensorType sensorType = sensorTypeManager.get("000050");
            SensorType sensorType2 = sensorTypeManager.get("000055");
            SensorType sensorType3 = sensorTypeManager.get("000033");
            alarmRule = findRuleByDevAndSensor(devCode, sensorType);
            alarmRule2 = findRuleByDevAndSensor(devCode, sensorType2);
            alarmRule3 = findRuleByDevAndSensor(devCode, sensorType3);
            ruleStr += (null != alarmRule ? (String.valueOf(alarmRule.getHighValue()) + ",") : ",");
            ruleStr += (null != alarmRule2 ? (String.valueOf(alarmRule2.getHighValue()) + ",") : ",");
            ruleStr += (null != alarmRule3 ? (String.valueOf(alarmRule3.getHighValue()) +
                    "^" + String.valueOf(alarmRule.getLowValue())) : "");
        }
        /* else if (devTypeName.indexOf("燃气泄漏形变光纤") > -1) {
            ruleStr += "突变范围:5℃以上";
        } else if (devTypeName.indexOf("形变光纤") > -1) {
            ruleStr = "200(Mpa)";
        } else if (devTypeName.indexOf("保温层下腐蚀速率监测仪") > -1) {
            ruleStr = "0.025(mm/y)";
        } else if (devTypeName.indexOf("保温层下腐蚀环境参数监测仪") > -1) {
            ruleStr = "内外温差值＞40℃,电位≤-0.3V";
        } */
        return ruleStr;
    }

//    public Long getTenantIdBydep(String depId) {
//        try {
//            String sql = "SELECT TENANT_ID FROM sys_dept_detail s WHERE s.id=" + depId;
//            List<Object> tenantList = getSession().createSQLQuery(sql).list();
//            if (tenantList.size() > 0)
//                return Long.valueOf(tenantList.get(0).toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

//    public List<Object[]> getIsAlarmAndJob(String devTypeName, String depId) {
//        Long tenantId = null;
//        if (StringUtils.isNotBlank(depId)) {
//            SysDept sysDept = deptManager.get(Long.valueOf(depId));
//            if (null != sysDept) {
//                String tenantStr = sysDept.getTenantId();
//                tenantId = StringUtils.isBlank(tenantStr) ? tenantId : Long.valueOf(tenantStr);
//            }
//        }
//
//        if (null == tenantId) return new ArrayList<Object[]>();
//        try {
//            Map<String, Object> paraMap = new HashMap<String, Object>();
//            String sql = "SELECT open_alarm,open_job FROM alarm_level a  " +
//                    "WHERE  a.name=:devTypeName LIMIT 1" ;
//            paraMap.put("devTypeName", devTypeName);
//            paraMap.put("tenantId", tenantId);
//            List<Object[]> alarmLevelList = getSession().createSQLQuery(sql).setProperties(paraMap).list();
//            return alarmLevelList;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return new ArrayList<Object[]>();
//    }


    public List<Object[]> getRuleRank(String devCode, String depId) {
        Long tenantId = null;
        if (StringUtils.isNotBlank(depId)) {
            SysDept sysDept = deptManager.get(Long.valueOf(depId));
            if (null != sysDept) {
                String tenantStr = sysDept.getTenantId();
                tenantId = StringUtils.isBlank(tenantStr) ? tenantId : Long.valueOf(tenantStr);
            }
        }

        if (null == tenantId) return new ArrayList<Object[]>();
        try {
            Map<String, Object> paraMap = new HashMap<String, Object>();
            String sql = "SELECT level,low_value,high_value,name, open_alarm,open_job FROM alarm_level_device_veiw a " +
                    "WHERE  a.devcode=:devcode AND a.tenant_id=:tenantId" +
                    " ORDER BY name,level";
            paraMap.put("devcode", devCode);
            paraMap.put("tenantId", tenantId);
            List<Object[]> ruleRankList = getSession().createSQLQuery(sql).setProperties(paraMap).list();
            return ruleRankList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<Object[]>();
    }
}
