首页查询优化
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
package com.sqx.modules.course.service.impl;
|
package com.sqx.modules.course.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.DateUtil;
|
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.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
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.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
import com.sqx.common.utils.DateUtils;
|
import com.sqx.common.utils.DateUtils;
|
||||||
import com.sqx.common.utils.PageUtils;
|
import com.sqx.common.utils.PageUtils;
|
||||||
|
import com.sqx.common.utils.RedisUtils;
|
||||||
import com.sqx.common.utils.Result;
|
import com.sqx.common.utils.Result;
|
||||||
import com.sqx.modules.app.utils.JwtUtils;
|
import com.sqx.modules.app.utils.JwtUtils;
|
||||||
import com.sqx.modules.common.service.CommonInfoService;
|
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.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -68,6 +73,29 @@ public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CourseUserService courseUserService;
|
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);
|
Page<Map<String, Object>> pages = new Page<>(page, limit);
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
// 一周的第一天
|
||||||
Calendar calendar = Calendar.getInstance();
|
DateTime begin = DateUtil.beginOfWeek(new Date());
|
||||||
// 一周第一天为周日,所以此处日+1
|
String startTime = begin.toString();
|
||||||
calendar.setWeekDate(calendar.getWeekYear(), calendar.get(Calendar.WEEK_OF_YEAR), 2);
|
// 一周的最后一天
|
||||||
calendar.set(calendar.get(Calendar.YEAR),
|
DateTime end = DateUtil.endOfWeek(new Date());
|
||||||
calendar.get(Calendar.MONTH),
|
String endTime = end.toString();
|
||||||
calendar.get(Calendar.DAY_OF_MONTH),
|
String cacheKey = getIndexKey(admin, userId, page, limit, classifyId, title, isRecommend, status, bannerId,
|
||||||
0, 0, 0);
|
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
|
||||||
String startTime = sdf.format(calendar.getTime());
|
String cache = getCache(cacheKey);
|
||||||
// 一周第一天为周日,所以此处为下一周第一天
|
if (StrUtil.isNotEmpty(cache)) {
|
||||||
calendar.setWeekDate(calendar.getWeekYear(), calendar.get(Calendar.WEEK_OF_YEAR) + 1, 1);
|
Map<String, Object> data = JSONUtil.toBean(cache, Map.class);
|
||||||
calendar.set(calendar.get(Calendar.YEAR),
|
return Result.success().put("data", data);
|
||||||
calendar.get(Calendar.MONTH),
|
}
|
||||||
calendar.get(Calendar.DAY_OF_MONTH),
|
|
||||||
23, 59, 59);
|
|
||||||
String endTime = sdf.format(calendar.getTime());
|
|
||||||
if (admin == null) {
|
if (admin == null) {
|
||||||
IPage<Map<String, Object>> mapIPage = baseMapper.selectCourse(pages, classifyId, title, isRecommend, status, bannerId,
|
IPage<Map<String, Object>> mapIPage = baseMapper.selectCourse(pages, classifyId, title, isRecommend, status, bannerId,
|
||||||
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
|
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
|
||||||
sort = ObjectUtil.defaultIfNull(sort, 0);
|
|
||||||
List<Map<String, Object>> records = mapIPage.getRecords();
|
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) {
|
for (Map<String, Object> map : records) {
|
||||||
Object courseDetailsId = map.get("courseDetailsId");
|
Object courseDetailsId = map.get("courseDetailsId");
|
||||||
Object courseId = map.get("courseId");
|
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));
|
return Result.success().put("data", new PageUtils(mapIPage));
|
||||||
}
|
}
|
||||||
IPage<Map<String, Object>> mapIPage = baseMapper.selectCourseAdmin(pages, classifyId, title, isRecommend, status, bannerId,
|
IPage<Map<String, Object>> mapIPage = baseMapper.selectCourseAdmin(pages, classifyId, title, isRecommend, status, bannerId,
|
||||||
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
|
sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow);
|
||||||
List<Map<String, Object>> records = mapIPage.getRecords();
|
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) {
|
for (Map<String, Object> map : records) {
|
||||||
Object courseId = map.get("courseId");
|
Object courseId = map.get("courseId");
|
||||||
//默认取第一集
|
//默认取第一集
|
||||||
@@ -205,6 +213,7 @@ public class CourseServiceImpl extends ServiceImpl<CourseDao, Course> implements
|
|||||||
map.put("wxCourseDetailsId", courseDetails.getWxCourseDetailsId());
|
map.put("wxCourseDetailsId", courseDetails.getWxCourseDetailsId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setCache(cacheKey, JSONUtil.toJsonStr(new PageUtils(mapIPage)));
|
||||||
return Result.success().put("data", new PageUtils(mapIPage));
|
return Result.success().put("data", new PageUtils(mapIPage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -116,14 +116,12 @@
|
|||||||
<if test="sort==null">
|
<if test="sort==null">
|
||||||
order by c.sort asc
|
order by c.sort asc
|
||||||
</if>
|
</if>
|
||||||
<!--
|
|
||||||
<if test="sort!=null and sort==1">
|
<if test="sort!=null and sort==1">
|
||||||
order by goodNum desc
|
order by goodNum desc
|
||||||
</if>
|
</if>
|
||||||
<if test="sort!=null and sort==2">
|
<if test="sort!=null and sort==2">
|
||||||
order by weekGoodNum desc
|
order by weekGoodNum desc
|
||||||
</if>
|
</if>
|
||||||
-->
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectCourseAdmin" resultType="Map">
|
<select id="selectCourseAdmin" resultType="Map">
|
||||||
@@ -246,14 +244,12 @@
|
|||||||
<if test="sort==null">
|
<if test="sort==null">
|
||||||
order by c.sort asc,c.create_time desc
|
order by c.sort asc,c.create_time desc
|
||||||
</if>
|
</if>
|
||||||
<!--
|
|
||||||
<if test="sort!=null and sort==1">
|
<if test="sort!=null and sort==1">
|
||||||
order by goodNum desc
|
order by goodNum desc
|
||||||
</if>
|
</if>
|
||||||
<if test="sort!=null and sort==2">
|
<if test="sort!=null and sort==2">
|
||||||
order by weekGoodNum desc
|
order by weekGoodNum desc
|
||||||
</if>
|
</if>
|
||||||
-->
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectCourseTitle" resultType="Map">
|
<select id="selectCourseTitle" resultType="Map">
|
||||||
|
|||||||
Reference in New Issue
Block a user