package com.yem.wm.fm.lettercredit.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.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.List;

/**
 * @author xwudd
 * @date 11:42
 * @Description
 **/
public class LetterCreditAuditOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_fm_materialinfo");
        fieldKeys.add("yem_salecontract");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            DynamicObjectCollection materialInfos = dataEntitie.getDynamicObjectCollection("yem_fm_materialinfo");
            for(DynamicObject materialInfo : materialInfos){
                DynamicObject saleContract = materialInfo.getDynamicObject("yem_salecontract");
                if(saleContract != null){
                    saleContract = BusinessDataServiceHelper.loadSingle(saleContract.getPkValue(),saleContract.getDynamicObjectType().getName());
                    DynamicObjectCollection salesContracs = saleContract.getDynamicObjectCollection("yem_es_salescontrac_s");
                    for(DynamicObject salesContrac : salesContracs){
                        DynamicObject rapStyle = salesContrac.getDynamicObject("yem_rapstyle");
                        if(rapStyle != null){
                            String number = rapStyle.getString("number");
                            if("JSFS08".equals(number) || "L/C".equals(number)){
                                salesContrac.set("yem_lcnumber",dataEntitie.getString("billno"));
                            }
                        }
                    }
                    SaveServiceHelper.save(new DynamicObject[]{saleContract});
                }

            }
        }

    }
}
