refactor(user-card): 优化用户卡包统计数据获取逻辑
- 移除对UserService的依赖,改为使用UserCardStatsMapper直接查询 - 新增UserCardStatsMapper接口用于跨库查询用户余额和积分 - 添加MyBatis XML映射文件实现跨库查询gxwebsoft_core.sys_user表 - 实现类型转换工具方法toBigDecimal和toIntObj确保数据类型安全 - 修复因模块间依赖导致无法访问sys_user表的问题 - 保持租户隔离支持,确保数据安全性
This commit is contained in:
@@ -4,11 +4,10 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||||
import com.gxwebsoft.common.core.web.ApiResult;
|
import com.gxwebsoft.common.core.web.ApiResult;
|
||||||
import com.gxwebsoft.common.core.web.BaseController;
|
import com.gxwebsoft.common.core.web.BaseController;
|
||||||
import com.gxwebsoft.common.system.entity.User;
|
|
||||||
import com.gxwebsoft.common.system.service.UserService;
|
|
||||||
import com.gxwebsoft.shop.dto.UserCardStats;
|
import com.gxwebsoft.shop.dto.UserCardStats;
|
||||||
import com.gxwebsoft.shop.entity.ShopGift;
|
import com.gxwebsoft.shop.entity.ShopGift;
|
||||||
import com.gxwebsoft.shop.entity.ShopUserCoupon;
|
import com.gxwebsoft.shop.entity.ShopUserCoupon;
|
||||||
|
import com.gxwebsoft.shop.mapper.UserCardStatsMapper;
|
||||||
import com.gxwebsoft.shop.service.ShopGiftService;
|
import com.gxwebsoft.shop.service.ShopGiftService;
|
||||||
import com.gxwebsoft.shop.service.ShopUserCouponService;
|
import com.gxwebsoft.shop.service.ShopUserCouponService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
@@ -22,6 +21,7 @@ import java.math.BigDecimal;
|
|||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Tag(name = "用户卡包")
|
@Tag(name = "用户卡包")
|
||||||
@@ -35,7 +35,7 @@ public class UserCardController extends BaseController {
|
|||||||
@Resource
|
@Resource
|
||||||
private RedisUtil redisUtil;
|
private RedisUtil redisUtil;
|
||||||
@Resource
|
@Resource
|
||||||
private UserService userService;
|
private UserCardStatsMapper userCardStatsMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private ShopUserCouponService shopUserCouponService;
|
private ShopUserCouponService shopUserCouponService;
|
||||||
@Resource
|
@Resource
|
||||||
@@ -57,10 +57,11 @@ public class UserCardController extends BaseController {
|
|||||||
return success("ok", cached);
|
return success("ok", cached);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 余额/积分从用户表取;优惠券/礼品卡用 COUNT 聚合
|
// 余额/积分:从 core 库取(避免 modules 库不存在 sys_user 导致报错);优惠券/礼品卡用 COUNT 聚合
|
||||||
User user = userService.getById(userId);
|
Integer tenantId = getTenantId();
|
||||||
BigDecimal balance = user == null || user.getBalance() == null ? BigDecimal.ZERO : user.getBalance();
|
Map<String, Object> bp = userCardStatsMapper.selectBalancePoints(userId, tenantId);
|
||||||
Integer points = user == null || user.getPoints() == null ? 0 : user.getPoints();
|
BigDecimal balance = toBigDecimal(bp == null ? null : bp.get("balance"));
|
||||||
|
Integer points = toIntObj(bp == null ? null : bp.get("points"));
|
||||||
|
|
||||||
long coupons = shopUserCouponService.count(new LambdaQueryWrapper<ShopUserCoupon>()
|
long coupons = shopUserCouponService.count(new LambdaQueryWrapper<ShopUserCoupon>()
|
||||||
.eq(ShopUserCoupon::getUserId, userId)
|
.eq(ShopUserCoupon::getUserId, userId)
|
||||||
@@ -81,6 +82,37 @@ public class UserCardController extends BaseController {
|
|||||||
return success("ok", stats);
|
return success("ok", stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static BigDecimal toBigDecimal(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return BigDecimal.ZERO;
|
||||||
|
}
|
||||||
|
if (value instanceof BigDecimal) {
|
||||||
|
return (BigDecimal) value;
|
||||||
|
}
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return BigDecimal.valueOf(((Number) value).doubleValue());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new BigDecimal(String.valueOf(value));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return BigDecimal.ZERO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer toIntObj(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return ((Number) value).intValue();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(String.valueOf(value));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static Integer toInt(long value) {
|
private static Integer toInt(long value) {
|
||||||
if (value <= 0) {
|
if (value <= 0) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -91,4 +123,3 @@ public class UserCardController extends BaseController {
|
|||||||
return (int) value;
|
return (int) value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.gxwebsoft.shop.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户卡包统计查询(跨库读取 gxwebsoft_core.sys_user)
|
||||||
|
*/
|
||||||
|
public interface UserCardStatsMapper {
|
||||||
|
|
||||||
|
@InterceptorIgnore(tenantLine = "true")
|
||||||
|
Map<String, Object> selectBalancePoints(@Param("userId") Integer userId, @Param("tenantId") Integer tenantId);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.gxwebsoft.shop.mapper.UserCardStatsMapper">
|
||||||
|
|
||||||
|
<select id="selectBalancePoints" resultType="java.util.HashMap">
|
||||||
|
SELECT balance, points
|
||||||
|
FROM gxwebsoft_core.sys_user
|
||||||
|
WHERE user_id = #{userId}
|
||||||
|
AND deleted = 0
|
||||||
|
<if test="tenantId != null">
|
||||||
|
AND tenant_id = #{tenantId}
|
||||||
|
</if>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
</mapper>
|
||||||
|
|
||||||
Reference in New Issue
Block a user