|
|
@@ -1463,83 +1463,169 @@ public class contractApproveController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Mapping(value = "com.awspaas.user.apps.donenow_ctt.updateAgentCommissionByIds")
|
|
|
- public ResponseObject updateAgentCommissionByIds(UserContext uc,
|
|
|
+ @Mapping("com.awspaas.user.apps.donenow_ctt.updateAgentCommissionById")
|
|
|
+ public ResponseObject updateAgentCommissionById(UserContext uc,
|
|
|
String ids,
|
|
|
String commissionPayId,
|
|
|
String agent,
|
|
|
String ACCOUNT_PAY_DATE,
|
|
|
- BigDecimal payAmount,
|
|
|
+ BigDecimal totalPayAmount, // 前端传递的“总支付金额”
|
|
|
String payer,
|
|
|
String remark,
|
|
|
String invoiceTypeId,
|
|
|
String agentInvoiceNo,
|
|
|
- String invoiceFile) {
|
|
|
+ String invoiceFile) throws SQLException {
|
|
|
+ Connection connUpdate = null;
|
|
|
try {
|
|
|
- if (ids == null || ids.trim().isEmpty()) {
|
|
|
+ // 1. 参数校验
|
|
|
+ if (StringUtils.isBlank(ids)) {
|
|
|
return ResponseObject.newErrResponse("ids不能为空");
|
|
|
}
|
|
|
- if (commissionPayId == null || commissionPayId.trim().isEmpty()) {
|
|
|
+ if (StringUtils.isBlank(commissionPayId)) {
|
|
|
return ResponseObject.newErrResponse("commissionPayId不能为空");
|
|
|
}
|
|
|
-
|
|
|
+ if (totalPayAmount == null || totalPayAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ return ResponseObject.newErrResponse("总支付金额不能为空且需大于0");
|
|
|
+ }
|
|
|
String[] idArr = ids.split(",");
|
|
|
|
|
|
- StringBuilder updateSql = new StringBuilder("UPDATE VIEW_EU_DNV_COMMISSION_PERIOD_AGENT SET ");
|
|
|
- List<Object> params = new ArrayList<>();
|
|
|
-
|
|
|
- updateSql.append("COMMISSION_PAY_ID = ?, AGENT = ?, ACCOUNT_PAY_DATE = ?, PAY_AMOUNT = ?, PAYER = ?, ");
|
|
|
- params.add(commissionPayId.trim());
|
|
|
- params.add(agent);
|
|
|
- params.add(ACCOUNT_PAY_DATE);
|
|
|
- params.add(payAmount);
|
|
|
- params.add(payer);
|
|
|
-
|
|
|
- // 可选字段
|
|
|
- if (remark != null && !remark.trim().isEmpty()) {
|
|
|
- updateSql.append("REMARK = ?, ");
|
|
|
- params.add(remark);
|
|
|
- }
|
|
|
- if (invoiceTypeId != null && !invoiceTypeId.trim().isEmpty()) {
|
|
|
- updateSql.append("INVOICE_TYPE_ID = ?, ");
|
|
|
- params.add(invoiceTypeId);
|
|
|
- }
|
|
|
- if (agentInvoiceNo != null && !agentInvoiceNo.trim().isEmpty()) {
|
|
|
- updateSql.append("AGENT_INVOICE_NO = ?, ");
|
|
|
- params.add(agentInvoiceNo);
|
|
|
- }
|
|
|
- if (invoiceFile != null && !invoiceFile.trim().isEmpty()) {
|
|
|
- updateSql.append("INVOICE_FILE = ?, ");
|
|
|
- params.add(invoiceFile);
|
|
|
+ // 2. 查询每个ID的“周期金额”(假设字段为 PERIOD_AMOUNT)
|
|
|
+ String querySql = "SELECT ID, PERIOD_ADJUSTED_PRICE FROM VIEW_EU_DNV_COMMISSION_PERIOD_AGENT WHERE ID IN ("
|
|
|
+ + String.join(",", Collections.nCopies(idArr.length, "?")) + ")";
|
|
|
+ List<Object> queryParams = new ArrayList<>(Arrays.asList(idArr));
|
|
|
+ List<RowMap> periodList = DBSql.getMaps(querySql, queryParams.toArray());
|
|
|
+
|
|
|
+ // 3. 预处理:统计总周期金额 + 校验数据
|
|
|
+ Map<String, BigDecimal> idPeriodAmountMap = new HashMap<>();
|
|
|
+ BigDecimal totalPeriodAmount = BigDecimal.ZERO;
|
|
|
+ for (RowMap row : periodList) {
|
|
|
+ String id = row.getString("ID");
|
|
|
+ // 处理周期金额:先转String,再转BigDecimal(兼容类型)
|
|
|
+ String periodAmountStr = row.getString("PERIOD_ADJUSTED_PRICE");
|
|
|
+ BigDecimal periodAmount = null;
|
|
|
+ if (periodAmountStr != null && !periodAmountStr.trim().isEmpty()) {
|
|
|
+ try {
|
|
|
+ periodAmount = new BigDecimal(periodAmountStr.trim());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ System.out.println("周期金额格式错误,ID:" + id + ",值:" + periodAmountStr);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (periodAmount == null || periodAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ System.out.println("周期金额非法(空或<=0),ID:" + id);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ idPeriodAmountMap.put(id, periodAmount);
|
|
|
+ totalPeriodAmount = totalPeriodAmount.add(periodAmount);
|
|
|
}
|
|
|
|
|
|
- if (updateSql.charAt(updateSql.length() - 2) == ',') {
|
|
|
- updateSql.setLength(updateSql.length() - 2);
|
|
|
+ // 校验总周期金额(避免除零)
|
|
|
+ if (totalPeriodAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("totalMatched", periodList.size());
|
|
|
+ result.put("updateCount", 0);
|
|
|
+ result.put("message", "选中周期的总金额为0,无法计算比例");
|
|
|
+ ResponseObject responseObject = ResponseObject.newOkResponse();
|
|
|
+ responseObject.setData(result);
|
|
|
+ return responseObject;
|
|
|
}
|
|
|
|
|
|
- StringBuilder placeholders = new StringBuilder();
|
|
|
- for (int i = 0; i < idArr.length; i++) {
|
|
|
- if (i > 0) placeholders.append(",");
|
|
|
- placeholders.append("?");
|
|
|
- }
|
|
|
- updateSql.append(" WHERE ID IN (").append(placeholders.toString()).append(")");
|
|
|
+ // 4. 初始化事务连接
|
|
|
+ connUpdate = DBSql.open();
|
|
|
+ connUpdate.setAutoCommit(false);
|
|
|
+
|
|
|
+ int updateCount = 0;
|
|
|
|
|
|
+ // 5. 循环按比例分配并更新
|
|
|
for (String id : idArr) {
|
|
|
- params.add(id.trim());
|
|
|
+ if (!idPeriodAmountMap.containsKey(id)) {
|
|
|
+ System.out.println("跳过未查询到周期金额的ID:" + id);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按比例计算单周期应分配金额
|
|
|
+ BigDecimal periodAmount = idPeriodAmountMap.get(id);
|
|
|
+ BigDecimal ratio = periodAmount.divide(totalPeriodAmount, 6, BigDecimal.ROUND_HALF_UP); // 比例保留6位小数
|
|
|
+ BigDecimal allocatedAmount = totalPayAmount.multiply(ratio).setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
+
|
|
|
+ // 构建更新SQL(累加 PAY_AMOUNT)
|
|
|
+ StringBuilder updateSql = new StringBuilder("UPDATE VIEW_EU_DNV_COMMISSION_PERIOD_AGENT SET ");
|
|
|
+ List<Object> updateParams = new ArrayList<>();
|
|
|
+
|
|
|
+ updateSql.append("COMMISSION_PAY_ID = ?, ");
|
|
|
+ updateSql.append("AGENT = ?, ");
|
|
|
+ updateSql.append("ACCOUNT_PAY_DATE = ?, ");
|
|
|
+ updateSql.append("PAY_AMOUNT = ?, "); // 核心:累加更新
|
|
|
+ updateSql.append("PAYER = ?, ");
|
|
|
+ updateParams.add(commissionPayId.trim());
|
|
|
+ updateParams.add(agent);
|
|
|
+ updateParams.add(ACCOUNT_PAY_DATE);
|
|
|
+ updateParams.add(allocatedAmount);
|
|
|
+ updateParams.add(payer);
|
|
|
+
|
|
|
+ // 可选字段(有值才更新)
|
|
|
+ if (remark != null && !remark.trim().isEmpty()) {
|
|
|
+ updateSql.append("REMARK = ?, ");
|
|
|
+ updateParams.add(remark.trim());
|
|
|
+ }
|
|
|
+ if (invoiceTypeId != null && !invoiceTypeId.trim().isEmpty()) {
|
|
|
+ updateSql.append("INVOICE_TYPE_ID = ?, ");
|
|
|
+ updateParams.add(invoiceTypeId.trim());
|
|
|
+ }
|
|
|
+ if (agentInvoiceNo != null && !agentInvoiceNo.trim().isEmpty()) {
|
|
|
+ updateSql.append("AGENT_INVOICE_NO = ?, ");
|
|
|
+ updateParams.add(agentInvoiceNo.trim());
|
|
|
+ }
|
|
|
+ if (invoiceFile != null && !invoiceFile.trim().isEmpty()) {
|
|
|
+ updateSql.append("INVOICE_FILE = ?, ");
|
|
|
+ updateParams.add(invoiceFile.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除最后多余的逗号
|
|
|
+ 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++;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- int updateCount = DBSql.update(updateSql.toString(), params.toArray());
|
|
|
+ // 6. 提交事务
|
|
|
+ connUpdate.commit();
|
|
|
|
|
|
+ // 7. 返回结果
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("totalMatched", idArr.length);
|
|
|
result.put("updateCount", updateCount);
|
|
|
+ result.put("totalPayAmount", totalPayAmount);
|
|
|
+ result.put("totalPeriodAmount", totalPeriodAmount);
|
|
|
+ result.put("message", "按比例分配更新完成,共更新" + updateCount + "条记录");
|
|
|
|
|
|
ResponseObject responseObject = ResponseObject.newOkResponse();
|
|
|
responseObject.setData(result);
|
|
|
return responseObject;
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (SQLException e) {
|
|
|
+ // 异常回滚
|
|
|
+ if (connUpdate != null && !connUpdate.isClosed()) {
|
|
|
+ try {
|
|
|
+ connUpdate.rollback();
|
|
|
+ } catch (SQLException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
e.printStackTrace();
|
|
|
return ResponseObject.newErrResponse("更新失败:" + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ // 关闭连接
|
|
|
+ if (connUpdate != null && !connUpdate.isClosed()) {
|
|
|
+ DBSql.close(connUpdate);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1733,6 +1819,9 @@ public class contractApproveController {
|
|
|
Double total = record.getDouble("基数");
|
|
|
String ratioStr = record.getString("比例");
|
|
|
String zbBillingDate = record.getString("账单日期");
|
|
|
+ if (commission != null) {
|
|
|
+ commission = commission / 1.06;
|
|
|
+ }
|
|
|
|
|
|
// 处理比例字段(不变)
|
|
|
Double ratio = 0.0;
|