package com.engine.htsc.zgsqs.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.wechat.util.Utils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName ZgsqsDao
 * @Author K1810015
 * @Date 2022/2/15 13:57
 * Version 1.0
 */
public class ZgsqsDao {



    public static List getOperator(Map<String,Object> map){

        String requestid = Util.null2String( map.get("requestid"));

        String unnumber =  Util.null2String(  map.get("unnumber") );

        String sql="select operator,agentorbyagentid from WORKFLOW_REQUESTLOG wr,workflow_nodebase wn where wr.requestid='"+requestid+"' and wr.NODEID=wn.id and wn.unnumber='"+unnumber+"'";

        List list = JAVATools.GetTableToListMap(sql,null);

        if(list == null || list.size() <= 0){
          return  null;
        }

        return  list;
    }









    public static Map<String,String> getLeadRange(Map<String,Object> map){
            String szbm=Util.null2String(map.get("szbm"));
            String szjg=Util.null2String(map.get("szjg"));
            String sqlx=Util.null2String(map.get("sqlx"));
            String value="";
            Map<String,String> codemap=new HashMap<>();
            if(StringUtils.isBlank(szbm) || StringUtils.isBlank(szjg)){
                return null;
            }
            String sql="select Ldmc,sqlx,dscbh from uf_zgsqsldspfw where 1=1 ";

            Map<String,String> wherejgmap=new HashMap<>(1);
            wherejgmap.put("zgsmc",szjg);
            Map<String,Object> jgretmap= JAVATools.GetTableToMap(sql,wherejgmap);
            if(jgretmap!=null && jgretmap.size()>=0){
                if(!StringUtils.isBlank(Utils.null2String(jgretmap.get("ldmc")))){
                    String sqlxvalue=Utils.null2String(jgretmap.get("sqlx"));
                        codemap.put("dscbh",getBossuserid(Utils.null2String(jgretmap.get("dscbh")),szjg).toJSONString());
                        codemap.put("ldmc",Utils.null2String(jgretmap.get("ldmc")));
                }
            }

        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("bmmc",szbm);
        Map<String,Object> retmap= JAVATools.GetTableToMap(sql,wheremap);
        if(retmap!=null && retmap.size()>0){
            if(!StringUtils.isBlank(Utils.null2String(retmap.get("ldmc")))){
                String sqlxvalue=Utils.null2String(retmap.get("sqlx"));
                if(StringUtils.isBlank(sqlxvalue)){
                    codemap.put("dscbh",getpublicleader(Utils.null2String(retmap.get("ldmc"))).toJSONString());
                     return codemap;
                }else if(sqlxvalue.equals(sqlx)){
                    codemap.put("dscbh",getpublicleader(Utils.null2String(retmap.get("ldmc"))).toJSONString());
                    return codemap;
                }
            }
        }
        /**默认领导 如果是资管、资金部门发起，则不需要默认领导 */
        String zgid=new PropBean().getPropName("zgsqs.zgsid");
        if(zgid.indexOf(szjg)>=0){
            codemap.put("dscbh",getpublicleader("").toJSONString());
        }
        return  codemap;
    }

    public static JSONObject getpublicleader(String ids){
        JSONObject jsonObject=new JSONObject();
        JSONArray jsonArray=new JSONArray();
        if(StringUtils.isBlank(ids))
        {
            jsonObject.put("value", "");
            jsonObject.put("specialobj", new JSONArray());
            return jsonObject;
        }
        List<String> userstr=new ArrayList<>();
        String sql="select  id,lastname from hrmresource where id in ("+ids+")";
        List<Map<String,Object>> list=  DBTools.getsqltolist(sql);
        if(list==null || list.size()<=0){
            jsonObject.put("value", "");
            jsonObject.put("specialobj", new JSONArray());
            return jsonObject;
        }
        list.forEach((item)->{
            String userid=Utils.null2String(item.get("id"));
            String lastname=Utils.null2String(item.get("lastname"));
            if(!StringUtils.isBlank(userid) && !StringUtils.isBlank(lastname)){
                userstr.add(userid);
                JSONObject userjob=new JSONObject();
                //获取公司名称
 //               String deptname=JAVATools.gethrmdeptjg(userid);
//                if(!StringUtils.isBlank(deptname)){
//                    lastname+="-"+deptname;
//                }
                userjob.put("id",userid);

                userjob.put("name",lastname);
                jsonArray.add(userjob);
            }
        });
        if(userstr.size()>0 && jsonArray.size()>0) {
            jsonObject.put("value", userstr.stream().collect(Collectors.joining(",")));
            jsonObject.put("specialobj", jsonArray);
        }else{
            jsonObject.put("value", "");
            jsonObject.put("specialobj", new JSONArray());
        }
        return jsonObject;
    }

