课程学习记录

master
wenyu441069198 4 months ago
parent 2cc709e009
commit 0cd1adf54c
  1. 48
      src/main/java/com/teaching/backend/controller/records/CourseLearningRecordController.java
  2. 31
      src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java
  3. 194
      src/main/java/com/teaching/backend/controller/records/LearningRecordsController.java
  4. 30
      src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java
  5. 17
      src/main/java/com/teaching/backend/mapper/courses/CourseLearningNumberMapper.java
  6. 44
      src/main/java/com/teaching/backend/model/entity/courses/CourseLearningNumber.java
  7. 4
      src/main/java/com/teaching/backend/model/entity/records/CourseLearningRecord.java
  8. 4
      src/main/java/com/teaching/backend/model/entity/records/KnowledgeLearningRecord.java
  9. 4
      src/main/java/com/teaching/backend/model/entity/records/LearningRecords.java
  10. 8
      src/main/java/com/teaching/backend/model/entity/records/ResourceLearningRecord.java
  11. 40
      src/main/java/com/teaching/backend/model/vo/records/CourseLearningRecordsVo.java
  12. 4
      src/main/java/com/teaching/backend/model/vo/records/LearningRecordsVo.java
  13. 17
      src/main/java/com/teaching/backend/service/courses/ICourseLearningNumberService.java
  14. 22
      src/main/java/com/teaching/backend/service/impl/courses/CourseLearningNumberServiceImpl.java
  15. 90
      src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java
  16. 384
      src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java
  17. 10
      src/main/java/com/teaching/backend/service/records/ICourseLearningRecordService.java
  18. 4
      src/main/java/com/teaching/backend/service/records/LearningRecordsService.java

@ -1,12 +1,54 @@
package com.teaching.backend.controller.records;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.entity.records.CourseLearningRecord;
import com.teaching.backend.service.impl.records.CourseLearningRecordServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/api/courselearingRecord")
@RequestMapping("/api/courselearingrecord")
public class CourseLearningRecordController {
@Resource
private CourseLearningRecordServiceImpl courseLearningRecordService;
@ApiOperation("添加课程学习记录")
@ValidateParams({"userId","courseId"})
@PostMapping("/saverecords")
public BaseResponse<String> saveCoursesRecords(@RequestBody CourseLearningRecord courseLearningRecord){
return courseLearningRecordService.saveCoursesRecords(courseLearningRecord);
}
/**
* 获取当前登录用户的学习课程记录
* @param pagenum
* @param pagesize
* @return
*/
@ApiOperation("根据用户id查询学习记录")
@ValidateParams({"userId"})
@GetMapping("/page")
public BaseResponse<Page> getAll(@RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
@RequestParam(value = "pagesize", defaultValue = "10") int pagesize,
@RequestParam String userId){
return courseLearningRecordService.getPage(pagenum, pagesize, userId);
}
/**
* 根据ids删除
* @param ids
* @return
*/
@ApiOperation("学习记录删除")
@DeleteMapping("/delete")
public BaseResponse<String> deleteRecords(@RequestParam List<Long> ids){
return courseLearningRecordService.removeCoursesRecords(ids);
}
}

@ -0,0 +1,31 @@
package com.teaching.backend.controller.records;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.entity.records.CourseLearningRecord;
import com.teaching.backend.model.entity.records.KnowledgeLearningRecord;
import com.teaching.backend.service.impl.records.KnowledgeLearningRecordServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/api/knowledgelearning")
public class KnowledgeLearningRecordController {
@Resource
private KnowledgeLearningRecordServiceImpl knowledgeLearningRecordService;
@ApiOperation("添加知识点学习记录")
// @ValidateParams({"userId","type","coursesId"})
@PostMapping("/saverecords")
public BaseResponse<String> saveKnowledgeRecords(@RequestBody KnowledgeLearningRecord knowledgeLearningRecord){
knowledgeLearningRecordService.save(knowledgeLearningRecord);
return ResultUtils.success("添加成功");
}
}

