Commit 39fcac4d5fb3c7d4d4217653f52863573e46fd86

Authored by YunaiV
1 parent 69b896a6

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

yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java
1 package cn.iocoder.yudao.framework.mybatis.core.util; 1 package cn.iocoder.yudao.framework.mybatis.core.util;
2 2
3 import cn.hutool.core.collection.CollectionUtil; 3 import cn.hutool.core.collection.CollectionUtil;
  4 +import cn.hutool.core.lang.func.Func1;
  5 +import cn.hutool.core.lang.func.LambdaUtil;
4 import cn.hutool.core.util.StrUtil; 6 import cn.hutool.core.util.StrUtil;
5 import cn.iocoder.yudao.framework.common.pojo.PageParam; 7 import cn.iocoder.yudao.framework.common.pojo.PageParam;
6 import cn.iocoder.yudao.framework.common.pojo.SortingField; 8 import cn.iocoder.yudao.framework.common.pojo.SortingField;
@@ -103,4 +105,18 @@ public class MyBatisUtils { @@ -103,4 +105,18 @@ public class MyBatisUtils {
103 .replace("#{value}", StrUtil.toString(value)); 105 .replace("#{value}", StrUtil.toString(value));
104 } 106 }
105 107
  108 + /**
  109 + * 将驼峰命名转换为下划线命名
  110 + *
  111 + * 使用场景:
  112 + * 1. <a href="https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1357/files">fix:修复"商品统计聚合函数的别名与排序字段不符"导致的 SQL 异常</a>
  113 + *
  114 + * @param func 字段名函数(驼峰命名)
  115 + * @return 字段名(下划线命名)
  116 + */
  117 + public static <T> String toUnderlineCase(Func1<T, ?> func) {
  118 + String fieldName = LambdaUtil.getFieldName(func);
  119 + return StrUtil.toUnderlineCase(fieldName);
  120 + }
  121 +
106 } 122 }
yudao-module-mall/yudao-module-promotion/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java
@@ -165,7 +165,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService { @@ -165,7 +165,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
165 ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId()); 165 ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
166 ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId()); 166 ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
167 CombinationRecordDO record = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku); 167 CombinationRecordDO record = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
168 - record.setPicUrl(spu.getPicUrl()); // fix:拼团记录创建时,需要设置拼团商品的图片  
169 // 2.1. 如果是团长需要设置 headId 为 CombinationRecordDO#HEAD_ID_GROUP 168 // 2.1. 如果是团长需要设置 headId 为 CombinationRecordDO#HEAD_ID_GROUP
170 if (record.getHeadId() == null) { 169 if (record.getHeadId() == null) {
171 record.setStartTime(LocalDateTime.now()) 170 record.setStartTime(LocalDateTime.now())
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;  
4 import cn.iocoder.yudao.framework.common.pojo.PageResult; 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 import cn.iocoder.yudao.framework.common.pojo.SortablePageParam; 4 import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
6 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; 5 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
7 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; 6 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
8 import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX; 7 import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
9 -import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;  
10 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsReqVO; 8 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsReqVO;
11 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsRespVO; 9 import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsRespVO;
12 import cn.iocoder.yudao.module.statistics.dal.dataobject.product.ProductStatisticsDO; 10 import cn.iocoder.yudao.module.statistics.dal.dataobject.product.ProductStatisticsDO;
13 import com.baomidou.mybatisplus.core.metadata.IPage; 11 import com.baomidou.mybatisplus.core.metadata.IPage;
14 -import com.baomidou.mybatisplus.core.toolkit.support.SFunction;  
15 import org.apache.ibatis.annotations.Mapper; 12 import org.apache.ibatis.annotations.Mapper;
16 import org.apache.ibatis.annotations.Param; 13 import org.apache.ibatis.annotations.Param;
17 -import org.dromara.hutool.core.func.LambdaUtil;  
18 14
19 import java.time.LocalDateTime; 15 import java.time.LocalDateTime;
20 import java.util.List; 16 import java.util.List;
21 17
  18 +import static cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils.toUnderlineCase;
  19 +
22 /** 20 /**
23 * 商品统计 Mapper 21 * 商品统计 Mapper
24 * 22 *
@@ -53,31 +51,16 @@ public interface ProductStatisticsMapper extends BaseMapperX&lt;ProductStatisticsDO @@ -53,31 +51,16 @@ public interface ProductStatisticsMapper extends BaseMapperX&lt;ProductStatisticsDO
53 private static MPJLambdaWrapperX<ProductStatisticsDO> buildWrapper(ProductStatisticsReqVO reqVO) { 51 private static MPJLambdaWrapperX<ProductStatisticsDO> buildWrapper(ProductStatisticsReqVO reqVO) {
54 return new MPJLambdaWrapperX<ProductStatisticsDO>() 52 return new MPJLambdaWrapperX<ProductStatisticsDO>()
55 .betweenIfPresent(ProductStatisticsDO::getTime, reqVO.getTimes()) 53 .betweenIfPresent(ProductStatisticsDO::getTime, reqVO.getTimes())
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); 54 + .selectSum(ProductStatisticsDO::getBrowseCount, toUnderlineCase(ProductStatisticsDO::getBrowseCount))
  55 + .selectSum(ProductStatisticsDO::getBrowseUserCount, toUnderlineCase(ProductStatisticsDO::getBrowseUserCount))
  56 + .selectSum(ProductStatisticsDO::getFavoriteCount, toUnderlineCase(ProductStatisticsDO::getFavoriteCount))
  57 + .selectSum(ProductStatisticsDO::getCartCount, toUnderlineCase(ProductStatisticsDO::getCartCount))
  58 + .selectSum(ProductStatisticsDO::getOrderCount, toUnderlineCase(ProductStatisticsDO::getOrderCount))
  59 + .selectSum(ProductStatisticsDO::getOrderPayCount, toUnderlineCase(ProductStatisticsDO::getOrderPayCount))
  60 + .selectSum(ProductStatisticsDO::getOrderPayPrice, toUnderlineCase(ProductStatisticsDO::getOrderPayPrice))
  61 + .selectSum(ProductStatisticsDO::getAfterSaleCount, toUnderlineCase(ProductStatisticsDO::getAfterSaleCount))
  62 + .selectSum(ProductStatisticsDO::getAfterSaleRefundPrice, toUnderlineCase(ProductStatisticsDO::getAfterSaleRefundPrice))
  63 + .selectAvg(ProductStatisticsDO::getBrowseConvertPercent, toUnderlineCase(ProductStatisticsDO::getBrowseConvertPercent));
81 } 64 }
82 65
83 /** 66 /**