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.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

public class costPickFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "合同产品拣货保存前，校验库存";
    }

    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);

        // 保存前的表单数据，注意：该参数针对不同场景获取内容会有所不同
        // 主表中的保存场景获取主表数据；普通子表页面的保存场景获取的是该条子表的数据；如果需要获得其他数据请使用BOQueryAPI获取
        BO formData = (BO) param.getParameter(ListenerConst.FORM_EVENT_PARAM_FORMDATA);

        String warehouseProductId = formData.getString("WAREHOUSE_PRODUCT_ID");
        String contractCostId = formData.getString("CONTRACT_COST_ID");
        int PICK_QUANTITY = ObjToInt(formData.get("PICK_QUANTITY"));//拣货数量
        //验库存
        //可用库存
        int warehouseProductAvailableQuantity = ObjToInt(DBSql.getString("SELECT SUM(AVAILABLE) FROM VIEW_EU_DNINVT_PRODUCT_INVENTORY where ID=?", new Object[]{warehouseProductId}));
        if (warehouseProductAvailableQuantity <= 0) throw new BPMNError("pickError1", "库存数量不足");

        if (PICK_QUANTITY > warehouseProductAvailableQuantity) throw new BPMNError("pickError1", "库存数量不足");

        int contractCostQuantity = ObjToInt(SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_COST", contractCostId, "QUANTITY"));//全部拣货数量
        //已拣货数量
        int contractProductPickQuantity = ObjToInt(DBSql.getString("SELECT SUM(QUANTITY) FROM BO_EU_DNCTT_CONTRACT_COST_PRODUCT WHERE CONTRACT_COST_ID=?  AND CLOSED=0", new Object[]{contractCostId}));
        if (contractProductPickQuantity + PICK_QUANTITY > contractCostQuantity)
            throw new BPMNError("pickError2", "拣货数量超出合同数量");

        String SERIAL_NUMBERS = formData.getString("SERIAL_NUMBERS");
        String PICKED_SNS = formData.getString("PICKED_SNS");
        int snCnt = 0;
        if (StringUtils.isNotBlank(SERIAL_NUMBERS)) {
            snCnt = SERIAL_NUMBERS.split(",").length;
        }
        if (StringUtils.isNotBlank(PICKED_SNS)) {
            snCnt = PICKED_SNS.split(",").length;
        }

        if (PICK_QUANTITY <= 0)
            throw new BPMNError("pickError3", "拣货数量不能小于0");

        if (snCnt > 0 && snCnt != PICK_QUANTITY) {
            //拣货数量和序列号个数不一致
            if (DBSql.getString("SELECT IS_SERIALIZED FROM BO_EU_DNIVT_PRODUCT WHERE ID=?", new Object[]{formData.getString("PRODUCT_ID")}).equals("1")) {
                throw new BPMNError("pickError4", "请填写序列号");
            }
        }
        //记录表单修改
        dn.recordFormChanges.execute(param);
        return true;
    }

    /**
     * 将Object转换为int
     * @param obj
     * @return
     */
    private int ObjToInt(Object obj) {
        if (obj == null)
            return 0;

        String objStr = obj.toString();
        if (objStr.equals(""))
            return 0;
        //如果有小数点，只取整数
        if (objStr.contains(".")) {
            objStr = objStr.substring(0, objStr.indexOf("."));
        }
        return Integer.parseInt(objStr);
    }
}
