package com.engine.htsc.itxqglpt.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.itxqglpt.service.itxqglptService;
import com.engine.htsc.itxqglpt.service.impl.itxqglptServiceImpl;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-08-19
 * @Description: IT需求管理平台流程
 * @ModifyLog:
 **/
public class itxqglptAction {

    public itxqglptService getService(HttpServletRequest request, HttpServletResponse response){
        return  ServiceUtil.getService(itxqglptServiceImpl.class);
    }

    @GET
    @Path("/checkJw")
    @Produces({MediaType.TEXT_PLAIN})
    public String checkJw(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            String requestid = Util.null2String(param.get("requestid"));
            apidatas = getService(request,response).checkJw(requestid);
            apidatas.put("api_status", "true");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", "false");
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @GET
    @Path("/getXxjsbInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getXxjsbInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            User user = HrmUserVarify.getUser( request, response );
            Map<String,Object> param = ParamUtil.request2Map(request);
            apidatas = getService(request,response).getXxjsbInfo(param);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @GET
    @Path("/getldsplist")
    @Produces({MediaType.TEXT_PLAIN})
    public String getldsplist(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            int requestid = Util.getIntValue(Util.null2String(request.getParameter("requestid")));    //
            int workflowid = Util.getIntValue(Util.null2String(request.getParameter("workflowid")));  //

            PropBean pb = new PropBean();
            String nodeid = pb.getNodeIdsByUnNumber("itxqglpt_gsld");

            List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
            String fieldvalue = "";
            RecordSet rs = new RecordSet();

            String sql =" select t.userid,h.lastname,k.departmentname \n" +
                    " from workflow_currentoperator t\n" +
                    " inner join hrmresource h on h.id = t.userid\n" +
                    " inner join hrmdepartment k on k.id = h.departmentid\n" +
                    " where t.requestid=" +requestid +
                    " and t.workflowid=" + workflowid+
                    " and t.nodeid in("+nodeid+") " +
                    " order by receivedate,receivetime";
            rs.execute(sql);
            while (rs.next()){
                String userid = Util.null2String(rs.getString("userid"));
                String lastname = Util.null2String(rs.getString("lastname"));
                String departmentname = Util.null2String(rs.getString("departmentname"));
                fieldvalue += fieldvalue == ""? userid : ","+userid ;
                Map<String,String> itemMap = new HashMap<String,String>();
                itemMap.put("id",userid);
                itemMap.put("name",lastname+"("+departmentname+")");
                dataList.add(itemMap);
            }
            apidatas.put("value",fieldvalue);
            apidatas.put("specialobj",dataList);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }



    @GET
    @Path("/getLsdpInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getLsdpInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            User user = HrmUserVarify.getUser( request, response );
            Map<String,Object> param = ParamUtil.request2Map(request);
            apidatas = getService(request,response).getLsdpInfo(param,user);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 查询会签节点第一操作人有没有江旺那个，或者江旺授权
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/checkHasJw")
    @Produces({MediaType.TEXT_PLAIN})
    public String checkHasJw(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            String requestid = Util.null2String(param.get("requestid"));
            String currentUserId = Util.null2String(param.get("currentUserId"));
            apidatas = getService(request,response).checkHasJw(requestid,currentUserId);
            apidatas.put("api_status", "true");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", "false");
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }



}