@ -1,89 +1,105 @@
//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;
//
//import com.teaching.backend.filter.ValidateParams;
//import com.teaching.backend.model.entity.records.LearningRecords;
//import com.teaching.backend.model.vo.records.LearningRecordsVo;
//import com.teaching.backend.service.impl.records.LearningRecordsServiceImpl;
//import io.swagger.annotations.ApiOperation;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.ArrayList;
//import java.util.List;
//
//@RestController
//@RequestMapping("/api/learningrecords")
//public class LearningRecordsController {
//
// @Autowired
// private LearningRecordsServiceImpl learningRecordsService;
//
//
// /**
// * 获取当前登录用户的学习记录
// * @param pagenum
// * @param pagesize
// * @return
// */
// @ApiOperation("根据用户id查询学习记录")
// @ValidateParams({"userId"})
// @GetMapping("/getall")
// 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);
//
// long total = page.getTotal(); // 总记录数
// Page<LearningRecordsVo> pageInfo = new Page<>(pagenum,pagesize,total);
// pageInfo.setRecords(cs);
// pageInfo.setPages(page.getPages());//设置总页数
// return ResultUtils.success(pageInfo);
// }
//
// @ApiOperation("查询所有")
// @GetMapping("/all")
// public BaseResponse<List<LearningRecords>> All(){
//// System.out.println("学习记录:"+learningRecordsService.list());
//
// return ResultUtils.success(learningRecordsService.list());
// }
//
// //资源类型: 1,课程学习记录,2,知识点学习记录,3课程资源学习记录
// /**
// * 添加课程记录
// * @param learningRecords
// * @return
// */
// @ApiOperation("添加学习记录")
// @ValidateParams({"userId","type","coursesId"})
// @PostMapping("/saverecords")
// public BaseResponse<String> saveRecords(@RequestBody LearningRecords learningRecords){
//// System.out.println(learningRecords);
// return ResultUtils.success(learningRecordsService.saveRecords(learningRecords));
// }
//
// /**
// * 根据id删除
// * @param ids
// * @return
// */
// @DeleteMapping("/delete")
// //@ValidateParams({"ids"})
// public BaseResponse<String> deleteRecords(@RequestParam List<Long> ids){
// return ResultUtils.success(learningRecordsService.delete(ids));
// }
//}
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;
import com.teaching.backend.filter.ValidateParams;
import com.teaching.backend.model.entity.records.CourseLearningRecord;
import com.teaching.backend.model.entity.records.LearningRecords;
import com.teaching.backend.model.vo.records.CourseLearningRecordsVo;
import com.teaching.backend.model.vo.records.LearningRecordsVo;
import com.teaching.backend.service.impl.records.LearningRecordsServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/learningrecords")
public class LearningRecordsController {
@Autowired
private LearningRecordsServiceImpl learningRecordsService;
/**
* 获取当前登录用户的学习记录
* @param pagenum
* @param pagesize
* @return
*/
@ApiOperation("根据用户id查询学习记录")
@ValidateParams({"userId"})
@GetMapping("/getall")
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);
long total = page.getTotal(); // 总记录数
Page<LearningRecordsVo> pageInfo = new Page<>(pagenum,pagesize,total);
pageInfo.setRecords(cs);
pageInfo.setPages(page.getPages());//设置总页数
return ResultUtils.success(pageInfo);
}
@ApiOperation("查询所有")
@GetMapping("/all")
public BaseResponse<List<LearningRecords>> All(){
// System.out.println("学习记录:"+learningRecordsService.list());
return ResultUtils.success(learningRecordsService.list());
}
//资源类型: 1,课程学习记录,2,知识点学习记录,3课程资源学习记录
/**
* 添加课程记录
* @param learningRecords
* @return
*/
@ApiOperation("添加学习记录")
@ValidateParams({"userId","type","coursesId"})
@PostMapping("/saverecords")
public BaseResponse<String> saveRecords(@RequestBody LearningRecords learningRecords){
// System.out.println(learningRecords);
return ResultUtils.success(learningRecordsService.saveRecords(learningRecords));
}
/**
*
*/
/**
* 根据ids删除
* @param ids
* @return
*/
@ApiOperation("学习记录删除")
@DeleteMapping("/delete")
public BaseResponse<String> deleteRecords(@RequestParam List<Long> ids){
return ResultUtils.success(learningRecordsService.delete(ids));
}
/**
* 统计单个学生的学习记录
*/
@ApiOperation("单个学生学习记录统计")
@GetMapping("/count")
public BaseResponse<CourseLearningRecordsVo> countLearningRecord(@RequestParam String userId, @RequestParam String courseId){
return learningRecordsService.countLearningRecord(userId,courseId);
}
}

