package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgAlarmType;
import com.casic.PgInterface.devTable.dto.PgAlarmTypeDto;
import com.casic.PgInterface.devTable.manager.PgAlarmTypeManager;
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/11/8.
 */
@Component
@Path("pgAlarmType")
public class PgAlarmTypeRs {

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

    @Resource
    private PgAlarmTypeManager pgAlarmTypeManager;


    /**
     * 获取报警类型
     */
    @POST
    @Path("getAlarmType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getAlarmTypeList()throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PgAlarmTypeDto> pgAlarmTypeDtoList=new ArrayList<PgAlarmTypeDto>();
        try {
            pgAlarmTypeDtoList = pgAlarmTypeManager.getAllAlarmTypeDto();
            if(pgAlarmTypeDtoList.size()==0)
                msg="没有报警类型信息";
            else
                msg="报警类型信息获取成功";
        }catch(Exception e){
            e.printStackTrace();
            msg="报警类型信息获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgAlarmTypeDtoList);
        return resultMap;
    }

    /**
     * 新增报警类型
     */
    @POST
    @Path("addAlarmType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addAlarmType(@FormParam("alarmType") String alarmType)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try {
            if(pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType)!=null)
            {
                result = "false";
                msg = "已存在改报警类型";
            }
            else
            {
                PgAlarmType pgAlarmType = new PgAlarmType();
                pgAlarmType.setAlarmType(alarmType);
                pgAlarmType.setActive(1);

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

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

    /**
     * 编辑报警类型
     */
    @POST
    @Path("editAlarmType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editAlarmType(@FormParam("id") String id,
                                             @FormParam("alarmType") String alarmType)throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try{
            if(pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType)!=null)
            {
                result = "false";
                msg = "已存在改报警类型";
            }
            else {
                PgAlarmType pgAlarmType = pgAlarmTypeManager.get(Long.valueOf(id));
                pgAlarmType.setAlarmType(alarmType);
                pgAlarmTypeManager.save(pgAlarmType);
                result = "true";
                msg = "编辑成功";
            }
        }
        catch(Exception e)
        {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }

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

        return resultMap;
    }


    /**
     * 删除报警类型
     */
    @POST
    @Path("deleteAlarmType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteAlarmType(@FormParam("id") String id)throws Exception {

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

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


}
