From 3466996f07d5a1aa620561e2dade35b0bf2e1e02 Mon Sep 17 00:00:00 2001 From: ziin Date: Thu, 20 Nov 2025 18:26:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(system):=20=E6=96=B0=E5=A2=9E=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E4=BD=99=E9=A2=9D=E7=89=88=E6=9C=AC=E5=8F=B7=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E4=B8=8E=E5=85=85=E5=80=BC=E9=94=99=E8=AF=AF=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充乐观锁场景下的版本号、余额异常及充值失败专用错误码,为后续余额并发更新提供精确异常定位。 --- .../tenantbalance/vo/TenantBalanceRespVO.java | 4 ++++ .../tenantbalance/TenantBalanceMapper.java | 18 ++++++++++++++---- .../system/enums/ErrorCodeConstants.java | 4 ++++ .../TenantBalanceServiceImpl.java | 18 ++++++++++++++---- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenantbalance/vo/TenantBalanceRespVO.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenantbalance/vo/TenantBalanceRespVO.java index 6458986..6cbc852 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenantbalance/vo/TenantBalanceRespVO.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenantbalance/vo/TenantBalanceRespVO.java @@ -12,6 +12,10 @@ import com.alibaba.excel.annotation.*; @ExcelIgnoreUnannotated public class TenantBalanceRespVO { + @Schema(description = "租户 Id", requiredMode = Schema.RequiredMode.REQUIRED) + @ExcelProperty("租户 Id") + private Long id; + @Schema(description = "当前积分余额", requiredMode = Schema.RequiredMode.REQUIRED) @ExcelProperty("当前积分余额") private Integer balance; diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenantbalance/TenantBalanceMapper.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenantbalance/TenantBalanceMapper.java index e0feea7..9b0a202 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenantbalance/TenantBalanceMapper.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenantbalance/TenantBalanceMapper.java @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.module.system.dal.dataobject.tenantbalance.TenantBalanceDO; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import org.apache.ibatis.annotations.Mapper; import cn.iocoder.yudao.module.system.controller.admin.tenantbalance.vo.*; @@ -25,9 +26,18 @@ public interface TenantBalanceMapper extends BaseMapperX { .orderByDesc(TenantBalanceDO::getId)); } - default Integer updateBalanceWithVersion(TenantBalanceDO updateObj){ - return update(updateObj, new LambdaQueryWrapperX() - .eq(TenantBalanceDO::getId, updateObj.getId()) - .eq(TenantBalanceDO::getVersion, updateObj.getVersion())); + default Integer updateBalanceWithVersion(Long id, int amount, Integer oldVersion) { + LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); + + wrapper.eq(TenantBalanceDO::getId, id); + wrapper.eq(TenantBalanceDO::getVersion, oldVersion); + + // balance = balance + amount + wrapper.setSql("balance = balance + " + amount); + + // version = version + 1 + wrapper.setSql("version = version + 1"); + + return this.update(null, wrapper); } } \ No newline at end of file diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java index e430946..337627e 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java @@ -121,6 +121,10 @@ public interface ErrorCodeConstants { // ========== 租户余额 1-003-017-000 ========== ErrorCode TENANT_BALANCE_NOT_EXISTS = new ErrorCode(1_003_017_000, "租户余额不存在"); + ErrorCode TENANT_BALANCE_VERSION_ERROR = new ErrorCode(1_003_017_001, "租户余额版本号错误"); + ErrorCode TENANT_BALANCE_BALANCE_ERROR = new ErrorCode(1_003_017_002, "租户余额余额错误"); + ErrorCode TENANT_BALANCE_BALANCE_NOT_ENOUGH = new ErrorCode(1_003_017_003, "租户余额余额不足"); + ErrorCode TENANT_BALANCE_RECHARGE_ERROR = new ErrorCode(1_003_017_004, "租户余额充值错误"); // ========== 社交用户 1-002-018-000 ========== diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/tenantbalance/TenantBalanceServiceImpl.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/tenantbalance/TenantBalanceServiceImpl.java index 2cbc6a9..a7e7ea5 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/tenantbalance/TenantBalanceServiceImpl.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/tenantbalance/TenantBalanceServiceImpl.java @@ -4,6 +4,9 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.RandomUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.framework.security.core.LoginUser; +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; import cn.iocoder.yudao.module.system.controller.admin.tenantbalance.vo.TenantBalanceAddReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenantbalance.vo.TenantBalancePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenantbalance.vo.TenantBalanceSaveReqVO; @@ -20,7 +23,7 @@ import javax.annotation.Resource; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_BALANCE_NOT_EXISTS; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.module.system.enums.balance.BalanceEnum.RECHARGE; /** @@ -98,6 +101,7 @@ public class TenantBalanceServiceImpl implements TenantBalanceService { @Override @Transactional(rollbackFor = Exception.class) public void addTenantBalance(TenantBalanceAddReqVO addReqVO) { + // 校验存在 if (tenantBalanceMapper.selectById(addReqVO.getId()) == null) { // 初始化余额为 0 @@ -118,16 +122,22 @@ public class TenantBalanceServiceImpl implements TenantBalanceService { updateObj.setVersion(oldVersion); updateObj.setBalance(newBalance); - Integer updateCount = tenantBalanceMapper.updateBalanceWithVersion(updateObj); + Integer updateCount = tenantBalanceMapper.updateBalanceWithVersion(addReqVO.getId(), addReqVO.getAmount(), oldVersion); if (updateCount == 0) { - throw exception(TENANT_BALANCE_NOT_EXISTS); + throw exception(TENANT_BALANCE_RECHARGE_ERROR); } + LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); + if (loginUser == null) { + throw exception(USER_NOT_EXISTS); + } + TenantPointsDO tenantPointsDO = new TenantPointsDO(); tenantPointsDO.setTenantId(addReqVO.getId()); tenantPointsDO.setPoints(addReqVO.getAmount()); tenantPointsDO.setBalance(newBalance); + tenantPointsDO.setOperatorId(loginUser.getId()); tenantPointsDO.setType(RECHARGE.getDesc()); // RECHARGE / BONUS / ... - tenantPointsDO.setDescription("后台加积分"); + tenantPointsDO.setDescription("后台变动积分"); tenantPointsDO.setBizNo(recharge); tenantPointsMapper.insert(tenantPointsDO); }