学习记录优化查询速度

master
wenyu441069198 4 months ago
parent 87ac8ad505
commit 395f909cf2
  1. 26
      pom.xml
  2. 12
      src/main/java/com/teaching/backend/controller/records/CourseLearningRecordController.java
  3. 21
      src/main/java/com/teaching/backend/controller/records/LearningRecordsController.java
  4. 15
      src/main/java/com/teaching/backend/model/entity/records/LearningRecords.java
  5. 2
      src/main/java/com/teaching/backend/model/vo/records/LearningRecordsVo.java
  6. 125
      src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java
  7. 2
      src/main/java/com/teaching/backend/service/records/LearningRecordsService.java

@ -39,63 +39,49 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- freemarker jar -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
@ -111,20 +97,17 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://doc.xiaominfo.com/docs/quick-start#openapi2 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
@ -193,20 +176,23 @@
<version>2.12.5</version>
<scope>compile</scope>
</dependency>
<!--easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.6</version>
</dependency>
</dependencies>

@ -0,0 +1,12 @@
package com.teaching.backend.controller.records;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/courselearingRecord")
public class CourseLearningRecordController {
}

@ -1,6 +1,7 @@
package com.teaching.backend.controller.records;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
@ -37,17 +38,19 @@ public class LearningRecordsController {
public BaseResponse<Page> getAll(@RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
@RequestParam(value = "pagesize", defaultValue = "10") int pagesize,
@RequestParam String userId){
//根据用户查询
Page<LearningRecords> page = learningRecordsService.query()
.orderByDesc("time")
.eq("status", "1")
.eq("user_id", userId).page(new Page<>(pagenum, pagesize));
//获取当前页数据
List<LearningRecords> list = page.getRecords();
List<LearningRecordsVo> cs= learningRecordsService.getAll(list);
List<LearningRecordsVo> cs= learningRecordsService.getAll(userId);
List<LearningRecordsVo> pageCs = new ArrayList<>();
//当前页面
for(int i = (pagenum - 1) * pagesize; i < cs.size() && i < (pagenum) * pagesize; i++){
pageCs.add(cs.get(i));
}
long total = cs.size(); // 总记录数
long total = page.getTotal(); // 总记录数
Page<LearningRecordsVo> pageInfo = new Page<>(pagenum,pagesize,total);
pageInfo.setRecords(pageCs);
pageInfo.setPages((int)(Math.ceil((double) total / pagesize)));//设置总页数
pageInfo.setRecords(cs);
pageInfo.setPages(page.getPages());//设置总页数
return ResultUtils.success(pageInfo);
}

@ -2,6 +2,7 @@ package com.teaching.backend.model.entity.records;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@ -36,7 +37,7 @@ public class LearningRecords {
/**
* 访问时间
*/
private LocalDateTime accessTime;
private LocalDateTime time;
/**
* 学习方式:打开; 1,打开过,2未打开
@ -74,11 +75,11 @@ public class LearningRecords {
*/
private String type;
/**
* 父节点
* 课程名称
*/
private String parentNode;
private String coursesName;
/**
* 课程名称
*/
private int number;
}

@ -33,7 +33,7 @@ public class LearningRecordsVo {
/**
* 资源观看人数
*/
private Long number;
private int number;
/**

@ -12,7 +12,6 @@ import com.teaching.backend.mapper.records.LearningRecordsMapper;
import com.teaching.backend.model.entity.records.LearningRecords;
import com.teaching.backend.model.vo.records.LearningRecordsVo;
import com.teaching.backend.service.impl.CourseResourcesServiceImpl;
import com.teaching.backend.service.impl.chapter.ChapterServiceImpl;
import com.teaching.backend.service.impl.courses.CoursesServiceImpl;
import com.teaching.backend.service.records.LearningRecordsService;
import org.springframework.beans.BeanUtils;
@ -36,8 +35,8 @@ public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMappe
private LearningRecordsMapper learningRecordsMapper;
@Autowired
private CourseResourcesServiceImpl courseResourcesService;
@Autowired
private ChapterServiceImpl chapterService;
// @Autowired
// private ChapterServiceImpl chapterService;
@Override
public IPage<LearningRecords> selectPageList(Page<LearningRecords> page, LearningRecords model) {
@ -53,76 +52,64 @@ public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMappe
for (LearningRecords learningRecords : list) {
learningRecords.setStatus("0");
}
this.updateBatchById(list);
updateBatchById(list);
// System.out.println(this.updateBatchById(list));
// this.remove(lambdaQueryWrapper);
return "删除成功";
}
@Override
public List<LearningRecordsVo> getAll(String userId){
//格式化时间
public List<LearningRecordsVo> getAll(List<LearningRecords> list){
List<LearningRecordsVo> records = new ArrayList<>();
//格式化时间
String strDateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
List<LearningRecordsVo> list = new ArrayList<>();
//将学习记录实体转成学习记录Vo实体
LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
lambdaQueryWrapper1
.orderByDesc(LearningRecords::getAccessTime)
.eq(LearningRecords::getUserId, userId)
.eq(LearningRecords::getStatus, "1");
// System.out.println("查询数据库结果为:"+this.list(lambdaQueryWrapper1));
//如果为空-------------------?????
for (LearningRecords learningRecords : this.list(lambdaQueryWrapper1)) {
//查找该用户的学习记录
// if (learningRecords.getUserId().equals(userId) && learningRecords.getStatus().equals("1")){
LearningRecordsVo learningRecordsVo = new LearningRecordsVo();//好像可以拿出去-----------??
BeanUtils.copyProperties(learningRecords,learningRecordsVo);//从多传到少(缺少人数,和资源名称,时间需要转格式)
//添加时间
//LocalDateTime转Date
Date date = Date.from(learningRecords.getAccessTime().atZone(ZoneId.systemDefault()).toInstant());
learningRecordsVo.setTime(sdf.format(date));
LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(LearningRecords::getCoursesId, learningRecords.getCoursesId());
// System.out.println(learningRecords.getResourceId());
// System.out.println("1");
String name;
//添加课程名称
switch (learningRecords.getType()) {
case "4": //如果是资源学习
learningRecordsVo.setCoursesName(courseResourcesService.getById(learningRecords.getCoursesId()).getName());//设置名称 --------------------------------------------待完善
break;
case "3": //如果是知识点学习记录
// learningRecordsVo.setCoursesName(knowledgePointService.getById(learningRecords.getCoursesId()).get());
// if (knowledgePointService.getById(learningRecords.getCoursesId()) != null){
// name=knowledgePointService.getById(learningRecords.getCoursesId()).getName();
// }else {
// name = "查无次记录";
list.forEach(learningRecords -> {
LearningRecordsVo learningRecordsVo = new LearningRecordsVo();//好像可以拿出去-----------??
BeanUtils.copyProperties(learningRecords,learningRecordsVo);//从多传到少(时间需要转格式)
Date date = Date.from(learningRecords.getTime().atZone(ZoneId.systemDefault()).toInstant());
learningRecordsVo.setTime(sdf.format(date));
// //统计记录表里有多少人查看过这门资源
// LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// lambdaQueryWrapper.eq(LearningRecords::getCoursesId, learningRecords.getCoursesId());
// long count = this.count(lambdaQueryWrapper);
learningRecordsVo.setNumber(learningRecords.getNumber());
//把整合后该用户的学习记录添加到List里
records.add(learningRecordsVo);
});
// for (LearningRecords learningRecords : list) {
// //查找该用户的学习记录
// LearningRecordsVo learningRecordsVo = new LearningRecordsVo();//好像可以拿出去-----------??
// BeanUtils.copyProperties(learningRecords,learningRecordsVo);//从多传到少(缺少人数,和资源名称,时间需要转格式)
// //添加时间
// //LocalDateTime转Date
// Date date = Date.from(learningRecords.getTime().atZone(ZoneId.systemDefault()).toInstant());
// learningRecordsVo.setTime(sdf.format(date));
// LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// lambdaQueryWrapper.eq(LearningRecords::getCoursesId, learningRecords.getCoursesId());
// String name;
// //添加课程名称
// switch (learningRecords.getType()) {
// case "4": //如果是资源学习
// if (courseResourcesService.getById(learningRecords.getCoursesId()) != null){
// learningRecordsVo.setCoursesName(courseResourcesService.getById(learningRecords.getCoursesId()).getName());
// }
// learningRecordsVo.setCoursesName(knowledgePointService.getById(learningRecords.getCoursesId()).getName());
//如果对应的资源不存在
break;
case "2": //如果是章节学习记录
learningRecordsVo.setCoursesName(chapterService.getById(learningRecords.getCoursesId()).getName());
//如果对应的资源不存在
break;
case "1": //如果是课程学习记录
learningRecordsVo.setCoursesName(coursesService.getById(learningRecords.getCoursesId()).getName());
break;
}
//添加观看人数
//统计记录表里有多少人查看过这门资源
long count = this.count(lambdaQueryWrapper);
learningRecordsVo.setNumber(count);
//把整合后该用户的学习记录添加到List里
list.add(learningRecordsVo);
// break;
// case "1": //如果是课程学习记录
// if (coursesService.getById(learningRecords.getCoursesId()) != null){
// learningRecordsVo.setCoursesName(coursesService.getById(learningRecords.getCoursesId()).getName());
// }
// break;
// }
}
// System.out.println("查询后结果:"+list);
return list;
// //添加观看人数
// //统计记录表里有多少人查看过这门资源
// long count = this.count(lambdaQueryWrapper);
// learningRecordsVo.setNumber(count);
// //把整合后该用户的学习记录添加到List里
// records.add(learningRecordsVo);
// }
return records;
}
/**
@ -148,14 +135,26 @@ public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMappe
.eq(LearningRecords::getCoursesId, coursesId)
.eq(LearningRecords::getType, learningRecords.getType());
long count = this.count(lambdaQueryWrapper);
System.out.println("数据库:"+count);
//System.out.println("数据库:"+count);
learningRecords.setStatus("1");//将该记录的状态设置为1
//更新观看时间
learningRecords.setAccessTime(LocalDateTime.now());
learningRecords.setTime(LocalDateTime.now());
//设置默认封面
if (learningRecords.getContent() == null || learningRecords.getContent().equals("")){
learningRecords.setContent("img/v2-c57eb7863ec4233f503176ca3f90f8d5_1440w.png");
}
List<LearningRecords> list = query().eq("courses_id", coursesId).eq("type", learningRecords.getType()).list();
int number;
if (list != null){
number = list.get(0).getNumber() + 1;
learningRecords.setNumber(number);
list.forEach(learningRecords1 -> {
learningRecords1.setNumber(number);
});
} else {
number = 0;
}
updateBatchById(list);
if (count == 0){//如果没有就新增并记录学习方式是打开还是下载
//根据传上来的type设置
// learningRecords.setType(learningRecords.getType());

@ -10,7 +10,7 @@ import com.teaching.backend.model.vo.records.LearningRecordsVo;
import java.util.List;
public interface LearningRecordsService extends IService<LearningRecords> {
public List<LearningRecordsVo> getAll(String username);
public List<LearningRecordsVo> getAll(List<LearningRecords> list);
public String saveRecords(LearningRecords learningRecords);
IPage<LearningRecords> selectPageList(Page<LearningRecords> page, LearningRecords model);

Loading…
Cancel
Save