package nckd.jimin.jyyy.hr.hrmp.hbpm.opplugin.web.position;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.hr.haos.mservice.api.IHAOSStaffService;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.model.org.staff.StaffResponse;
import kd.hrmp.hrpi.business.infrastructure.utils.QFilterUtil;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PositionUtils {
    public static final HRBaseServiceHelper depempServiceHelper = new HRBaseServiceHelper("hrpi_depemp");

    /**
     * 获取岗位下人员
     * @param posType 1:所有类型
     * @param positionIds
     * @return Map key = 岗位Id, value = 企业人Dyn
     */
    public static Map<Long, List<DynamicObject>> getAllUPersonOfPosition(String posType, List<Long> positionIds) {
        Map<Long, List<DynamicObject>> map = new HashMap(16);
        Map<Long, DynamicObject> personMap = new HashMap(16);
        QFilter orgFilter = new QFilter("position.id", "in", positionIds);
        orgFilter.and(new QFilter("businessstatus", "=", "1"));
        if (!"1".equals(posType)) {
            orgFilter.and(new QFilter("postype.number", "=", posType));
        }
        String selectFields = "person.id,person.name,position.id,postype.number";
        DynamicObject[] dynamicObjects = depempServiceHelper.queryOriginalArray(selectFields
                ,new QFilter[]{orgFilter, QFilterUtil.getCurrentQf(), QFilterUtil.getDataStatusFilter(), QFilterUtil.getInitStatusFinish()});

        List<DynamicObject> list = Arrays.stream(dynamicObjects).collect(Collectors.toList());
        map = list.stream().collect(Collectors.groupingBy(dyx -> dyx.getLong("position.id")));
        return map;
    }

    /**
     * 获取职位的编制信息
     * @param positionIds
     */
    public static StaffResponse getStaffByPosition (List<Long> positionIds) {
        Object[] param = new Object[2];
        param[0] = new Date();
        param[1] = positionIds;
        StaffResponse<Map<String, Map<String, Object>>> staffResponse =  DispatchServiceHelper.invokeService("kd.hrmp.haos.servicehelper","haos","IHAOSStaffService","queryPositionStaffInfo",param);
        return staffResponse;
    }

}
