package com.yem.tws.op;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.List;

/**
 * @author zhouc
 * @date 2023/7/5 15:11
 * @className RepairNoteSaveOp
 * @description 修撤单保存反写单一窗口报关单状态
 */
public class RepairNoteSaveOp  extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("yem_entryid");//报关单号
    }
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if ("save".equals(e.getOperationKey())) {
            DynamicObject[] bills = e.getDataEntities();
            for (int i = 0; i < bills.length; ++i) {
                DynamicObject bill = bills[i];
                String yem_entryid = bill.getString("yem_entryid");
                String yem_modcusno =  bill.getString("yem_modcusno");
                String  yem_decmodtypename = bill.getString("yem_decmodtypename");
                QFilter qFilter = new QFilter("billno", QCP.equals, yem_entryid);
                DynamicObject[] yem_ca_cusdec = BusinessDataServiceHelper.load("yem_ca_cusdec",
                        "id,billno,billstatus,yem_isforbidden", new QFilter[]{qFilter}, null);
                if(yem_ca_cusdec != null&&yem_ca_cusdec.length>0){
                    DynamicObject cusdec = yem_ca_cusdec[0];
                    cusdec.set("yem_isforbidden","A");
                    if (yem_decmodtypename.equals("修改申请")){
                        cusdec.set("billno",yem_modcusno);
                    }
                    //调用保存
                    OperationResult Operate = OperationServiceHelper.executeOperate("save",
                            cusdec.getDynamicObjectType().getName(),
                            new DynamicObject[]{cusdec}, OperateOption.create());
                }
            }
        }
    }
}
