package nckd.jxccl.hr.psms.plugin.form.other.impt;

import com.google.common.collect.Lists;
import kd.bos.dataentity.OperateOption;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.formplugin.web.HRDataBaseList;
import kd.hr.impt.common.dto.ImportBillData;
import kd.hr.impt.common.dto.ImptPluginContext;
import kd.hr.impt.common.plugin.AfterLoadStartPageEventArgs;
import kd.hr.impt.common.plugin.BeforeCallOperationEventArgs;
import kd.hr.impt.common.plugin.BeforeShowTemplateSelectListEventArgs;
import kd.hr.impt.common.plugin.HRImportPlugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author W.Y.C
 * @version 1.0
 * @date 2025/12/25 21:55
 */
public class SeniorAppointMgmtImportPlugin extends HRDataBaseList implements HRImportPlugin {

    private static String SENIORAPPOINTMGMT_IMPT = "seniorappointmgmt_IMPT";
    public SeniorAppointMgmtImportPlugin() {
    }

    //kd.hrmp.hbpm.formplugin.web.impt.PositionReviseHRImportPlugin，校验例子
    //kd.hr.haos.formplugin.web.impo.OrgBatchHRImportPlugin，校验例子


    @Override
    public void afterLoadStartPage(AfterLoadStartPageEventArgs args) {
        // 启用串行模式：先校验所有数据，再统一入库
        // 开启之后如有校验失败，前端会提示：数据校验失败
        args.setSerialModel(true);
    }

    @Override
    public void beforeShowTemplateSelectList(BeforeShowTemplateSelectListEventArgs args) {
        List<QFilter> fileter = args.getqFilterList();
        if(fileter == null){
            fileter = Lists.newArrayList();
        }
        //模板列表只显示：新入职人员初定/在职人员初定导入模板
        fileter.add(new QFilter(FormConstant.NUMBER_KEY, QCP.in, new String[]{SENIORAPPOINTMGMT_IMPT}));
        args.setqFilterList(fileter);
    }

    @Override
    public void beforeCallOperation(BeforeCallOperationEventArgs args) {
        String mainEntityId = ((ImportBillData)args.getImportBillDatas().get(0)).getMainEntityId();
        OperateOption operateOption = (OperateOption)args.getOperateOptions().get(mainEntityId);
        Map<String, String> submitOPs = args.getSubmitOPs();
        String targetKey = PositionStructureConstant.NCKD_PERSONPOSFILE.toLowerCase();
        //删除默认save方法
        submitOPs.entrySet().removeIf(entry -> entry.getKey().toLowerCase().equals(targetKey));

        ImptPluginContext context = args.getContext();
        String tplNumber = context.getTplNumber();

        if(tplNumber.equalsIgnoreCase(SENIORAPPOINTMGMT_IMPT)){
            //调用高级人才聘任
            submitOPs.put(PositionStructureConstant.NCKD_PERSONPOSFILE.toLowerCase(),"newseniorappoint");
        }
    }
}
