package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgAlarm;
import com.casic.PgInterface.devTable.domain.PgDevice;
import com.casic.PgInterface.devTable.dto.PgDeviceDto;
import com.casic.PgInterface.devTable.manager.PgAlarmManager;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.reservePlan.dto.PgReservePlanDto;
import com.casic.PgInterface.reservePlan.manager.PgReservePlanManager;
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 2018/3/29.
 */
@Component
@Path("pgAlarmLink")
public class PgAlarmLinkRs {

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

    @Resource
    private PgAlarmManager pgAlarmManager;
    @Resource
    private PgDeviceManager pgDeviceManager;
    @Resource
    private PgReservePlanManager pgReservePlanManager;

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

        PgDeviceDto pgDeviceDto = null;
        try {
            PgAlarm pgAlarm = pgAlarmManager.get(Long.valueOf(id));
            PgDevice pgDevice = pgAlarm.getAlarmDevId();

            pgDeviceDto=new PgDeviceDto(pgDevice);
            msg = "报警关联设备获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "获取关联设备失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgDeviceDto);

        return resultMap;
    }

    /*
    * 获取报警设备所在区域的所有设备信息
     */
    @POST
    @Path("linkPartitionDevice")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> linkPartitionDevice(@FormParam("id") String id) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        List<PgDeviceDto> pgDeviceDtoList = new ArrayList<PgDeviceDto>();
        try {
            PgAlarm pgAlarm = pgAlarmManager.get(Long.valueOf(id));
            PgDevice pgDevice = pgAlarm.getAlarmDevId();
            pgDeviceDtoList = pgDeviceManager.getPgDeviceDtoListByPartition(pgDevice.getPartition());

            msg = "关联设备获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "分区设备关联失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgDeviceDtoList);
        return resultMap;
    }

    /*
     * 根据报警记录获取关联的预案
    */
    @POST
    @Path("linkReservePlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> linkReservePlan(@FormParam("id") String id) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        List<PgReservePlanDto> pgReservePlanDtoList=new ArrayList<PgReservePlanDto>();
        try{
            PgAlarm pgAlarm=pgAlarmManager.get(Long.valueOf(id));
            pgReservePlanDtoList=pgReservePlanManager.getReservePlanDtoByAlarmType(pgAlarm);

            msg="关联预案获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="预案关联失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgReservePlanDtoList);

        return resultMap;
    }

}