package com.yem.wm.es.contactbook.op;

import com.yem.wm.es.contactbook.validator.ContactBookSubmitValidator;
import com.yem.wm.utils.YEM;
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.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description 提交时校验 对应发运明细是否有欠款
 * @Date 2024/1/25 10:33
 * @Created by ZZSLL
 */

public class ContactBookSubmitOp extends AbstractOperationServicePlugIn
{
    @Override
    public void onAddValidators(AddValidatorsEventArgs e)
    {
        super.onAddValidators(e);
//        e.addValidator(new ContactBookSubmitValidator());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] entities = e.getDataEntities();
        for (DynamicObject entity : entities) {
            entity = BusinessDataServiceHelper.loadSingle(entity.getPkValue(), entity.getDynamicObjectType().getName());
            updateShipping(entity, "B");
        }
    }

    public static void updateShipping(DynamicObject entity, String status) {
        String procbilltype = entity.getString("yem_procbilltype");
        if ("Z".equals(procbilltype)) {

            Set<String> set = new HashSet<>();
            DynamicObjectCollection collection = entity.getDynamicObjectCollection("yem_es_materialinfo");
            for (DynamicObject dynamicObject : collection) {
                String businesssort = dynamicObject.getString("yem_businesssort");
                if ("Z".equals(businesssort)) {
                    String shipmentnumbers = dynamicObject.getString("yem_shipmentnumbers_m");
                    set.add(shipmentnumbers);
                }
            }

            DynamicObject[] load = BusinessDataServiceHelper.load("yem_shippingdetails", "id, billno, yem_bookstatus_z", new QFilter[]{new QFilter("billno", "in", set)});
            for (DynamicObject ship : load) {
                ship.set("yem_bookstatus_z", status);
            }
            SaveServiceHelper.update(load);
        }
    }
}
