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

This commit is contained in:
YunaiV 2025-06-15 15:05:27 +08:00
parent 69b896a688
commit 39fcac4d5f
3 changed files with 28 additions and 30 deletions

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.framework.mybatis.core.util; package cn.iocoder.yudao.framework.mybatis.core.util;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.SortingField; import cn.iocoder.yudao.framework.common.pojo.SortingField;
@ -103,4 +105,18 @@ public class MyBatisUtils {
.replace("#{value}", StrUtil.toString(value)); .replace("#{value}", StrUtil.toString(value));
} }
/**
* 将驼峰命名转换为下划线命名
*
* 使用场景
* 1. <a href="https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1357/files">fix:修复"商品统计聚合函数的别名与排序字段不符"导致的 SQL 异常</a>
*
* @param func 字段名函数(驼峰命名)
* @return 字段名(下划线命名)
*/
public static <T> String toUnderlineCase(Func1<T, ?> func) {
String fieldName = LambdaUtil.getFieldName(func);
return StrUtil.toUnderlineCase(fieldName);
}
} }

View File

@ -165,7 +165,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId()); ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId()); ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
CombinationRecordDO record = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku); CombinationRecordDO record = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
record.setPicUrl(spu.getPicUrl()); // fix拼团记录创建时需要设置拼团商品的图片
// 2.1. 如果是团长需要设置 headId CombinationRecordDO#HEAD_ID_GROUP // 2.1. 如果是团长需要设置 headId CombinationRecordDO#HEAD_ID_GROUP
if (record.getHeadId() == null) { if (record.getHeadId() == null) {
record.setStartTime(LocalDateTime.now()) record.setStartTime(LocalDateTime.now())

View File

@ -1,24 +1,22 @@
package cn.iocoder.yudao.module.statistics.dal.mysql.product; package cn.iocoder.yudao.module.statistics.dal.mysql.product;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortablePageParam; import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsReqVO; import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsReqVO;
import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsRespVO; import cn.iocoder.yudao.module.statistics.controller.admin.product.vo.ProductStatisticsRespVO;
import cn.iocoder.yudao.module.statistics.dal.dataobject.product.ProductStatisticsDO; import cn.iocoder.yudao.module.statistics.dal.dataobject.product.ProductStatisticsDO;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.dromara.hutool.core.func.LambdaUtil;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils.toUnderlineCase;
/** /**
* 商品统计 Mapper * 商品统计 Mapper
* *
@ -53,31 +51,16 @@ public interface ProductStatisticsMapper extends BaseMapperX<ProductStatisticsDO
private static MPJLambdaWrapperX<ProductStatisticsDO> buildWrapper(ProductStatisticsReqVO reqVO) { private static MPJLambdaWrapperX<ProductStatisticsDO> buildWrapper(ProductStatisticsReqVO reqVO) {
return new MPJLambdaWrapperX<ProductStatisticsDO>() return new MPJLambdaWrapperX<ProductStatisticsDO>()
.betweenIfPresent(ProductStatisticsDO::getTime, reqVO.getTimes()) .betweenIfPresent(ProductStatisticsDO::getTime, reqVO.getTimes())
.selectSum(ProductStatisticsDO::getBrowseCount,toUnderlineCase(ProductStatisticsDO::getBrowseCount)) .selectSum(ProductStatisticsDO::getBrowseCount, toUnderlineCase(ProductStatisticsDO::getBrowseCount))
.selectSum(ProductStatisticsDO::getBrowseUserCount,toUnderlineCase(ProductStatisticsDO::getBrowseUserCount)) .selectSum(ProductStatisticsDO::getBrowseUserCount, toUnderlineCase(ProductStatisticsDO::getBrowseUserCount))
.selectSum(ProductStatisticsDO::getFavoriteCount,toUnderlineCase(ProductStatisticsDO::getFavoriteCount)) .selectSum(ProductStatisticsDO::getFavoriteCount, toUnderlineCase(ProductStatisticsDO::getFavoriteCount))
.selectSum(ProductStatisticsDO::getCartCount,toUnderlineCase(ProductStatisticsDO::getCartCount)) .selectSum(ProductStatisticsDO::getCartCount, toUnderlineCase(ProductStatisticsDO::getCartCount))
.selectSum(ProductStatisticsDO::getOrderCount,toUnderlineCase(ProductStatisticsDO::getOrderCount)) .selectSum(ProductStatisticsDO::getOrderCount, toUnderlineCase(ProductStatisticsDO::getOrderCount))
.selectSum(ProductStatisticsDO::getOrderPayCount,toUnderlineCase(ProductStatisticsDO::getOrderPayCount)) .selectSum(ProductStatisticsDO::getOrderPayCount, toUnderlineCase(ProductStatisticsDO::getOrderPayCount))
.selectSum(ProductStatisticsDO::getOrderPayPrice,toUnderlineCase(ProductStatisticsDO::getOrderPayPrice)) .selectSum(ProductStatisticsDO::getOrderPayPrice, toUnderlineCase(ProductStatisticsDO::getOrderPayPrice))
.selectSum(ProductStatisticsDO::getAfterSaleCount,toUnderlineCase(ProductStatisticsDO::getAfterSaleCount)) .selectSum(ProductStatisticsDO::getAfterSaleCount, toUnderlineCase(ProductStatisticsDO::getAfterSaleCount))
.selectSum(ProductStatisticsDO::getAfterSaleRefundPrice,toUnderlineCase(ProductStatisticsDO::getAfterSaleRefundPrice)) .selectSum(ProductStatisticsDO::getAfterSaleRefundPrice, toUnderlineCase(ProductStatisticsDO::getAfterSaleRefundPrice))
.selectAvg(ProductStatisticsDO::getBrowseConvertPercent,toUnderlineCase(ProductStatisticsDO::getBrowseConvertPercent)); .selectAvg(ProductStatisticsDO::getBrowseConvertPercent, toUnderlineCase(ProductStatisticsDO::getBrowseConvertPercent));
}
/**
* 将驼峰命名转换为下划线命名
*
* @param func 字段名函数(驼峰命名)
* @return 字段名(下划线命名)
* @param <T> 泛型
* <p>
* fix:排序时排序字段被 {@link MyBatisUtils#buildPage} 由驼峰命名被转为下划线命名
* 因此聚合函数的别名也做统一统一为下划线命名解决排序时找不到字段的问题
*/
private static <T> String toUnderlineCase(SFunction<T, ?> func) {
String fieldName = LambdaUtil.getFieldName(func);
return StrUtil.toUnderlineCase(fieldName);
} }
/** /**