package com.engine.htxc.cgglpt.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htxc.cgglpt.service.CgglptService;
import com.engine.htxc.cgglpt.service.impl.CgglptServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;

public class CgglptAction {

    public CgglptService getCgglptService (){
        return ServiceUtil.getService(CgglptServiceImpl.class);
    }
    /**
     * 这个接口用来判断采购项目列表是否允许删除
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/allowDelete")
    @Produces({MediaType.TEXT_PLAIN})
    public String allowDelete(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try{
            String ids = request.getParameter("ids");
            boolean b = getCgglptService().allowDelete(ids);
            map.put("code",200);
            map.put("msg","success");
            map.put("data",b);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }

    /**
     * 这个接口用来废弃采购项目
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/scrapPurchase")
    @Produces({MediaType.TEXT_PLAIN})
    public String scrapPurchase(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try{
            String id = request.getParameter("id");
            Boolean aBoolean = getCgglptService().scrapPurchase(id);
            map.put("code",200);
            map.put("msg","success");
            map.put("data",aBoolean);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }

    /**
     * 这个接口用来恢复
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/recovery")
    @Produces({MediaType.TEXT_PLAIN})
    public String recovery(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try{
            String id = request.getParameter("id");
            getCgglptService().recovery(id);
            map.put("code",200);
            map.put("msg","success");
        }catch (Exception e){
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }
    /**
     * 通过建模id判断当前的采购项目是否存在在途流程
     */
    @GET
    @Path("/getWorkFlowByJmid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkFlowByJmid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try{
            String id = request.getParameter("id");
            boolean flag = getCgglptService().getWorkFlowByJmid(id);
            map.put("code",200);
            map.put("msg","success");
            map.put("data",flag);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }
    /**
     * 触发合同预设流程
     */
    @GET
    @Path("/triggerWorkflow")
    @Produces({MediaType.TEXT_PLAIN})
    public String triggerWorkflow(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try{
            String userId = request.getParameter("userId");
            String billId = request.getParameter("billId");
            String requestId = getCgglptService().triggerWorkflow(userId, billId);
            map.put("code",200);
            map.put("msg","success");
            map.put("data",requestId);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }
    /**
     * 这个接口使用分送出来的节点，更新采购项目明细表1
     */
    @GET
    @Path("/fsUpdateDt1")
    @Produces({MediaType.TEXT_PLAIN})
    public String fsUpdateDt1(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        try{
            String userId = request.getParameter("userId");
            String billId = request.getParameter("billId");
            String requestId = getCgglptService().triggerWorkflow(userId, billId);
            map.put("code",200);
            map.put("msg","success");
            map.put("data",requestId);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }
}
