package com.engine.htsc.ReimbursementFlow.tool;

import com.engine.htsc.ReimbursementFlow.model.NewUser;
import com.engine.htsc.ReimbursementFlow.model.ReportUfZbtybxsp;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.*;

/**
 * 总部通用报销的工具类
 */
public class ZbspgzbTool {

    //根据userId获取角色深度，0:一级部门负责人， 1:一级部门普通员工， 2:二级部门负责人， 3:二级部门普通员工
    public static String getZbspgzbTool(String userId){
        String sql="select a.deptdepth,a.deptdirectors,h.SECLEVEL " +
                "from hrmdepartmentdefined a,hrmresource h where a.deptid = h.departmentid " +
                " and h.id = "+userId+"";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        Integer deptdepth = null;
        Integer secLevel = null;
        String deptdirectors = null;
        String result = "";
        if(rs.next()){
            deptdepth = Util.getIntValue(Util.null2String(rs.getString("deptdepth")));
            deptdirectors = Util.null2String(rs.getString("deptdirectors"));
            secLevel = Util.getIntValue(Util.null2String(rs.getString("SECLEVEL")));
            String[] deptdirectorsArray = deptdirectors.split(",");
            if (1==deptdepth.intValue()){
                if(secLevel==50&&Arrays.asList(deptdirectorsArray).contains(userId.toString())){
                    result = "ST_0";
                }else{
                    if(secLevel<50){
                        result = "ST_1";
                    }
                }
            }else if(2==deptdepth.intValue()){
                if(secLevel==50&&Arrays.asList(deptdirectorsArray).contains(userId.toString())){
                    result = "ST_2";
                }else{
                    if(secLevel<50) {
                        result = "ST_3";
                    }
                }
            }
        }
        return result;
    }

    //根据userId和所在部门获取角色深度，0:一级部门负责人， 1:一级部门普通员工， 2:二级部门负责人， 3:二级部门普通员工
    public static String getZbspgzbToolDept(String userId,String szbm){
        String sql="select a.deptdepth,a.deptdirectors,h.SECLEVEL " +
                "from hrmdepartmentdefined a,hrmresource h where a.deptid ="+szbm+
                " and h.id = "+userId+"";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        Integer deptdepth = null;
        Integer secLevel = null;
        String deptdirectors = null;
        String result = "";
        if(rs.next()){
            deptdepth = Util.getIntValue(Util.null2String(rs.getString("deptdepth")));
            deptdirectors = Util.null2String(rs.getString("deptdirectors"));
            secLevel = Util.getIntValue(Util.null2String(rs.getString("SECLEVEL")));
            String[] deptdirectorsArray = deptdirectors.split(",");
            if (1==deptdepth.intValue()){
                if(secLevel==50&&Arrays.asList(deptdirectorsArray).contains(userId.toString())){
                    result = "ST_0";
                }else{
                    if(secLevel<50){
                        result = "ST_1";
                    }
                }
            }else if(2==deptdepth.intValue()){
                if(secLevel==50&&Arrays.asList(deptdirectorsArray).contains(userId.toString())){
                    result = "ST_2";
                }else{
                    if(secLevel<50) {
                        result = "ST_3";
                    }
                }
            }
        }
        return result;
    }
    //获取报销人工号,安全级别,是否是部门负责人
    public static String getRoleCode(String userIds) {
        String roleCode = "";
        RecordSet rs = new RecordSet();
        if(null!=userIds&&!"".equals(userIds)){
            //报销人为公司领导 && 公司领导包含总裁 PT_LV1
            String sql = "select t1.*,t2.* from hrmresource t1,hrmdepartment t2 where t1.departmentid = t2.id " +
                    "and t2.departmentcode in ('ZZ001001','ZZ323471') and t1.workcode = '001008' and t1.id in (?) ";
            //报销人为公司领导 && 公司领导包含党委委员/副书记 PT_LV2
            String sql2 = "select t1.*,t2.* from hrmresource t1,hrmdepartment t2 where t1.departmentid = t2.id " +
                    "and t2.departmentcode = 'ZZ323470' and t1.workcode != '015150' and t1.id in (?) ";
            //报销人为公司领导 && 公司领导包含党委书记 PT_LV3
            String sql3 = " select t1.*,t2.* from hrmresource t1,hrmdepartment t2 where t1.departmentid = t2.id " +
                    "and t2.departmentcode = 'ZZ323470' and t1.workcode = '015150' and t1.id in (?) ";
            //报销人为公司领导 && 公司领导不包含总裁 PT_LV4
            String sql4 = "select t1.* from hrmresource t1 where t1.seclevel = '70' and t1.workcode ! = '001008' and  departmentid not in (select id from hrmdepartment where departmentcode ='ZZ323470') and t1.id in (?)";
            rs.executeQuery(sql,userIds);
            if(rs.getCounts()>0){
                roleCode = "PT_1";
            }else {
                rs.executeQuery(sql2,userIds);
                if (rs.getCounts()>0){
                    roleCode = "PT_2";
                }else {
                    rs.executeQuery(sql3,userIds);
                    if (rs.getCounts()>0){
                        roleCode = "PT_3";
                    }else {
                        rs.executeQuery(sql4,userIds);
                        if (rs.getCounts()>0){
                            roleCode = "PT_4";
                        }else {
                            //普通员工PT_LV5
                            roleCode = "PT_5";
                        }
                    }
                }
            }
        }
        return roleCode;
    }

    //像二级部负责人过滤拟稿人自己是负责人的情况 将这些重复的id进行过滤
    public static List<String> getRoleCode(List<ReportUfZbtybxsp> ReportList) {
        List<String> resultList = new ArrayList<>();
        if (null!=ReportList&&ReportList.size()>0){
            for (ReportUfZbtybxsp reportUfZbtybxsp : ReportList){
//                if (null!=reportUfZbtybxsp.getUfZbtybxsp().getId()
//                        &&!resultList.contains(reportUfZbtybxsp.getUfZbtybxsp().getJbbmfzr())
//                        &&!reportUfZbtybxsp.getUfZbtybxsp().getJbbmfzr().equals(reportUfZbtybxsp.getNewUser().getUserId())){
//                    resultList.add(reportUfZbtybxsp.getUfZbtybxsp().getJbbmfzr().toString());
//                }
            }
        }
        return resultList;
    }


}
