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.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);
        if(map.isEmpty())
            return rows;
        for(DynamicObject position : rows) {
            Long positionId = position.getLong("id");
            //在岗数：
            List<DynamicObject> personList = map.get(positionId);
            if(personList == null)
                continue;
            //获取在岗数，排除兼职
            List filterList = personList.stream().filter(dyx -> dyx.getString("postype.number").equals("1010_S")).collect(Collectors.toList());
            position.set("nckd_personcount", filterList.size());
            //人员姓名拼接：
            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);
        }

        return rows;
    }





}
