package nckd.jxccl.sit.hcsi.opplugin.web.tp;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.sit.hcsi.business.importtaskguide.utils.ImportTaskUtils;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Tyx 2025-10-31
 * 外部数据中间表数据操作插件
 */
public class SinsurTempDataOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    private static HRBaseServiceHelper helper = new HRBaseServiceHelper("nckd_sinsurtempdata");

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        String key = e.getOperationKey();
        switch(key) {
            case "submit":
                doUpper(e);
                break;
            case "save":
                doUpper(e);
                break;
        }
    }

    /**
     * 对应列转换成大写
     * @param e
     */
    private void doUpper(BeginOperationTransactionArgs e) {
        DynamicObject[] bills = e.getDataEntities();
        for (DynamicObject bill : bills) {
            DynamicObjectCollection entryCols = bill.getDynamicObjectCollection("nckd_sinsurtplentry");
            for(DynamicObject entry : entryCols) {
                entry.set("nckd_matchcolumn", entry.getString("nckd_matchcolumn").toUpperCase());
            }
        }
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        String opKey = e.getOperationKey();
        switch(opKey) {
            case "delete":
                doAfterDelete(e);
                break;
        }
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
    }

    /**
     * 删除成功后反写任务的导入数量
     * @param e
     */
    public void doAfterDelete(EndOperationTransactionArgs e) {
        DynamicObject[] temporaryDatas = e.getDataEntities();
        Set<Long> importTaskIdSet = (Set) Arrays.stream(temporaryDatas).map((temporaryData) -> {
            return temporaryData.getLong("nckd_importtask.id");
        }).collect(Collectors.toSet());
        Iterator it = importTaskIdSet.iterator();

        while(it.hasNext()) {
            Long importTaskId = (Long)it.next();
            ImportTaskUtils.updateImportTaskStatus(importTaskId);
        }
    }
}