package nckd.jxccl.swc.hsas.opplugin.web;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.swc.hsas.business.utils.OutImportTaskUtils;

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

/**
 * 单据操作插件
 */
public class OutTempDataOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("nckd_importtask.id");
    }

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

    /**
     * 删除成功后反写任务的导入数量
     * @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();
            OutImportTaskUtils.updateImportTaskStatus(importTaskId);
        }
    }
}