package com.awspaas.user.apps.donenow_ivt.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.sdk.local.SDK;

import java.util.List;

public class ivtOrderFormAfter extends ExecuteListener {
    public String getDescription() {
        return "采购订单表单保存后，数据处理事件";
    }

    /**
     *
     * @param processExecutionContext
     * @throws Exception
     */
    @Override
    public void execute(ProcessExecutionContext processExecutionContext) throws Exception {
        BO orderBO = SDK.getBOAPI().getByProcess("BO_EU_DNIVT_ORDER", processExecutionContext.getProcessInstance().getId());
        List<BO> orderProducts = SDK.getBOAPI().query("BO_EU_DNIVT_ORDER_PRODUCT").bindId(orderBO.getBindId()).list();
        List<BO> costProducts = SDK.getBOAPI().query("BO_EU_DNCTT_CONTRACT_COST_PRODUCT").addQuery("ORDER_ID=", orderBO.getId()).list();

        for (BO costProduct : costProducts) {

            BO orderProduct = orderProducts.stream().filter(op -> op.getString("CONTRACT_COST_ID").equals(costProduct.getString("CONTRACT_COST_ID"))).findFirst().orElse(null);
            if (orderProduct == null) {
                continue;
            }
            costProduct.set("WAREHOUSE_ID", orderProduct.get("WAREHOUSE_ID"));
            costProduct.set("QUANTITY", orderProduct.get("QUANTITY"));
            costProduct.set("UNIT_COST", orderProduct.get("UNIT_COST"));
            costProduct.set("QUANTITY", orderProduct.get("QUANTITY"));
            costProduct.set("OLD_QUANTITY", orderProduct.get("QUANTITY"));

            SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_COST_PRODUCT", costProduct);
        }


    }
}
