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 com.actionsoft.sdk.local.api.Logger;

import java.math.BigDecimal;
import java.util.List;

public class payRequestProcessAfterComplete extends ExecuteListener {
    public String getDescription() {
        return "流程结束事件：付款申请审批完成后触发";
    }

    private static final Logger logger = SDK.getLogAPI().getLogger(payRequestProcessAfterComplete.class);//记录日志
    private final String[] excludesArray = new String[]{"ID", "ORGID", "BINDID", "CREATEDATE", "CREATEUSER", "UPDATEDATE", "UPDATEUSER"};

    /**
     *
     * @param processExecutionContext
     * @throws Exception
     */
    @Override
    public void execute(ProcessExecutionContext processExecutionContext) throws Exception {
        String bindid = processExecutionContext.getProcessInstance().getId();

        logger.info("流程结束事件：付款申请审批完成后触发");

        BO main = SDK.getBOAPI().getByProcess("BO_EU_DNIVT_PAY_REQUEST", bindid);
        List<BO> details = SDK.getBOAPI().query("BO_EU_DNIVT_PAY_REQUEST_DETAIL").addQuery("BINDID =", bindid).list();

        if (details != null && details.size() > 0)
            for (BO detail : details) {
                String PAYMENT_PLAN_ID = detail.getString("PAYMENT_PLAN_ID");
                BigDecimal AMOUNT = detail.get("AMOUNT", BigDecimal.class);//时间付款金额

                BO paymentPlan = SDK.getBOAPI().get("BO_EU_DNIVT_ORDER_PAYMENT_PLAN", PAYMENT_PLAN_ID);

                BigDecimal PLAN_AMOUNT_OLD = paymentPlan.get("PLAN_AMOUNT", BigDecimal.class);
                BigDecimal PAY_AMOUNT = paymentPlan.get("PAY_AMOUNT", BigDecimal.class);//已付金额

                PAY_AMOUNT = PAY_AMOUNT.add(AMOUNT);

                BigDecimal REMAIN_AMOUNT = PLAN_AMOUNT_OLD.subtract(PAY_AMOUNT);
                paymentPlan.set("REMAIN_AMOUNT", REMAIN_AMOUNT);
                paymentPlan.set("PAY_AMOUNT", PAY_AMOUNT);
                //PAYER、PAY_DATE
                paymentPlan.set("PAYER", main.get("PAY_DATE"));
                paymentPlan.set("PAY_DATE", main.get("PAY_USERID"));

                SDK.getBOAPI().update("BO_EU_DNIVT_ORDER_PAYMENT_PLAN", paymentPlan);

            }

    }
}
