package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgIntelPlan;
import com.casic.PgInterface.devTable.dto.PgIntelPlanDto;
import com.casic.PgInterface.devTable.dto.PgIntelPlanTypeDto;
import com.casic.PgInterface.devTable.manager.PgIntelPlanManager;
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;

@Component
@Path("pgIntelPlan")
public class PgIntelPlanRs {

    @Resource
    private PgIntelPlanManager pgIntelPlanManager;

    @POST
    @Path("addIntelPlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addIntelPlan(@FormParam("parName") String parName,
                                            @FormParam("lineName") String lineName,
                                            @FormParam("maxNum") String maxNum,
                                            @FormParam("existNum") String existNum) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";
        String result="";

        try {
            pgIntelPlanManager.savePgIntelPlan(parName,lineName,maxNum,existNum);
            msg="规划数据保存成功";
            result="true";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "规划数据保存失败";
            result="false";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", result);
        return resultMap;
    }

    @POST
    @Path("getIntelLineType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getIntelLineType() throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";

        List<PgIntelPlanTypeDto> pgIntelPlanTypeDtoList=new ArrayList<>();
        try {
            pgIntelPlanTypeDtoList=pgIntelPlanManager.getIntelPlanType();
            msg="获取排线类型成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "获取排线类型失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgIntelPlanTypeDtoList);
        return resultMap;
    }

    @POST
    @Path("getIntelPlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getIntelPlan(@FormParam("lineName") String lineName,
                                            @FormParam("partitionStr") String partitionStr) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";

        List<PgIntelPlanDto> pgIntelPlanDtoList = new ArrayList<>();

        try {
            pgIntelPlanDtoList = pgIntelPlanManager.getPgIntelPlanListByPartion(lineName, partitionStr);
            if(pgIntelPlanDtoList==null||pgIntelPlanDtoList.size()==0)
                msg="请输入正确的规划参数";
            else
                msg="规划数据获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "规划数据获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgIntelPlanDtoList);
        return resultMap;
    }

    @POST
    @Path("addLineUse")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addLineUse(@FormParam("lineName") String lineName,
                                          @FormParam("partitionStr") String partitionStr,
                                          @FormParam("num") String num) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";
        String result="";

        try {
            boolean bl=pgIntelPlanManager.addLineUse(lineName,partitionStr,num);
            if(bl){
                msg="排线入廊成功";
                result="true";
            }
            else {
                msg="排线入廊失败";
                result="false";
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = "排线入廊失败";
            result="false";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", result);
        return resultMap;
    }

    @POST
    @Path("removeIntelPlan")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> removeIntelPlan(@FormParam("parName") String parName,
                                               @FormParam("lineName") String lineName) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";
        String result="";

        try {
            PgIntelPlan pgIntelPlan=pgIntelPlanManager.getPgIntelPlanByParNameAndLine(lineName,parName);

            pgIntelPlan.setActive(false);
            pgIntelPlanManager.save(pgIntelPlan);
            msg="规划数据删除成功";
            result="true";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "规划数据删除失败";
            result="false";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", result);
        return resultMap;
    }

}