Browse Source

采购订单,待付款金额获取

HULEI 2 months ago
parent
commit
6b05a9fbb5

+ 122 - 0
com.awspaas.user.apps.donenow_ctt/src/com/awspaas/user/apps/donenow_ctt/controller/contractApproveController.java

@@ -1637,6 +1637,128 @@ public class contractApproveController {
         }
     }
 
+    @Mapping("com.awspaas.user.apps.donenow_ctt.updateInvoiceReceiveAmountByBindId")
+    public ResponseObject updateInvoiceReceiveAmountByBindId(UserContext uc,
+                                                             String bindid, // 前端传递:用于定位明细记录的bindid
+                                                             BigDecimal totalReceiveAmount // 前端传递:总收款金额(用于按比例分配)
+    ) throws SQLException {
+        Connection connUpdate = null;
+        try {
+            // 1. 参数校验
+            if (StringUtils.isBlank(bindid)) {
+                return ResponseObject.newErrResponse("bindid不能为空");
+            }
+            if (totalReceiveAmount == null || totalReceiveAmount.compareTo(BigDecimal.ZERO) <= 0) {
+                return ResponseObject.newErrResponse("总收款金额不能为空且需大于0");
+            }
+
+            // 2. 查询bindid关联的明细记录(核心修改:查询字段改为INVOICE_AMOUNT)
+            String querySql = "SELECT ID, INVOICE_AMOUNT FROM BO_EU_DNCTT_INVOICE_PLAN_DETAIL WHERE BINDID = ?";
+            List<Object> queryParams = new ArrayList<>();
+            queryParams.add(bindid.trim());
+            List<RowMap> detailList = DBSql.getMaps(querySql, queryParams.toArray());
+
+            // 3. 预处理:统计总发票金额 + 校验数据(变量名同步改为发票金额相关)
+            Map<String, BigDecimal> idInvoiceAmountMap = new HashMap<>(); // 原idBaseAmountMap → 改为发票金额映射
+            BigDecimal totalInvoiceAmount = BigDecimal.ZERO; // 原totalBaseAmount → 改为总发票金额(比例计算基数)
+            for (RowMap row : detailList) {
+                String id = row.getString("ID");
+                // 处理发票金额:从INVOICE_AMOUNT字段获取(核心修改)
+                String invoiceAmountStr = row.getString("INVOICE_AMOUNT");
+                BigDecimal invoiceAmount = null;
+                if (invoiceAmountStr != null && !invoiceAmountStr.trim().isEmpty()) {
+                    try {
+                        invoiceAmount = new BigDecimal(invoiceAmountStr.trim());
+                    } catch (NumberFormatException e) {
+                        System.out.println("发票金额格式错误,ID:" + id + ",值:" + invoiceAmountStr); // 日志同步修改
+                        continue;
+                    }
+                }
+                if (invoiceAmount == null || invoiceAmount.compareTo(BigDecimal.ZERO) <= 0) {
+                    System.out.println("发票金额非法(空或<=0),ID:" + id); // 日志同步修改
+                    continue;
+                }
+                idInvoiceAmountMap.put(id, invoiceAmount);
+                totalInvoiceAmount = totalInvoiceAmount.add(invoiceAmount);
+            }
+
+            // 校验总发票金额(避免除零,提示信息同步修改)
+            if (totalInvoiceAmount.compareTo(BigDecimal.ZERO) <= 0) {
+                Map<String, Object> result = new HashMap<>();
+                result.put("totalMatched", detailList.size());
+                result.put("updateCount", 0);
+                result.put("message", "bindid关联明细的总发票金额为0,无法计算比例"); // 提示信息修改
+                ResponseObject responseObject = ResponseObject.newOkResponse();
+                responseObject.setData(result);
+                return responseObject;
+            }
+
+            // 4. 初始化事务连接
+            connUpdate = DBSql.open();
+            connUpdate.setAutoCommit(false);
+
+            int updateCount = 0;
+
+            // 5. 循环按比例分配并更新 RECEIVE_AMOUNT 字段(使用总发票金额计算比例)
+            for (String id : idInvoiceAmountMap.keySet()) {
+                // 按比例计算:使用发票金额作为基数(变量名同步修改)
+                BigDecimal invoiceAmount = idInvoiceAmountMap.get(id);
+                BigDecimal ratio = invoiceAmount.divide(totalInvoiceAmount, 6, BigDecimal.ROUND_HALF_UP);
+                BigDecimal allocatedReceiveAmount = totalReceiveAmount.multiply(ratio).setScale(2, BigDecimal.ROUND_HALF_UP);
+
+                // 构建更新SQL(仅更新RECEIVE_AMOUNT)
+                StringBuilder updateSql = new StringBuilder("UPDATE BO_EU_DNCTT_INVOICE_PLAN_DETAIL SET ");
+                List<Object> updateParams = new ArrayList<>();
+
+                updateSql.append("RECEIVE_AMOUNT = ?, ");
+                updateParams.add(allocatedReceiveAmount);
+
+                // 移除多余逗号
+                if (updateSql.charAt(updateSql.length() - 2) == ',') {
+                    updateSql.setLength(updateSql.length() - 2);
+                }
+                updateSql.append(" WHERE ID = ?");
+                updateParams.add(id.trim());
+
+                // 执行更新
+                int affectedRows = DBSql.update(connUpdate, updateSql.toString(), updateParams.toArray());
+                if (affectedRows > 0) {
+                    updateCount++;
+                }
+            }
+
+            // 6. 提交事务
+            connUpdate.commit();
+
+            // 7. 返回结果(字段同步改为总发票金额)
+            Map<String, Object> result = new HashMap<>();
+            result.put("totalMatched", detailList.size());
+            result.put("updateCount", updateCount);
+            result.put("totalReceiveAmount", totalReceiveAmount);
+            result.put("totalInvoiceAmount", totalInvoiceAmount); // 原totalBaseAmount → 改为totalInvoiceAmount
+            result.put("message", "按比例更新RECEIVE_AMOUNT完成,共更新" + updateCount + "条明细记录");
+
+            ResponseObject responseObject = ResponseObject.newOkResponse();
+            responseObject.setData(result);
+            return responseObject;
+
+        } catch (SQLException e) {
+            if (connUpdate != null && !connUpdate.isClosed()) {
+                try {
+                    connUpdate.rollback();
+                } catch (SQLException ex) {
+                    ex.printStackTrace();
+                }
+            }
+            e.printStackTrace();
+            return ResponseObject.newErrResponse("更新RECEIVE_AMOUNT失败:" + e.getMessage());
+        } finally {
+            if (connUpdate != null && !connUpdate.isClosed()) {
+                DBSql.close(connUpdate);
+            }
+        }
+    }
+
     /**
      * 佣金审批
      * @param uc

+ 20 - 12
com.awspaas.user.apps.donenow_ivt/src/com/awspaas/user/apps/donenow_ivt/controller/caiController.java

@@ -22,27 +22,35 @@ public class caiController {
 
     @Mapping(value = "com.awspaas.user.apps.donenow_ivt.queryCai")
     public ResponseObject queryOrderByOrderId(UserContext uc, String orderId) {
-
         if (StringUtils.isBlank(orderId)) {
             return ResponseObject.newErrResponse("订单ID不能为空");
         }
 
+        String sql = "select COST_TOTAL, ORDER_ID from bo_eu_dnivt_order_product where ORDER_ID = ?";
+        List<RowMap> rowMaps = DBSql.getMaps(sql, new Object[]{orderId});
+        System.out.println("采购项查询结果数量:" + (rowMaps != null ? rowMaps.size() : 0));
 
-        String sql = "select UNIT_COST_ADD_TAX, ORDER_ID from bo_eu_dnivt_order_product where ORDER_ID = ?";
-        RowMap map = DBSql.getMap(sql, new Object[]{orderId});
-
-
-        if (map == null) {
-            return ResponseObject.newErrResponse("未找到ORDER_ID为【" + orderId + "】的采购项");
+        List<Map<String, Object>> resultList = new ArrayList<>();
+        if (rowMaps != null && !rowMaps.isEmpty()) {
+            for (RowMap row : rowMaps) {
+                Map<String, Object> orderItem = new HashMap<>();
+                orderItem.put("COST_TOTAL", row.getDouble("COST_TOTAL"));
+                orderItem.put("ORDER_ID", row.getString("ORDER_ID"));
+                resultList.add(orderItem);
+                System.out.println("已添加采购项数据:" + orderItem);
+            }
+        } else {
+            ResponseObject responseObject = ResponseObject.newOkResponse();
+            responseObject.put("message", "未找到ORDER_ID为【" + orderId + "】的采购项,可尝试检查订单ID或新建");
+            responseObject.setData(Collections.emptyList());
+            System.out.println("未查询到采购项数据,ORDER_ID:" + orderId);
+            return responseObject;
         }
 
-        Map<String, Object> result = new HashMap<>();
-        result.put("UNIT_COST_ADD_TAX", map.getString("UNIT_COST_ADD_TAX"));
-        result.put("ORDER_ID", map.getString("ORDER_ID"));
+        System.out.println("采购项查询最终结果总数:" + resultList.size());
 
         ResponseObject responseObject = ResponseObject.newOkResponse();
-        responseObject.setData(result);
-
+        responseObject.setData(resultList);
         return responseObject;
     }
     @Mapping(value = "com.awspaas.user.apps.donenow_ivt.queryCount")