diff --git a/src/main/java/com/teaching/backend/controller/records/CourseLearningRecordController.java b/src/main/java/com/teaching/backend/controller/records/CourseLearningRecordController.java index cb67129..f44ee58 100644 --- a/src/main/java/com/teaching/backend/controller/records/CourseLearningRecordController.java +++ b/src/main/java/com/teaching/backend/controller/records/CourseLearningRecordController.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 saveCoursesRecords(@RequestBody CourseLearningRecord courseLearningRecord){ + return courseLearningRecordService.saveCoursesRecords(courseLearningRecord); + } + + /** + * 获取当前登录用户的学习课程记录 + * @param pagenum + * @param pagesize + * @return + */ + @ApiOperation("根据用户id查询学习记录") + @ValidateParams({"userId"}) + @GetMapping("/page") + public BaseResponse 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 deleteRecords(@RequestParam List ids){ + return courseLearningRecordService.removeCoursesRecords(ids); + } } diff --git a/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java b/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java new file mode 100644 index 0000000..eb924c9 --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java @@ -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 saveKnowledgeRecords(@RequestBody KnowledgeLearningRecord knowledgeLearningRecord){ + knowledgeLearningRecordService.save(knowledgeLearningRecord); + return ResultUtils.success("添加成功"); + } + +} diff --git a/src/main/java/com/teaching/backend/controller/records/LearningRecordsController.java b/src/main/java/com/teaching/backend/controller/records/LearningRecordsController.java index 474e0e9..9b88c24 100644 --- a/src/main/java/com/teaching/backend/controller/records/LearningRecordsController.java +++ b/src/main/java/com/teaching/backend/controller/records/LearningRecordsController.java @@ -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 getAll(@RequestParam(value = "pagenum", defaultValue = "1") int pagenum, -// @RequestParam(value = "pagesize", defaultValue = "10") int pagesize, -// @RequestParam String userId){ -// //根据用户查询 -// Page page = learningRecordsService.query() -// .orderByDesc("time") -// .eq("status", "1") -// .eq("user_id", userId).page(new Page<>(pagenum, pagesize)); -// //获取当前页数据 -// List list = page.getRecords(); -// List cs= learningRecordsService.getAll(list); -// -// long total = page.getTotal(); // 总记录数 -// Page pageInfo = new Page<>(pagenum,pagesize,total); -// pageInfo.setRecords(cs); -// pageInfo.setPages(page.getPages());//设置总页数 -// return ResultUtils.success(pageInfo); -// } -// -// @ApiOperation("查询所有") -// @GetMapping("/all") -// public BaseResponse> 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 saveRecords(@RequestBody LearningRecords learningRecords){ -//// System.out.println(learningRecords); -// return ResultUtils.success(learningRecordsService.saveRecords(learningRecords)); -// } -// -// /** -// * 根据id删除 -// * @param ids -// * @return -// */ -// @DeleteMapping("/delete") -// //@ValidateParams({"ids"}) -// public BaseResponse deleteRecords(@RequestParam List 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 getAll(@RequestParam(value = "pagenum", defaultValue = "1") int pagenum, + @RequestParam(value = "pagesize", defaultValue = "10") int pagesize, + @RequestParam String userId){ + //根据用户查询 + Page page = learningRecordsService.query() + .orderByDesc("time") + .eq("status", "1") + .eq("user_id", userId).page(new Page<>(pagenum, pagesize)); + //获取当前页数据 + List list = page.getRecords(); + List cs= learningRecordsService.getAll(list); + + long total = page.getTotal(); // 总记录数 + Page pageInfo = new Page<>(pagenum,pagesize,total); + pageInfo.setRecords(cs); + pageInfo.setPages(page.getPages());//设置总页数 + return ResultUtils.success(pageInfo); + } + + @ApiOperation("查询所有") + @GetMapping("/all") + public BaseResponse> 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 saveRecords(@RequestBody LearningRecords learningRecords){ +// System.out.println(learningRecords); + return ResultUtils.success(learningRecordsService.saveRecords(learningRecords)); + } + /** + * + */ + + + /** + * 根据ids删除 + * @param ids + * @return + */ + @ApiOperation("学习记录删除") + @DeleteMapping("/delete") + public BaseResponse deleteRecords(@RequestParam List ids){ + return ResultUtils.success(learningRecordsService.delete(ids)); + } + + /** + * 统计单个学生的学习记录 + */ + @ApiOperation("单个学生学习记录统计") + @GetMapping("/count") + public BaseResponse countLearningRecord(@RequestParam String userId, @RequestParam String courseId){ + return learningRecordsService.countLearningRecord(userId,courseId); + } + +} diff --git a/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java b/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java new file mode 100644 index 0000000..751f0fa --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java @@ -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 saveResourceRecords(@RequestBody ResourceLearningRecord resourceLearningRecord) { + resourceLearningRecordService.save(resourceLearningRecord); + return ResultUtils.success("添加成功"); + } +} diff --git a/src/main/java/com/teaching/backend/mapper/courses/CourseLearningNumberMapper.java b/src/main/java/com/teaching/backend/mapper/courses/CourseLearningNumberMapper.java new file mode 100644 index 0000000..e30dc6d --- /dev/null +++ b/src/main/java/com/teaching/backend/mapper/courses/CourseLearningNumberMapper.java @@ -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; + +/** + *

+ * Mapper 接口 + *

+ * + * @author author + * @since 2024-07-29 + */ +public interface CourseLearningNumberMapper extends BaseMapper { + +} diff --git a/src/main/java/com/teaching/backend/model/entity/courses/CourseLearningNumber.java b/src/main/java/com/teaching/backend/model/entity/courses/CourseLearningNumber.java new file mode 100644 index 0000000..72566b0 --- /dev/null +++ b/src/main/java/com/teaching/backend/model/entity/courses/CourseLearningNumber.java @@ -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; + +/** + *

+ * + *

+ * + * @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; + + +} diff --git a/src/main/java/com/teaching/backend/model/entity/records/CourseLearningRecord.java b/src/main/java/com/teaching/backend/model/entity/records/CourseLearningRecord.java index acd207b..80223e7 100644 --- a/src/main/java/com/teaching/backend/model/entity/records/CourseLearningRecord.java +++ b/src/main/java/com/teaching/backend/model/entity/records/CourseLearningRecord.java @@ -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") diff --git a/src/main/java/com/teaching/backend/model/entity/records/KnowledgeLearningRecord.java b/src/main/java/com/teaching/backend/model/entity/records/KnowledgeLearningRecord.java index f7c1ed5..2aa091b 100644 --- a/src/main/java/com/teaching/backend/model/entity/records/KnowledgeLearningRecord.java +++ b/src/main/java/com/teaching/backend/model/entity/records/KnowledgeLearningRecord.java @@ -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; diff --git a/src/main/java/com/teaching/backend/model/entity/records/LearningRecords.java b/src/main/java/com/teaching/backend/model/entity/records/LearningRecords.java index 510f29d..0ea46e7 100644 --- a/src/main/java/com/teaching/backend/model/entity/records/LearningRecords.java +++ b/src/main/java/com/teaching/backend/model/entity/records/LearningRecords.java @@ -49,8 +49,8 @@ public class LearningRecords { private String download; /** *视频学习时长 - * - private String videoDuration; + */ + private Integer videoDuration; /** *是否完成观看:1,已完成,0未完成 diff --git a/src/main/java/com/teaching/backend/model/entity/records/ResourceLearningRecord.java b/src/main/java/com/teaching/backend/model/entity/records/ResourceLearningRecord.java index 95de9f2..2b9f027 100644 --- a/src/main/java/com/teaching/backend/model/entity/records/ResourceLearningRecord.java +++ b/src/main/java/com/teaching/backend/model/entity/records/ResourceLearningRecord.java @@ -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; diff --git a/src/main/java/com/teaching/backend/model/vo/records/CourseLearningRecordsVo.java b/src/main/java/com/teaching/backend/model/vo/records/CourseLearningRecordsVo.java new file mode 100644 index 0000000..1260a73 --- /dev/null +++ b/src/main/java/com/teaching/backend/model/vo/records/CourseLearningRecordsVo.java @@ -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; +} diff --git a/src/main/java/com/teaching/backend/model/vo/records/LearningRecordsVo.java b/src/main/java/com/teaching/backend/model/vo/records/LearningRecordsVo.java index 345c436..136c8d3 100644 --- a/src/main/java/com/teaching/backend/model/vo/records/LearningRecordsVo.java +++ b/src/main/java/com/teaching/backend/model/vo/records/LearningRecordsVo.java @@ -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; diff --git a/src/main/java/com/teaching/backend/service/courses/ICourseLearningNumberService.java b/src/main/java/com/teaching/backend/service/courses/ICourseLearningNumberService.java new file mode 100644 index 0000000..ba882e1 --- /dev/null +++ b/src/main/java/com/teaching/backend/service/courses/ICourseLearningNumberService.java @@ -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; + +/** + *

+ * 服务类 + *

+ * + * @author author + * @since 2024-07-29 + */ +public interface ICourseLearningNumberService extends IService { + +} diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CourseLearningNumberServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CourseLearningNumberServiceImpl.java new file mode 100644 index 0000000..ce62b53 --- /dev/null +++ b/src/main/java/com/teaching/backend/service/impl/courses/CourseLearningNumberServiceImpl.java @@ -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; + +/** + *

+ * 服务实现类 + *

+ * + * @author author + * @since 2024-07-29 + */ +@Service +public class CourseLearningNumberServiceImpl extends ServiceImpl implements ICourseLearningNumberService { + + +} diff --git a/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java index 0ed2c5c..0b16ab3 100644 --- a/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java @@ -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; + /** *

* 服务实现类 @@ -19,4 +35,78 @@ import org.springframework.stereotype.Service; @Service public class CourseLearningRecordServiceImpl extends ServiceImpl implements ICourseLearningRecordService { + @Resource + private CourseLearningNumberServiceImpl courseLearningNumberService; + @Resource + private CoursesServiceImpl coursesService; + @Override + public BaseResponse 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 getPage(int pagenum, int pagesize, String userId) { + //格式化时间 + String strDateFormat = "yyyy-MM-dd HH:mm:ss"; + SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); + //分页查询 + Page page = query() + .orderByDesc("time") + .eq("user_id", userId).page(new Page<>(pagenum, pagesize)); + Page page1 = new Page<>(); + List records = page.getRecords(); + BeanUtil.copyProperties(page, page1); + List 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 removeCoursesRecords(List ids) { + removeByIds(ids); +// List list = query().in("id", ids).list(); +// removeBatchByIds(list); + return ResultUtils.success("删除成功!"); + } } diff --git a/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java index 0b1c4fa..68b0a1b 100644 --- a/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java @@ -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 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 implements LearningRecordsService { + + @Autowired + private CoursesServiceImpl coursesService; + @Autowired + private LearningRecordsMapper learningRecordsMapper; // @Autowired // private CourseResourcesServiceImpl courseResourcesService; -//// @Autowired -//// private ChapterServiceImpl chapterService; -// -// @Override -// public IPage selectPageList(Page page, LearningRecords model) { -// return learningRecordsMapper.selectPageList(page,model); -// } -// -// @Override -// public String delete(List ids) { -// LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); -// lambdaQueryWrapper.in(LearningRecords::getId, ids); -// List list = this.list(lambdaQueryWrapper); -// //把即将要删除的记录状态调成0 +// @Autowired +// private ChapterServiceImpl chapterService; + + @Override + public IPage selectPageList(Page page, LearningRecords model) { + return learningRecordsMapper.selectPageList(page,model); + } + + @Override + public String delete(List ids) { + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.in(LearningRecords::getId, ids); + List 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 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 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 getAll(List list){ + List 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 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 getAll(List list){ -// List 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 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 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 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 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 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 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 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 "修改成功"; + } + } + + + + +} diff --git a/src/main/java/com/teaching/backend/service/records/ICourseLearningRecordService.java b/src/main/java/com/teaching/backend/service/records/ICourseLearningRecordService.java index 6cdaadf..62b940c 100644 --- a/src/main/java/com/teaching/backend/service/records/ICourseLearningRecordService.java +++ b/src/main/java/com/teaching/backend/service/records/ICourseLearningRecordService.java @@ -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; + /** *

* 服务类 @@ -14,4 +18,10 @@ import com.teaching.backend.model.entity.records.CourseLearningRecord; */ public interface ICourseLearningRecordService extends IService { + BaseResponse saveCoursesRecords(CourseLearningRecord courseLearningRecord); + + BaseResponse getPage(int pagenum, int pagesize, String userId); + + BaseResponse removeCoursesRecords(List ids); + } diff --git a/src/main/java/com/teaching/backend/service/records/LearningRecordsService.java b/src/main/java/com/teaching/backend/service/records/LearningRecordsService.java index 6ab95e7..4309fd5 100644 --- a/src/main/java/com/teaching/backend/service/records/LearningRecordsService.java +++ b/src/main/java/com/teaching/backend/service/records/LearningRecordsService.java @@ -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 { public String delete(List ids); + + BaseResponse countLearningRecord(String userId, String courseId); }