Sfoglia il codice sorgente

Merge remote-tracking branch 'origin/master'

zhangyao 3 mesi fa
parent
commit
740d018806

+ 222 - 1
com.awspaas.user.apps.donenow_ivt/src/com/awspaas/user/apps/donenow_ivt/event/ivtOrderProcessAfterComplete.java

@@ -4,13 +4,14 @@ 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.bpmn.engine.model.run.delegate.ProcessInstance;
+import com.actionsoft.bpms.commons.database.RowMap;
 import com.actionsoft.bpms.server.UserContext;
 import com.actionsoft.bpms.util.DBSql;
 import com.actionsoft.sdk.local.SDK;
 import com.awspaas.user.apps.donenow_ivt.constant.IVTConstant;
 import org.apache.commons.lang3.StringUtils;
 
-import java.util.List;
+import java.util.*;
 
 public class ivtOrderProcessAfterComplete extends ExecuteListener {
     public String getDescription() {
@@ -33,6 +34,9 @@ public class ivtOrderProcessAfterComplete extends ExecuteListener {
         purchaseOrder.set("STATUS_ID", "2152");//采购中
         SDK.getBOAPI().update("BO_EU_DNIVT_ORDER", purchaseOrder);
 
+        String orderId = purchaseOrder.getId(); // 获取订单ID
+        System.out.println("采购订单审批完成,开始自动生成付款计划,订单ID:" + orderId);
+
         List<BO> orderProducts = SDK.getBOAPI().query("BO_EU_DNIVT_ORDER_PRODUCT").bindId(BINDID).list();
         for (BO orderProduct : orderProducts) {
 
@@ -86,5 +90,222 @@ public class ivtOrderProcessAfterComplete extends ExecuteListener {
 
         }
 
+        System.out.println("==== 开始执行自动生成付款计划逻辑 ====");
+        try {
+
+            List<Map<String, Object>> servicePlanData = queryPaymentPlan(uc, orderId);
+            boolean isServiceOrder = !servicePlanData.isEmpty();
+            System.out.println("==== 订单类型判断:" + (isServiceOrder ? "服务类" : "非服务类") + ",服务类计划数量:" + servicePlanData.size() + " ====");
+
+            List<Map<String, Object>> payAmounts = queryPayAmount(uc, orderId);
+            boolean hasValidPayment = payAmounts.stream()
+                    .anyMatch(amountMap -> {
+                        Double amount = (Double) amountMap.get("实际付款金额");
+                        return amount != null && amount > 0;
+                    });
+            System.out.println("==== 有效付款检查结果:" + (hasValidPayment ? "存在有效付款" : "无有效付款") + " ====");
+
+            if (hasValidPayment) {
+                System.out.println("==== 因存在有效付款,终止付款计划生成 ====");
+                return;
+            }
+
+            int deletedCount = deleteUnpaidPaymentPlan(uc, orderId);
+            System.out.println("==== 已删除旧未付款计划数量:" + deletedCount + " ====");
+
+            if (isServiceOrder) {
+                int createCount = 0;
+                for (Map<String, Object> plan : servicePlanData) {
+                    String planDate = (String) plan.get("计划付款时间");
+                    Double planAmount = (Double) plan.get("计划付款金额");
+                    if (StringUtils.isNotBlank(planDate) && planAmount != null && planAmount > 0) {
+                        createPaymentPlan(uc, orderId, planDate, planAmount);
+                        createCount++;
+                    }
+                }
+                System.out.println("==== 服务类付款计划创建完成,成功创建:" + createCount + "条 ====");
+            } else {
+                Map<String, Object> dateInfo = queryNonServiceOrderDate(uc, orderId);
+                Map<String, Object> priceInfo = queryNonServiceOrderPrice(uc, orderId);
+                System.out.println("==== 非服务类订单 - 日期信息:" + (dateInfo != null ? dateInfo : "无数据") + ",价格信息:" + (priceInfo != null ? priceInfo : "无数据") + " ====");
+
+                if (dateInfo != null && priceInfo != null) {
+                    String planDate = (String) dateInfo.get("EXPECTED_SHIP_DATE");
+                    Double planAmount = (Double) priceInfo.get("UNIT_COST_ADD_TAX");
+
+                    if (StringUtils.isNotBlank(planDate) && planAmount != null && planAmount > 0) {
+                        createPaymentPlan(uc, orderId, planDate, planAmount);
+                        System.out.println("==== 非服务类付款计划创建完成,日期:" + planDate + ",金额:" + planAmount + " ====");
+                    } else {
+                        throw new Exception("非服务类订单数据不完整:日期=" + planDate + ",金额=" + planAmount);
+                    }
+                } else {
+                    throw new Exception("未查询到非服务类订单的日期或价格信息");
+                }
+            }
+
+            System.out.println("==== 订单【" + orderId + "】付款计划自动生成流程全部完成 ====");
+        } catch (Exception e) {
+            System.err.println("==== 自动生成付款计划失败:" + e.getMessage() + " ====");
+            e.printStackTrace();
+        }
+
+    }
+
+    private List<Map<String, Object>> queryPaymentPlan(UserContext uc, String orderId) {
+        if (StringUtils.isBlank(orderId)) {
+            return Collections.emptyList();
+        }
+
+        String mainSql = "SELECT " +
+                "a.order_id 采购订单id, " +
+                "a.CONTRACT_SERVICE_ID 合同服务ID, " +
+                "b.PERIOD_END_DATE 计划付款时间, " +
+                "b.PERIOD_COST 计划付款金额 " +
+                "FROM `BO_EU_DNIVT_ORDER_PRODUCT` a " +
+                "JOIN `BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD` b " +
+                "ON a.CONTRACT_SERVICE_ID = b.CONTRACT_SERVICE_ID " +
+                "WHERE a.ORDER_ID = ?";
+        List<RowMap> mainRowMaps = DBSql.getMaps(mainSql, new Object[]{orderId});
+
+        List<Map<String, Object>> resultList = new ArrayList<>();
+        Set<String> processedServiceIds = new HashSet<>();
+
+        if (mainRowMaps != null && !mainRowMaps.isEmpty()) {
+            for (RowMap mainRow : mainRowMaps) {
+                Map<String, Object> plan = new HashMap<>();
+                plan.put("order_id", mainRow.getString("采购订单id"));
+                plan.put("计划付款时间", mainRow.getString("计划付款时间"));
+                plan.put("计划付款金额", mainRow.getDouble("计划付款金额"));
+                resultList.add(plan);
+
+                String contractServiceId = mainRow.getString("合同服务ID");
+                if (StringUtils.isNotBlank(contractServiceId) && !processedServiceIds.contains(contractServiceId)) {
+                    processedServiceIds.add(contractServiceId);
+
+                    String adjustSql = "SELECT " +
+                            "effective_date 计划付款时间, " +
+                            "prorated_cost_change 计划付款金额 " +
+                            "FROM `BO_EU_DNCTT_CONTRACT_SERVICE_ADJUST` " +
+                            "WHERE CAST(CONTRACT_SERVICE_ID AS CHAR) = ?";
+                    List<RowMap> adjustRowMaps = DBSql.getMaps(adjustSql, new Object[]{contractServiceId});
+
+                    if (adjustRowMaps != null && !adjustRowMaps.isEmpty()) {
+                        for (RowMap adjustRow : adjustRowMaps) {
+                            Map<String, Object> adjustPlan = new HashMap<>();
+                            adjustPlan.put("order_id", orderId);
+                            adjustPlan.put("计划付款时间", adjustRow.getString("计划付款时间"));
+                            adjustPlan.put("计划付款金额", adjustRow.getDouble("计划付款金额"));
+                            resultList.add(adjustPlan);
+                        }
+                    }
+                }
+            }
+        }
+        return resultList;
+    }
+
+    private List<Map<String, Object>> queryPayAmount(UserContext uc, String orderId) {
+        if (StringUtils.isBlank(orderId)) {
+            return Collections.emptyList();
+        }
+
+        String sql = "select " +
+                "ORDER_ID 采购订单id, " +
+                "PAY_AMOUNT 实际付款金额 " +
+                "from `BO_EU_DNIVT_ORDER_PAYMENT_PLAN` " +
+                "where ORDER_ID = ?";
+        List<RowMap> rowMaps = DBSql.getMaps(sql, new Object[]{orderId});
+
+        List<Map<String, Object>> resultList = new ArrayList<>();
+        if (rowMaps != null && !rowMaps.isEmpty()) {
+            for (RowMap row : rowMaps) {
+                Map<String, Object> amountInfo = new HashMap<>();
+                amountInfo.put("order_id", orderId);
+                amountInfo.put("实际付款金额", row.getDouble("实际付款金额"));
+                resultList.add(amountInfo);
+            }
+        }
+        return resultList;
+    }
+
+
+    private int deleteUnpaidPaymentPlan(UserContext uc, String orderId) {
+        if (StringUtils.isBlank(orderId)) {
+            return 0;
+        }
+
+        String sql = "delete from `BO_EU_DNIVT_ORDER_PAYMENT_PLAN` " +
+                "where ORDER_ID = ? " +
+                "and (PAY_AMOUNT is null or PAY_AMOUNT = 0)";
+        return DBSql.update(sql, new Object[]{orderId});
+    }
+
+
+    private void createPaymentPlan(UserContext uc, String orderId, String planDate, Double planAmount) throws Exception {
+        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(planDate) || planAmount == null) {
+            throw new Exception("创建付款计划参数不完整");
+        }
+
+        ProcessInstance processInstance = SDK.getProcessAPI()
+                .createProcessInstance("obj_5cb4ae4a42944fd0a9a284ff4c64c65d",
+                        uc.getUID(),
+                        "付款计划");
+
+        BO bo = new BO();
+        bo.setBindId(processInstance.getId());
+        bo.set("ORDER_ID", orderId);
+        bo.set("PLAN_DATE", planDate);
+        bo.set("PLAN_AMOUNT", planAmount);
+        SDK.getBOAPI().createDataBO("BO_EU_DNIVT_ORDER_PAYMENT_PLAN", bo, uc);
+    }
+
+
+    private Map<String, Object> queryNonServiceOrderDate(UserContext uc, String orderId) {
+        if (StringUtils.isBlank(orderId)) {
+            return null;
+        }
+
+        String sql = "select EXPECTED_SHIP_DATE " +
+                "from `BO_EU_DNIVT_ORDER` " +
+                "where id = ? ";
+        String expectedShipDate = DBSql.getString(sql, new Object[]{orderId});
+
+        if (StringUtils.isBlank(expectedShipDate)) {
+            return null;
+        }
+
+        Map<String, Object> dateInfo = new HashMap<>();
+        dateInfo.put("order_id", orderId);
+        dateInfo.put("EXPECTED_SHIP_DATE", expectedShipDate);
+        return dateInfo;
+    }
+
+
+    private Map<String, Object> queryNonServiceOrderPrice(UserContext uc, String orderId) {
+        if (StringUtils.isBlank(orderId)) {
+            return null;
+        }
+
+        String sql = "select UNIT_COST_ADD_TAX " +
+                "from `BO_EU_DNIVT_ORDER_PRODUCT` " +
+                "where order_id = ? ";
+
+        String priceStr = DBSql.getString(sql, new Object[]{orderId});
+        if (StringUtils.isBlank(priceStr)) {
+            return null;
+        }
+
+        try {
+            Double unitCostAddTax = Double.parseDouble(priceStr);
+            Map<String, Object> priceInfo = new HashMap<>();
+            priceInfo.put("order_id", orderId);
+            priceInfo.put("UNIT_COST_ADD_TAX", unitCostAddTax);
+            return priceInfo;
+        } catch (NumberFormatException e) {
+            return null;
+        }
+
+
     }
 }