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 14:06
 * @Description
 **/
public class LetterCreditUnAuditOp extends AbstractOperationServicePlugIn {

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

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            String billno = dataEntitie.getString("billno");
            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)){
                                String lcNumber = salesContrac.getString("yem_lcnumber");
                                if(billno.equals(lcNumber)){
                                    salesContrac.set("yem_lcnumber",null);
                                }
                            }
                        }
                    }
                    SaveServiceHelper.save(new DynamicObject[]{saleContract});
                }

            }
        }
    }
}
