package com.engine.htsc.HKContract.dao;

import com.engine.integration.util.StringUtils;
import weaver.general.Util;
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;

/**
 * 香港合同
 */
public class HKContractDao {

    /**
     * 查询审批领导id
     * @param map
     * @return
     */
    public static Set<String> getAppLeaderIdSet(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("hkcontract.xght_gsld"));
        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().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;
    }

    /**
     * 查询ceoid
     * @param jobcodes
     * @return
     */
    public static Set<String> getCeoIdSet(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 List getApprovedLeaders(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;
    }
}
