feat(shop): 实现商品销量累加和跨租户查询功能
- 添加商品销量累加功能,确保支付成功后更新销量- 实现跨租户查询用户和订单商品的功能 - 修复支付回调中的错误代码 -优化日志记录和异常处理
This commit is contained in:
@@ -113,7 +113,7 @@ public class RequestUtil {
|
||||
.execute().body();
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
System.out.println("jsonObject = " + jsonObject);
|
||||
System.out.println("jsonObject1111111111 = " + jsonObject);
|
||||
final String data = jsonObject.getString("data");
|
||||
return JSONObject.parseObject(data, User.class);
|
||||
} catch (Exception e) {
|
||||
@@ -131,7 +131,7 @@ public class RequestUtil {
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
System.out.println("jsonObject = " + jsonObject);
|
||||
System.out.println("jsonObject1111 = " + jsonObject);
|
||||
final String data = jsonObject.getString("data");
|
||||
return JSONObject.parseObject(data, User.class);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -54,6 +54,14 @@ public interface UserMapper extends BaseMapper<User> {
|
||||
@InterceptorIgnore(tenantLine = "true")
|
||||
void updateByUserId(@Param("param") User param);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询用户(忽略租户隔离)
|
||||
* @param userId 用户ID
|
||||
* @return User
|
||||
*/
|
||||
@InterceptorIgnore(tenantLine = "true")
|
||||
User selectByIdIgnoreTenant(@Param("userId") Integer userId);
|
||||
|
||||
@InterceptorIgnore(tenantLine = "true")
|
||||
List<User> pageAdminByPhone(@Param("param") UserParam param);
|
||||
|
||||
|
||||
@@ -245,4 +245,20 @@
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据用户ID查询用户(忽略租户隔离) -->
|
||||
<select id="selectByIdIgnoreTenant" resultType="com.gxwebsoft.common.system.entity.User">
|
||||
SELECT a.*,
|
||||
c.dict_data_name sex_name,
|
||||
e.tenant_name,
|
||||
h.dealer_id
|
||||
FROM gxwebsoft_core.sys_user a
|
||||
LEFT JOIN (
|
||||
<include refid="selectSexDictSql"/>
|
||||
) c ON a.sex = c.dict_data_code
|
||||
LEFT JOIN gxwebsoft_core.sys_tenant e ON a.tenant_id = e.tenant_id
|
||||
LEFT JOIN gxwebsoft_core.sys_user_referee h ON a.user_id = h.user_id and h.deleted = 0
|
||||
WHERE a.user_id = #{userId}
|
||||
AND a.deleted = 0
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -210,7 +210,7 @@ public class UserParam extends BaseParam {
|
||||
|
||||
@Schema(description = "最后结算时间")
|
||||
@TableField(exist = false)
|
||||
private LocalDateTime settlementTime;
|
||||
private String settlementTime;
|
||||
|
||||
@Schema(description = "报餐时间")
|
||||
@TableField(exist = false)
|
||||
|
||||
@@ -110,6 +110,13 @@ public interface UserService extends IService<User>, UserDetailsService {
|
||||
*/
|
||||
void updateByUserId(User user);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询用户(忽略租户隔离)
|
||||
* @param userId 用户ID
|
||||
* @return User
|
||||
*/
|
||||
User getByIdIgnoreTenant(Integer userId);
|
||||
|
||||
List<User> pageAdminByPhone(UserParam param);
|
||||
|
||||
List<User> listByAlert();
|
||||
|
||||
@@ -231,6 +231,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
return baseMapper.listByAlert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByIdIgnoreTenant(Integer userId) {
|
||||
if (userId == null) {
|
||||
return null;
|
||||
}
|
||||
return baseMapper.selectByIdIgnoreTenant(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询用户的角色
|
||||
*
|
||||
|
||||
@@ -419,7 +419,7 @@ public class ShopOrderController extends BaseController {
|
||||
System.out.println("amount = " + total);
|
||||
// 1. 查询要处理的订单
|
||||
ShopOrder order = shopOrderService.getByOutTradeNo(outTradeNo);
|
||||
logger.info("order = " + order);
|
||||
logger.info("查询要处理的订单order = " + order);
|
||||
// 2. 已支付则跳过
|
||||
if (order.getPayStatus().equals(true)) {
|
||||
return "SUCCESS";
|
||||
@@ -434,6 +434,7 @@ public class ShopOrderController extends BaseController {
|
||||
order.setPayPrice(new BigDecimal(NumberUtil.decimalFormat("0.00", total * 0.01)));
|
||||
order.setExpirationTime(LocalDateTime.now().plusYears(10));
|
||||
System.out.println("实际付款金额 = " + order.getPayPrice());
|
||||
// 更新订单状态并处理支付成功后的业务逻辑(包括累加商品销量)
|
||||
shopOrderService.updateByOutTradeNo(order);
|
||||
return "SUCCESS";
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public class ShopDealerOrder implements Serializable {
|
||||
private Integer isSettled;
|
||||
|
||||
@Schema(description = "结算时间")
|
||||
private Long settleTime;
|
||||
private LocalDateTime settleTime;
|
||||
|
||||
@Schema(description = "商城ID")
|
||||
private Integer tenantId;
|
||||
|
||||
@@ -130,10 +130,6 @@ public class ShopGoods implements Serializable {
|
||||
@Schema(description = "用户ID")
|
||||
private Integer userId;
|
||||
|
||||
@Schema(description = "是否删除, 0否, 1是")
|
||||
@TableLogic
|
||||
private Integer deleted;
|
||||
|
||||
@Schema(description = "租户id")
|
||||
private Integer tenantId;
|
||||
|
||||
|
||||
@@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.gxwebsoft.shop.entity.ShopGoods;
|
||||
import com.gxwebsoft.shop.param.ShopGoodsParam;
|
||||
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -34,4 +36,16 @@ public interface ShopGoodsMapper extends BaseMapper<ShopGoods> {
|
||||
*/
|
||||
List<ShopGoods> selectListRel(@Param("param") ShopGoodsParam param);
|
||||
|
||||
/**
|
||||
* 累加商品销售数量
|
||||
* 使用@InterceptorIgnore忽略租户隔离,确保能更新成功
|
||||
*
|
||||
* @param goodsId 商品ID
|
||||
* @param saleCount 累加的销售数量
|
||||
* @return 影响的行数
|
||||
*/
|
||||
@InterceptorIgnore(tenantLine = "true")
|
||||
@Update("UPDATE shop_goods SET sales = IFNULL(sales, 0) + #{saleCount} WHERE goods_id = #{goodsId}")
|
||||
int addSaleCount(@Param("goodsId") Integer goodsId, @Param("saleCount") Integer saleCount);
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.gxwebsoft.shop.entity.ShopOrderGoods;
|
||||
import com.gxwebsoft.shop.param.ShopOrderGoodsParam;
|
||||
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -34,4 +36,13 @@ public interface ShopOrderGoodsMapper extends BaseMapper<ShopOrderGoods> {
|
||||
*/
|
||||
List<ShopOrderGoods> selectListRel(@Param("param") ShopOrderGoodsParam param);
|
||||
|
||||
/**
|
||||
* 根据订单ID查询订单商品列表(忽略租户隔离)
|
||||
* @param orderId 订单ID
|
||||
* @return List<ShopOrderGoods>
|
||||
*/
|
||||
@InterceptorIgnore(tenantLine = "true")
|
||||
@Select("SELECT * FROM shop_order_goods WHERE order_id = #{orderId}")
|
||||
List<ShopOrderGoods> selectListByOrderIdIgnoreTenant(@Param("orderId") Integer orderId);
|
||||
|
||||
}
|
||||
|
||||
@@ -72,6 +72,6 @@ public class ShopDealerOrderParam extends BaseParam {
|
||||
|
||||
@Schema(description = "结算时间")
|
||||
@QueryField(type = QueryType.EQ)
|
||||
private Integer settleTime;
|
||||
private String settleTime;
|
||||
|
||||
}
|
||||
|
||||
@@ -39,4 +39,14 @@ public interface ShopGoodsService extends IService<ShopGoods> {
|
||||
*/
|
||||
ShopGoods getByIdRel(Integer goodsId);
|
||||
|
||||
/**
|
||||
* 累加商品销售数量
|
||||
* 忽略租户隔离,确保能更新成功
|
||||
*
|
||||
* @param goodsId 商品ID
|
||||
* @param saleCount 累加的销售数量
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
boolean addSaleCount(Integer goodsId, Integer saleCount);
|
||||
|
||||
}
|
||||
|
||||
@@ -40,4 +40,11 @@ public interface ShopOrderGoodsService extends IService<ShopOrderGoods> {
|
||||
ShopOrderGoods getByIdRel(Integer id);
|
||||
|
||||
List<ShopOrderGoods> getListByOrderId(Integer orderId);
|
||||
|
||||
/**
|
||||
* 根据订单ID查询订单商品列表(忽略租户隔离)
|
||||
* @param orderId 订单ID
|
||||
* @return List<ShopOrderGoods>
|
||||
*/
|
||||
List<ShopOrderGoods> getListByOrderIdIgnoreTenant(Integer orderId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.gxwebsoft.shop.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.gxwebsoft.shop.mapper.ShopGoodsMapper;
|
||||
import com.gxwebsoft.shop.service.ShopGoodsService;
|
||||
@@ -8,6 +9,7 @@ import com.gxwebsoft.shop.param.ShopGoodsParam;
|
||||
import com.gxwebsoft.common.core.web.PageParam;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -17,6 +19,7 @@ import java.util.List;
|
||||
* @author 科技小王子
|
||||
* @since 2025-04-24 20:52:13
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoods> implements ShopGoodsService {
|
||||
|
||||
@@ -44,4 +47,29 @@ public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoods
|
||||
return param.getOne(baseMapper.selectListRel(param));
|
||||
}
|
||||
|
||||
@InterceptorIgnore(tenantLine = "true")
|
||||
@Override
|
||||
public boolean addSaleCount(Integer goodsId, Integer saleCount) {
|
||||
try {
|
||||
if (goodsId == null || saleCount == null || saleCount <= 0) {
|
||||
log.warn("累加商品销量参数无效 - 商品ID: {}, 销量: {}", goodsId, saleCount);
|
||||
return false;
|
||||
}
|
||||
|
||||
int affectedRows = baseMapper.addSaleCount(goodsId, saleCount);
|
||||
boolean success = affectedRows > 0;
|
||||
|
||||
if (success) {
|
||||
log.info("商品销量累加成功 - 商品ID: {}, 累加数量: {}, 影响行数: {}", goodsId, saleCount, affectedRows);
|
||||
} else {
|
||||
log.warn("商品销量累加失败 - 商品ID: {}, 累加数量: {}, 影响行数: {}", goodsId, saleCount, affectedRows);
|
||||
}
|
||||
|
||||
return success;
|
||||
} catch (Exception e) {
|
||||
log.error("累加商品销量异常 - 商品ID: {}, 累加数量: {}", goodsId, saleCount, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.gxwebsoft.shop.param.ShopOrderGoodsParam;
|
||||
import com.gxwebsoft.common.core.web.PageParam;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,6 +19,7 @@ import java.util.List;
|
||||
* @author 科技小王子
|
||||
* @since 2025-01-11 10:45:12
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ShopOrderGoodsServiceImpl extends ServiceImpl<ShopOrderGoodsMapper, ShopOrderGoods> implements ShopOrderGoodsService {
|
||||
|
||||
@@ -53,4 +55,24 @@ public class ShopOrderGoodsServiceImpl extends ServiceImpl<ShopOrderGoodsMapper,
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ShopOrderGoods> getListByOrderIdIgnoreTenant(Integer orderId) {
|
||||
try {
|
||||
if (orderId == null) {
|
||||
log.warn("查询订单商品列表参数无效 - 订单ID: {}", orderId);
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<ShopOrderGoods> orderGoodsList = baseMapper.selectListByOrderIdIgnoreTenant(orderId);
|
||||
|
||||
log.info("忽略租户隔离查询订单商品成功 - 订单ID: {}, 商品数量: {}",
|
||||
orderId, orderGoodsList != null ? orderGoodsList.size() : 0);
|
||||
|
||||
return orderGoodsList != null ? orderGoodsList : List.of();
|
||||
} catch (Exception e) {
|
||||
log.error("忽略租户隔离查询订单商品异常 - 订单ID: {}", orderId, e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.gxwebsoft.common.core.utils.DateTimeUtil.formatDateTime;
|
||||
|
||||
/**
|
||||
* 订单Service实现
|
||||
*
|
||||
@@ -297,6 +299,7 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
|
||||
@Override
|
||||
public void updateByOutTradeNo(ShopOrder order) {
|
||||
order.setExpirationTime(null);
|
||||
baseMapper.updateByOutTradeNo(order);
|
||||
|
||||
// 处理支付成功后的业务逻辑
|
||||
@@ -352,11 +355,8 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
*/
|
||||
private void updateGoodsSales(ShopOrder order) {
|
||||
try {
|
||||
// 获取订单商品列表
|
||||
List<ShopOrderGoods> orderGoodsList = shopOrderGoodsService.list(
|
||||
new LambdaQueryWrapper<ShopOrderGoods>()
|
||||
.eq(ShopOrderGoods::getOrderId, order.getOrderId())
|
||||
);
|
||||
// 获取订单商品列表(忽略租户隔离)
|
||||
final List<ShopOrderGoods> orderGoodsList = shopOrderGoodsService.getListByOrderIdIgnoreTenant(order.getOrderId());
|
||||
|
||||
if (orderGoodsList.isEmpty()) {
|
||||
log.warn("订单商品列表为空 - 订单号:{}", order.getOrderNo());
|
||||
@@ -376,29 +376,29 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
||||
|
||||
/**
|
||||
* 累计单个商品的销量
|
||||
* 使用新的addSaleCount方法,忽略租户隔离确保更新成功
|
||||
*/
|
||||
private void updateSingleGoodsSales(ShopOrderGoods orderGoods) {
|
||||
try {
|
||||
ShopGoods goods = shopGoodsService.getById(orderGoods.getGoodsId());
|
||||
if (goods != null) {
|
||||
// 累计商品销量
|
||||
Integer currentSales = goods.getSales() != null ? goods.getSales() : 0;
|
||||
Integer newSales = currentSales + orderGoods.getTotalNum();
|
||||
goods.setSales(newSales);
|
||||
if (orderGoods.getGoodsId() == null || orderGoods.getTotalNum() == null || orderGoods.getTotalNum() <= 0) {
|
||||
log.warn("商品销量累计参数无效 - 商品ID:{},购买数量:{}",
|
||||
orderGoods.getGoodsId(), orderGoods.getTotalNum());
|
||||
return;
|
||||
}
|
||||
|
||||
boolean updated = shopGoodsService.updateById(goods);
|
||||
if (updated) {
|
||||
log.info("商品销量累计成功 - 商品ID:{},商品名称:{},购买数量:{},累计销量:{} -> {}",
|
||||
orderGoods.getGoodsId(), orderGoods.getGoodsName(), orderGoods.getTotalNum(), currentSales, newSales);
|
||||
} else {
|
||||
log.warn("商品销量更新失败 - 商品ID:{}", orderGoods.getGoodsId());
|
||||
}
|
||||
// 使用新的addSaleCount方法,忽略租户隔离
|
||||
boolean updated = shopGoodsService.addSaleCount(orderGoods.getGoodsId(), orderGoods.getTotalNum());
|
||||
|
||||
if (updated) {
|
||||
log.info("商品销量累计成功 - 商品ID:{},商品名称:{},购买数量:{}",
|
||||
orderGoods.getGoodsId(), orderGoods.getGoodsName(), orderGoods.getTotalNum());
|
||||
} else {
|
||||
log.warn("商品不存在,无法累计销量 - 商品ID:{}", orderGoods.getGoodsId());
|
||||
log.warn("商品销量累计失败 - 商品ID:{},商品名称:{},购买数量:{}",
|
||||
orderGoods.getGoodsId(), orderGoods.getGoodsName(), orderGoods.getTotalNum());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("累计单个商品销量失败 - 商品ID:{},商品名称:{}",
|
||||
orderGoods.getGoodsId(), orderGoods.getGoodsName(), e);
|
||||
log.error("累计单个商品销量异常 - 商品ID:{},商品名称:{},购买数量:{}",
|
||||
orderGoods.getGoodsId(), orderGoods.getGoodsName(), orderGoods.getTotalNum(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,14 @@ import cn.hutool.core.date.DateUtil;
|
||||
import com.gxwebsoft.common.core.utils.RequestUtil;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import com.gxwebsoft.shop.entity.*;
|
||||
import com.gxwebsoft.shop.service.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
@@ -30,6 +32,8 @@ public class ShopOrderUpdate10550ServiceImpl implements ShopOrderUpdate10550Serv
|
||||
private ShopOrderGoodsService shopOrderGoodsService;
|
||||
@Resource
|
||||
private ShopGoodsService shopGoodsService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
public void update(ShopOrder order){
|
||||
@@ -41,49 +45,49 @@ public class ShopOrderUpdate10550ServiceImpl implements ShopOrderUpdate10550Serv
|
||||
String dictDataCode = (String) dictDataList.get(0).get("dictDataCode");
|
||||
BigDecimal partnerCondition = new BigDecimal(dictDataCode);
|
||||
|
||||
User user = requestUtil.getByUserIdWithoutLogin(order.getUserId());
|
||||
if (user != null) {
|
||||
final User user = userService.getByIdIgnoreTenant(order.getUserId());
|
||||
if (user != null) {
|
||||
user.setExpendMoney(user.getExpendMoney().add(order.getPayPrice()));
|
||||
if (user.getExpendMoney().compareTo(partnerCondition) >= 0) {
|
||||
user.setGradeId(3);
|
||||
}
|
||||
requestUtil.updateWithoutLogin(user);
|
||||
// requestUtil.updateWithoutLogin(user);
|
||||
|
||||
// 上级
|
||||
User parent = requestUtil.getParent(order.getUserId());
|
||||
if (parent != null) {
|
||||
// User parent = requestUtil.getParent(order.getUserId());
|
||||
// if (parent != null) {
|
||||
|
||||
List<ShopOrderGoods> shopOrderGoodsList = shopOrderGoodsService.getListByOrderId(order.getOrderId());
|
||||
List<Integer> goodsIds = shopOrderGoodsList.stream().map(ShopOrderGoods::getGoodsId).toList();
|
||||
List<ShopGoods> shopGoodsList = shopGoodsService.listByIds(goodsIds);
|
||||
BigDecimal commission = BigDecimal.ZERO;
|
||||
for (ShopOrderGoods shopOrderGoods : shopOrderGoodsList) {
|
||||
ShopGoods shopGoods = shopGoodsList.stream().filter(sG -> sG.getGoodsId().equals(shopOrderGoods.getGoodsId())).findFirst().orElse(null);
|
||||
if (shopGoods != null) {
|
||||
commission = commission.add(shopGoods.getCommission().multiply(BigDecimal.valueOf(shopOrderGoods.getTotalNum())));
|
||||
}
|
||||
}
|
||||
parent.setBalance(parent.getBalance().add(commission));
|
||||
requestUtil.updateWithoutLogin(user);
|
||||
// List<ShopOrderGoods> shopOrderGoodsList = shopOrderGoodsService.getListByOrderId(order.getOrderId());
|
||||
// List<Integer> goodsIds = shopOrderGoodsList.stream().map(ShopOrderGoods::getGoodsId).toList();
|
||||
// List<ShopGoods> shopGoodsList = shopGoodsService.listByIds(goodsIds);
|
||||
// BigDecimal commission = BigDecimal.ZERO;
|
||||
// for (ShopOrderGoods shopOrderGoods : shopOrderGoodsList) {
|
||||
// ShopGoods shopGoods = shopGoodsList.stream().filter(sG -> sG.getGoodsId().equals(shopOrderGoods.getGoodsId())).findFirst().orElse(null);
|
||||
// if (shopGoods != null) {
|
||||
// commission = commission.add(shopGoods.getCommission().multiply(BigDecimal.valueOf(shopOrderGoods.getTotalNum())));
|
||||
// }
|
||||
// }
|
||||
// parent.setBalance(parent.getBalance().add(commission));
|
||||
// requestUtil.updateWithoutLogin(user);
|
||||
|
||||
// 分销订单
|
||||
ShopDealerOrder shopDealerOrder = new ShopDealerOrder();
|
||||
shopDealerOrder.setUserId(parent.getUserId());
|
||||
shopDealerOrder.setOrderId(order.getOrderId());
|
||||
shopDealerOrder.setOrderPrice(order.getTotalPrice());
|
||||
shopDealerOrder.setFirstUserId(order.getUserId());
|
||||
shopDealerOrder.setFirstMoney(commission);
|
||||
shopDealerOrder.setIsSettled(1);
|
||||
shopDealerOrder.setSettleTime(DateUtil.currentSeconds());
|
||||
shopDealerOrderService.save(shopDealerOrder);
|
||||
// ShopDealerOrder shopDealerOrder = new ShopDealerOrder();
|
||||
// shopDealerOrder.setUserId(parent.getUserId());
|
||||
// shopDealerOrder.setOrderId(order.getOrderId());
|
||||
// shopDealerOrder.setOrderPrice(order.getTotalPrice());
|
||||
// shopDealerOrder.setFirstUserId(order.getUserId());
|
||||
// shopDealerOrder.setFirstMoney(commission);
|
||||
// shopDealerOrder.setIsSettled(1);
|
||||
// shopDealerOrder.setSettleTime(LocalDateTime.now());
|
||||
// shopDealerOrderService.save(shopDealerOrder);
|
||||
|
||||
// 分销资明细
|
||||
ShopDealerCapital shopDealerCapital = new ShopDealerCapital();
|
||||
shopDealerCapital.setUserId(parent.getUserId());
|
||||
shopDealerCapital.setOrderId(order.getOrderId());
|
||||
shopDealerCapital.setFlowType(10);
|
||||
shopDealerCapitalService.save(shopDealerCapital);
|
||||
}
|
||||
// ShopDealerCapital shopDealerCapital = new ShopDealerCapital();
|
||||
// shopDealerCapital.setUserId(parent.getUserId());
|
||||
// shopDealerCapital.setOrderId(order.getOrderId());
|
||||
// shopDealerCapital.setFlowType(10);
|
||||
// shopDealerCapitalService.save(shopDealerCapital);
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.gxwebsoft.common.system.service;
|
||||
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 用户忽略租户隔离功能测试
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-08-23
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("dev")
|
||||
public class UserIgnoreTenantTest {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 测试忽略租户隔离查询用户
|
||||
*/
|
||||
@Test
|
||||
public void testGetByIdIgnoreTenant() {
|
||||
// 测试用户ID(请根据实际数据库中的用户ID进行调整)
|
||||
Integer testUserId = 1;
|
||||
|
||||
log.info("=== 开始测试忽略租户隔离查询用户功能 ===");
|
||||
|
||||
// 1. 使用普通方法查询用户(受租户隔离影响)
|
||||
User userNormal = userService.getById(testUserId);
|
||||
log.info("普通查询结果 - 用户ID: {}, 用户信息: {}", testUserId,
|
||||
userNormal != null ? userNormal.getUsername() : "null");
|
||||
|
||||
// 2. 使用忽略租户隔离方法查询用户
|
||||
User userIgnoreTenant = userService.getByIdIgnoreTenant(testUserId);
|
||||
log.info("忽略租户隔离查询结果 - 用户ID: {}, 用户信息: {}", testUserId,
|
||||
userIgnoreTenant != null ? userIgnoreTenant.getUsername() : "null");
|
||||
|
||||
// 3. 验证结果
|
||||
if (userIgnoreTenant != null) {
|
||||
log.info("✅ 忽略租户隔离查询成功!");
|
||||
log.info("用户详情 - ID: {}, 用户名: {}, 昵称: {}, 租户ID: {}",
|
||||
userIgnoreTenant.getUserId(),
|
||||
userIgnoreTenant.getUsername(),
|
||||
userIgnoreTenant.getNickname(),
|
||||
userIgnoreTenant.getTenantId());
|
||||
} else {
|
||||
log.error("❌ 忽略租户隔离查询失败!");
|
||||
}
|
||||
|
||||
log.info("=== 忽略租户隔离查询用户功能测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试参数验证
|
||||
*/
|
||||
@Test
|
||||
public void testGetByIdIgnoreTenantValidation() {
|
||||
log.info("=== 开始测试参数验证 ===");
|
||||
|
||||
// 测试null用户ID
|
||||
User result1 = userService.getByIdIgnoreTenant(null);
|
||||
log.info("null用户ID测试结果: {}", result1 == null ? "成功(返回null)" : "失败");
|
||||
|
||||
// 测试不存在的用户ID
|
||||
User result2 = userService.getByIdIgnoreTenant(999999);
|
||||
log.info("不存在用户ID测试结果: {}", result2 == null ? "成功(返回null)" : "失败");
|
||||
|
||||
log.info("=== 参数验证测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试跨租户查询
|
||||
*/
|
||||
@Test
|
||||
public void testCrossTenantQuery() {
|
||||
log.info("=== 开始测试跨租户查询 ===");
|
||||
|
||||
// 查询不同租户的用户(请根据实际数据调整)
|
||||
Integer[] testUserIds = {1, 2, 3, 4, 5};
|
||||
|
||||
for (Integer userId : testUserIds) {
|
||||
User user = userService.getByIdIgnoreTenant(userId);
|
||||
if (user != null) {
|
||||
log.info("用户ID: {}, 用户名: {}, 租户ID: {}",
|
||||
user.getUserId(), user.getUsername(), user.getTenantId());
|
||||
} else {
|
||||
log.info("用户ID: {} - 不存在", userId);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("=== 跨租户查询测试完成 ===");
|
||||
}
|
||||
}
|
||||
145
src/test/java/com/gxwebsoft/shop/service/ShopGoodsSalesTest.java
Normal file
145
src/test/java/com/gxwebsoft/shop/service/ShopGoodsSalesTest.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package com.gxwebsoft.shop.service;
|
||||
|
||||
import com.gxwebsoft.shop.entity.ShopGoods;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 商品销量累加功能测试
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-08-23
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("dev")
|
||||
public class ShopGoodsSalesTest {
|
||||
|
||||
@Resource
|
||||
private ShopGoodsService shopGoodsService;
|
||||
|
||||
/**
|
||||
* 测试累加商品销量功能
|
||||
*/
|
||||
@Test
|
||||
public void testAddSaleCount() {
|
||||
// 测试商品ID(请根据实际数据库中的商品ID进行调整)
|
||||
Integer testGoodsId = 1;
|
||||
Integer addCount = 5;
|
||||
|
||||
log.info("=== 开始测试商品销量累加功能 ===");
|
||||
|
||||
// 1. 查询商品当前销量
|
||||
ShopGoods goodsBefore = shopGoodsService.getById(testGoodsId);
|
||||
if (goodsBefore == null) {
|
||||
log.error("测试失败:商品不存在 - 商品ID: {}", testGoodsId);
|
||||
return;
|
||||
}
|
||||
|
||||
Integer salesBefore = goodsBefore.getSales() != null ? goodsBefore.getSales() : 0;
|
||||
log.info("累加前商品销量 - 商品ID: {}, 商品名称: {}, 当前销量: {}",
|
||||
testGoodsId, goodsBefore.getName(), salesBefore);
|
||||
|
||||
// 2. 执行销量累加
|
||||
boolean result = shopGoodsService.addSaleCount(testGoodsId, addCount);
|
||||
log.info("销量累加操作结果: {}", result ? "成功" : "失败");
|
||||
|
||||
// 3. 查询累加后的销量
|
||||
ShopGoods goodsAfter = shopGoodsService.getById(testGoodsId);
|
||||
Integer salesAfter = goodsAfter.getSales() != null ? goodsAfter.getSales() : 0;
|
||||
log.info("累加后商品销量 - 商品ID: {}, 商品名称: {}, 累加后销量: {}",
|
||||
testGoodsId, goodsAfter.getName(), salesAfter);
|
||||
|
||||
// 4. 验证结果
|
||||
Integer expectedSales = salesBefore + addCount;
|
||||
if (salesAfter.equals(expectedSales)) {
|
||||
log.info("✅ 测试成功!销量正确累加 - 预期: {}, 实际: {}", expectedSales, salesAfter);
|
||||
} else {
|
||||
log.error("❌ 测试失败!销量累加不正确 - 预期: {}, 实际: {}", expectedSales, salesAfter);
|
||||
}
|
||||
|
||||
log.info("=== 商品销量累加功能测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试参数验证
|
||||
*/
|
||||
@Test
|
||||
public void testAddSaleCountValidation() {
|
||||
log.info("=== 开始测试参数验证 ===");
|
||||
|
||||
// 测试null商品ID
|
||||
boolean result1 = shopGoodsService.addSaleCount(null, 5);
|
||||
log.info("null商品ID测试结果: {}", result1 ? "失败(应该返回false)" : "成功");
|
||||
|
||||
// 测试null销量
|
||||
boolean result2 = shopGoodsService.addSaleCount(1, null);
|
||||
log.info("null销量测试结果: {}", result2 ? "失败(应该返回false)" : "成功");
|
||||
|
||||
// 测试负数销量
|
||||
boolean result3 = shopGoodsService.addSaleCount(1, -1);
|
||||
log.info("负数销量测试结果: {}", result3 ? "失败(应该返回false)" : "成功");
|
||||
|
||||
// 测试零销量
|
||||
boolean result4 = shopGoodsService.addSaleCount(1, 0);
|
||||
log.info("零销量测试结果: {}", result4 ? "失败(应该返回false)" : "成功");
|
||||
|
||||
// 测试不存在的商品ID
|
||||
boolean result5 = shopGoodsService.addSaleCount(999999, 5);
|
||||
log.info("不存在商品ID测试结果: {}", result5 ? "失败(应该返回false)" : "成功");
|
||||
|
||||
log.info("=== 参数验证测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试大批量累加
|
||||
*/
|
||||
@Test
|
||||
public void testBatchAddSaleCount() {
|
||||
Integer testGoodsId = 1;
|
||||
|
||||
log.info("=== 开始测试批量累加 ===");
|
||||
|
||||
// 查询初始销量
|
||||
ShopGoods goodsBefore = shopGoodsService.getById(testGoodsId);
|
||||
if (goodsBefore == null) {
|
||||
log.error("测试失败:商品不存在 - 商品ID: {}", testGoodsId);
|
||||
return;
|
||||
}
|
||||
|
||||
Integer salesBefore = goodsBefore.getSales() != null ? goodsBefore.getSales() : 0;
|
||||
log.info("批量累加前销量: {}", salesBefore);
|
||||
|
||||
// 模拟多次购买
|
||||
int totalAdded = 0;
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
boolean result = shopGoodsService.addSaleCount(testGoodsId, i);
|
||||
if (result) {
|
||||
totalAdded += i;
|
||||
log.info("第{}次累加成功,累加数量: {}", i, i);
|
||||
} else {
|
||||
log.error("第{}次累加失败", i);
|
||||
}
|
||||
}
|
||||
|
||||
// 验证最终结果
|
||||
ShopGoods goodsAfter = shopGoodsService.getById(testGoodsId);
|
||||
Integer salesAfter = goodsAfter.getSales() != null ? goodsAfter.getSales() : 0;
|
||||
Integer expectedSales = salesBefore + totalAdded;
|
||||
|
||||
log.info("批量累加结果 - 累加前: {}, 总累加量: {}, 累加后: {}, 预期: {}",
|
||||
salesBefore, totalAdded, salesAfter, expectedSales);
|
||||
|
||||
if (salesAfter.equals(expectedSales)) {
|
||||
log.info("✅ 批量累加测试成功!");
|
||||
} else {
|
||||
log.error("❌ 批量累加测试失败!");
|
||||
}
|
||||
|
||||
log.info("=== 批量累加测试完成 ===");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.gxwebsoft.shop.service;
|
||||
|
||||
import com.gxwebsoft.shop.entity.ShopOrderGoods;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 订单商品忽略租户隔离功能测试
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-08-23
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("dev")
|
||||
public class ShopOrderGoodsIgnoreTenantTest {
|
||||
|
||||
@Resource
|
||||
private ShopOrderGoodsService shopOrderGoodsService;
|
||||
|
||||
/**
|
||||
* 测试忽略租户隔离查询订单商品
|
||||
*/
|
||||
@Test
|
||||
public void testGetListByOrderIdIgnoreTenant() {
|
||||
// 测试订单ID(请根据实际数据库中的订单ID进行调整)
|
||||
Integer testOrderId = 1;
|
||||
|
||||
log.info("=== 开始测试忽略租户隔离查询订单商品功能 ===");
|
||||
|
||||
// 1. 使用普通方法查询订单商品(受租户隔离影响)
|
||||
List<ShopOrderGoods> orderGoodsNormal = shopOrderGoodsService.getListByOrderId(testOrderId);
|
||||
log.info("普通查询结果 - 订单ID: {}, 商品数量: {}", testOrderId,
|
||||
orderGoodsNormal != null ? orderGoodsNormal.size() : 0);
|
||||
|
||||
// 2. 使用忽略租户隔离方法查询订单商品
|
||||
List<ShopOrderGoods> orderGoodsIgnoreTenant = shopOrderGoodsService.getListByOrderIdIgnoreTenant(testOrderId);
|
||||
log.info("忽略租户隔离查询结果 - 订单ID: {}, 商品数量: {}", testOrderId,
|
||||
orderGoodsIgnoreTenant != null ? orderGoodsIgnoreTenant.size() : 0);
|
||||
|
||||
// 3. 验证结果
|
||||
if (orderGoodsIgnoreTenant != null && !orderGoodsIgnoreTenant.isEmpty()) {
|
||||
log.info("✅ 忽略租户隔离查询成功!");
|
||||
for (ShopOrderGoods orderGoods : orderGoodsIgnoreTenant) {
|
||||
log.info("订单商品详情 - ID: {}, 商品ID: {}, 商品名称: {}, 数量: {}, 租户ID: {}",
|
||||
orderGoods.getId(),
|
||||
orderGoods.getGoodsId(),
|
||||
orderGoods.getGoodsName(),
|
||||
orderGoods.getTotalNum(),
|
||||
orderGoods.getTenantId());
|
||||
}
|
||||
} else {
|
||||
log.warn("⚠️ 忽略租户隔离查询结果为空,可能订单不存在或没有商品");
|
||||
}
|
||||
|
||||
log.info("=== 忽略租户隔离查询订单商品功能测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试参数验证
|
||||
*/
|
||||
@Test
|
||||
public void testGetListByOrderIdIgnoreTenantValidation() {
|
||||
log.info("=== 开始测试参数验证 ===");
|
||||
|
||||
// 测试null订单ID
|
||||
List<ShopOrderGoods> result1 = shopOrderGoodsService.getListByOrderIdIgnoreTenant(null);
|
||||
log.info("null订单ID测试结果: {}", result1.isEmpty() ? "成功(返回空列表)" : "失败");
|
||||
|
||||
// 测试不存在的订单ID
|
||||
List<ShopOrderGoods> result2 = shopOrderGoodsService.getListByOrderIdIgnoreTenant(999999);
|
||||
log.info("不存在订单ID测试结果: {}", result2.isEmpty() ? "成功(返回空列表)" : "失败");
|
||||
|
||||
log.info("=== 参数验证测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试跨租户查询
|
||||
*/
|
||||
@Test
|
||||
public void testCrossTenantQuery() {
|
||||
log.info("=== 开始测试跨租户查询 ===");
|
||||
|
||||
// 查询不同租户的订单商品(请根据实际数据调整)
|
||||
Integer[] testOrderIds = {1, 2, 3, 4, 5};
|
||||
|
||||
for (Integer orderId : testOrderIds) {
|
||||
List<ShopOrderGoods> orderGoodsList = shopOrderGoodsService.getListByOrderIdIgnoreTenant(orderId);
|
||||
if (orderGoodsList != null && !orderGoodsList.isEmpty()) {
|
||||
log.info("订单ID: {}, 商品数量: {}", orderId, orderGoodsList.size());
|
||||
for (ShopOrderGoods orderGoods : orderGoodsList) {
|
||||
log.info(" - 商品: {} (ID: {}), 数量: {}, 租户: {}",
|
||||
orderGoods.getGoodsName(),
|
||||
orderGoods.getGoodsId(),
|
||||
orderGoods.getTotalNum(),
|
||||
orderGoods.getTenantId());
|
||||
}
|
||||
} else {
|
||||
log.info("订单ID: {} - 无商品或不存在", orderId);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("=== 跨租户查询测试完成 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试批量查询性能
|
||||
*/
|
||||
@Test
|
||||
public void testBatchQuery() {
|
||||
log.info("=== 开始测试批量查询性能 ===");
|
||||
|
||||
Integer[] testOrderIds = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
int totalGoods = 0;
|
||||
for (Integer orderId : testOrderIds) {
|
||||
List<ShopOrderGoods> orderGoodsList = shopOrderGoodsService.getListByOrderIdIgnoreTenant(orderId);
|
||||
totalGoods += orderGoodsList.size();
|
||||
}
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
long duration = endTime - startTime;
|
||||
|
||||
log.info("批量查询结果 - 查询订单数: {}, 总商品数: {}, 耗时: {}ms",
|
||||
testOrderIds.length, totalGoods, duration);
|
||||
|
||||
log.info("=== 批量查询性能测试完成 ===");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user