package nckd.jxccl.hr.sdm.plugin.operate;

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.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.hr.sdm.common.SanDingConstant;
import nckd.jxccl.hr.sdm.common.SanDingPlanEntryStatus;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 更新三定计划操作插件
 * @entity: nckd_sandingtask
 * @operat: submit、unsubmit、audit
 * @author: jtd
 * @date: 2025/12/12 17:05
 */
public class UpdateSanDingPlanOpPlugin extends AbstractOperationServicePlugIn {

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

        e.getFieldKeys().addAll(billEntityType.getAllFields().keySet());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);

        String operationKey = e.getOperationKey();
        DynamicObject[] dyos = e.getDataEntities();
        List<Long> sanDingPlanEntryIdList = Arrays.stream(dyos).map(dyo -> dyo.getLong(String.join(".", SanDingConstant.NCKD_SDP_ENTRY_KEY, SanDingConstant.ID_KEY))).collect(Collectors.toList());
        DynamicObject[] sanDingPlanEntryDyos = HRBaseServiceHelper.create(SanDingConstant.NCKD_SANDINGPLAN_ENTRY_ENTITY).load(SanDingConstant.NCKD_STATUS_KEY, new QFilter[]{new QFilter(SanDingConstant.ID_KEY, QCP.in, sanDingPlanEntryIdList)});
        for (DynamicObject sanDingPlanEntryDyo : sanDingPlanEntryDyos) {
            switch (operationKey) {
                case SanDingConstant.SUBMIT_OP:
                    sanDingPlanEntryDyo.set(SanDingConstant.NCKD_STATUS_KEY, SanDingPlanEntryStatus.SUBMIT.getCode());
                    break;
                case SanDingConstant.UNSUBMIT_OP:
                    sanDingPlanEntryDyo.set(SanDingConstant.NCKD_STATUS_KEY, SanDingPlanEntryStatus.UNSUBMIT.getCode());
                    break;
                case SanDingConstant.AUDIT_OP:
                    sanDingPlanEntryDyo.set(SanDingConstant.NCKD_STATUS_KEY, SanDingPlanEntryStatus.COMPLETED.getCode());
            }
        }
        SaveServiceHelper.save(sanDingPlanEntryDyos);
        // 判断该计划下的所有任务是否都已完成
        if (HRStringUtils.equals(SanDingConstant.AUDIT_OP, operationKey)) {
            List<Long> sanDingPlanIdList = Arrays.stream(dyos).map(dyo -> dyo.getLong(String.join(".", SanDingConstant.NCKD_SANDINGPLAN_KEY, SanDingConstant.ID_KEY))).collect(Collectors.toList());
            // 过滤 三定计划ID 并且 单据体.状态<>已完成
            QFilter qFilter = new QFilter(SanDingConstant.ID_KEY, QCP.in, sanDingPlanIdList)
                    .and(String.join(".", SanDingConstant.NCKD_ENTRYENTITY, SanDingConstant.NCKD_STATUS_KEY), QCP.not_equals, SanDingPlanEntryStatus.COMPLETED.getCode());
            // 查询出三定计划中存在未完成三定任务的数据
            HRBaseServiceHelper sanDingPlanServiceHelper = HRBaseServiceHelper.create(SanDingConstant.NCKD_SANDINGPLAN_ENTITY);
            DynamicObject[] sanDingPlanUnComplateDyos = sanDingPlanServiceHelper.queryOriginalArray(SanDingConstant.ID_KEY, new QFilter[]{qFilter});
            List<Long> sanDingPlanUnComplateIdList = Arrays.stream(sanDingPlanUnComplateDyos).map(dyo -> dyo.getLong(SanDingConstant.ID_KEY)).collect(Collectors.toList());
            // 剔除
            sanDingPlanIdList.removeAll(sanDingPlanUnComplateIdList);
            if (sanDingPlanIdList.size() > 0) {
                DynamicObject[] sanDingPlanDyos = sanDingPlanServiceHelper.load(SanDingConstant.NCKD_ISCOMPLATED_KEY, new QFilter[]{new QFilter(SanDingConstant.ID_KEY, QCP.in, sanDingPlanIdList)});
                for (DynamicObject sanDingPlanDyo : sanDingPlanDyos) {
                    sanDingPlanDyo.set(SanDingConstant.NCKD_ISCOMPLATED_KEY, Boolean.TRUE);
                }
                SaveServiceHelper.save(sanDingPlanDyos);
            }
        }
    }

}
