package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgAlarm;
import com.casic.PgInterface.devTable.dto.PgAlarmDto;
import com.casic.PgInterface.devTable.manager.PgAlarmManager;
import com.casic.PgInterface.devTable.manager.PgAlarmTypeManager;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.patroler.manager.PgUserManager;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yxw on 2017/11/4.
 */
@Component
@Path("pgAlarm")
public class PgAlarmRs {

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

    @Resource
    private PgAlarmTypeManager pgAlarmTypeManager;

    @Resource
    private PgAlarmManager pgAlarmManager;

    @Resource
    private PgUserManager pgPatrolerManager;

    @Resource
    private PgDeviceManager pgDeviceManager;

    /**
     * 获取报警信息
     */
    @POST
    @Path("getAlarmInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getAlarmList() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgAlarmDto> pgAlarmDtoList = new ArrayList<PgAlarmDto>();
        try {
            pgAlarmDtoList = pgAlarmManager.getAllAlarm();
            if (pgAlarmDtoList.size() == 0)
                msg = "没有报警信息";
            else
                msg = "报警信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "报警信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgAlarmDtoList);
        return resultMap;
    }

    /**
     * 消除报警信息
     */
    @POST
    @Path("clearAlarmInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> clearAlarmList(@FormParam("id") String id) throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        String result = "false";

        try {
            PgAlarm pgAlarm = new PgAlarm();
            pgAlarm = pgAlarmManager.get(Long.valueOf(id));
            if (pgAlarm == null) {
                msg = "不存在该报警信息";
                result = "false";
            } else {
                pgAlarm.setActive(0);
                pgAlarmManager.save(pgAlarm);
                msg = "报警信息清除成功";
                result = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = "报警信息清除失败";
            result = "false";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", result);
        return resultMap;
    }

    /**
     * 新增报警
     */
    @POST
    @Path("addAlarm")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addAlarm(@FormParam("alarmType") String alarmType,
                                        @FormParam("alarmValue") String alarmValue,
                                        @FormParam("alarmDate") String alarmDate,
                                        @FormParam("description") String description,
                                        @FormParam("devCode") String devCode,
                                        @FormParam("userName") String userName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            PgAlarm pgAlarm = new PgAlarm();
                pgAlarm.setAlarmTypeId(pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType));
                pgAlarm.setAlarmValue(alarmValue);
                pgAlarm.setAlarmDate(sdf.parse(alarmDate));
                pgAlarm.setDescription(description);
                pgAlarm.setUserId(pgPatrolerManager.getPatrolerByuserName(userName));
                pgAlarm.setAlarmDevId(pgDeviceManager.getDeviceBydevCode(devCode));
                pgAlarm.setActive(1);

                logger.info("成功保存");
                pgAlarmManager.save(pgAlarm);
                result = "true";
                msg = "保存成功";
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

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

    /*
    * 根据报警类型和时间查询报警记录
     */
    @POST
    @Path("getAlarmByTypeOrTime")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getAlarmByTypeOrTime(@FormParam("alarmType") String alarmType,
                                                    @FormParam("startTime") String startTime,
                                                    @FormParam("endTime") String endTime) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        List<PgAlarmDto> pgAlarmDtoList = new ArrayList<PgAlarmDto>();
        try {
            pgAlarmDtoList = pgAlarmManager.getAlarmByTypeAndTime(alarmType, startTime, endTime);
            if (pgAlarmDtoList == null || pgAlarmDtoList.size() == 0)
                msg = "报警信息不存在";
            else
                msg = "报警信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "报警信息获取失败";
        }

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

        return resultMap;
    }

}