From f4e78612f79276e0dcf9a31dde90033736000bdb Mon Sep 17 00:00:00 2001 From: wenyu441069198 <14186472+wenyu441069198@user.noreply.gitee.com> Date: Wed, 7 Aug 2024 17:23:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../KnowledgeLearningRecordController.java | 15 ++- .../ResourceLearningRecordController.java | 27 +++- .../KnowledgeLearningRecordMapper.java | 6 + .../records/ResourceLearningRecordMapper.java | 6 + .../KnowledgeLearningRecordServiceImpl.java | 15 ++- .../IKnowledgeLearningRecordService.java | 2 + .../com/teaching/backend/utils/Constants.java | 5 + .../teaching/backend/utils/UploadUtils.java | 115 ++++++++++++++++++ src/test/java/com/teaching/test.java | 37 ++++++ 9 files changed, 221 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/teaching/backend/utils/Constants.java create mode 100644 src/main/java/com/teaching/backend/utils/UploadUtils.java create mode 100644 src/test/java/com/teaching/test.java diff --git a/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java b/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java index dac49ac..f723a2d 100644 --- a/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java +++ b/src/main/java/com/teaching/backend/controller/records/KnowledgeLearningRecordController.java @@ -5,9 +5,9 @@ import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.CommonResult; import com.teaching.backend.common.ResultUtils; import com.teaching.backend.filter.ValidateParams; +import com.teaching.backend.mapper.records.KnowledgeLearningRecordMapper; import com.teaching.backend.model.entity.records.CourseLearningRecord; import com.teaching.backend.model.entity.records.KnowledgeLearningRecord; -import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.knowGraph.KnowVO; import com.teaching.backend.service.impl.records.KnowledgeLearningRecordServiceImpl; import io.swagger.annotations.ApiOperation; @@ -21,6 +21,8 @@ import java.util.List; public class KnowledgeLearningRecordController { @Resource private KnowledgeLearningRecordServiceImpl knowledgeLearningRecordService; + @Resource + private KnowledgeLearningRecordMapper knowledgeLearningRecordMapper; @ApiOperation("添加知识点学习记录") // @ValidateParams({"userId","type","coursesId"}) @@ -56,6 +58,17 @@ public class KnowledgeLearningRecordController { return knowledgeLearningRecordService.removeKnowledgeRecord(ids); } + /** + * 统计单个学生学习知识点数量 + * @param userId + * @return + */ + @ApiOperation("统计单个学生学习知识点数量") + @GetMapping("/countknowledgenumber") + public BaseResponse countKnowledgeNumber(@RequestParam String userId) { + return ResultUtils.success(knowledgeLearningRecordMapper.selectDistinctKnowledgeIdsByUser(userId).size()); + } + /** * 获取学习最多的知识点 * diff --git a/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java b/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java index 98ddfe4..e025d4d 100644 --- a/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java +++ b/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java @@ -4,10 +4,12 @@ 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.mapper.records.ResourceLearningRecordMapper; 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 com.teaching.backend.utils.UploadUtils; import io.swagger.annotations.ApiOperation; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; @@ -17,6 +19,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.IOException; import java.util.List; @RestController @@ -24,6 +27,10 @@ import java.util.List; public class ResourceLearningRecordController { @Resource private ResourceLearningRecordServiceImpl resourceLearningRecordService; + @Resource + private ResourceLearningRecordMapper resourceLearningRecordMapper; + + @ApiOperation("添加资源学习记录") // @ValidateParams({"userId","type","coursesId"}) @@ -60,11 +67,27 @@ public class ResourceLearningRecordController { return resourceLearningRecordService.removeResourceRecord(ids); } + /** + * 统计单个学生学习资源数量 + * @param userId + * @return + */ + @ApiOperation("统计单个学生学习资源数量") + @GetMapping("/countresourcenumber") + public BaseResponse countResourceNumber(@RequestParam String userId) { + return ResultUtils.success(resourceLearningRecordMapper.selectDistinctResourceIdsByUser(userId).size()); + } + + @ApiOperation("上传图片") @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public BaseResponse upload(@RequestPart("filedata") MultipartFile filedata){ + public BaseResponse upload(@RequestPart("filedata") MultipartFile filedata) throws IOException { //调用ser - + System.out.println(filedata.getResource()); + System.out.println(UploadUtils.saveFileByDirectory(filedata)); +// File file = filedata.getResource().getFile(); +// UploadUtils.Chunk(file); +// UploadUtils.Merge(file); //源文件 // File sourseFile = (File) file; return ResultUtils.success("hello"); diff --git a/src/main/java/com/teaching/backend/mapper/records/KnowledgeLearningRecordMapper.java b/src/main/java/com/teaching/backend/mapper/records/KnowledgeLearningRecordMapper.java index 78f4ada..2ddda90 100644 --- a/src/main/java/com/teaching/backend/mapper/records/KnowledgeLearningRecordMapper.java +++ b/src/main/java/com/teaching/backend/mapper/records/KnowledgeLearningRecordMapper.java @@ -3,6 +3,10 @@ package com.teaching.backend.mapper.records; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.teaching.backend.model.entity.records.KnowledgeLearningRecord; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; import java.util.List; @@ -16,6 +20,8 @@ import java.util.List; */ public interface KnowledgeLearningRecordMapper extends BaseMapper { + @Select("SELECT knowledge_id FROM knowledge_learning_record WHERE user_id = #{userId} GROUP BY knowledge_id") + List selectDistinctKnowledgeIdsByUser(@Param("userId") String userId); List queryStudyKnowledgeMaxNumber(); List queryStudyMaxKnowledgeId(String userId); diff --git a/src/main/java/com/teaching/backend/mapper/records/ResourceLearningRecordMapper.java b/src/main/java/com/teaching/backend/mapper/records/ResourceLearningRecordMapper.java index c1235c1..4f7151f 100644 --- a/src/main/java/com/teaching/backend/mapper/records/ResourceLearningRecordMapper.java +++ b/src/main/java/com/teaching/backend/mapper/records/ResourceLearningRecordMapper.java @@ -3,6 +3,10 @@ package com.teaching.backend.mapper.records; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.teaching.backend.model.entity.records.ResourceLearningRecord; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; /** *

@@ -13,5 +17,7 @@ import com.teaching.backend.model.entity.records.ResourceLearningRecord; * @since 2024-07-24 */ public interface ResourceLearningRecordMapper extends BaseMapper { + @Select("SELECT resource_id FROM resource_learning_record WHERE user_id = #{userId} GROUP BY resource_id") + List selectDistinctResourceIdsByUser(@Param("userId") String userId); } diff --git a/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java index da5e2be..0c10b1f 100644 --- a/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java @@ -72,13 +72,18 @@ public class KnowledgeLearningRecordServiceImpl extends ServiceImpl 0){ + if (count > 0){//有人学过 //自己没学过,人数加1 if (count1 == 0){ - knowledgeLearningNumberService.update().setSql("number = number + 1").eq("course_id", courseId).eq("knowledge_id",knowledgeId).update(); -// System.out.println("用户:"+userId+"第一次学这门课"); + //该知识点学习人数 + knowledgeLearningNumberService.update() + .setSql("number = number + 1") + .eq("course_id", courseId) + .eq("knowledge_id",knowledgeId).update(); + System.out.println("用户:"+userId+":第一次学这门课"); + } - }else { + }else {//没人学过(自己肯定也没学) //该课程第一次被学,人数设置为1 KnowledgeLearningNumber knowledgeLearningNumber = new KnowledgeLearningNumber(); knowledgeLearningNumber.setNumber(1); @@ -96,6 +101,7 @@ public class KnowledgeLearningRecordServiceImpl extends ServiceImpl getPage(int pagenum, int pagesize, String userId, String courseId) { //格式化时间 @@ -167,4 +173,5 @@ public class KnowledgeLearningRecordServiceImpl extends ServiceImpl removeKnowledgeRecord(List ids); + List queryMaxKnowledge(String userId); } diff --git a/src/main/java/com/teaching/backend/utils/Constants.java b/src/main/java/com/teaching/backend/utils/Constants.java new file mode 100644 index 0000000..821d3f2 --- /dev/null +++ b/src/main/java/com/teaching/backend/utils/Constants.java @@ -0,0 +1,5 @@ +package com.teaching.backend.utils; + +public class Constants { + public static final String UPLOAD_URL = "E:\\workspace\\file\\"; +} diff --git a/src/main/java/com/teaching/backend/utils/UploadUtils.java b/src/main/java/com/teaching/backend/utils/UploadUtils.java new file mode 100644 index 0000000..bd844af --- /dev/null +++ b/src/main/java/com/teaching/backend/utils/UploadUtils.java @@ -0,0 +1,115 @@ +package com.teaching.backend.utils; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.FileUtils; +import org.springframework.util.DigestUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.*; + +import static com.teaching.backend.utils.Constants.UPLOAD_URL; + +@Slf4j +//文件上传工具类 +public class UploadUtils { + + public static String saveFileByDirectory (MultipartFile file) { + String url = ""; + try { + // 将文件保存在服务器目录中 + // 文件名称 + String uuid = UUID.randomUUID().toString(); + // 得到上传文件后缀 + String originalName = file.getOriginalFilename(); + String ext = "." + FilenameUtils.getExtension(originalName); + // 新生成的文件名称 + String fileName = uuid + ext; + // 复制文件 + File targetFile = new File(UPLOAD_URL, fileName); + url = UPLOAD_URL + fileName; + FileUtils.writeByteArrayToFile(targetFile, file.getBytes()); + System.out.println("保存成功"); + } catch (IOException e) { + log.error("保存文件到服务器(本地)失败",e); + } + return url; + } + + public static void Chunk(File sourseFile) throws IOException { +// //源文件 +// File sourseFile = new File("D:/Users/Desktop/image/剪映/7.18/7月20.mp4"); + //分块文件存储路径 + String chunkFilePath = "E:\\workspace\\chunk"; + //分块文件大小 + int chunkSize = 1024 * 1024 * 1; + //分块文件个数 + int chunkNum = (int) Math.ceil(sourseFile.length() * 1.0 / chunkSize); + //使用流从源文件读数据,向分块文件中写数据 + RandomAccessFile raf_r = new RandomAccessFile(sourseFile, "r"); + //缓存区 + byte[] bytes = new byte[1024]; + for (int i = 0; i < chunkNum; i++) { + File chunkFile = new File(chunkFilePath + i); + //分块文件写入流 + RandomAccessFile raf_rw = new RandomAccessFile(chunkFile, "rw"); + int len = -1; + while((len=raf_r.read(bytes)) != -1){ + raf_rw.write(bytes, 0, len); + if (chunkFile.length() >= chunkSize){ + break; + } + } + raf_rw.close(); + } + raf_r.close(); + } + + public static void Merge(File sourseFile) throws IOException { + //块文件目录 + File chunkFolder = new File("E:\\workspace\\chunk"); +// //源文件 +// File sourseFile = new File("D:/Users/Desktop/image/剪映/7.18/7月20.mp4"); + //合并后的文件 + File mergeFile = new File("E:\\workspace\\file\\"+sourseFile.getName()); + + //取出所有分块文件 + File[] files = chunkFolder.listFiles(); + //将数组转成list + List fileList = Arrays.asList(files); + //对分块文件排序 + Collections.sort(fileList, new Comparator() { + @Override + public int compare(File o1, File o2) { + return Integer.parseInt(o1.getName()) - Integer.parseInt(o2.getName()); //升序 + } + }); + //向合并文件写的流 + RandomAccessFile raf_rw = new RandomAccessFile(mergeFile, "rw"); + //缓存区 + byte[] bytes = new byte[1024]; + //遍历分块文件,向合并的文件写 + for (File file : fileList) { + //读分块的流 + RandomAccessFile raf_r = new RandomAccessFile(file, "r"); + int len = -1; + while((len = raf_r.read(bytes)) != -1){ + raf_rw.write(bytes, 0, len); + } + raf_r.close(); + } + raf_rw.close(); + //合并文件完成后对合并的文件校验 + FileInputStream fileInputStream_merge = new FileInputStream(mergeFile); + FileInputStream fileInputStream_source = new FileInputStream(sourseFile); + String md5_merge = DigestUtils.md5DigestAsHex(fileInputStream_merge); + String md5_source = DigestUtils.md5DigestAsHex(fileInputStream_source); + if (md5_merge.equals(md5_source)){ + System.out.println("文件合并成功!"); + } + } +} diff --git a/src/test/java/com/teaching/test.java b/src/test/java/com/teaching/test.java new file mode 100644 index 0000000..9fe8f16 --- /dev/null +++ b/src/test/java/com/teaching/test.java @@ -0,0 +1,37 @@ +package com.teaching; + + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.annotation.Resource; + +@Slf4j +@SpringBootTest +public class test { + + @Resource + private KnowledgeLearningCountServiceImpl knowledgeLearningCountService; + @Test + public void countknowledge(){ + String userId = "2"; +// System.out.println(knowledgeLearningCountService.list()); +// List list = knowledgeLearningCountService.query().eq("user_id", userId).list(); +// HashSet set = new HashSet<>(list); +// System.out.println(set.size()); +// Long count = knowledgeLearningCountService.query().eq("user_id", userId).count(); +// if (count == 0){ +// KnowledgeLearningCount knowledgeLearningCount = new KnowledgeLearningCount(); +// knowledgeLearningCount.setUserId(userId); +// knowledgeLearningCount.setNumber(1); +// knowledgeLearningCountService.save(knowledgeLearningCount); +// }else { +// //该用户学习知识点数+1 +// boolean is = knowledgeLearningCountService.update() +// .setSql("number = "+set.size()) +// .eq("user_id", userId).update(); +// System.out.println(is); +// } + } +}