feat(shop): 实现商品销量累加和跨租户查询功能

- 添加商品销量累加功能,确保支付成功后更新销量- 实现跨租户查询用户和订单商品的功能
- 修复支付回调中的错误代码
-优化日志记录和异常处理
This commit is contained in:
2025-08-23 03:43:21 +08:00
parent 2a8d87a4d1
commit cccc13df79
25 changed files with 1453 additions and 63 deletions

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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>

View File

@@ -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)

View File

@@ -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();

View File

@@ -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);
}
/**
* 批量查询用户的角色
*

View File

@@ -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";
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -72,6 +72,6 @@ public class ShopDealerOrderParam extends BaseParam {
@Schema(description = "结算时间")
@QueryField(type = QueryType.EQ)
private Integer settleTime;
private String settleTime;
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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;
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
// }
}
}
}