package com.engine.htsc.doc.util;

import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;

import java.util.*;

public class CommonUtil {

    //公告流程unnumber
    private final static List<String> UNNUMBER = Arrays.asList("zbggfb","tzgg");

    /**
     * 获取流程类型
     * @param requestid
     * @param tablename
     * @return type 0 公文 1 公告 2 公文中的公告
     */
    public String getWorkflowType(String requestid,String tablename,String unnumber) {
        new BaseBean().writeLog("公文type入参"+requestid+tablename+unnumber);
        RecordSet rs = new RecordSet();
        if (UNNUMBER.contains(unnumber)) {
            return "1";
        } else {
            String result = "0";
            switch (unnumber) {
                case "gsfw":
                    rs.executeQuery("select wz,lx from " + tablename + " where requestid = ?", requestid);
                    if (rs.next()) {
                        String wz = Util.null2String(rs.getString("wz"));
                        String lx = Util.null2String(rs.getString("lx"));
                        if (("-1".equals(wz) && ("10".equals(lx) || "11".equals(lx)))
                                || ("-8".equals(wz))) {
                            result = "2";
                        }
                    }
                    break;
                case "zhfw":
                    rs.executeQuery("select fzzdzh from " + tablename + " where requestid = ?", requestid);
                    if (rs.next()) {
                        String fzzdzh = Util.null2String(rs.getString("fzzdzh"));
                        if ("0".equals(fzzdzh)) {
                            result = "2";
                        }
                    }
                    break;

            }
            return result;
        }
    }

    /**
     * 获取送阅辅助字段值  readuserids（送阅人员工号，英文逗号分割）  readgroupids（送阅群组编号，英文逗号分割）
     * @param readerIds
     * @param readerDepIds
     * @param readerGroupIds
     * @param readerOrgIds
     * @return
     */
    public Map<String,String> getSendReadDatas(String readerIds,String readerDepIds,String readerOrgIds,String readerGroupIds){
        Map<String,String> datas = new HashMap<>();
        List<String> readuseridsList = new ArrayList<>(); //送阅人员工号
        List<String> readgroupidsList = new ArrayList<>(); //送阅群组编号
        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            if (StringUtils.isNotBlank(readerIds)) { //送阅人员
                Arrays.asList(readerIds.split(",")).forEach(userid -> {
                    readuseridsList.add(resourceComInfo.getWorkcode(userid));
                });
            }
            if (StringUtils.isNotBlank(readerDepIds)){ //送阅部门
                Arrays.asList(readerDepIds.split(",")).forEach(deptid -> {
                    readgroupidsList.add(departmentComInfo.getDepartmentCode(deptid));
                });
            }
            if (StringUtils.isNotBlank(readerOrgIds)){ //送阅机构
                Arrays.asList(readerOrgIds.split(",")).forEach(deptid -> {
                    readgroupidsList.add(departmentComInfo.getDepartmentCode(deptid));
                });
            }
            if (StringUtils.isNotBlank(readerGroupIds)) { //送阅群组
                Arrays.asList(readerGroupIds.split(",")).forEach(groupid -> {
                    readgroupidsList.add("Group_"+groupid);
                });
            }

        }catch (Exception e) {
            e.printStackTrace();
        }
        datas.put("readuserids",StringUtils.join(readuseridsList,","));
        datas.put("readgroupids",StringUtils.join(readgroupidsList,","));
        return datas;
    }
}
