|
|
@@ -1974,4 +1974,72 @@ public class CompanyServiceImpl implements ICompanyService
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 总后台租户充值/扣款
|
|
|
+ * 使用 selectCompanyByIdForUpdate 行锁保证原子性,
|
|
|
+ * 同一事务内更新余额并写入 company_money_logs 日志
|
|
|
+ *
|
|
|
+ * @param companyId 租户ID
|
|
|
+ * @param operateType 操作类型: recharge-充值, deduct-扣款
|
|
|
+ * @param amount 金额(正数)
|
|
|
+ * @param remark 备注
|
|
|
+ * @return 操作结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R rechargeCompany(Long companyId, String operateType, BigDecimal amount, String remark) {
|
|
|
+ if (companyId == null || companyId <= 0) {
|
|
|
+ return R.fail("租户ID不能为空");
|
|
|
+ }
|
|
|
+ if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ return R.fail("金额必须大于0");
|
|
|
+ }
|
|
|
+ if (!"recharge".equals(operateType) && !"deduct".equals(operateType)) {
|
|
|
+ return R.fail("操作类型只能为 recharge 或 deduct");
|
|
|
+ }
|
|
|
+
|
|
|
+ Company company = companyMapper.selectCompanyByIdForUpdate(companyId);
|
|
|
+ if (company == null) {
|
|
|
+ return R.fail("租户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal newBalance;
|
|
|
+ BigDecimal logMoney;
|
|
|
+ int logsType;
|
|
|
+ String logRemark;
|
|
|
+
|
|
|
+ if ("recharge".equals(operateType)) {
|
|
|
+ newBalance = company.getMoney().add(amount);
|
|
|
+ logMoney = amount;
|
|
|
+ logsType = 20; // 后台充值
|
|
|
+ logRemark = StringUtils.isNotEmpty(remark) ? "后台充值: " + remark : "后台充值";
|
|
|
+ } else {
|
|
|
+ // deduct
|
|
|
+ if (company.getMoney().compareTo(amount) < 0) {
|
|
|
+ return R.fail("余额不足,当前余额: " + company.getMoney());
|
|
|
+ }
|
|
|
+ newBalance = company.getMoney().subtract(amount);
|
|
|
+ logMoney = amount.multiply(new BigDecimal(-1));
|
|
|
+ logsType = 21; // 后台扣款
|
|
|
+ logRemark = StringUtils.isNotEmpty(remark) ? "后台扣款: " + remark : "后台扣款";
|
|
|
+ }
|
|
|
+
|
|
|
+ company.setMoney(newBalance);
|
|
|
+ companyMapper.updateCompany(company);
|
|
|
+
|
|
|
+ CompanyMoneyLogs log = new CompanyMoneyLogs();
|
|
|
+ log.setCompanyId(companyId);
|
|
|
+ log.setRemark(logRemark);
|
|
|
+ log.setMoney(logMoney);
|
|
|
+ log.setLogsType(logsType);
|
|
|
+ log.setBalance(newBalance);
|
|
|
+ log.setCreateTime(new Date());
|
|
|
+ moneyLogsMapper.insertCompanyMoneyLogs(log);
|
|
|
+
|
|
|
+ logger.info("后台操作租户余额, companyId={}, operateType={}, amount={}, newBalance={}",
|
|
|
+ companyId, operateType, amount, newBalance);
|
|
|
+
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
}
|