package nckd.jxccl.base.sit.helper;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SITHelper {

    /*党政职务*/
    public static final HRBaseServiceHelper PARTYPOSH_HELPER = new HRBaseServiceHelper("nckd_hrpi_partyposh");
    /*雇佣信息*/
    public static final HRBaseServiceHelper EMPENTREL_HELPER = new HRBaseServiceHelper("hsbs_empentrel");
    /*服务年限*/
    public static final HRBaseServiceHelper PERSERLEN_HELPER = new HRBaseServiceHelper("hsbs_perserlen");
    /*职称信息*/
    public static final HRBaseServiceHelper PERPROTITLE_HELPER = new HRBaseServiceHelper("hsbs_perprotitle");
    /**
     * 根据员工ID获取入职日期
     * 员工档案-雇佣信息-入职日期
     *
     * @param employeeIds
     * @return
     */
    public Map<Long, Date> getJoinDateByEmployeeID(List<Long> employeeIds) {
        QFilter filter = new QFilter("employee.id", QCP.in, employeeIds);
        filter.and("startdate", QCP.less_equals, new Date());
        filter.and("enddate", QCP.large_equals, new Date());
        filter.and("empstagelatestrecord", QCP.equals, "1");
        String selectFields = "employee.id, entrydate";
        DynamicObjectCollection cols = EMPENTREL_HELPER.queryOriginalCollection(selectFields, filter.toArray());
        return (Map<Long, Date>) cols.stream().filter(obj -> obj.getDate("entrydate") != null).collect(Collectors.toMap((obj) -> {
            return obj.getLong("employee.id");
        }, (obj) -> {
            return obj.getDate("entrydate");
        }, (k1, k2) -> {
            return k1;
        }));
    }

    /**
     * 根据员工ID获取离职日期
     * 员工档案-服务年限-离职日期
     *
     * @param employeeIds
     * @return
     */
    public Map<Long, Date> getQuitDateByEmployeeID(List<Long> employeeIds) {
        QFilter filter = new QFilter("employee.id", QCP.in, employeeIds);
        filter.and("iscurrentdata", QCP.equals, "1");
        String selectFields = "employee.id, nckd_departdate";
        DynamicObjectCollection cols = PERSERLEN_HELPER.queryOriginalCollection(selectFields, filter.toArray());
        return (Map<Long, Date>) cols.stream().filter(obj -> obj.getDate("nckd_departdate") != null).collect(Collectors.toMap((obj) -> {
            return obj.getLong("employee.id");
        }, (obj) -> {
            return obj.getDate("nckd_departdate");
        }, (k1, k2) -> {
            return k1;
        }));
    }

    /**
     * 根据员工ID获取职务层级
     * 员工档案-党政职务变更履历-职务层级
     * @param employeeIds
     * @return Map, k = employeeId, v = 职位层级名称
     */
    public Map<Long, String> getZwcjByEmployeeID(List<Long> employeeIds) {
        QFilter filter = new QFilter("employee.id", QCP.in, employeeIds);
        filter.and("startdate", QCP.less_equals, new Date());
        filter.and("enddate", QCP.large_equals, new Date());
        String selectFields = "employee.id, nckd_posgrade.name";
        DynamicObjectCollection cols = PARTYPOSH_HELPER.queryOriginalCollection(selectFields, filter.toArray());
        return (Map<Long, String>) cols.stream().collect(Collectors.toMap((obj) -> {
            return obj.getLong("employee.id");
        }, (obj) -> {
            return obj.getString("nckd_posgrade.name");
        }, (k1, k2) -> {
            return k1;
        }));
    }

    /**
     * 根据员工ID获取职称级别ID
     * 员工档案-职称信息-职称级别
     *
     * @param employeeIds
     * @return
     */
    public Map<Long, Long> getProtitleLevelIdByEmployeeID(List<Long> employeeIds) {
        QFilter filter = new QFilter("employee.id", QCP.in, employeeIds);
        filter.and("iscompany", QCP.equals, "1");
        String selectFields = "employee.id, prolevel.id";
        DynamicObjectCollection cols = PERPROTITLE_HELPER.queryOriginalCollection(selectFields, filter.toArray());
        return (Map<Long, Long>) cols.stream().collect(Collectors.toMap((obj) -> {
            return obj.getLong("employee.id");
        }, (obj) -> {
            return obj.getLong("prolevel.id");
        }, (k1, k2) -> {
            return k1;
        }));
    }

    /**
     * 根据员工ID获取职称级别
     * 员工档案-职称信息-职称级别
     *
     * @param employeeIds
     * @return
     */
    public Map<Long, DynamicObject> getProtitleLevelDynByEmployeeID(List<Long> employeeIds) {
        QFilter filter = new QFilter("employee.id", QCP.in, employeeIds);
        filter.and("iscompany", QCP.equals, "1");
        String selectFields = "employee.id, prolevel.name";
        DynamicObject[] cols = PERPROTITLE_HELPER.load(selectFields, filter.toArray());
        return (Map<Long, DynamicObject>) Arrays.stream(cols).collect(Collectors.toMap((obj) -> {
            return obj.getLong("employee.id");
        }, (obj) -> {
            return obj.getDynamicObject("prolevel");
        }, (k1, k2) -> {
            return k1;
        }));
    }

}
