合并远程分支,解决房产模块排序功能冲突
This commit is contained in:
@@ -8,6 +8,7 @@ import com.gxwebsoft.house.entity.HouseViewsLog;
|
||||
import com.gxwebsoft.house.service.HouseInfoService;
|
||||
import com.gxwebsoft.house.entity.HouseInfo;
|
||||
import com.gxwebsoft.house.param.HouseInfoParam;
|
||||
import com.gxwebsoft.house.util.SortSceneUtil;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.common.core.web.PageParam;
|
||||
@@ -44,10 +45,17 @@ public class HouseInfoController extends BaseController {
|
||||
@Operation(summary = "分页查询房源信息表")
|
||||
@GetMapping("/page")
|
||||
public ApiResult<PageResult<HouseInfo>> page(HouseInfoParam param) {
|
||||
// 标准化排序参数,解决URL编码问题
|
||||
if (param.getSortScene() != null) {
|
||||
String normalizedSortScene = SortSceneUtil.normalizeSortScene(param.getSortScene());
|
||||
param.setSortScene(normalizedSortScene);
|
||||
}
|
||||
|
||||
// 使用关联查询
|
||||
return success(houseInfoService.pageRel(param));
|
||||
}
|
||||
|
||||
|
||||
@PreAuthorize("hasAuthority('house:houseInfo:list')")
|
||||
@Operation(summary = "查询全部房源信息表")
|
||||
@GetMapping()
|
||||
|
||||
@@ -129,47 +129,12 @@
|
||||
)
|
||||
</if>
|
||||
<!-- 价格区间筛选 -->
|
||||
<if test="param.priceScene != null">
|
||||
<choose>
|
||||
<when test="param.priceScene == '1000以下'">
|
||||
AND a.monthly_rent < 1000
|
||||
</when>
|
||||
<when test="param.priceScene == '1000-3000'">
|
||||
AND a.monthly_rent >= 1000 AND a.monthly_rent <= 3000
|
||||
</when>
|
||||
<when test="param.priceScene == '3000-5000'">
|
||||
AND a.monthly_rent >= 3000 AND a.monthly_rent <= 5000
|
||||
</when>
|
||||
<when test="param.priceScene == '5000-10000'">
|
||||
AND a.monthly_rent >= 5000 AND a.monthly_rent <= 10000
|
||||
</when>
|
||||
<when test="param.priceScene == '10000-20000'">
|
||||
AND a.monthly_rent >= 10000 AND a.monthly_rent <= 20000
|
||||
</when>
|
||||
<when test="param.priceScene == '20000以上'">
|
||||
AND a.monthly_rent > 20000
|
||||
</when>
|
||||
</choose>
|
||||
<if test="param.priceScene != null and param.priceScene.indexOf('~') > -1">
|
||||
<bind name="priceMin" value="param.priceScene.substring(0, param.priceScene.indexOf('~'))" />
|
||||
<bind name="priceMax" value="param.priceScene.substring(param.priceScene.indexOf('~') + 1)" />
|
||||
AND a.monthly_rent >= CAST(#{priceMin} AS DECIMAL(10,2))
|
||||
AND a.monthly_rent <= CAST(#{priceMax} AS DECIMAL(10,2))
|
||||
</if>
|
||||
<!-- 面积区间筛选 -->
|
||||
<if test="param.extentScene != null">
|
||||
<choose>
|
||||
<when test="param.extentScene == '50以下'">
|
||||
AND a.extent < 50
|
||||
</when>
|
||||
<when test="param.extentScene == '50-100'">
|
||||
AND a.extent >= 50 AND a.extent <= 100
|
||||
</when>
|
||||
<when test="param.extentScene == '100-150'">
|
||||
AND a.extent >= 100 AND a.extent <= 150
|
||||
</when>
|
||||
<when test="param.extentScene == '150-200'">
|
||||
AND a.extent >= 150 AND a.extent <= 200
|
||||
</when>
|
||||
<when test="param.extentScene == '200以上'">
|
||||
AND a.extent > 200
|
||||
</when>
|
||||
</choose>
|
||||
</if>
|
||||
</where>
|
||||
<trim prefix="ORDER BY" suffixOverrides=",">
|
||||
@@ -186,10 +151,28 @@
|
||||
CAST(IFNULL(a.monthly_rent, 0) AS DECIMAL(10,2)) desc,
|
||||
</if>
|
||||
<if test="param.sortScene == '面积(小-大)'">
|
||||
CAST(IFNULL(a.extent, 0) AS DECIMAL(10,2)) asc,
|
||||
CASE WHEN a.extent IS NULL OR a.extent = '' THEN 1 ELSE 0 END,
|
||||
CAST(COALESCE(NULLIF(a.extent, ''), '0') AS DECIMAL(10,2)) asc,
|
||||
</if>
|
||||
<if test="param.sortScene == '面积(大-小)'">
|
||||
CAST(IFNULL(a.extent, 0) AS DECIMAL(10,2)) desc,
|
||||
CASE WHEN a.extent IS NULL OR a.extent = '' THEN 1 ELSE 0 END,
|
||||
CAST(COALESCE(NULLIF(a.extent, ''), '0') AS DECIMAL(10,2)) desc,
|
||||
</if>
|
||||
<if test="param.priceScene != null and param.priceScene.indexOf('~') == -1">
|
||||
ABS(CAST(COALESCE(a.monthly_rent, 0) AS DECIMAL(10,2)) - CAST(#{param.priceScene} AS DECIMAL(10,2))),
|
||||
</if>
|
||||
<if test="param.extentScene != null and param.extentScene.indexOf('~') == -1">
|
||||
ABS(CAST(COALESCE(NULLIF(a.extent, ''), '0') AS DECIMAL(10,2)) - CAST(#{param.extentScene} AS DECIMAL(10,2))),
|
||||
</if>
|
||||
<!-- 默认排序:只有在没有指定排序场景时才使用 -->
|
||||
<if test="param.sortScene == null or param.sortScene == '' or param.sortScene == '综合排序'">
|
||||
a.sort_number asc, a.create_time desc
|
||||
</if>
|
||||
<if test="param.sortScene == '最新发布'">
|
||||
<!-- 最新发布已经在上面处理了 -->
|
||||
</if>
|
||||
<if test="param.sortScene != null and param.sortScene != '' and param.sortScene != '综合排序' and param.sortScene != '最新发布' and param.sortScene != '价格(低-高)' and param.sortScene != '价格(高-低)' and param.sortScene != '面积(小-大)' and param.sortScene != '面积(大-小)'">
|
||||
a.create_time desc
|
||||
</if>
|
||||
<!-- 默认排序:推荐优先,然后按创建时间倒序 -->
|
||||
a.recommend desc, a.create_time desc
|
||||
|
||||
@@ -63,7 +63,7 @@ public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo
|
||||
@Override
|
||||
public PageResult<HouseInfo> pageRel(HouseInfoParam param) {
|
||||
PageParam<HouseInfo, HouseInfoParam> page = new PageParam<>(param);
|
||||
// 只有在没有指定排序场景时才使用默认排序
|
||||
// 只有在没有指定排序场景时才设置默认排序
|
||||
if (param.getSortScene() == null || param.getSortScene().isEmpty()) {
|
||||
page.setDefaultOrder("create_time desc");
|
||||
}
|
||||
|
||||
128
src/main/java/com/gxwebsoft/house/util/SortSceneUtil.java
Normal file
128
src/main/java/com/gxwebsoft/house/util/SortSceneUtil.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package com.gxwebsoft.house.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
/**
|
||||
* 排序场景工具类
|
||||
* 用于处理前端传递的排序参数,解决URL编码和字符串匹配问题
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-04
|
||||
*/
|
||||
public class SortSceneUtil {
|
||||
|
||||
/**
|
||||
* 标准化排序场景参数
|
||||
* @param sortScene 原始排序场景参数
|
||||
* @return 标准化后的排序场景参数
|
||||
*/
|
||||
public static String normalizeSortScene(String sortScene) {
|
||||
if (StrUtil.isBlank(sortScene)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 尝试URL解码
|
||||
String decoded = sortScene;
|
||||
try {
|
||||
// 如果包含%,尝试URL解码
|
||||
if (sortScene.contains("%")) {
|
||||
decoded = URLDecoder.decode(sortScene, "UTF-8");
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// 解码失败,使用原始值
|
||||
decoded = sortScene;
|
||||
}
|
||||
|
||||
// 去除首尾空格
|
||||
decoded = decoded.trim();
|
||||
|
||||
// 标准化常见的排序场景
|
||||
if (decoded.contains("价格") && decoded.contains("低") && decoded.contains("高")) {
|
||||
if (decoded.contains("低-高") || decoded.contains("低到高") || decoded.contains("升序")) {
|
||||
return "价格(低-高)";
|
||||
} else if (decoded.contains("高-低") || decoded.contains("高到低") || decoded.contains("降序")) {
|
||||
return "价格(高-低)";
|
||||
}
|
||||
}
|
||||
|
||||
if (decoded.contains("面积") && decoded.contains("小") && decoded.contains("大")) {
|
||||
if (decoded.contains("小-大") || decoded.contains("小到大") || decoded.contains("升序")) {
|
||||
return "面积(小-大)";
|
||||
} else if (decoded.contains("大-小") || decoded.contains("大到小") || decoded.contains("降序")) {
|
||||
return "面积(大-小)";
|
||||
}
|
||||
}
|
||||
|
||||
if (decoded.contains("最新") || decoded.contains("时间") || decoded.contains("发布")) {
|
||||
return "最新发布";
|
||||
}
|
||||
|
||||
if (decoded.contains("综合") || decoded.contains("默认")) {
|
||||
return "综合排序";
|
||||
}
|
||||
|
||||
return decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为价格升序排序
|
||||
* @param sortScene 排序场景参数
|
||||
* @return true表示价格升序
|
||||
*/
|
||||
public static boolean isPriceAsc(String sortScene) {
|
||||
String normalized = normalizeSortScene(sortScene);
|
||||
return "价格(低-高)".equals(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为价格降序排序
|
||||
* @param sortScene 排序场景参数
|
||||
* @return true表示价格降序
|
||||
*/
|
||||
public static boolean isPriceDesc(String sortScene) {
|
||||
String normalized = normalizeSortScene(sortScene);
|
||||
return "价格(高-低)".equals(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为面积升序排序
|
||||
* @param sortScene 排序场景参数
|
||||
* @return true表示面积升序
|
||||
*/
|
||||
public static boolean isAreaAsc(String sortScene) {
|
||||
String normalized = normalizeSortScene(sortScene);
|
||||
return "面积(小-大)".equals(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为面积降序排序
|
||||
* @param sortScene 排序场景参数
|
||||
* @return true表示面积降序
|
||||
*/
|
||||
public static boolean isAreaDesc(String sortScene) {
|
||||
String normalized = normalizeSortScene(sortScene);
|
||||
return "面积(大-小)".equals(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为最新发布排序
|
||||
* @param sortScene 排序场景参数
|
||||
* @return true表示最新发布排序
|
||||
*/
|
||||
public static boolean isLatest(String sortScene) {
|
||||
String normalized = normalizeSortScene(sortScene);
|
||||
return "最新发布".equals(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为综合排序
|
||||
* @param sortScene 排序场景参数
|
||||
* @return true表示综合排序
|
||||
*/
|
||||
public static boolean isComprehensive(String sortScene) {
|
||||
String normalized = normalizeSortScene(sortScene);
|
||||
return "综合排序".equals(normalized);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.gxwebsoft.house.util;
|
||||
|
||||
/**
|
||||
* SortSceneUtil手动测试类
|
||||
* 用于验证URL解码功能
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-04
|
||||
*/
|
||||
public class SortSceneUtilManualTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 测试URL编码的参数
|
||||
String urlEncoded = "%E4%BB%B7%E6%A0%BC(%E4%BD%8E-%E9%AB%98)";
|
||||
System.out.println("原始URL编码参数: " + urlEncoded);
|
||||
|
||||
String result = SortSceneUtil.normalizeSortScene(urlEncoded);
|
||||
System.out.println("标准化后的参数: " + result);
|
||||
System.out.println("是否为价格升序: " + SortSceneUtil.isPriceAsc(urlEncoded));
|
||||
|
||||
// 测试其他格式
|
||||
String[] testCases = {
|
||||
"价格(低-高)",
|
||||
"价格(高-低)",
|
||||
"%E4%BB%B7%E6%A0%BC(%E9%AB%98-%E4%BD%8E)",
|
||||
"最新发布",
|
||||
"综合排序",
|
||||
"面积(小-大)",
|
||||
"面积(大-小)"
|
||||
};
|
||||
|
||||
System.out.println("\n=== 测试各种排序场景 ===");
|
||||
for (String testCase : testCases) {
|
||||
String normalized = SortSceneUtil.normalizeSortScene(testCase);
|
||||
System.out.println("输入: " + testCase + " -> 输出: " + normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.gxwebsoft.house.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* SortSceneUtil测试类
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-04
|
||||
*/
|
||||
public class SortSceneUtilTest {
|
||||
|
||||
@Test
|
||||
public void testNormalizeSortScene() {
|
||||
// 测试URL编码的参数
|
||||
String urlEncoded = "%E4%BB%B7%E6%A0%BC(%E4%BD%8E-%E9%AB%98)";
|
||||
String result = SortSceneUtil.normalizeSortScene(urlEncoded);
|
||||
assertEquals("价格(低-高)", result);
|
||||
|
||||
// 测试已解码的参数
|
||||
String decoded = "价格(低-高)";
|
||||
result = SortSceneUtil.normalizeSortScene(decoded);
|
||||
assertEquals("价格(低-高)", result);
|
||||
|
||||
// 测试空值
|
||||
result = SortSceneUtil.normalizeSortScene(null);
|
||||
assertNull(result);
|
||||
|
||||
result = SortSceneUtil.normalizeSortScene("");
|
||||
assertNull(result);
|
||||
|
||||
result = SortSceneUtil.normalizeSortScene(" ");
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPriceAsc() {
|
||||
assertTrue(SortSceneUtil.isPriceAsc("价格(低-高)"));
|
||||
assertTrue(SortSceneUtil.isPriceAsc("%E4%BB%B7%E6%A0%BC(%E4%BD%8E-%E9%AB%98)"));
|
||||
assertFalse(SortSceneUtil.isPriceAsc("价格(高-低)"));
|
||||
assertFalse(SortSceneUtil.isPriceAsc("最新发布"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPriceDesc() {
|
||||
assertTrue(SortSceneUtil.isPriceDesc("价格(高-低)"));
|
||||
assertFalse(SortSceneUtil.isPriceDesc("价格(低-高)"));
|
||||
assertFalse(SortSceneUtil.isPriceDesc("最新发布"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsLatest() {
|
||||
assertTrue(SortSceneUtil.isLatest("最新发布"));
|
||||
assertFalse(SortSceneUtil.isLatest("价格(低-高)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsComprehensive() {
|
||||
assertTrue(SortSceneUtil.isComprehensive("综合排序"));
|
||||
assertFalse(SortSceneUtil.isComprehensive("价格(低-高)"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user