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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author xwudd
 * @Description
 * @date 11:38
 **/
public class ExportInvoiceDelOP extends AbstractOperationServicePlugIn{
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_exportinvoice_c");
        fieldKeys.add("yem_sourcebillid_ec");
        fieldKeys.add("yem_sourcebillentryid");
        fieldKeys.add("yem_cusqty");
        fieldKeys.add("billno");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities){
            Set<Long> ids = new HashSet<>();
            DynamicObjectCollection exportInvoice_cs = dataEntitie.getDynamicObjectCollection("yem_exportinvoice_c");
            for(DynamicObject exportInvoice_c : exportInvoice_cs){
                long sourceBillId = exportInvoice_c.getLong("yem_sourcebillid_ec");
                ids.add(sourceBillId);
            }
            DynamicObject[] declareDocxs = BusinessDataServiceHelper.load("yem_es_declaredocx", "id,yem_es_declare_ec.yem_cusqty,yem_es_declare_ec.yem_billingnoticeqty",
                    new QFilter[]{new QFilter("id", QCP.in, ids)});
            for(int i = 0; i < exportInvoice_cs.size(); i++){//出口发票报关商品分录
                DynamicObject exportInvoice_c = exportInvoice_cs.get(i);
                long sourceBillEntryId = exportInvoice_c.getLong("yem_sourcebillentryid");
                BigDecimal sourceCusQty = exportInvoice_c.getBigDecimal("yem_cusqty");
                for(DynamicObject declareDocx : declareDocxs){//源单（出口报关单）
                    DynamicObjectCollection declare_ecs = declareDocx.getDynamicObjectCollection("yem_es_declare_ec");//源单报关商品分录
                    for(int j = 0; j < declare_ecs.size(); j++){
                        DynamicObject declare_ec = declare_ecs.get(j);
                        long declare_ecId = declare_ec.getLong("id");
                        if(sourceBillEntryId == declare_ecId){
                            BigDecimal billIngNoticeQty = declare_ec.getBigDecimal("yem_billingnoticeqty");
                            declare_ec.set("yem_billingnoticeqty",billIngNoticeQty.subtract(sourceCusQty));
                        }
                    }
                }
            }
            SaveServiceHelper.save(declareDocxs);
        }
    }
}
