Commit 20241b8bcc221db9cd62cacd3c064c78a1826370

Authored by SuchJack
1 parent 837f1556

fix:修复"商品统计聚合函数的别名与排序字段不符"导致的SQL异常

yudao-module-mall/yudao-module-statistics/src/main/java/cn/iocoder/yudao/module/statistics/dal/mysql/product/ProductStatisticsMapper.java
1 package cn.iocoder.yudao.module.statistics.dal.mysql.product; 1 package cn.iocoder.yudao.module.statistics.dal.mysql.product;
2 2
  3 +import cn.hutool.core.util.StrUtil;
3 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.framework.common.pojo.SortablePageParam; 5 import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
5 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; 6 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
6 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; 7 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
7 import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX; 8 import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
  9 +import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
8 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsReqVO; 10 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsReqVO;
9 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsRespVO; 11 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsRespVO;
10 import cn.iocoder.yudao.module.statistics.dal.dataobject.product.ProductStatisticsDO; 12 import cn.iocoder.yudao.module.statistics.dal.dataobject.product.ProductStatisticsDO;
11 import com.baomidou.mybatisplus.core.metadata.IPage; 13 import com.baomidou.mybatisplus.core.metadata.IPage;
  14 +import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
12 import org.apache.ibatis.annotations.Mapper; 15 import org.apache.ibatis.annotations.Mapper;
13 import org.apache.ibatis.annotations.Param; 16 import org.apache.ibatis.annotations.Param;
  17 +import org.dromara.hutool.core.func.LambdaUtil;
14 18
15 import java.time.LocalDateTime; 19 import java.time.LocalDateTime;
16 import java.util.List; 20 import java.util.List;
@@ -49,16 +53,31 @@ public interface ProductStatisticsMapper extends BaseMapperX<ProductStatisticsDO @@ -49,16 +53,31 @@ public interface ProductStatisticsMapper extends BaseMapperX<ProductStatisticsDO
49 private static MPJLambdaWrapperX<ProductStatisticsDO> buildWrapper(ProductStatisticsReqVO reqVO) { 53 private static MPJLambdaWrapperX<ProductStatisticsDO> buildWrapper(ProductStatisticsReqVO reqVO) {
50 return new MPJLambdaWrapperX<ProductStatisticsDO>() 54 return new MPJLambdaWrapperX<ProductStatisticsDO>()
51 .betweenIfPresent(ProductStatisticsDO::getTime, reqVO.getTimes()) 55 .betweenIfPresent(ProductStatisticsDO::getTime, reqVO.getTimes())
52 - .selectSum(ProductStatisticsDO::getBrowseCount)  
53 - .selectSum(ProductStatisticsDO::getBrowseUserCount)  
54 - .selectSum(ProductStatisticsDO::getFavoriteCount)  
55 - .selectSum(ProductStatisticsDO::getCartCount)  
56 - .selectSum(ProductStatisticsDO::getOrderCount)  
57 - .selectSum(ProductStatisticsDO::getOrderPayCount)  
58 - .selectSum(ProductStatisticsDO::getOrderPayPrice)  
59 - .selectSum(ProductStatisticsDO::getAfterSaleCount)  
60 - .selectSum(ProductStatisticsDO::getAfterSaleRefundPrice)  
61 - .selectAvg(ProductStatisticsDO::getBrowseConvertPercent); 56 + .selectSum(ProductStatisticsDO::getBrowseCount,toUnderlineCase(ProductStatisticsDO::getBrowseCount))
  57 + .selectSum(ProductStatisticsDO::getBrowseUserCount,toUnderlineCase(ProductStatisticsDO::getBrowseUserCount))
  58 + .selectSum(ProductStatisticsDO::getFavoriteCount,toUnderlineCase(ProductStatisticsDO::getFavoriteCount))
  59 + .selectSum(ProductStatisticsDO::getCartCount,toUnderlineCase(ProductStatisticsDO::getCartCount))
  60 + .selectSum(ProductStatisticsDO::getOrderCount,toUnderlineCase(ProductStatisticsDO::getOrderCount))
  61 + .selectSum(ProductStatisticsDO::getOrderPayCount,toUnderlineCase(ProductStatisticsDO::getOrderPayCount))
  62 + .selectSum(ProductStatisticsDO::getOrderPayPrice,toUnderlineCase(ProductStatisticsDO::getOrderPayPrice))
  63 + .selectSum(ProductStatisticsDO::getAfterSaleCount,toUnderlineCase(ProductStatisticsDO::getAfterSaleCount))
  64 + .selectSum(ProductStatisticsDO::getAfterSaleRefundPrice,toUnderlineCase(ProductStatisticsDO::getAfterSaleRefundPrice))
  65 + .selectAvg(ProductStatisticsDO::getBrowseConvertPercent,toUnderlineCase(ProductStatisticsDO::getBrowseConvertPercent));
  66 + }
  67 +
  68 + /**
  69 + * 将驼峰命名转换为下划线命名
  70 + *
  71 + * @param func 字段名函数(驼峰命名)
  72 + * @return 字段名(下划线命名)
  73 + * @param <T> 泛型
  74 + * <p>
  75 + * fix:排序时排序字段被 {@link MyBatisUtils#buildPage} 由驼峰命名被转为下划线命名,
  76 + * 因此聚合函数的别名也做统一,统一为下划线命名,解决排序时找不到字段的问题。
  77 + */
  78 + private static <T> String toUnderlineCase(SFunction<T, ?> func) {
  79 + String fieldName = LambdaUtil.getFieldName(func);
  80 + return StrUtil.toUnderlineCase(fieldName);
62 } 81 }
63 82
64 /** 83 /**