|
|
@@ -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
|