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.ExecuteListener;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

public class costCanclePickFormAfterSave extends ExecuteListener {

    /**
     *取消拣货表单保存后
     * @param processExecutionContext
     * @throws Exception
     */
    @Override
    public void execute(ProcessExecutionContext processExecutionContext) throws Exception {
        BO canclePickBO = SDK.getBOAPI().getByProcess("BO_EU_DNCTT_CONTRACT_PRODUCT_CANCLEPICK", processExecutionContext.getProcessInstance().getId());
        int PICK_QUANTITY = ObjToInt(canclePickBO.get("PICK_QUANTITY"));//拣货数量

        Connection conn = null;
        try {
            conn = DBSql.open();
            conn.setAutoCommit(false);
            //获取成本产品
            BO costProductBO = SDK.getBOAPI().query("BO_EU_DNCTT_CONTRACT_COST_PRODUCT").connection(conn).detailById(canclePickBO.getString("COST_PRODUCT_ID"));

            int QUANTITY = ObjToInt(costProductBO.get("QUANTITY"));

            if (PICK_QUANTITY > QUANTITY) {
                throw new Exception("取消拣货数量不能大于已拣货数量");
            }
            if (QUANTITY - PICK_QUANTITY == 0) {
                costProductBO.set("QUANTITY", 0);
                costProductBO.set("CLOSED", 1);
                costProductBO.set("SN", "");
                //记录表单修改
                dn.recordFormChanges.record(processExecutionContext.getUserContext(), costProductBO, "取消拣货", conn);
                SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_COST_PRODUCT", costProductBO, conn);
            } else {
                costProductBO.set("QUANTITY", QUANTITY - PICK_QUANTITY);
                costProductBO.set("OLD_QUANTITY", costProductBO.get("QUANTITY"));//配送数量

                //记录表单修改
                dn.recordFormChanges.record(processExecutionContext.getUserContext(), costProductBO, "取消拣货", conn);

                //产品序列号更新
                if (StringUtils.isNotBlank(canclePickBO.getString("SERIAL_NUMBERS"))) {
                    String[] serialNumbers = canclePickBO.getString("SERIAL_NUMBERS").split(",");
                    String sns = costProductBO.getString("SN");//产品序列号
                    sns = "," + sns + ",";
                    for (int i = 0; i < serialNumbers.length; i++) {
                        sns = sns.replace("," + serialNumbers[i] + ",", ",");
                    }
                    costProductBO.set("SN", sns.substring(1, sns.length() - 1));
                }
                SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_COST_PRODUCT", costProductBO, conn);
            }

            //修改采购合同状态
            if (DBSql.getInt(conn, "SELECT DOES_NOT_REQUIRE_PROCUREMENT FROM BO_EU_DNIVT_PRODUCT WHERE ID=?", new Object[]{canclePickBO.getString("PRODUCT_ID")}) == 1) { //无需采购产品

                Map<String, Object> contractCostEdit = new HashMap<>();
                contractCostEdit.put("ID", canclePickBO.getString("CONTRACT_COST_ID"));
                contractCostEdit.put("STATUS_ID", 1234);//待采购/待拣货
                //记录表单修改
                dn.recordFormChanges.record(processExecutionContext.getUserContext(), "BO_EU_DNCTT_CONTRACT_COST", contractCostEdit, "修改采购合同状态", conn);

                DBSql.update(conn, "UPDATE BO_EU_DNCTT_CONTRACT_COST SET STATUS_ID=? WHERE ID=?", new Object[]{1233, canclePickBO.get("CONTRACT_COST_ID")});//1233

            } else {
                DBSql.update(conn, "UPDATE BO_EU_DNCTT_CONTRACT_COST SET STATUS_ID=? WHERE ID=?", new Object[]{1234, canclePickBO.get("CONTRACT_COST_ID")});//待采购/待拣货
            }

            conn.commit();
        } catch (Exception e) {
            conn.rollback();
            throw e;
        } finally {
            DBSql.close(conn);
        }


    }

    /**
     * 将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);
    }
}
