package nckd.jxccl.base.common.plugins;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.form.IPageCache;
import kd.bos.form.events.BeforeCreateListDataProviderArgs;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.mvc.list.ListDataProvider;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 根据任职经历加载多级组织通用插件
 * 实体有任职经历需要在列表显示出1~6级组织
 * 列表需要有动态字段：nckd_firstorg、nckd_secondorg、nckd_thirdorg、nckd_fourthorg、nckd_fifthorg、nckd_sixthorg
 * @author W.Y.C
 * @date 2025/11/4 13:07
 * @version 1.0
 */
public class LoadMultiOrgByEmpPosOrgRelListPlugin extends AbstractListPlugin implements Plugin {

    Map<Long, DynamicObject> map = null;
    @Override
    public void packageData(PackageDataEvent e) {
        if(!adminOrgIds.isEmpty() && map == null) {
            QueryFieldBuilder queryFieldBuilder = QueryFieldBuilder.create()
                    .add(FormConstant.ID_KEY)
                    .addIdNumberName(FormConstant.NCKD_FIRSTORG)
                    .addIdNumberName(FormConstant.NCKD_SECONDORG)
                    .addIdNumberName(FormConstant.NCKD_THIRDORG)
                    .addIdNumberName(FormConstant.NCKD_FOURTHORG)
                    .addIdNumberName(FormConstant.NCKD_FIFTHORG)
                    .addIdNumberName(FormConstant.NCKD_SIXTHORG);
            QFilter qFilter = new QFilter(FormConstant.ID_KEY, QCP.in, adminOrgIds);
            DynamicObjectCollection adminOrgColl = QueryServiceHelper.query(FormConstant.ADMINORG_ENTITYID, queryFieldBuilder.buildSelect(), new QFilter[]{qFilter});
            map = adminOrgColl.stream()
                    .collect(Collectors.toMap(
                            obj -> obj.getLong(FormConstant.ID_KEY),
                            obj -> obj
                    ));
        }
        if(map != null) {
            String colKey = e.getColKey();
            if (e.getRowData().containsProperty(FormConstant.NCKD_EMPPOSORGREL)) {
                DynamicObject adminOrg = e.getRowData().getDynamicObject(FormConstant.NCKD_EMPPOSORGREL).getDynamicObject(FormConstant.ADMINORG);
                if (adminOrg != null) {
                    DynamicObject org = map.get(adminOrg.getLong(FormConstant.ID_KEY));
                    if (FormConstant.NCKD_FIRSTORG.equalsIgnoreCase(colKey)) {
                        e.setFormatValue(org.getString(String.join(".", FormConstant.NCKD_FIRSTORG, FormConstant.NAME_KEY)));
                    } else if (FormConstant.NCKD_SECONDORG.equalsIgnoreCase(colKey)) {
                        e.setFormatValue(org.getString(String.join(".", FormConstant.NCKD_SECONDORG, FormConstant.NAME_KEY)));
                    } else if (FormConstant.NCKD_THIRDORG.equalsIgnoreCase(colKey)) {
                        e.setFormatValue(org.getString(String.join(".", FormConstant.NCKD_THIRDORG, FormConstant.NAME_KEY)));
                    } else if (FormConstant.NCKD_FOURTHORG.equalsIgnoreCase(colKey)) {
                        e.setFormatValue(org.getString(String.join(".", FormConstant.NCKD_FOURTHORG, FormConstant.NAME_KEY)));
                    } else if (FormConstant.NCKD_FIFTHORG.equalsIgnoreCase(colKey)) {
                        e.setFormatValue(org.getString(String.join(".", FormConstant.NCKD_FIFTHORG, FormConstant.NAME_KEY)));
                    } else if (FormConstant.NCKD_SIXTHORG.equalsIgnoreCase(colKey)) {
                        e.setFormatValue(org.getString(String.join(".", FormConstant.NCKD_SIXTHORG, FormConstant.NAME_KEY)));
                    }
                }
            }
        }


    }

    Set<Long> adminOrgIds = new HashSet<>();
    @Override
    public void beforeCreateListDataProvider(BeforeCreateListDataProviderArgs args) {
        args.setListDataProvider(new ListDataProvider() {
            @Override
            public DynamicObjectCollection getData(int start, int size) {
                DynamicObjectCollection rows = super.getData(start, size);
                IPageCache pageCache = getView().getPageCache();
                for (DynamicObject row : rows) {
                    if(row.containsProperty(FormConstant.NCKD_EMPPOSORGREL)){
                        DynamicObject empPosOrgRel = row.getDynamicObject(FormConstant.NCKD_EMPPOSORGREL);
                        if(empPosOrgRel != null){
                            DynamicObject adminOrg = empPosOrgRel.getDynamicObject(FormConstant.ADMINORG);
                            if(adminOrg != null) {
                                adminOrgIds.add(adminOrg.getLong(FormConstant.ID_KEY));
                            }
                        }
                    }
                }
                return rows;
            }
        });
    }
}
