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.InterruptListener;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ivtOrderProcessBeforeDeleted extends InterruptListener {
    /**
     *采购订单删除后，删除关联的采购合同产品
     * @param ctx
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        String bindId = ctx.getProcessInstance().getId();
        List<BO> orderProducts = SDK.getBOAPI().query("BO_EU_DNIVT_ORDER_PRODUCT").addQuery("BINDID =", bindId).list();
        for (BO op : orderProducts) {
            String costProductID = DBSql.getString("SELECT ID FROM BO_EU_DNCTT_CONTRACT_COST_PRODUCT WHERE ORDER_ID=? AND CONTRACT_COST_ID=?", new Object[]{op.getString("ORDER_ID"), op.getString("CONTRACT_COST_ID")});
            if (StringUtils.isNotBlank(costProductID))
                DBSql.update("delete from BO_EU_DNCTT_CONTRACT_COST_PRODUCT where ID=?", new Object[]{costProductID});
        }

        return true;
    }
}
