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.exception.BPMNError;
import org.apache.commons.lang3.StringUtils;

public class costProductFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "合同产品配送前，校验库存";
    }

    /**
     * 监听器执行方法
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(ProcessExecutionContext param) throws Exception {

        //记录ID
        String boId = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BOID);
        //表单ID
        String formId = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_FORMID);
        //BO表名 ，当表单中有主子表多个表时，该事件会执行多次，开发人员需要判断当前执行换取到的表名，进一步进行业务处理
        String boName = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);
        BO formData = (BO) param.getParameter(ListenerConst.FORM_EVENT_PARAM_FORMDATA);


        int QUANTITY = Integer.parseInt(formData.getString("QUANTITY"));//配送数量
        if (QUANTITY <= 0)
            throw new BPMNError("quantity", "配送数量必须大于等于0");
        String sn = formData.getString("SN");//序列号
        String shipSn = formData.getString("SHIPPING_SN");

        if (StringUtils.isNotBlank(sn) && StringUtils.isBlank(shipSn))
            throw new BPMNError("sn", "请选择序列号");

        if (StringUtils.isNotBlank(shipSn)) {
            if (shipSn.split(",").length != QUANTITY)
                throw new BPMNError("shipSn", "请选择正确的序列号");
        }

        //记录表单修改
        dn.recordFormChanges.execute(param);
        return true;
    }
}
