package com.awspaas.user.apps.donenow_ctt.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.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.bpms.util.TypeUtil;
import com.actionsoft.exception.BPMNError;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class invoicePlanFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "开票申请表单保存前的事件测试";
    }

    /**
     *
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(ProcessExecutionContext param) throws Exception {
        BO formData = (BO) param.getParameter(ListenerConst.FORM_EVENT_PARAM_FORMDATA);
        String bindid = param.getProcessInstance().getId();
        String boName = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);
        if (boName.equals("BO_EU_DNCTT_INVOICE_PLAN")) {
            String INVOICE_NO = formData.getString("INVOICE_NO");//发票号
            //1、逗号、分号分割的多个发票号 例如 A01,A02,A03
            //2、使用-、~表示的发票号范围 例如 A05-A10,A15~A20
            if (StringUtils.isNotBlank(INVOICE_NO)) {
                List<String> invoiceNoList = new ArrayList<>();//发票号集合

            }

        } else if (boName.equals("BO_EU_DNCTT_INVOICE_PLAN_DETAIL")) {
            //验证发票金额
            List<BO> gridData = (List) param.getParameter(ListenerConst.FORM_EVENT_PARAM_GRIDDATA);

            Map<String, BigDecimal> invoiceAmountMap = new HashMap<>();
            Map<String, BigDecimal> maxInvoiceAmountMap = new HashMap<>();
            for (BO bo : gridData) {
                BigDecimal INVOICE_AMOUNT = bo.get("INVOICE_AMOUNT", BigDecimal.class);

                String INVOICE_DETAIL_ID = bo.getString("INVOICE_DETAIL_ID");

                if (invoiceAmountMap.containsKey(INVOICE_DETAIL_ID))
                    invoiceAmountMap.put(INVOICE_DETAIL_ID, invoiceAmountMap.get(INVOICE_DETAIL_ID).add(INVOICE_AMOUNT));
                else
                    invoiceAmountMap.put(INVOICE_DETAIL_ID, INVOICE_AMOUNT);

                if (!maxInvoiceAmountMap.containsKey(INVOICE_DETAIL_ID))
                    maxInvoiceAmountMap.put(INVOICE_DETAIL_ID, bo.get("DOLLARS", BigDecimal.class));

            }
            for (Map.Entry<String, BigDecimal> entry : invoiceAmountMap.entrySet()) {
                String INVOICE_DETAIL_ID = entry.getKey();
                BigDecimal INVOICE_AMOUNT = entry.getValue();

                BigDecimal exitsAmount = TypeUtil.convert(DBSql.getString("select IFNULL(sum(INVOICE_AMOUNT),0) from BO_EU_DNCTT_INVOICE_PLAN_DETAIL where BINDID<>? AND INVOICE_DETAIL_ID=?", new Object[]{bindid, INVOICE_DETAIL_ID}), BigDecimal.class);

                //相加
                BigDecimal totalAmount = exitsAmount.add(INVOICE_AMOUNT);
                if (totalAmount.compareTo(maxInvoiceAmountMap.get(INVOICE_DETAIL_ID)) > 0) {
                    throw new BPMNError("500", "发票金额超出最大金额限制");
                }
            }
        }

        return true;
    }
}
