学习记录优化查询速度

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> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.11.0</version> <version>2.11.0</version>
</dependency> </dependency>
<!-- freemarker jar --> <!-- freemarker jar -->
<dependency> <dependency>
<groupId>org.freemarker</groupId> <groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId> <artifactId>freemarker</artifactId>
<version>2.3.28</version> <version>2.3.28</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId> <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version> <version>2.2.2</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version> <version>3.5.2</version>
</dependency> </dependency>
<!-- Apache POI --> <!-- Apache POI -->
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>4.1.2</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>4.1.2</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId> <artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.aliyun.oss</groupId> <groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId> <artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version> <version>3.15.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
@ -111,20 +97,17 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://doc.xiaominfo.com/docs/quick-start#openapi2 --> <!-- https://doc.xiaominfo.com/docs/quick-start#openapi2 -->
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.4.0</version> <version>4.4.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
@ -193,20 +176,23 @@
<version>2.12.5</version> <version>2.12.5</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!--easypoi--> <!--easypoi-->
<dependency> <dependency>
<groupId>cn.afterturn</groupId> <groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId> <artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version> <version>4.1.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.8.0</version> <version>2.8.0</version>
</dependency> </dependency>
<!--redis-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.6</version>
</dependency>
</dependencies> </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; package com.teaching.backend.controller.records;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils; import com.teaching.backend.common.ResultUtils;
@ -37,17 +38,19 @@ public class LearningRecordsController {
public BaseResponse<Page> getAll(@RequestParam(value = "pagenum", defaultValue = "1") int pagenum, public BaseResponse<Page> getAll(@RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
@RequestParam(value = "pagesize", defaultValue = "10") int pagesize, @RequestParam(value = "pagesize", defaultValue = "10") int pagesize,
@RequestParam String userId){ @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); long total = page.getTotal(); // 总记录数
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(); // 总记录数
Page<LearningRecordsVo> pageInfo = new Page<>(pagenum,pagesize,total); Page<LearningRecordsVo> pageInfo = new Page<>(pagenum,pagesize,total);
pageInfo.setRecords(pageCs); pageInfo.setRecords(cs);
pageInfo.setPages((int)(Math.ceil((double) total / pagesize)));//设置总页数 pageInfo.setPages(page.getPages());//设置总页数
return ResultUtils.success(pageInfo); 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.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
@ -36,7 +37,7 @@ public class LearningRecords {
/** /**
* 访问时间 * 访问时间
*/ */
private LocalDateTime accessTime; private LocalDateTime time;
/** /**
* 学习方式:打开; 1,打开过,2未打开 * 学习方式:打开; 1,打开过,2未打开
@ -74,11 +75,11 @@ public class LearningRecords {
*/ */
private String type; 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.entity.records.LearningRecords;
import com.teaching.backend.model.vo.records.LearningRecordsVo; import com.teaching.backend.model.vo.records.LearningRecordsVo;
import com.teaching.backend.service.impl.CourseResourcesServiceImpl; 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.impl.courses.CoursesServiceImpl;
import com.teaching.backend.service.records.LearningRecordsService; import com.teaching.backend.service.records.LearningRecordsService;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -36,8 +35,8 @@ public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMappe
private LearningRecordsMapper learningRecordsMapper; private LearningRecordsMapper learningRecordsMapper;
@Autowired @Autowired
private CourseResourcesServiceImpl courseResourcesService; private CourseResourcesServiceImpl courseResourcesService;
@Autowired // @Autowired
private ChapterServiceImpl chapterService; // private ChapterServiceImpl chapterService;
@Override @Override
public IPage<LearningRecords> selectPageList(Page<LearningRecords> page, LearningRecords model) { public IPage<LearningRecords> selectPageList(Page<LearningRecords> page, LearningRecords model) {
@ -53,76 +52,64 @@ public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMappe
for (LearningRecords learningRecords : list) { for (LearningRecords learningRecords : list) {
learningRecords.setStatus("0"); learningRecords.setStatus("0");
} }
this.updateBatchById(list); updateBatchById(list);
// System.out.println(this.updateBatchById(list)); // System.out.println(this.updateBatchById(list));
// this.remove(lambdaQueryWrapper); // this.remove(lambdaQueryWrapper);
return "删除成功"; return "删除成功";
} }
@Override @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"; String strDateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); 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)) { list.forEach(learningRecords -> {
//查找该用户的学习记录 LearningRecordsVo learningRecordsVo = new LearningRecordsVo();//好像可以拿出去-----------??
// if (learningRecords.getUserId().equals(userId) && learningRecords.getStatus().equals("1")){ BeanUtils.copyProperties(learningRecords,learningRecordsVo);//从多传到少(时间需要转格式)
LearningRecordsVo learningRecordsVo = new LearningRecordsVo();//好像可以拿出去-----------?? Date date = Date.from(learningRecords.getTime().atZone(ZoneId.systemDefault()).toInstant());
BeanUtils.copyProperties(learningRecords,learningRecordsVo);//从多传到少(缺少人数,和资源名称,时间需要转格式) learningRecordsVo.setTime(sdf.format(date));
//添加时间 // //统计记录表里有多少人查看过这门资源
//LocalDateTime转Date // LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
Date date = Date.from(learningRecords.getAccessTime().atZone(ZoneId.systemDefault()).toInstant()); // lambdaQueryWrapper.eq(LearningRecords::getCoursesId, learningRecords.getCoursesId());
learningRecordsVo.setTime(sdf.format(date)); // long count = this.count(lambdaQueryWrapper);
LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>(); learningRecordsVo.setNumber(learningRecords.getNumber());
lambdaQueryWrapper.eq(LearningRecords::getCoursesId, learningRecords.getCoursesId()); //把整合后该用户的学习记录添加到List里
// System.out.println(learningRecords.getResourceId()); records.add(learningRecordsVo);
// System.out.println("1"); });
// for (LearningRecords learningRecords : list) {
String name; // //查找该用户的学习记录
//添加课程名称 // LearningRecordsVo learningRecordsVo = new LearningRecordsVo();//好像可以拿出去-----------??
switch (learningRecords.getType()) { // BeanUtils.copyProperties(learningRecords,learningRecordsVo);//从多传到少(缺少人数,和资源名称,时间需要转格式)
case "4": //如果是资源学习 // //添加时间
learningRecordsVo.setCoursesName(courseResourcesService.getById(learningRecords.getCoursesId()).getName());//设置名称 --------------------------------------------待完善 // //LocalDateTime转Date
break; // Date date = Date.from(learningRecords.getTime().atZone(ZoneId.systemDefault()).toInstant());
case "3": //如果是知识点学习记录 // learningRecordsVo.setTime(sdf.format(date));
// learningRecordsVo.setCoursesName(knowledgePointService.getById(learningRecords.getCoursesId()).get()); // LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// if (knowledgePointService.getById(learningRecords.getCoursesId()) != null){ // lambdaQueryWrapper.eq(LearningRecords::getCoursesId, learningRecords.getCoursesId());
// name=knowledgePointService.getById(learningRecords.getCoursesId()).getName(); // String name;
// }else { // //添加课程名称
// 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 "1": //如果是课程学习记录
break; // if (coursesService.getById(learningRecords.getCoursesId()) != null){
case "2": //如果是章节学习记录 // learningRecordsVo.setCoursesName(coursesService.getById(learningRecords.getCoursesId()).getName());
learningRecordsVo.setCoursesName(chapterService.getById(learningRecords.getCoursesId()).getName()); // }
//如果对应的资源不存在 // break;
break;
case "1": //如果是课程学习记录
learningRecordsVo.setCoursesName(coursesService.getById(learningRecords.getCoursesId()).getName());
break;
}
//添加观看人数
//统计记录表里有多少人查看过这门资源
long count = this.count(lambdaQueryWrapper);
learningRecordsVo.setNumber(count);
//把整合后该用户的学习记录添加到List里
list.add(learningRecordsVo);
// } // }
} // //添加观看人数
// 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::getCoursesId, coursesId)
.eq(LearningRecords::getType, learningRecords.getType()); .eq(LearningRecords::getType, learningRecords.getType());
long count = this.count(lambdaQueryWrapper); long count = this.count(lambdaQueryWrapper);
System.out.println("数据库:"+count); //System.out.println("数据库:"+count);
learningRecords.setStatus("1");//将该记录的状态设置为1 learningRecords.setStatus("1");//将该记录的状态设置为1
//更新观看时间 //更新观看时间
learningRecords.setAccessTime(LocalDateTime.now()); learningRecords.setTime(LocalDateTime.now());
//设置默认封面 //设置默认封面
if (learningRecords.getContent() == null || learningRecords.getContent().equals("")){ if (learningRecords.getContent() == null || learningRecords.getContent().equals("")){
learningRecords.setContent("img/v2-c57eb7863ec4233f503176ca3f90f8d5_1440w.png"); 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){//如果没有就新增并记录学习方式是打开还是下载 if (count == 0){//如果没有就新增并记录学习方式是打开还是下载
//根据传上来的type设置 //根据传上来的type设置
// learningRecords.setType(learningRecords.getType()); // learningRecords.setType(learningRecords.getType());

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

Loading…
Cancel
Save