首页查询优化

This commit is contained in:
谭凯凯
2024-12-27 10:43:25 +08:00
committed by Tankaikai
parent bce2fac418
commit 0618f81f82
2 changed files with 45 additions and 40 deletions

View File

@@ -1,8 +1,12 @@
package com.sqx.modules.course.service.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -13,6 +17,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.RedisUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.utils.JwtUtils;
import com.sqx.modules.common.service.CommonInfoService;
@@ -40,13 +45,13 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
@Service
@Slf4j
@@ -68,6 +73,29 @@ public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements
@Autowired
private CourseUserService courseUserService;
@Resource
private RedisUtils redisUtils;
private static String getIndexKey(Object... objs) {
final StrBuilder sb = new StrBuilder();
for (Object obj : objs) {
sb.append(obj == null ? "XXX" : Convert.toStr(obj));
sb.append("_");
}
String key = SecureUtil.md5(sb.toString());
return StrUtil.format("app:index:{}", key);
}
private void setCache(String key, String value) {
key = getIndexKey(key);
redisUtils.set(key, value, 60);
}
private String getCache(String key) {
key = getIndexKey(key);
return redisUtils.get(key);
}
/**
* 创建线程池处理业务逻辑
*/
@@ -128,35 +156,23 @@ public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements
}
}
Page<Map<String, Object>> pages = new Page<>(page, limit);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
// 一周第一天为周日,所以此处日+1
calendar.setWeekDate(calendar.getWeekYear(), calendar.get(Calendar.WEEK_OF_YEAR), 2);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
0, 0, 0);
String startTime = sdf.format(calendar.getTime());
// 一周第一天为周日,所以此处为下一周第一天
calendar.setWeekDate(calendar.getWeekYear(), calendar.get(Calendar.WEEK_OF_YEAR) + 1, 1);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
23, 59, 59);
String endTime = sdf.format(calendar.getTime());
// 一周的第一天
DateTime begin = DateUtil.beginOfWeek(new Date());
String startTime = begin.toString();
// 一周的最后一天
DateTime end = DateUtil.endOfWeek(new Date());
String endTime = end.toString();
String cacheKey = getIndexKey(admin, userId, page, limit, classifyId, title, isRecommend, status, bannerId,
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
String cache = getCache(cacheKey);
if (StrUtil.isNotEmpty(cache)) {
Map<String, Object> data = JSONUtil.toBean(cache, Map.class);
return Result.success().put("data", data);
}
if (admin == null) {
IPage<Map<String, Object>> mapIPage = baseMapper.selectCourse(pages, classifyId, title, isRecommend, status, bannerId,
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
sort = ObjectUtil.defaultIfNull(sort, 0);
List<Map<String, Object>> records = mapIPage.getRecords();
if (sort != null && sort == 1) {
records = records.stream().sorted(Comparator.comparingLong(item -> Convert.toLong(item.get("goodNum")))).collect(Collectors.toList());
Collections.reverse(records);
}
if (sort != null && sort == 2) {
records = records.stream().sorted(Comparator.comparingLong(item -> Convert.toLong(item.get("weekGoodNum")))).collect(Collectors.toList());
Collections.reverse(records);
}
for (Map<String, Object> map : records) {
Object courseDetailsId = map.get("courseDetailsId");
Object courseId = map.get("courseId");
@@ -179,20 +195,12 @@ public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements
}
}
}
setCache(cacheKey, JSONUtil.toJsonStr(new PageUtils(mapIPage)));
return Result.success().put("data", new PageUtils(mapIPage));
}
IPage<Map<String, Object>> mapIPage = baseMapper.selectCourseAdmin(pages, classifyId, title, isRecommend, status, bannerId,
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
List<Map<String, Object>> records = mapIPage.getRecords();
sort = ObjectUtil.defaultIfNull(sort, 0);
if (sort != null && sort == 1) {
records = records.stream().sorted(Comparator.comparingLong(item -> Convert.toLong(item.get("goodNum")))).collect(Collectors.toList());
Collections.reverse(records);
}
if (sort != null && sort == 2) {
records = records.stream().sorted(Comparator.comparingLong(item -> Convert.toLong(item.get("weekGoodNum")))).collect(Collectors.toList());
Collections.reverse(records);
}
for (Map<String, Object> map : records) {
Object courseId = map.get("courseId");
//默认取第一集
@@ -205,6 +213,7 @@ public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements
map.put("wxCourseDetailsId", courseDetails.getWxCourseDetailsId());
}
}
setCache(cacheKey, JSONUtil.toJsonStr(new PageUtils(mapIPage)));
return Result.success().put("data", new PageUtils(mapIPage));
}