package com.engine.htxc.tycgsq.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;

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


/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-24 16:23
 * @Description:
 * @ModifyLog:
 **/
public class GetSyfwModelAjax {
    @GET
    @Path("/getSyfwModelAjax")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSyfwModelAjax(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<>();
        ResourceComInfo resourceInfo = null;
        try {
            resourceInfo = new ResourceComInfo();

        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        BaseBean bb = new BaseBean();
        DepartmentComInfo departInfo = new DepartmentComInfo();
        String sblb = Util.null2String( request.getParameter( "sblb" ) );//申报类别
//    String sblx = Util.null2String( request.getParameter( "sblx" ) );//申报类型
        String sblx = Util.null2String( request.getParameter( "sblb" ) );//申报类型
        String deptid = Util.null2String( request.getParameter( "szbm" ) );//部门ID
        int requestid = Util.getIntValue( request.getParameter( "requestid" ) );//部门ID

        JSONObject jsonData = new JSONObject();
        String sql = " select syfw,bjhqbm,fgldsf,qzsydx,qzsyry,zdfgld,kxhqbm from uf_tycgsqsyfw where  sblx='" + sblx + "'";
        rs.writeLog("sql:"+sql);
        rs.executeQuery( sql );
        bb.writeLog( sql );
        JSONArray jsonArray = new JSONArray();
        String fgldsf = "";
        String deptleaders = "";
        String bjhqbms = "" ;  //必经会签部门
        String kxhqbms = "" ;  // 可选会签部门
        String qzsydx = "" ;  //强制会签部门
        String qzsyryid = ""; //强制送阅人员
        if (rs.next()) {
            fgldsf = Util.null2String(rs.getString("fgldsf"));//分管领导审批
            bjhqbms = Util.null2String( rs.getString( "bjhqbm" ));//必经会签部门
            kxhqbms = Util.null2String( rs.getString( "kxhqbm" ) );//可选会签部门
            qzsydx = Util.null2String( rs.getString( "qzsydx" ) );//强制送阅部门
            qzsyryid = Util.null2String( rs.getString( "qzsyry" ) );//强制送阅人员
        }

        bb.writeLog("fgldsf:"+fgldsf);
        if (fgldsf.equals( "0" ))
        {
            deptleaders = getDeptleaders(deptid);
            bb.writeLog("通用审批deptleaders："+deptleaders);
            if(!"".equals(deptleaders)){
                bb.writeLog("通用审批deptleaders："+deptleaders);
                List<String> leadersArry = Arrays.asList(deptleaders.split(","));

                for(String leaders : leadersArry)
                {
                    String deptleadersname = resourceInfo.getLastname(leaders);//部门分管领导名称
                    bb.writeLog("通用审批deptleadersname："+deptleadersname);
                    JSONObject json = new JSONObject();
                    json.put( "id", leaders );
                    json.put( "name", deptleadersname );
                    jsonArray.add( json );
                }
            }
        }

        jsonData.put("ldspid", deptleaders );
        jsonData.put("ldsp", jsonArray);
        bb.writeLog("通用审批jsonArray："+jsonArray);

        // 会签部门
        String bjhqbmids = "" ;
        JSONArray bjhqbmArray = new JSONArray();
        if(!"".equals(bjhqbms)){
            List<String> arr = Arrays.asList(bjhqbms.split(","));
            for (String bjhqbm : arr) {
                if(!bjhqbm.equals(deptid))
                {
                    JSONObject json1 = new JSONObject();
                    String bjhqbmname = departInfo.getDepartmentName( bjhqbm );//必经会签部门名称
                    json1.put( "id", bjhqbm );
                    json1.put( "name", bjhqbmname );
                    bjhqbmArray.add( json1 );

                    bjhqbmids += bjhqbmids == "" ? bjhqbm : ","+bjhqbm ;
                }
            }
        }

        jsonData.put( "bjhqbmid", bjhqbmids );
        if(!"".equals(kxhqbms)){
            List<String> arr1 = Arrays.asList(kxhqbms.split(","));
            for (String kxhqbm : arr1) {
                if(!kxhqbm.equals(deptid)){
                    String kxhqbmname = departInfo.getDepartmentName( kxhqbm );//可选会签部门名称
                    JSONObject json11 = new JSONObject();
                    json11.put( "id", kxhqbm );
                    json11.put( "name", kxhqbmname );
                    bjhqbmArray.add( json11 );
                }
            }
        }
        jsonData.put( "kxhqbmid", kxhqbms );
        jsonData.put( "hqbm", bjhqbmArray );
        /***
         *   通知阅读对象汇总
         */
        String qzsydxids = "";
        List<String> qzsydxList = new ArrayList<String>();
        if(!"".equals(qzsydx))
        {
            String sql1 = " select id,lastname,departmentid from hrmresource where departmentid in (" + qzsydx + ") and status = 1 order by dsporder";
            rs1.executeQuery( sql1 );

            while (rs1.next()) {
                String id = Util.null2String( rs1.getString( "id" ) );
                qzsydxids += qzsydxids == "" ? id : "," + id;
                String departmentid =  Util.null2String(rs1.getString("departmentid"));
                if(qzsydxList !=null && qzsydxList.size() > 0){
                    if(!qzsydxList.contains(departmentid)){
                        qzsydxList.add(departmentid);
                    }
                }else{
                    qzsydxList.add(departmentid);
                }
            }
        }

        JSONArray replaceObject  = getReplaceDatas(sblx);
        jsonData.put( "qzsyryid", qzsyryid ); //部门里人员ID
        jsonData.put( "qzsydxid", qzsydxids ); // 人员汇总ID
        jsonData.put( "tzyddx", replaceObject );  //人员名称，部门展示

        String qzsyry = "";
        if(!"".equals(qzsyryid))
        {
            List<String> tzryArray = Arrays.asList(qzsyryid.split(","));
            if(tzryArray!=null && tzryArray.size() > 0){
                for (String tzryid : tzryArray) {
                    qzsyry += qzsyry == "" ? "hrm_"+tzryid : "," + "hrm_"+tzryid;
                }
            }
        }

        String qzsydxbm = "";
        if(qzsydxList !=null && qzsydxList.size() >0){
            for (String str : qzsydxList)
            {
                qzsydxbm += qzsydxbm == "" ? "dept_"+str : "," + "dept_"+str;
            }
        }

        jsonData.put( "qzsyrys", qzsyry ); //人员
        jsonData.put( "qzsydxs", qzsydxbm );  //部门

        bb.writeLog( "getSyfwModel_Ajax=================jsonData:" + jsonData.toJSONString() );
//        out.print( jsonData.toJSONString() );
        map.put("code",200);
        map.put("msg","SUCCESS");
        map.put("data",jsonData);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg",e.getMessage());
        }
        return  JSONObject.toJSONString(map);
    }
    public String getDeptleaders(String deptid)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        DepartmentComInfo departmentComInfo = null;
        try{
            departmentComInfo = new DepartmentComInfo();
        }catch (Exception e){
        }
        String deptleaders = "";
        rs.executeQuery( "select deptleaders from hrmdepartmentdefined where deptid='" + deptid + "'" );
        if (rs.next()) {
            deptleaders = Util.null2String( rs.getString( "deptleaders" ) );
        }
        bb.writeLog("deptleaders:"+deptleaders);
        return deptleaders;
    }

    /***
     *
     * @param sblx
     * @return
     */
    public JSONArray getReplaceDatas(String sblx)
    {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean bb = new BaseBean();
        JSONArray replaceDatas = new JSONArray();
        DepartmentComInfo  departInfo = null;
        try{
            departInfo = new DepartmentComInfo();

            String qzsydxs = "";
            String qzsyrys = "";
            String sql = " select qzsydx,qzsyry from uf_tycgsqsyfw where  sblx='" + sblx + "'";
            rs.executeQuery( sql );
            if (rs.next()) {
                qzsydxs = Util.null2String(rs.getString("qzsydx"));//强制送阅部门
                qzsyrys = Util.null2String(rs.getString("qzsyry"));//强制送阅人员
            }

            bb.writeLog("qzsydxs:"+qzsydxs);
            bb.writeLog("qzsyrys:"+qzsyrys);

            if (!"".equals(qzsydxs))
            {
                String[] children = new String[0];
                List<String> qzsydxList = Arrays.asList(qzsydxs.split(","));
                for(String qzsydx : qzsydxList)
                {
                    JSONObject deptJson = new JSONObject();
                    bb.writeLog("qzsydx:"+qzsydx);
                    String qzsydxname = departInfo.getDepartmentName( qzsydx );//强制送阅部门名称
                    String sql1 = " select h.id,h.lastname,h.workcode,h.pinyinlastname,h.subcompanyid1,t.supdepid from hrmresource h,hrmdepartment t where h.departmentid=t.id and h.departmentid= " + qzsydx + " and h.status = 1 order by dsporder";
                    bb.writeLog("sql1:"+sql1);

                    rs1.executeQuery( sql1 );
                    JSONArray users = new JSONArray();
                    String ids = "";
                    String subcompanyid1 = "";
                    String supdepid = "";
                    while (rs1.next()) {
                        String[] displayKeys = {"lastnamespan", "workcodespan", "departmentidspan", "jobtitlespan"};
                        String id = Util.null2String( rs1.getString( "id" ) );
                        String lastname = Util.null2String( rs1.getString( "lastname" ) );
                        String workcode = Util.null2String( rs1.getString( "workcode" ) );
                        subcompanyid1 = Util.null2String( rs1.getString( "subcompanyid1" ) );
                        String pinyinlastname = Util.null2String( rs1.getString( "pinyinlastname" ) );
                        supdepid = Util.null2String( rs1.getString( "supdepid" ) );
                        ids += ids == "" ? id+ "" : "," + id;
                        JSONObject userJson = new JSONObject();
                        userJson.put( "departmentid", qzsydx );
                        userJson.put( "departmentidspan", qzsydx );
                        userJson.put( "departmentname", qzsydx );
                        userJson.put( "displayKeys", displayKeys );
                        userJson.put( "icon", "/htoa/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                        userJson.put( "id", id );
                        userJson.put( "jobtitlename", getJob( id ) );
                        userJson.put( "jobtitlespan", getJob( id ) );
                        userJson.put( "lastname", lastname );
                        userJson.put( "lastnamespan", lastname );
                        userJson.put( "nodeid", "resource_" + id + "x" );
                        userJson.put( "pinyinlastname", pinyinlastname );
                        userJson.put( "subcompanyid", subcompanyid1 );
                        userJson.put( "subcompanyname", "华泰集团" );
                        userJson.put( "supsubcompanyid", "0" );
                        userJson.put( "supsubcompanyname", "" );
                        userJson.put( "type", "resource" );
                        userJson.put( "workcodespan", workcode );
                        users.add( userJson );
                    }
                    deptJson.put( "children", children );
                    deptJson.put( "crmNames", "" );
                    deptJson.put( "icon", "/images/treeimages/subCopany_Colse_wev8.gif" );
                    deptJson.put( "id", qzsydx );
                    deptJson.put( "isLeaf", false );
                    deptJson.put( "isParent", "true" );
                    deptJson.put( "lastname", qzsydxname );
                    deptJson.put( "name", qzsydxname );
                    deptJson.put( "nocheck", "Y" );
                    deptJson.put( "nodeid", "dept_" + qzsydx + "x" );
                    deptJson.put( "treeKey", "wea0-com_1x-subcom_" + subcompanyid1 + "x-dept_" + supdepid + "x-dept_" + qzsydx + "x" );
                    deptJson.put( "type", "dept" );
                    deptJson.put( "users", users );
                    replaceDatas.add( deptJson );
                }
            }

            if(!"".equals(qzsyrys))
            {
                String sql2 = " select id,lastname,workcode,departmentid,jobtitle,mobile,pinyinlastname,messagerurl,subcompanyid1 from hrmresource where id in (" + qzsyrys + ") and status = 1 order by dsporder";
                bb.writeLog("sql2:"+sql2);
                rs2.executeQuery( sql2 );
                while (rs2.next()) {
                    JSONObject hrmJson = new JSONObject();
                    String id = Util.null2String( rs2.getString( "id" ) );
                    String lastname = Util.null2String( rs2.getString( "lastname" ) );
                    String workcode = Util.null2String( rs2.getString( "workcode" ) );
                    String jobtitle = Util.null2String( rs2.getString( "jobtitle" ) );
                    String mobile = Util.null2String( rs2.getString( "mobile" ) );
                    String pinyinlastname = Util.null2String( rs2.getString( "pinyinlastname" ) );
                    String departmentid = Util.null2String( rs2.getString( "departmentid" ) );
                    String messagerurl = Util.null2String( rs2.getString( "messagerurl" ) );
                    String subcompanyid = Util.null2String( rs2.getString( "subcompanyid1" ) );
                    String departmentname = departInfo.getDepartmentName( departmentid );
                    hrmJson.put( "crmNames", "" + departmentname + "／华泰集团" );
                    hrmJson.put( "departmentid", departmentid );
                    hrmJson.put( "departmentidspan", departmentname );
                    hrmJson.put( "departmentname", departmentname );
                    hrmJson.put( "icon", "/htoa/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                    hrmJson.put( "id", id );
                    hrmJson.put( "img", "/htoa/messager/usericon/" + workcode + "/" + workcode + ".jpg" );
                    hrmJson.put( "jobtitle", jobtitle );
                    hrmJson.put( "jobtitlename", getJob( id ) );
                    hrmJson.put( "jobtitlespan", getJob( id ) );
                    hrmJson.put( "lastname", lastname );
                    hrmJson.put( "lastnamespan", lastname );
                    hrmJson.put( "messagerurl", messagerurl );
                    hrmJson.put( "mobile", mobile );
                    hrmJson.put( "name", lastname );
                    hrmJson.put( "nodeid", "resource_" + id + "x" );
                    hrmJson.put( "pinyinlastname", pinyinlastname );
                    hrmJson.put( "subcompanyid", subcompanyid );
                    hrmJson.put( "subcompanyid1", subcompanyid );
                    hrmJson.put( "subcompanyname", "华泰集团" );
                    hrmJson.put( "supsubcompanyid", "0" );
                    hrmJson.put( "supsubcompanyname", "" );
                    hrmJson.put( "type", "resource" );
                    hrmJson.put( "workcode", workcode );
                    hrmJson.put( "workcodespan", workcode );
                    replaceDatas.add( hrmJson );
                }
            }
        }catch (Exception e){
        }
        return replaceDatas;
    }

    /***
     *
     * @param id
     * @return
     */
    public static String getJob(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select s.jobtitlename from hrmresource h,hrmjobtitles s where h.jobtitle =s.id and h.id='" + id + "'" );
        String jobtitlename = "";
        if (rs.next()) {
            jobtitlename = Util.null2String( rs.getString( "jobtitlename" ) );
        }
        return jobtitlename;

    }

}
