package nckd.jimin.jyyy.fi.plugin.form.travelcheck;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.mvc.list.ListDataProvider;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import nckd.jimin.jyyy.fi.common.constant.BillTypeConstants;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.TravelFinanDraftConstant;

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

public class PersonPositionListProvider extends ListDataProvider {

    @Override
    public DynamicObjectCollection getData(int start, int limit) {
        DynamicObjectCollection data = super.getData(start, limit);

        List<String> applierNumberList = data.stream()
                .map(dyx -> dyx.getString("nckd_tripreqbill.applier.number"))
                .collect(Collectors.toList());

        DynamicObjectCollection queryUserCol = QueryServiceHelper.query("bos_user", "number,entryentity.orgstructure.fullname", new QFilter[]{
                new QFilter("number", QCP.in, applierNumberList),
                new QFilter("entryentity.ispartjob", QCP.equals, Boolean.FALSE),
        });
        // 查询人员及主岗部门映射
        Map<String, String> userDeptMap = queryUserCol.stream()
                .collect(Collectors.toMap(dyx -> dyx.getString("number"), dyx -> dyx.getString("entryentity.orgstructure.fullname"), (k1, k2) -> k1));


        // 查询当前生效的人员岗位信息
        Map<String, String> positionMap = QueryServiceHelper.query(BillTypeConstants.HRPI_EMPPOSORGREL, "person.number,position.name", new QFilter[]{
                new QFilter("businessstatus", QCP.equals, "1"),
                new QFilter("iscurrentversion", QCP.equals, "1"),
                new QFilter("datastatus", QCP.equals, "1"),
                new QFilter("isprimary", QCP.equals, "1"),
                new QFilter("person.number", QCP.in, applierNumberList)
        }).stream().collect(Collectors.toMap(r -> r.getString("person.number"), r -> r.getString("position.name"), (k1, k2) -> k1));

        // 查询当前生效的人员职级信息
        Map<String, String> jobleverMap = QueryServiceHelper.query(BillTypeConstants.HRPI_EMPJOBREL, "person.number,joblevel.name", new QFilter[]{
                new QFilter("businessstatus", QCP.equals, "1"),
                new QFilter("datastatus", QCP.equals, "1"),
                new QFilter("iscurrentversion", QCP.equals, "1"),
                new QFilter("person.number", QCP.in, applierNumberList)
        }).stream().collect(Collectors.toMap(r -> r.getString("person.number"), r -> r.getString("joblevel.name"), (k1, k2) -> k1));

        for(DynamicObject listRow : data){
            String applierName = listRow.getString("nckd_tripreqbill.applier.number");
            if(userDeptMap.containsKey(applierName)){
                listRow.set(TravelFinanDraftConstant.NCKD_DEPARTMENT, userDeptMap.get(applierName));
            }
            if(positionMap.containsKey(applierName)){
                listRow.set(TravelFinanDraftConstant.NCKD_POSITION, positionMap.get(applierName));
            }
            if(jobleverMap.containsKey(applierName)){
                listRow.set(TravelFinanDraftConstant.NCKD_JOBLEVER, jobleverMap.get(applierName));
            }
        }
        return data;
    }

}