    public static JSONObject getBossuserid(String jobcode,String deptid){
      JSONObject jsonObject=new JSONObject();
      JSONArray jsonArray=new JSONArray();
      List<String> userstr=new ArrayList<>();
        //如果是香港子公司
        String xgzgs=new PropBean().getPropName("zgsqs.xgfgsid");
        List<Map<String,Object>> list=new ArrayList<>();
        if(deptid.equals(xgzgs)){
            String sql="select  id,lastname from hrmresource where jobtitle   in ( " +
                    "select id from hrmjobtitles where jobtitlecode =? " +
                    " )";
            list=  DBTools.getsqltolist(sql,jobcode);
        }else{
            String sql="select  id,lastname from hrmresource where jobtitle   in ( " +
                    "select id from hrmjobtitles where jobtitlecode =? and  jobdepartmentid=? " +
                    " )";
            list=  DBTools.getsqltolist(sql,jobcode,deptid);
        }

       if(list==null || list.size()<=0){
           jsonObject.put("value", "");
           jsonObject.put("specialobj", new JSONArray());
           return jsonObject;
       }
       list.forEach((item)->{
           String userid=Utils.null2String(item.get("id"));
           String lastname=Utils.null2String(item.get("lastname"));
           if(!StringUtils.isBlank(userid) && !StringUtils.isBlank(lastname)){
               userstr.add(userid);
               JSONObject userjob=new JSONObject();
               userjob.put("id",userid);
               //获取公司名称
//               String deptname=JAVATools.gethrmdeptjg(userid);
//               if(!StringUtils.isBlank(deptname)){
//                   lastname+="-"+deptname;
//               }

               userjob.put("name",lastname);
               jsonArray.add(userjob);
           }
       });
       if(userstr.size()>0 && jsonArray.size()>0) {
           jsonObject.put("value", userstr.stream().collect(Collectors.joining(",")));
           jsonObject.put("specialobj", jsonArray);
       }else{
           jsonObject.put("value", "");
           jsonObject.put("specialobj", new JSONArray());
       }
        return jsonObject;
    }

    public static Set<String> getflowleaderids(Map<String, Object> map){

        PropBean pb=new PropBean();
        List<String> userlist=new ArrayList<>();
        Set<String> jobset=new HashSet<>(1);
        if(map==null ||map.size()<=0){
            return null;
        }
        String nunmber=Util.null2String(pb.getPropName("zgsqs.zgsqssp_gsldsp"));
        String requestid=Util.null2String( map.get("requestid"));
        if(StringUtils.isBlank(nunmber) || StringUtils.isBlank(requestid)){
            return null;
        }
        String sql = "select operator,agentorbyagentid from workflow_requestlog where logtype='0' and requestid = '"+requestid+"'  " +
                "  and nodeid in ( select id from workflow_nodebase where unnumber='"+nunmber+"')   order by  to_date((operatedate || ' ' || OPERATETIME),'yyyy-MM-dd hh24:mi:ss')  desc";
        List<Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list==null ||list.size()<=0){
            return null;
        }
        list.forEach((item)->{
            String agentorbyagentid = Util.null2String(item.get("agentorbyagentid"));
            if("-1".equals(agentorbyagentid)){//授权，获取原处理人
                String operator = Util.null2String(item.get("operator"));
                userlist.add(operator);
            }else{
                userlist.add(agentorbyagentid);
            }
        });
        if(userlist==null ||userlist.size()<=0){
            return null;
        }
       String userids= userlist.stream().map((item)->{return "\'"+item+"\'";}).collect(Collectors.joining(","));
       String usersql="select distinct jobtitle from hrmresource where id in ("+userids+")";
       List<Map<String,Object>> joblist= JAVATools.GetTableToListMap(usersql,null);
       joblist.forEach((item)->{
            String jobtitle= Utils.null2String(item.get("jobtitle"));
            if(!StringUtils.isBlank(jobtitle)){
                jobset.add(jobtitle);
            }
       });
        return jobset;
    }

    public static Set<String> getjobtitleid(String jobcodes){
        if(StringUtils.isBlank(jobcodes)){
            return null;
        }
        Arrays.stream(jobcodes.split(",")).map(name -> "'" + name + "'").collect(Collectors.joining(","));

        Set<String> set =new HashSet<>();
       String sql="select id from hrmjobtitles where jobtitlecode in( "+jobcodes+" ) " ;
       List <Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
       if(list==null || list.size()<0){
           return null;
       }
       list.forEach((item)->{
          String jobid= Utils.null2String(item.get("id"));
          if(!StringUtils.isBlank(jobid)){
              set.add(jobid);
          }
       });
       return set;
    }

    public static Map<String,Object> getdeptrange(Map<String,Object> map){
        String bmid=Util.null2String(map.get("bmid"));
        String jgid=Util.null2String(map.get("jgid"));
        String value="";
        Map<String,String> codemap=new HashMap<>();
        if(StringUtils.isBlank(bmid) || StringUtils.isBlank(jgid)){
            return null;
        }
        /*
        sql
        * */
        String sql="select hd.id as deptid,(select wm_concat_old(id) " +
                "  from (select t.* " +
                "          from HrmDepartment t " +
                "         start with t.id = hd.id " +
                "        connect by prior t.supdepid = t.id) " +
                " where SUPDEPID is null " +
                "    or SUPDEPID = 0) as jgid " +
                " from HrmDepartment hd where hd.id in ("+bmid+") ";
        List<Map<String,Object>> list=JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return null;
        }
        Map<String,Object> retmap=new HashMap<>();
        list.forEach((item)->{
          String jgiditem=  Utils.null2String(item.get("jgid"));
          String deptiditem=  Utils.null2String(item.get("deptid"));
          if(!StringUtils.isBlank(jgiditem) && !StringUtils.isBlank(deptiditem)){
              retmap.put(deptiditem,jgiditem);
          }

        });
        return retmap;
    }

}
