package com.casic.PgInterface.rs;

import com.casic.PgInterface.core.util.StringUtils;
import com.casic.PgInterface.devTable.domain.PgAlarmType;
import com.casic.PgInterface.devTable.manager.PgAlarmTypeManager;
import com.casic.PgInterface.reservePlan.domain.PgReservePlan;
import com.casic.PgInterface.reservePlan.dto.PgReservePlanDto;
import com.casic.PgInterface.reservePlan.manager.PgReservePlanManager;
import org.springframework.http.ResponseEntity;
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/4/10.
 */
@Component
@Path("PgReservePlan")
public class PgReservePlanRs {

    @Resource
    private PgReservePlanManager pgReservePlanManager;

    @Resource
    private PgAlarmTypeManager pgAlarmTypeManager;

    /*
   * 获取角色信息
    */
    @POST
    @Path("getReservePlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getReservePlan() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgReservePlanDto> pgReservePlanDtoList = new ArrayList<PgReservePlanDto>();
        try {
            pgReservePlanDtoList = pgReservePlanManager.getpgReservePlanDto();
            if (pgReservePlanDtoList == null)
                msg = "不存在预案信息";
            else
                msg = "预案信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "预案信息获取是失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgReservePlanDtoList);
        return resultMap;
    }

    @POST
    @Path("downLoadPlanFile")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> downLoadPlanFile(@FormParam("id") String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        String msg = "";
        ResponseEntity<byte[]> path = null;
        try {
            PgReservePlan pgReservePlan = pgReservePlanManager.get(Long.valueOf(id));
            path = pgReservePlanManager.downloadFile(pgReservePlan);
            if (path == null)
                msg = "不存在该文档";
            else
                msg = "下载成功";

        } catch (Exception e) {
            e.printStackTrace();
            msg = "下载失败";
        }

        resultMap.put("result", path);
        resultMap.put("msg", msg);
        return resultMap;
    }
    /*
        新增&上传
     */
    @POST
    @Path("addReservePlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> addReservePlan(@FormParam("planName") String planName,
                                             @FormParam("planType") String planType,
                                             @FormParam("resUnit") String resUnit,
                                             @FormParam("planFile") String planFile,
                                             @FormParam("alarmType") String alarmType)throws Exception {

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

        try {
            PgReservePlan pgReservePlan = new PgReservePlan();

            if(pgReservePlanManager.getPgReservePlanByFileName(planName))
            {
                msg="已存在同名称文档";
                result="false";
            }
            else {
                pgReservePlan.setPlanName(planName);
                pgReservePlan.setPlanType(planType);
                pgReservePlan.setResUnit(resUnit);
                pgReservePlan.setActive(1);
                PgAlarmType pgAlarmType = pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType);
                pgReservePlan.setPgAlarmTypeId(pgAlarmType);
                if (StringUtils.isBlank(planFile) || StringUtils.isBlank(planName))
                    pgReservePlan.setPlanFile("");
                else {
                    pgReservePlan.setPlanFile(pgReservePlanManager.saveFiles(planFile, planName, "ReservePlan"));
                }

                pgReservePlanManager.save(pgReservePlan);
                msg = "新增成功";
                result = "true";
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="新增失败";
            result="false";
        }

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

        return resultMap;
    }

    //编辑文档
    @POST
    @Path("editReservePlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editReservePlan(@FormParam("id") String id,
                                               @FormParam("planName") String planName,
                                               @FormParam("planType") String planType,
                                               @FormParam("resUnit") String resUnit,
                                               @FormParam("alarmType") String alarmType) throws Exception {

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

        try {
            PgReservePlan pgReservePlan=pgReservePlanManager.get(Long.valueOf(id));

            if (pgReservePlan == null) {
                result = "false";
                msg = "不存在该预案信息";
            } else {
                if(pgReservePlanManager.getPgReservePlanByFileName(planName))
                {
                    msg="已存在同名称文档";
                    result="false";
                }
                else {
                    PgAlarmType pgAlarmType = pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType);
                    pgReservePlan.setPlanName(planName);
                    pgReservePlan.setPlanType(planType);
                    pgReservePlan.setResUnit(resUnit);
                    if (pgAlarmType == null)
                        pgReservePlan.setPgAlarmTypeId(null);
                    else
                        pgReservePlan.setPgAlarmTypeId(pgAlarmType);

                    pgReservePlanManager.save(pgReservePlan);

                    result = "true";
                    msg = "编辑成功";
                }
            }
        } catch (Exception e) {

            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }

    //删除预案文档
    @POST
    @Path("delReservePlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> delReservePlan(@FormParam("id") String id)throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        String result="";

        try{

            PgReservePlan pgReservePlan = pgReservePlanManager.get(Long.valueOf(id));
            if(pgReservePlan==null)
            {
                result="false";
                msg="不存在该预案信息";
            }
            else{
                pgReservePlan.setActive(0);
                pgReservePlanManager.save(pgReservePlan);
                result="true";
                msg="删除成功";
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="预案删除失败";
            result="false";
        }

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

}