@ -0,0 +1,30 @@
package com.teaching.backend.controller.records;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.model.entity.records.KnowledgeLearningRecord;
import com.teaching.backend.model.entity.records.ResourceLearningRecord;
import com.teaching.backend.service.impl.records.KnowledgeLearningRecordServiceImpl;
import com.teaching.backend.service.impl.records.ResourceLearningRecordServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/api/resourcelearningrecords")
public class ResourceLearningRecordController {
@Resource
private ResourceLearningRecordServiceImpl resourceLearningRecordService;
@ApiOperation("添加资源学习记录")
// @ValidateParams({"userId","type","coursesId"})
@PostMapping("/saverecords")
public BaseResponse<String> saveResourceRecords(@RequestBody ResourceLearningRecord resourceLearningRecord) {
resourceLearningRecordService.save(resourceLearningRecord);
return ResultUtils.success("添加成功");
}
}

@ -0,0 +1,17 @@
package com.teaching.backend.mapper.courses;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.courses.CourseLearningNumber;
/**
* <p>
* Mapper 接口
* </p>
*
* @author author
* @since 2024-07-29
*/
public interface CourseLearningNumberMapper extends BaseMapper<CourseLearningNumber> {
}

@ -0,0 +1,44 @@
package com.teaching.backend.model.entity.courses;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author author
* @since 2024-07-29
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("course_learning_number")
@ApiModel(value="CourseLearningNumber对象", description="")
public class CourseLearningNumber implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "课程学习id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
private String id;
@ApiModelProperty(value = "课程id")
@TableField("course_id")
private String courseId;
@ApiModelProperty(value = "课程学习人数")
@TableField("number")
private Integer number;
}

