package com.engine.htsc.zbzysx.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.collaboration.service.CoWorkService;
import com.engine.htsc.receipt.service.ReceiptAgentService;
import com.engine.htsc.zbzysx.service.SubImportantService;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubImportantAction {

    SubImportantService getService(){
        return ServiceUtil.getService(SubImportantService.class);
    }

    @POST
    @Path("/getMember")
    @Produces({MediaType.TEXT_PLAIN})
    public String getMember(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String deptid = request.getParameter("deptid");

            SubImportantService subImportantService = getService();
            apidatas = subImportantService.getMember(deptid);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getDefalutNotice")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDefalutNotice(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String sblb = request.getParameter("sblb");
            String sblx = request.getParameter("sblx");

            SubImportantService subImportantService = getService();
            JSONObject jsonObject = subImportantService.getDefaultNotice(sblb,sblx);
            return JSON.toJSONString(jsonObject);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            return JSON.toJSONString(apidatas);
        }
    }

    @POST
    @Path("/getNextValue")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNextValue(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String requestid = request.getParameter("requestid");

            SubImportantService subImportantService = getService();
            String result = subImportantService.getNextValue(requestid);
            apidatas.put("result",result);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    /**
     * 分支机构会签部门（命名格式 部门名称+部门职责）（copy）
     * @param request
     * @param response
     * @param formid
     * @param requestid
     * @param workflowid
     * @return
     */
    @GET
    @Path("/signDepartment")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> signDepartment(@Context HttpServletRequest request,
                                             @Context HttpServletResponse response,
                                             @QueryParam("formid") String formid,
                                             @QueryParam("requestid") Integer requestid,
                                             @QueryParam("workflowid") String workflowid) {
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService().signDepartment(formid,requestid,workflowid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }

    @POST
    @Path("/getSupdepid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSupdepid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String deptid = request.getParameter("deptid");

            SubImportantService subImportantService = getService();
            String result = subImportantService.getSupdepid(deptid);
            apidatas.put("result",result);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getSubordinateDeptid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSubordinateDeptid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String deptid = request.getParameter("deptid");

            SubImportantService subImportantService = getService();
            List<String> result = subImportantService.getSubordinateDeptid(deptid);
            apidatas.put("result",result);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String ssfgs = request.getParameter("ssfgs");
            String sblb = request.getParameter("sblb");
            String sblx = request.getParameter("sblx");

            SubImportantService subImportantService = getService();
            apidatas = subImportantService.getInfo(ssfgs,sblb,sblx);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getOptionRange")
    @Produces({MediaType.TEXT_PLAIN})
    public String getOptionRange(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String ywzgbm = request.getParameter("ywzgbm");

            SubImportantService subImportantService = getService();
            apidatas = subImportantService.getOptionRange(ywzgbm);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/getPassLeader")
    @Produces({MediaType.TEXT_PLAIN})
    public String getPassLeader(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String requestid = request.getParameter("requestid");

            SubImportantService subImportantService = getService();
            apidatas = subImportantService.getPassLeader(requestid);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }
}
