package nckd.jxccl.hr.gzbb;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.BasedataItem;
import kd.bos.entity.datamodel.events.QueryImportBasedataEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.BeforeExportEntryEvent;
import kd.bos.form.plugin.IExportEntryPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * @author duanyuqi
 * @date 2025-12-03
 * @desc
 * 央国企人事云》国资报表ext》单据》数据填报
 * 干预导出数据
 * nckd_sjtbrw
 */
public class ImportentryPlugin extends AbstractFormPlugin {


    @Override
    public void queryImportBasedata(QueryImportBasedataEventArgs e) {
        Map<BasedataItem, List<Object>> searchResult = e.getSearchResult();
        for (Map.Entry<BasedataItem, List<Object>> entry : searchResult.entrySet()) {
            BasedataItem basedataItem = entry.getKey();
            String fieldKey = basedataItem.getFieldKey();
            List<Object> basedata = entry.getValue();
            if (basedata.isEmpty()) { // 查不到基础资料数据
                if ("nckd_fyhdata".equals(fieldKey)) { // 企业负责人履职待遇、业务支出和社会保险统计表  人员
                    basedata.add(getUserDepempId(basedataItem));
                } else if ("nckd_dxmdata".equals(fieldKey)) {// 各级单位职工收入情况统计表  人员
                    basedata.add(getUserDepempId(basedataItem));
                }
            } else {
                // 查到多个基础资料数据
                if (basedata.size() > 1) {
                    // 业务场景，应根据需求剔除不合法的基础资料数据（最终保留一个）
                    List<Object> basedata_new = new ArrayList<Object>();
                    if ("nckd_fzzdata".equals(fieldKey)) { // 企业负责人履职待遇、业务支出和社会保险统计表  单位名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_gzzdata".equals(fieldKey)) { // 各级单位劳动情况统计表  单位名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_dzqydwdata".equals(fieldKey)) { // 各级单位职工收入情况统计表  子企业单位名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_dbmbadata".equals(fieldKey)) { // 各级单位职工收入情况统计表  部门
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_dgw".equals(fieldKey)) { // 各级单位职工收入情况统计表  岗位
                        basedata_new.add(getPositionhrId(basedataItem, basedata));
                    } else if ("nckd_czzdata".equals(fieldKey)) { // 各级单位人工成本统计表  子企业单位名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_bssjtdata".equals(fieldKey)) { // 各级单位参股企业信息统计表  所属集团注册名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_bcgqydata".equals(fieldKey)) { // 各级单位参股企业信息统计表  参股企业单位名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_bsssjqydata".equals(fieldKey)) { // 各级单位参股企业信息统计表  所属上级企业名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_ajtzcdata".equals(fieldKey)) { // 各级单位信息统计表  所属集团注册名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_azqydwdata".equals(fieldKey)) { // 各级单位信息统计表  子企业单位名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else if ("nckd_asjqydata".equals(fieldKey)) { // 各级单位信息统计表  所属上级企业名称
                        basedata_new.add(getOrgHrId(basedataItem, basedata));
                    } else {
                        basedata_new.add(basedata.get(0));
                    }
                    entry.setValue(basedata_new);
                }
            }
        }
    }

    public Object getUserDepempId(BasedataItem basedataItem) {
        Object userDepempId = 0L;
        String searchValue = basedataItem.getSearchValue();
        List<QFilter> qfilter = new ArrayList<QFilter>();
        qfilter.add(new QFilter("person.number", "=", searchValue));
        QFilter[] filtersQuery = (QFilter[]) qfilter.toArray(new QFilter[qfilter.size()]);
        DynamicObject dynamicObject = QueryServiceHelper.queryOne("hrpi_depemp", "id,person.id", filtersQuery);
        if (dynamicObject != null) {
            userDepempId = dynamicObject.getLong("id");
        }
        return userDepempId;
    }

    public Object getOrgHrId(BasedataItem basedataItem, List<Object> basedata) {
        Object orgHrId = 0L;
        Object[] basedataArray = basedata.toArray();
        List<QFilter> qfilter = new ArrayList<QFilter>();
        qfilter.add(new QFilter("id", "in", basedataArray));
        qfilter.add(new QFilter("iscurrentversion", "=", "1"));
        qfilter.add(new QFilter("enable", "=", "1"));
        QFilter[] filtersQuery = (QFilter[]) qfilter.toArray(new QFilter[qfilter.size()]);
        DynamicObject dynamicObject = QueryServiceHelper.queryOne("haos_adminorghr", "id,number,name", filtersQuery);
        if (dynamicObject != null) {
            orgHrId = dynamicObject.getLong("id");
        } else {
            orgHrId = basedata.get(0);
        }
        return orgHrId;
    }

    public Object getPositionhrId(BasedataItem basedataItem, List<Object> basedata) {
        Object positionhrId = 0L;
        Object[] basedataArray = basedata.toArray();
        List<QFilter> qfilter = new ArrayList<QFilter>();
        qfilter.add(new QFilter("id", "in", basedataArray));
        qfilter.add(new QFilter("iscurrentversion", "=", "1"));
        qfilter.add(new QFilter("datastatus", "=", "1"));
        QFilter[] filtersQuery = (QFilter[]) qfilter.toArray(new QFilter[qfilter.size()]);
        DynamicObject dynamicObject = QueryServiceHelper.queryOne("hbpm_positionhr", "id,number,name", filtersQuery);
        if (dynamicObject != null) {
            positionhrId = dynamicObject.getLong("id");
        } else {
            positionhrId = basedata.get(0);
        }
        return positionhrId;
    }

}
