package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.manager.PgAlarmTypeManager;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.patroler.domain.PgAlarmRule;
import com.casic.PgInterface.patroler.dto.PgAlarmRuleDto;
import com.casic.PgInterface.patroler.manager.PgAlarmRuleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yxw on 2017/12/14.
 */
@Component
@Path("pgAlarmRule")
public class PgAlarmRuleRs {

    private static Logger logger = LoggerFactory.getLogger(PgAlarmRule.class);

    @Resource
    private PgDeviceManager pgDeviceManager;
    @Resource
    private PgAlarmTypeManager pgAlarmTypeManager;
    @Resource
    private PgAlarmRuleManager pgAlarmRuleManager;



    /**
     * 新增报警阈值
     */
    @POST
    @Path("addAlarmRule")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addAlarmRule(@FormParam("highValue") String highValue,
                                            @FormParam("lowValue") String lowValue,
                                            @FormParam("unit") String unit,
                                            @FormParam("devType") String devType,
                                            @FormParam("alarmType") String alarmType) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        String result = "";

        PgAlarmRule pgAlarmRule = null;

        try {
            pgAlarmRule = pgAlarmRuleManager.getAlarmRuleBydevType(devType);
            if (pgAlarmRule != null) {
                result = "false";
                msg = "设备阈值已经存在";
            } else {
                pgAlarmRule = new PgAlarmRule();
                pgAlarmRule.setHighValue(highValue);
                pgAlarmRule.setLowValue(lowValue);
                pgAlarmRule.setDevType(devType);
                pgAlarmRule.setUnit(unit);
                pgAlarmRule.setAlarmTypeId(pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType));

                pgAlarmRuleManager.save(pgAlarmRule);
                result = "true";
                msg = "报警阈值新增成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "false";
            msg = "报警阈值新增失败";
        }

        resultMap.put("msg", msg);
        resultMap.put("result", result);
        return resultMap;
    }

    /**
     * 编辑报警阈值
     */
    @POST
    @Path("editAlarmRule")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editAlarmRule(@FormParam("id") String id,
                                             @FormParam("devType") String devType,
                                             @FormParam("highValue") String highValue,
                                             @FormParam("lowValue") String lowValue,
                                             @FormParam("unit") String unit,
                                             @FormParam("alarmType") String alarmType) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        String result = "";

        PgAlarmRule pgAlarmRule = null;

        try {
            pgAlarmRule = pgAlarmRuleManager.get(Long.valueOf(id));
            if (pgAlarmRule == null) {
                msg = "不存在报警阈值记录";
                result = "false";
            } else {
                pgAlarmRule.setDevType(devType);
                pgAlarmRule.setHighValue(highValue);
                pgAlarmRule.setLowValue(lowValue);
                pgAlarmRule.setUnit(unit);
                pgAlarmRule.setAlarmTypeId(pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType));

                pgAlarmRuleManager.save(pgAlarmRule);
                msg = "编辑成功";
                result = "true";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = "false";
            msg = "报警阈值编辑失败";
        }

        resultMap.put("msg", msg);
        resultMap.put("result", result);

        return resultMap;
    }

    /**
     * 获取报警阈值信息
     */
    @POST
    @Path("getAlarmRule")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getAlarmRule() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        List<PgAlarmRuleDto> pgAlarmRuleDtoList = new ArrayList<PgAlarmRuleDto>();
        try {
            pgAlarmRuleDtoList = pgAlarmRuleManager.getAlarmRuleDto();
            if (pgAlarmRuleDtoList == null)
                msg = "不存在报警阈值信息";
            else
                msg = "报警阈值获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "报警阈值信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgAlarmRuleDtoList);
        return resultMap;
    }

    /**
     * 删除报警阈值
     */
    @POST
    @Path("deleteAlarmRule")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteAlarmRule(@FormParam("id") String id) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        String result = "";
        PgAlarmRule pgAlarmRule = null;

        try {
            pgAlarmRule = pgAlarmRuleManager.get(Long.valueOf(id));
            if (pgAlarmRule == null) {
                msg = "不存在报警阈值记录";
                result = "false";
            } else {
                pgAlarmRuleManager.remove(pgAlarmRule);
                msg = "删除成功";
                result = "true";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result = "false";
            msg = "报警阈值删除失败";
        }

        resultMap.put("msg", msg);
        resultMap.put("result", result);

        return resultMap;
    }

    /**
     * 根据设备类型报警阈值
     */
    @POST
    @Path("getAlarmRuleByDevType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getAlarmRuleByDevType(@FormParam("devType") String devType) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        PgAlarmRule pgAlarmRule = null;
        List<PgAlarmRuleDto> pgAlarmRuleDtos = new ArrayList<>();
        try {
            pgAlarmRule = pgAlarmRuleManager.getAlarmRuleBydevType(devType);
            if (pgAlarmRule == null)
                msg = "不存在报警阈值记录";
            else {
                PgAlarmRuleDto pgAlarmRuleDto= new PgAlarmRuleDto(pgAlarmRule);
                pgAlarmRuleDtos.add(pgAlarmRuleDto);
                msg = "报警阈值获取成功";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            msg = "报警阈值获取失败";
        }

        resultMap.put("msg", msg);
        resultMap.put("result", pgAlarmRuleDtos);

        return resultMap;
    }

}