From 0618f81f82100b2e9b56a39785ec6670e8eb92b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=AD=E5=87=AF=E5=87=AF?= Date: Fri, 27 Dec 2024 10:43:25 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=9F=A5=E8=AF=A2=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CourseServiceImpl.java | 81 ++++++++++--------- .../resources/mapper/course/CourseDao.xml | 4 - 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java b/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java index 88f10b1a..75cc7180 100644 --- a/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java +++ b/src/main/java/com/sqx/modules/course/service/impl/CourseServiceImpl.java @@ -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 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 implements } } Page> 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 data = JSONUtil.toBean(cache, Map.class); + return Result.success().put("data", data); + } if (admin == null) { IPage> 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> 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 map : records) { Object courseDetailsId = map.get("courseDetailsId"); Object courseId = map.get("courseId"); @@ -179,20 +195,12 @@ public class CourseServiceImpl extends ServiceImpl implements } } } + setCache(cacheKey, JSONUtil.toJsonStr(new PageUtils(mapIPage))); return Result.success().put("data", new PageUtils(mapIPage)); } IPage> mapIPage = baseMapper.selectCourseAdmin(pages, classifyId, title, isRecommend, status, bannerId, sort, startTime, endTime, userId, isPrice, over, wxCourse, dyCourse, wxShow, dyShow); List> 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 map : records) { Object courseId = map.get("courseId"); //默认取第一集 @@ -205,6 +213,7 @@ public class CourseServiceImpl extends ServiceImpl implements map.put("wxCourseDetailsId", courseDetails.getWxCourseDetailsId()); } } + setCache(cacheKey, JSONUtil.toJsonStr(new PageUtils(mapIPage))); return Result.success().put("data", new PageUtils(mapIPage)); } diff --git a/src/main/resources/mapper/course/CourseDao.xml b/src/main/resources/mapper/course/CourseDao.xml index a9e536f1..a8118457 100644 --- a/src/main/resources/mapper/course/CourseDao.xml +++ b/src/main/resources/mapper/course/CourseDao.xml @@ -116,14 +116,12 @@ order by c.sort asc -