package com.engine.htsc.jgxt.dao;


import com.sap.db.jdbc.Hash;
import org.apache.commons.lang.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.text.MessageFormat;
import java.util.*;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName JgxtVDao.java
 * @createTime 2021年09月15日
 */
public class JgxtVDao {
    /**
     * @title getdeptvalue
     * @remark 获取部门编码和部门名称
     * @author xf
     * @Params：
       * @param: map
     * @updateTime 2021/9/15
     * @return: java.util.List
     */
    public static List getdeptvalue(Map<String,Object> map){
       String deptids= Util.null2String(map.get("deptids"));
       if("".equals(deptids)){
           return null;
       }
        String sql= "select hd.id,hd.departmentcode,hdf.depttypecode,hd.departmentname,(select wm_concat_old(t.departmentname) from hrmdepartment  t where t.id=hd.supdepid) as supdepname from hrmdepartment hd join  hrmdepartmentdefined hdf on hd.id=hdf.deptid " +
                "where 1=1 and hd.id in ("+deptids+") ";
        List list= DBTools.getsqltolist(sql);
        return list;
    }

    /**
     * @title getdtuserdis
     * @remark 获取明细表10的部门负责人
     * @author xf
     * @Params：
       * @param: requestids
     * @updateTime 2021/9/15
     * @return: java.util.List
     */
    public static List getdtuserdis(String requestids){
        List<String> liststr=new ArrayList<>();
        if("".equals(requestids)){
            return null;
        }
        String tablename=JAVATools.getMaintable(requestids);
        String sql= "select bmfzr from {0}_dt10 where mainid in (select id from {1} where requestid=?) and sfsdbm=1 ";
        String sqlvalue=MessageFormat.format(sql,tablename,tablename);
        List<Map<String,Object>> list= DBTools.getsqltolist(sqlvalue,requestids);
        list.stream().forEach( model->{
           String bmfzrstr=Util.null2String(model.get("bmfzr"));
           if(!"".equals(bmfzrstr)){
               Arrays.stream(bmfzrstr.split(",")).forEach((item)->{
                   liststr.add(item);
               });
           }

        });
        return liststr;
    }

    public static Map<String,Object>  getleaderuserMap(Map<String,Object> map){
        String reqid=Util.null2String(map.get("reqid"));
        String nodeunnumber=Util.null2String(map.get("nodeunnumber"));
        reqid=JAVATools.getpreventsql(reqid);
        nodeunnumber=JAVATools.getpreventsql(nodeunnumber);
        if("".equals(reqid) || "".equals(nodeunnumber)){
            return null;
        }
        String sql="select wm_concat_old(distinct userid) as counuserid from workflow_currentoperator"+
                " where  nodeid in (select id from workflow_nodebase where unnumber='"+nodeunnumber+"') "+
                " and requestid ='"+reqid+"' ";
      return  JAVATools.GetTableToMap(sql,null);
    }


    public static List getBusinessName(String id,String requestid){
        String value="";
        String sql="  select operatorids,id  from workflow_freenode  where  requestid =? and nodeorder in (     select nodeorder-1 from workflow_freenode where id=?)";
       Map<String,Object> map= DBTools.getsqltomap(sql,requestid,id);
       if(map==null|| map.size()<=0){
           return  null;
       }
       String operatorids=Util.null2String(map.get("operatorids"));
       String tableid=Util.null2String(map.get("id"));
        if(StringUtils.isBlank(tableid) || StringUtils.isBlank(operatorids)){
            return null;
        }
        List<Map<String,String>> list=new ArrayList<>();
        Arrays.stream(operatorids.split(",")).forEach((strvalue)->{
            Map<String,String> retmap=new HashMap<>(2);
            retmap.put("operatorids",strvalue);
            retmap.put("agentorbyagentid",agentorbyagentid(requestid,id,strvalue));
            list.add(retmap);
        });
       return list;
    }

    private static String agentorbyagentid(String requestid,String nodeid,String userid){
        if(StringUtils.isBlank(requestid) ||StringUtils.isBlank(nodeid) || StringUtils.isBlank(userid)){
            return "";
        }
        String sql="select agentorbyagentid from workflow_currentoperator where  userid in ("+userid+") ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("requestid",requestid);
        Map<String,Object> retumap=JAVATools.GetTableToMap(sql,wheremap);
        if(retumap==null || retumap.size()<=0){
            return "";
        }
        String agentorbyagentid=Util.null2String(retumap.get("agentorbyagentid"));
        if(StringUtils.isBlank(agentorbyagentid)){
            return "";
        }
        return agentorbyagentid;



    }
}
