From c0792dcbc27d0580d9ed0e7aa3cf34c96c651840 Mon Sep 17 00:00:00 2001
From: fuliqi <fuliqi@qq.com>
Date: 星期四, 04 七月 2024 17:50:58 +0800
Subject: [PATCH] mq配置修改

---
 src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java |   48 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java
index 97380a9..c29ac89 100644
--- a/src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java
+++ b/src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java
@@ -1,21 +1,26 @@
 package com.ycl.jxkg.service.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.github.benmanes.caffeine.cache.Cache;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.ycl.jxkg.base.Result;
+import com.ycl.jxkg.context.WebContext;
 import com.ycl.jxkg.domain.entity.EducationResource;
 import com.ycl.jxkg.domain.entity.Subject;
+import com.ycl.jxkg.domain.entity.StudyRecord;
 import com.ycl.jxkg.domain.vo.admin.education.EducationResourceVO;
-import com.ycl.jxkg.domain.vo.student.education.StudentOnlineVO;
+import com.ycl.jxkg.mapper.ClassesUserMapper;
 import com.ycl.jxkg.mapper.EducationResourceMapper;
 import com.ycl.jxkg.mapper.SubjectMapper;
 import com.ycl.jxkg.service.EducationResourceService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Objects;
@@ -30,7 +35,10 @@
 
     private final EducationResourceMapper mapper;
     private final SubjectMapper subjectMapper;
-
+    private final WebContext webContext;
+    private final ClassesUserMapper classesUserMapper;
+    @Autowired
+    private Cache<String, Object> caffeineCache;
     @Override
     public Result add(EducationResourceVO form) {
         EducationResource educationResource = new EducationResource();
@@ -41,6 +49,7 @@
         }
         educationResource.setCreateTime(new Date());
         educationResource.setUpdateTime(new Date());
+        educationResource.setCreateUser(webContext.getCurrentUser().getId());
         mapper.add(educationResource);
         return Result.ok("娣诲姞鎴愬姛");
     }
@@ -80,9 +89,17 @@
     }
 
     @Override
-    public Result byType(StudentOnlineVO query) {
+    public Result studentPage(EducationResourceVO query) {
+        Integer id = webContext.getCurrentUser().getId();
+        List<Integer> classes = classesUserMapper.getClassesByUserId(id);
+        if (CollectionUtils.isEmpty(classes)) {
+            //瀛︾敓娌℃湁鐝骇
+            List<EducationResourceVO> list = new ArrayList<>();
+            return Result.ok(list).put("total", 0);
+        }
+        query.setClassIds(classes);
         PageInfo<EducationResourceVO> page = PageHelper.startPage(query.getPageNum(), query.getPageSize()).doSelectPageInfo(() ->
-                mapper.byType(query));
+                mapper.studentPage(query));
         page.getList().stream().forEach(item -> {
             item.setContentUrl(JSON.parseObject(item.getContentUrlString(), EducationResourceVO.UploadFile.class));
             item.setAttachment(JSON.parseArray(item.getAttachmentString(), EducationResourceVO.UploadFile.class));
@@ -95,4 +112,27 @@
         List<Subject> subjects = subjectMapper.allSubject();
         return Result.ok(subjects);
     }
+    //璁板綍瑙嗛鏃堕暱
+    @Override
+    public Result recordTime(Integer userId) {
+        if (userId ==null){
+            throw new RuntimeException("鐢ㄦ埛id涓虹┖");
+        }
+        StudyRecord studyRecord = (StudyRecord) caffeineCache.getIfPresent("STUDENT_"+userId);
+        if (studyRecord != null) {
+            //瀛樺湪缂撳瓨
+            Date lastTime = studyRecord.getLastTime();
+            Date now = new Date();
+            studyRecord.setStudyTime(studyRecord.getStudyTime()+(now.getTime()-lastTime.getTime())/1000);
+            studyRecord.setLastTime(now);
+        }else {
+            //涓嶅瓨鍦ㄧ紦瀛�
+            studyRecord = new StudyRecord();
+            studyRecord.setStudentId(userId);
+            studyRecord.setStudyTime(0L);
+            studyRecord.setLastTime(new Date());
+        }
+        caffeineCache.put("STUDENT_" + userId, studyRecord);
+        return Result.ok();
+    }
 }

--
Gitblit v1.8.0