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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.mvc.list.ListDataProvider;
import kd.bos.orm.query.QFilter;
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;


/**
 * Tyx 2025-06-19
 * 岗位信息维护取数器
 */
public class PositionDataProviderEx extends ListDataProvider {

    private static final Log log = LogFactory.getLog(PositionDataProviderEx.class);

    @Override
    public DynamicObjectCollection getData(int start, int limit) {
        DynamicObjectCollection rows = super.getData(start, limit);
        //为空直接返回
        if(rows.isEmpty())
            return rows;
        //获取所有Id
        List<Long> ids = rows.stream().map(dyx -> dyx.getLong("id")).collect(Collectors.toList());

        Map<Long, List<DynamicObject>> map = PositionUtils.getAllUPersonOfPosition("1", ids);

        Map staffMap = new HashMap();
        StaffResponse staffResponse = PositionUtils.getStaffByPosition(ids);
        if(staffResponse.isSuccess()) {
            staffMap = (Map) staffResponse.getData();
        }
        log.info("岗位数：" + ids.size());
        log.info("获取在岗人员信息成功：" + staffMap);
        for(DynamicObject position : rows) {
            int personCount = 0;
            int personCountPart = 0;
            int staffCount = 0;
            int lackCount = 0;
            Long positionId = position.getLong("id");
            if(map.keySet().contains(positionId)) {
                List<DynamicObject> personList = map.get(positionId);
                //在岗数：
                //获取在岗数，排除兼职
                List filterList = personList.stream().filter(dyx -> dyx.getString("postype.number").equals("1010_S")).collect(Collectors.toList());
                personCount = filterList.size();
                position.set("nckd_personcount", personCount);
                //获取兼职数
                List filterList1 = personList.stream().filter(dyx -> dyx.getString("postype.number").equals("1020_S")).collect(Collectors.toList());
                personCountPart = filterList1.size();
                position.set("nckd_personcountpart", personCountPart);
                //人员姓名拼接：
                String personName = personList.stream().map(dyx -> dyx.getString("postype.number").equals("1010_S") ? dyx.getString("person.name") : dyx.getString("person.name") + "(兼)")
                        .collect(Collectors.joining(", "));
                position.set("nckd_personname", personName);
            }
            //获取编制数
            if(staffMap.keySet().contains(positionId.toString())) {
                staffCount = (int) ((Map) staffMap.get(positionId.toString())).get("staffNum");
                position.set("nckd_staffcount", staffCount);
            }
            //缺岗数 = 编制数 - 在岗数
            position.set("nckd_lackcount", staffCount - personCount);
        }
        return rows;
    }





}
