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.commons.database.RowMap;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.donenow_ivt.constant.IVTConstant;
import com.awspaas.user.apps.donenow_ivt.utils.BigDecimalUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class reserveFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "采购接收/取消接收保存前，校验库存";
    }

    /**
     *
     * @param processExecutionContext
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(ProcessExecutionContext processExecutionContext) throws Exception {
        UserContext uc = processExecutionContext.getUserContext();
        BO formData = processExecutionContext.getFormData();
        String boName = processExecutionContext.getParameterOfString("$BONAME");
        String bindId = processExecutionContext.getProcessInstance().getId();

        //采购接收主表保存
        if (IVTConstant.BO_EU_DNIVT_RECEIVE_MAIN.equals(boName)) {

            List<BO> list = SDK.getBOAPI().query(IVTConstant.BO_EU_DNIVT_RECEIVE_SUB).bindId(bindId).list();
            //序列号不能重复
            for (BO bo : list) {

                String BEN_RECEIVE = BigDecimalUtil.isNull(bo.getString("BEN_RECEIVE"));
                int intBenReceive = Integer.valueOf(BEN_RECEIVE).intValue();

                String sns = bo.getString("SN");
                if (StringUtils.isNotBlank(sns)) {
                    for (String sn : sns.split(",")) {
                        List<RowMap> exitsLots = DBSql.getMaps("select * from BO_EU_DNIVT_WAREHOUSE_PRODUCT_LOT WHERE FIND_IN_SET(?,SN)", new Object[]{sn});
                        if (exitsLots.size() > 0 && intBenReceive > 0) {
                            //已存在，不能再新增
                            throw new BPMNError("505", sn + "已存在此序列号，不能再新增！");
                        }

                        if (intBenReceive < 0) {
                            if (exitsLots.size() <= 0) throw new BPMNError("505", sn + "不存在此序列号，不能再删除！");
                            else if (exitsLots.size() > 0) {
                                boolean sameOrderProduct = false;
                                for (RowMap rowMap : exitsLots) {
                                    if (rowMap.getString("ORDER_PRODUCT_ID").equals(bo.getString("ORDER_PRODUCT_ID"))) {
                                        sameOrderProduct = true;
                                    }
                                }
                                if (!sameOrderProduct) {
                                    throw new BPMNError("505", sn + "不是此采购单接收的，不能在此采购单上取消！");
                                }
                            }
                        }
                    }
                }
            }
        }

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