package com.engine.htsc.receipt.service;

import com.engine.core.impl.Service;
import org.apache.commons.lang3.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 ReceiptDirectorService extends Service {

    /**
     * 根据部门编号和安全等级获取人员
     * @param deptid
     * @param secLevelMix
     * @param secLevelMax
     * @return
     */
    public List<Map<String,Object>> getDirectorByDeptid(String deptid,int secLevelMix,int secLevelMax){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        List<Map<String,Object>> directorList = new ArrayList<>();

        String sql = "select id,lastname from HRMRESOURCE where departmentid = '"+deptid+"' and seclevel >= "+secLevelMix+" and seclevel <= "+secLevelMax;
        bs.writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            Map<String,Object> director = new HashMap<>();
            director.put("id", Util.null2String(rs.getString("id")));
            director.put("name", Util.null2String(rs.getString("lastname")));
            directorList.add(director);
        }
        return directorList;
    }

    /**
     * 根据userid和安全等级获取所在部门的人员
     * @param userid
     * @param secLevelMix
     * @param secLevelMax
     * @return
     */
    public List<Map<String,Object>> getDirectorByUserid(String userid,int secLevelMix,int secLevelMax){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        String deptid = "";
        String sql = "select departmentid from HRMRESOURCE where id = '"+userid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if (rs.next()){
            deptid =  Util.null2String(rs.getString("departmentid"));
        }
        return getDirectorByDeptid(deptid,secLevelMix,secLevelMax);
    }

    /**
     * 根据userids获取所在部门名称
     * @param userids
     * @return
     */
    public Map<String,Object> getDepts(String userids){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departInfo = new DepartmentComInfo();

            Set<String> deptSet = new LinkedHashSet<>();
            for(String userid:userids.split(",")){
                deptSet.add(resourceComInfo.getDepartmentID(userid));
            }
            List<Map<String,String>> deptMapList = new ArrayList<>();
            for(String deptid:deptSet){
                Map<String,String> deptMap = new HashMap<>();
                deptMap.put("id",deptid);
                deptMap.put("name",departInfo.getDepartmentmark(deptid));
                deptMapList.add(deptMap);
            }
            resultMap.put("dept",deptMapList);
            resultMap.put("deptids", StringUtils.join(deptSet, ","));
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }
}
