package com.engine.htsc.itlxlc.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.itlxlc.service.ItlxlcService;
import com.engine.htsc.itlxlc.service.impl.ItlxlcServiceImpl;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
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;

public class ItlxlcAction {

    public ItlxlcService getService(HttpServletRequest request, HttpServletResponse response){
        User user = HrmUserVarify.getUser( request, response );
        return  (ItlxlcService) ServiceUtil.getService(ItlxlcServiceImpl.class,user);
    }

    @POST
    @Path("/createFreeNode")
    @Produces({MediaType.TEXT_PLAIN})
    public String createFreeNode(@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).createFreeNode(user,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("/getYxxgNode")
    @Produces({MediaType.TEXT_PLAIN})
    public String getYxxgNode(@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).getYsxgFreeNode(user,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);
    }


    @POST
    @Path("/getItlxProject")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getItlxProject(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        ResourceComInfo ResourceComInfo = null;
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            String userid = Util.null2String(param.get("usercode"));
            System.out.println("userid:"+userid);
            apidatas = getService(request,response).getItlxProject(param);

            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/getItlxProjectInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getItlxProjectInfo(@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).getItlxProjectInfo(user,param);
            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } 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(user,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("/getnbshInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getnbshInfo(@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).getnbshInfo(user,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("/getnodename")
    @Produces({MediaType.TEXT_PLAIN})
    public String getnodename(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            RecordSet rs = new RecordSet();
            Map<String,Object> param = ParamUtil.request2Map(request);
            String nodename = "" ;
            String nodeid = Util.null2String(param.get("nodeid"));
            if(!"".equals(nodeid)){
                String sql = " select nodename from workflow_freenode where id="+nodeid+"  ";
                rs.execute(sql);
                if(rs.next()){
                    nodename = Util.null2String(rs.getString("nodename"));
                }
            }
            PropBean pb = new PropBean();
            String ysxgbm_mc = pb.getPropName("ysxgbm_mc");
            if(!"".equals(nodename) && nodename.equals(ysxgbm_mc)){
                apidatas.put("flag", "1");
            }else{
                apidatas.put("flag", "0");
            }
            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("/getNbshAndGsld")
    @Produces({MediaType.TEXT_PLAIN})
    public String getNbshAndGsld(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            String ngbm = Util.null2String(request.getParameter("ngbm"));
            String fycdbms = Util.null2String(request.getParameter("fycdbms"));

            Map nbsh = new HashMap();
            RecordSet rs = new RecordSet();
            if(!"".equals(ngbm)){
                String sql = "select b.id,b.lastname from uf_ysgly a,hrmresource b " +
                        "where ','||a.ysgly||',' like  '%,'||b.id||',' and a.bm=? ";
                rs.executeQuery(sql,ngbm);
                nbsh = getHrmBrowserData(rs);
            }

            Map gsld = new HashMap();
            if(!"".equals(fycdbms)){
                String sql = "select distinct b.id,b.lastname from hrmdepartmentdefined a,hrmresource b \n" +
                        "where a.deptleaders=b.id and a.deptid in("+fycdbms+") ";
                rs.executeQuery(sql);
                gsld = getHrmBrowserData(rs);
            }

            apidatas.put("nbsh",nbsh);
            apidatas.put("gsld",gsld);
            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);
    }


    public Map getHrmBrowserData(RecordSet rs){
        Map dataMap = new HashMap();
        List hrmlist = new ArrayList<>();
        String hrmvalues = "";
        while(rs.next()){
            String id = Util.null2String(rs.getString("id"));
            String name = Util.null2String(rs.getString("lastname"));
            Map item = new HashMap();
            item.put("id",id);
            item.put("name",name);
            hrmlist.add(item);

            hrmvalues += id+",";
        }
        if(!"".equals(hrmvalues)){
            hrmvalues = hrmvalues.substring(0,hrmvalues.length()-1);
        }
        dataMap.put("value",hrmvalues);
        dataMap.put("specialobj",hrmlist);
        return dataMap;
    }


    @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("itlxsq_gsldsp");

            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);
    }


}