@ -30,7 +30,7 @@ public class CourseLearningRecord implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = " 课程记录id")
@TableId(value = "id", type = IdType.AUTO)
@TableId(value = "id", type = IdType.ASSIGN_ID)
private String id;
@ApiModelProperty(value = "课程id")
@ -43,7 +43,7 @@ public class CourseLearningRecord implements Serializable {
@ApiModelProperty(value = "观看人数")
@TableField("number")
private Integer number;
private String number;
@ApiModelProperty(value = "记录封面")
@TableField("img")

@ -37,6 +37,10 @@ public class KnowledgeLearningRecord implements Serializable {
@TableField("knowledge_id")
private String knowledgeId;
@ApiModelProperty(value = "课程id")
@TableField("courses_id")
private String coursesId;
@ApiModelProperty(value = "用户id")
@TableField("user_id")
private String userId;

@ -49,8 +49,8 @@ public class LearningRecords {
private String download;
/**
*视频学习时长
*
private String videoDuration;
*/
private Integer videoDuration;
/**
*是否完成观看1已完成0未完成

@ -37,6 +37,14 @@ public class ResourceLearningRecord implements Serializable {
@TableField("resource_id")
private String resourceId;
@ApiModelProperty(value = "知识点id")
@TableField("knowledge_id")
private String knowledgeId;
@ApiModelProperty(value = "课程id")
@TableField("courses_id")
private String coursesId;
@ApiModelProperty(value = "用户id")
@TableField("user_id")
private String userId;

@ -0,0 +1,40 @@
package com.teaching.backend.model.vo.records;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@ApiModel(description = "课程学习记录")
public class CourseLearningRecordsVo {
/**
* 课程学习id
*/
private String id;
/**
* 课程名称
*/
private String courseName;
/**
* 用户id
*/
private String userId;
/**
* 学习人数
*/
private Integer number;
/**
* 封面
*/
private String img;
/**
* 学习时间
*/
private String time;
}

@ -1,12 +1,12 @@
package com.teaching.backend.model.vo.records;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(description = "学习记录")
public class LearningRecordsVo {
// private static final long serialVersionUID = 1L;

@ -0,0 +1,17 @@
package com.teaching.backend.service.courses;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.model.entity.courses.CourseLearningNumber;
/**
* <p>
* 服务类
* </p>
*
* @author author
* @since 2024-07-29
*/
public interface ICourseLearningNumberService extends IService<CourseLearningNumber> {
}

@ -0,0 +1,22 @@
package com.teaching.backend.service.impl.courses;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.mapper.courses.CourseLearningNumberMapper;
import com.teaching.backend.model.entity.courses.CourseLearningNumber;
import com.teaching.backend.service.courses.ICourseLearningNumberService;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author author
* @since 2024-07-29
*/
@Service
public class CourseLearningNumberServiceImpl extends ServiceImpl<CourseLearningNumberMapper, CourseLearningNumber> implements ICourseLearningNumberService {
}

@ -2,12 +2,28 @@ package com.teaching.backend.service.impl.records;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.mapper.records.CourseLearningRecordMapper;
import com.teaching.backend.model.entity.courses.CourseLearningNumber;
import com.teaching.backend.model.entity.records.CourseLearningRecord;
import com.teaching.backend.model.vo.records.CourseLearningRecordsVo;
import com.teaching.backend.service.impl.courses.CourseLearningNumberServiceImpl;
import com.teaching.backend.service.impl.courses.CoursesServiceImpl;
import com.teaching.backend.service.records.ICourseLearningRecordService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* <p>
* 服务实现类
@ -19,4 +35,78 @@ import org.springframework.stereotype.Service;
@Service
public class CourseLearningRecordServiceImpl extends ServiceImpl<CourseLearningRecordMapper, CourseLearningRecord> implements ICourseLearningRecordService {
@Resource
private CourseLearningNumberServiceImpl courseLearningNumberService;
@Resource
private CoursesServiceImpl coursesService;
@Override
public BaseResponse<String> saveCoursesRecords(CourseLearningRecord courseLearningRecord) {
//课程id
String courseId = courseLearningRecord.getCourseId();
String userId = courseLearningRecord.getUserId();
//1.生成学习时间
courseLearningRecord.setTime(LocalDateTime.now());
//2.设置人数
//2.1查人数
Long count = courseLearningNumberService.query()
.eq("course_id", courseId).count();
//2.2判断自己是否学习过
Long count1 = query().eq("user_id", userId).count();
if (count > 0){
//自己没学过,人数加1
if (count1 == 0){
courseLearningNumberService.update().setSql("number = number + 1").eq("course_id",courseId).update();
// System.out.println("用户:"+userId+"第一次学这门课");
}
}else {
//该课程第一次被学,人数设置为1
CourseLearningNumber courseLearningNumber = new CourseLearningNumber();
courseLearningNumber.setNumber(1);
courseLearningNumber.setCourseId(courseId);
courseLearningNumberService.save(courseLearningNumber);
}
//设置人数
String numberId = courseLearningNumberService.query().eq("course_id", courseId).one().getId();
courseLearningRecord.setNumber(numberId);
//保存记录
save(courseLearningRecord);
return ResultUtils.success("添加成功");
}
@Override
public BaseResponse<Page> getPage(int pagenum, int pagesize, String userId) {
//格式化时间
String strDateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
//分页查询
Page<CourseLearningRecord> page = query()
.orderByDesc("time")
.eq("user_id", userId).page(new Page<>(pagenum, pagesize));
Page<CourseLearningRecordsVo> page1 = new Page<>();
List<CourseLearningRecord> records = page.getRecords();
BeanUtil.copyProperties(page, page1);
List<CourseLearningRecordsVo> recordsVos = new ArrayList<>();
//copy集合
records.forEach(courseLearningRecord -> {
CourseLearningRecordsVo courseLearningRecordsVo = new CourseLearningRecordsVo();
BeanUtil.copyProperties(courseLearningRecord,courseLearningRecordsVo);
Date date = Date.from(courseLearningRecord.getTime().atZone(ZoneId.systemDefault()).toInstant());
courseLearningRecordsVo.setTime(sdf.format(date));
//填充课程名称
courseLearningRecordsVo.setCourseName(coursesService.getById(courseLearningRecord.getCourseId()).getName());
// //填充学习人数
courseLearningRecordsVo.setNumber(courseLearningNumberService.getById(courseLearningRecord.getNumber()).getNumber());
recordsVos.add(courseLearningRecordsVo);
});
page1.setRecords(recordsVos);
return ResultUtils.success(page1);
}
@Override
public BaseResponse<String> removeCoursesRecords(List<Long> ids) {
removeByIds(ids);
// List<CourseLearningRecord> list = query().in("id", ids).list();
// removeBatchByIds(list);
return ResultUtils.success("删除成功!");
}
}

@ -1,179 +1,215 @@
//package com.teaching.backend.service.impl.records;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//
//import com.teaching.backend.common.ErrorCode;
//import com.teaching.backend.exception.BusinessException;
//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.courses.CoursesServiceImpl;
//import com.teaching.backend.service.records.LearningRecordsService;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.text.SimpleDateFormat;
//import java.time.LocalDateTime;
//import java.time.ZoneId;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//@Service
//public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMapper, LearningRecords> implements LearningRecordsService {
//
// @Autowired
// private CoursesServiceImpl coursesService;
// @Autowired
// private LearningRecordsMapper learningRecordsMapper;
package com.teaching.backend.service.impl.records;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.records.LearningRecordsMapper;
import com.teaching.backend.model.entity.records.LearningRecords;
import com.teaching.backend.model.vo.records.CourseLearningRecordsVo;
import com.teaching.backend.model.vo.records.LearningRecordsVo;
//import com.teaching.backend.service.impl.CourseResourcesServiceImpl;
import com.teaching.backend.service.impl.courses.CoursesServiceImpl;
import com.teaching.backend.service.records.LearningRecordsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class LearningRecordsServiceImpl extends ServiceImpl<LearningRecordsMapper, LearningRecords> implements LearningRecordsService {
@Autowired
private CoursesServiceImpl coursesService;
@Autowired
private LearningRecordsMapper learningRecordsMapper;
// @Autowired
// private CourseResourcesServiceImpl courseResourcesService;
//// @Autowired
//// private ChapterServiceImpl chapterService;
//
// @Override
// public IPage<LearningRecords> selectPageList(Page<LearningRecords> page, LearningRecords model) {
// return learningRecordsMapper.selectPageList(page,model);
// }
//
// @Override
// public String delete(List<Long> ids) {
// LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// lambdaQueryWrapper.in(LearningRecords::getId, ids);
// List<LearningRecords> list = this.list(lambdaQueryWrapper);
// //把即将要删除的记录状态调成0
// @Autowired
// private ChapterServiceImpl chapterService;
@Override
public IPage<LearningRecords> selectPageList(Page<LearningRecords> page, LearningRecords model) {
return learningRecordsMapper.selectPageList(page,model);
}
@Override
public String delete(List<Long> ids) {
LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(LearningRecords::getId, ids);
List<LearningRecords> list = this.list(lambdaQueryWrapper);
//把即将要删除的记录状态调成0
for (LearningRecords learningRecords : list) {
learningRecords.setStatus("0");
}
updateBatchById(list);
// System.out.println(this.updateBatchById(list));
// this.remove(lambdaQueryWrapper);
return "删除成功";
}
@Override
public BaseResponse<CourseLearningRecordsVo> countLearningRecord(String userId, String courseId) {
// CourseLearningRecordsVo courseLearningRecordsVo = new CourseLearningRecordsVo();
// //统计章节学习数量
// Long chapterCount = query()
// .eq("courses_id", courseId)
// .eq("user_id", userId)
// .eq("type", 2).count();
// //统计知识点学习数量
// Long knowledgeCount = query()
// .eq("courses_id", courseId)
// .eq("user_id", userId)
// .eq("type", 3).count();
// //统计资源学习数量
// Long resourceCount = query()
// .eq("courses_id", courseId)
// .eq("user_id", userId)
// .eq("type", 4).count();
// courseLearningRecordsVo.setChapterCount(chapterCount.intValue());
// courseLearningRecordsVo.setKnowledgeCount(knowledgeCount.intValue());
// courseLearningRecordsVo.setResourceCount(resourceCount.intValue());
// //统计课程学习时长
// List<LearningRecords> list = query()
// .eq("courses_id", courseId)
// .eq("user_id", userId).list();
// int duration = list.stream().mapToInt(LearningRecords::getVideoDuration).sum();
// courseLearningRecordsVo.setDuration(duration);
return null;
}
@Override
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.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) {
// learningRecords.setStatus("0");
// }
// updateBatchById(list);
//// System.out.println(this.updateBatchById(list));
//// this.remove(lambdaQueryWrapper);
// return "删除成功";
// }
//
// @Override
// 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.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());
//// }
//// break;
//// case "1": //如果是课程学习记录
//// if (coursesService.getById(learningRecords.getCoursesId()) != null){
//// learningRecordsVo.setCoursesName(coursesService.getById(learningRecords.getCoursesId()).getName());
//// }
//// break;
//// }
//// //添加观看人数
//// //统计记录表里有多少人查看过这门资源
//// long count = this.count(lambdaQueryWrapper);
//// learningRecordsVo.setNumber(count);
//// //把整合后该用户的学习记录添加到List里
//// records.add(learningRecordsVo);
//// }
// return records;
// }
//
// /**
// * 根据用户id和学习资源id来判断记录是否存在
// * @param learningRecords
// * @return
// */
// @Override
// @Transactional
// public String saveRecords(LearningRecords learningRecords) {
// String userId = learningRecords.getUserId();
// String coursesId = learningRecords.getCoursesId();
// //如果userId或者coursesId为空就抛出异常事务回滚----------------------------
// if (userId == null || userId.equals("") || coursesId == null || coursesId.equals("")){
// throw new BusinessException(ErrorCode.PARAMS_ERROR);
// }
//
//// System.out.println("学习记录:"+learningRecords);
// //查找该用户是否观看过该学习资源
// LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// lambdaQueryWrapper
// .eq(LearningRecords::getUserId, userId)
// .eq(LearningRecords::getCoursesId, coursesId)
// .eq(LearningRecords::getType, learningRecords.getType());
// long count = this.count(lambdaQueryWrapper);
// //System.out.println("数据库:"+count);
// learningRecords.setStatus("1");//将该记录的状态设置为1
// //更新观看时间
// 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();
// LearningRecords one1 = query().eq("courses_id", coursesId).eq("type", learningRecords.getType()).eq("user_id", userId).one();
// int number = list.size();
// //该用户是否学习过
// if (one1 != null){//学习过观看人数不加
// learningRecords.setNumber(number);
// list.forEach(learningRecords1 -> {
// learningRecords1.setNumber(number);
// });
// } else {//否则观看人数加1
// learningRecords.setNumber(number + 1);
// }
// updateBatchById(list);
// if (count == 0){//如果没有就新增并记录学习方式是打开还是下载
// //根据传上来的type设置
//// learningRecords.setType(learningRecords.getType());
// this.save(learningRecords);
// return "添加成功";
// }else {//有就修改记录,更新学习方式
// LearningRecords one = this.getOne(lambdaQueryWrapper);
// System.out.println(one);
// learningRecords.setId(one.getId());//找到该条记录的id并赋值给本次观看记录
// if ( one.getOpen() != null && one.getOpen().equals("1")){
// learningRecords.setOpen(one.getOpen());
// }
// if (one.getDownload() != null && one.getDownload().equals("1")){
// learningRecords.setDownload(one.getDownload());
// //查找该用户的学习记录
// 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());
// }
// break;
// case "1": //如果是课程学习记录
// if (coursesService.getById(learningRecords.getCoursesId()) != null){
// learningRecordsVo.setCoursesName(coursesService.getById(learningRecords.getCoursesId()).getName());
// }
// break;
// }
// this.updateById(learningRecords);
// return "修改成功";
// //添加观看人数
// //统计记录表里有多少人查看过这门资源
// long count = this.count(lambdaQueryWrapper);
// learningRecordsVo.setNumber(count);
// //把整合后该用户的学习记录添加到List里
// records.add(learningRecordsVo);
// }
// }
//
//
//}
return records;
}
/**
* 根据用户id和学习资源id来判断记录是否存在
* @param learningRecords
* @return
*/
@Override
@Transactional
public String saveRecords(LearningRecords learningRecords) {
String userId = learningRecords.getUserId();
String coursesId = learningRecords.getCoursesId();
//如果userId或者coursesId为空就抛出异常事务回滚----------------------------
if (userId == null || userId.equals("") || coursesId == null || coursesId.equals("")){
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// System.out.println("学习记录:"+learningRecords);
//查找该用户是否观看过该学习资源
LambdaQueryWrapper<LearningRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper
.eq(LearningRecords::getUserId, userId)
.eq(LearningRecords::getCoursesId, coursesId)
.eq(LearningRecords::getType, learningRecords.getType());
long count = this.count(lambdaQueryWrapper);
//System.out.println("数据库:"+count);
learningRecords.setStatus("1");//将该记录的状态设置为1
//更新观看时间
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();
LearningRecords one1 = query().eq("courses_id", coursesId).eq("type", learningRecords.getType()).eq("user_id", userId).one();
int number = list.size();
//该用户是否学习过
if (one1 != null){//学习过观看人数不加
learningRecords.setNumber(number);
list.forEach(learningRecords1 -> {
learningRecords1.setNumber(number);
});
} else {//否则观看人数加1
learningRecords.setNumber(number + 1);
}
updateBatchById(list);
if (count == 0){//如果没有就新增并记录学习方式是打开还是下载
//根据传上来的type设置
// learningRecords.setType(learningRecords.getType());
this.save(learningRecords);
return "添加成功";
}else {//有就修改记录,更新学习方式
LearningRecords one = this.getOne(lambdaQueryWrapper);
System.out.println(one);
learningRecords.setId(one.getId());//找到该条记录的id并赋值给本次观看记录
if ( one.getOpen() != null && one.getOpen().equals("1")){
learningRecords.setOpen(one.getOpen());
}
if (one.getDownload() != null && one.getDownload().equals("1")){
learningRecords.setDownload(one.getDownload());
}
this.updateById(learningRecords);
return "修改成功";
}
}
}

@ -1,9 +1,13 @@
package com.teaching.backend.service.records;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.model.entity.records.CourseLearningRecord;
import java.util.List;
/**
* <p>
* 服务类
@ -14,4 +18,10 @@ import com.teaching.backend.model.entity.records.CourseLearningRecord;
*/
public interface ICourseLearningRecordService extends IService<CourseLearningRecord> {
BaseResponse<String> saveCoursesRecords(CourseLearningRecord courseLearningRecord);
BaseResponse<Page> getPage(int pagenum, int pagesize, String userId);
BaseResponse<String> removeCoursesRecords(List<Long> ids);
}

@ -3,7 +3,9 @@ package com.teaching.backend.service.records;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.model.entity.records.LearningRecords;
import com.teaching.backend.model.vo.records.CourseLearningRecordsVo;
import com.teaching.backend.model.vo.records.LearningRecordsVo;
@ -16,4 +18,6 @@ public interface LearningRecordsService extends IService<LearningRecords> {
public String delete(List<Long> ids);
BaseResponse<CourseLearningRecordsVo> countLearningRecord(String userId, String courseId);
}

Loading…
Cancel
Save