package com.yem.wm.sys.opplugin.op;

import com.yem.wm.sys.utils.NewVersionHelper;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.entity.validate.ErrorLevel;
import kd.bos.exception.KDBizException;
import kd.bos.monitor.log.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.BFTrackerServiceHelper;

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

/**
 * @author zhouc
 * @date 2024/1/30 16:16
 * @className VerifyExistOrPush
 * @description 校验
 * 1.单据不存在下游单据时，不允许进行变更
 * 2.变更单未审核时，不允许下推单据
 */
public class VerifyExistOrPush extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
//        fieldKeys.add("yem_masterid");//主数据内码
//        fieldKeys.add("yem_version");//版本号
//        fieldKeys.add("yem_effective");//已生效
//        fieldKeys.add("billstatus");//单据状态
//        fieldKeys.add("yem_changing");//变更中
    }

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

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        String operationKey = e.getOperationKey();
        for (DynamicObject dataEntity : dataEntities) {
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            long id = dataEntity.getLong("id");
            String entityNumber = dataEntity.getDataEntityType().getName();
            DynamicObject tplObj = NewVersionHelper.queryTemplate(entityNumber);//查询是否配置变更功能
            if (tplObj == null) {
                return;
            }
            //单据不存在下游单据时，不允许进行变更
            if ("newversion".equals(operationKey)) {
                boolean push = BFTrackerServiceHelper.isPush(entityNumber, id);
                Map<String, HashSet<Long>> targetBills = BFTrackerServiceHelper.findTargetBills(entityNumber, new Long[]{id});
                Map<String, HashSet<Long>> sourceBills = BFTrackerServiceHelper.findSourceBills(entityNumber, new Long[]{id});
                if (targetBills.size() == 0) {
                    e.setCancel(true);
                    throw new KDBizException("当前单据没有下游单，不允许变更");
                }
            }
            //变更单未审核时，不允许下推单据
//            if ("push".equals(operationKey)) {
//                String yem_changebill = tplObj.getString("yem_changebill.number");
//                DynamicObject showBill = BusinessDataServiceHelper.loadSingle(yem_changebill, "id",
//                        new QFilter[]{new QFilter("yem_masterid", QCP.equals, dataEntity.getPkValue()),
//                                new QFilter("yem_effective", QCP.equals, false)});
//                if (showBill != null) {
//                    e.setCancel(true);
////                    throw new KDBizException("检测到有未审核的变更单，无法下推！");
//                }
//            }
        }
    }

}
