package com.yem.wm.bc.bill.op;

import com.yem.wm.utils.YunzhijiaUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.yem.wm.im.orderproreqbill.from.OrderproreqbillEdit.fillWithCanUseNum;

/**
 * @author Eric
 * @ClassName InquirylistOp
 * @description: 询价单服务插件
 * @date 2023年07月25日
 * @version: 1.0
 */
public class YemBillSubmitToYZJOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("billno");
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        DynamicObject[] dataEntities1 = e.getDataEntities();
        List<DynamicObject> passDataEntitys = new ArrayList<>();
        for (DynamicObject dynamicObject : dataEntities1) {
            String name = dynamicObject.getDynamicObjectType().getName();
            dynamicObject = BusinessDataServiceHelper.loadSingle(dynamicObject.getPkValue(), name);

            if ("yem_es_specialapplica".equals(name)) {
                passDataEntitys.add(dynamicObject);
                continue;
            }

            String billtype = dynamicObject.getString("yem_billtype.number");
            if ("yem_orderproreqbill".equals(name) && "yem_orderproreqbill_DB".equals(billtype)) {
                fillWithCanUseNum(dynamicObject);
            }

            if (dynamicObject.containsProperty("yem_billtype")) {
                String typename = dynamicObject.getString("yem_billtype.name");
                Object billno = dynamicObject.get("billno");
                dynamicObject.set("yem_title", typename + "-" + billno);
                SaveServiceHelper.update(new DynamicObject[]{dynamicObject});
            }
            YunzhijiaUtils.submit2YZJ(this.operationResult,passDataEntitys, dynamicObject);
        }

        List<String> unpass = new ArrayList<>();

        Set<String> passDataEntitysBillNos = new HashSet<>();
        for (DynamicObject obj : passDataEntitys) {
            passDataEntitysBillNos.add(obj.getString("billno"));
        }

        for (DynamicObject obj : dataEntities1) {
            String billno = obj.getString("billno");
            if (!passDataEntitysBillNos.contains(billno)) {
                unpass.add(billno);
            }
        }

        if (!unpass.isEmpty()){
            e.setCancelOperation(true);
//            throw new KDBizException("单据" + unpass + "未通过校验，已取消后续操作");
        }
    }


    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        //获取按钮
        String key = e.getOperationKey();
        //定义通过校验实体
        List<ExtendedDataEntity> passDataEntitys = new ArrayList<>();

        /*if (key.equals("submit")) {
            for (ExtendedDataEntity dataEntity : e.getSelectedRows())
                YunzhijiaUtils.submit2YZJ(this.operationResult, passDataEntitys, dataEntity);
        }
        e.getSelectedRows().clear();
        if (passDataEntitys.isEmpty()) {
            e.cancel = true;     // 没有单据通过了校验，取消后续操作
        } else {
            e.getSelectedRows().addAll(passDataEntitys);
        }*/
    }

}
