Jelajahi Sumber

添加接口

aaa 4 tahun lalu
induk
melakukan
6648342bd7

+ 67 - 0
src/main/java/com/jtxt/demo/model/TagToResources.java

@@ -0,0 +1,67 @@
+package com.jtxt.demo.model;
+
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Document
+public class TagToResources {
+    @Id
+    private String name; // 如果是date的话,一定要遵守 YYYYMMDD format, 比如 20210401
+    private List<String> materials;
+    private List<String> questions;
+
+    public TagToResources(String name) {
+        this.name = name;
+        this.materials = new ArrayList<>();
+        this.questions = new ArrayList<>();
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<String> getMaterials() {
+        return materials;
+    }
+
+    public void setMaterials(List<String> materials) {
+        this.materials = materials;
+    }
+
+    public List<String> getQuestions() {
+        return questions;
+    }
+
+    public void setQuestions(List<String> questions) {
+        this.questions = questions;
+    }
+
+    public void removeMaterial(String materialId) {
+        this.materials.remove(materialId);
+    }
+
+    public void addMaterial(String materialId) {
+        if (this.materials.contains(materialId)) {
+            return;
+        }
+        this.materials.add(materialId);
+    }
+
+    public void removeQuestion(String qid) {
+        this.questions.remove(qid);
+    }
+
+    public void addQuestion(String qid) {
+        if (this.questions.contains(qid)) {
+            return;
+        }
+        this.questions.add(qid);
+    }
+}

+ 8 - 0
src/main/java/com/jtxt/demo/repository/TagToResourcesRepository.java

@@ -0,0 +1,8 @@
+package com.jtxt.demo.repository;
+
+import com.jtxt.demo.model.TagToResources;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+public interface TagToResourcesRepository extends MongoRepository<TagToResources, String> {
+    TagToResources findByName(String name);
+}

+ 11 - 0
src/main/java/com/jtxt/demo/service/TagToResourcesService.java

@@ -0,0 +1,11 @@
+package com.jtxt.demo.service;
+
+import com.jtxt.demo.model.TagToResources;
+
+public interface TagToResourcesService {
+    TagToResources findResourcesByTag(String tag);
+    boolean addMaterialToTag(String tag, String materialId);
+    boolean addQuestionToTag(String tag, String questionId);
+    boolean removeMaterialFromTag(String tag, String materialId);
+    boolean removeQuestionFromTag(String tag, String questionId);
+}

+ 109 - 0
src/main/java/com/jtxt/demo/service/TagToResourcesServiceImpl.java

@@ -0,0 +1,109 @@
+package com.jtxt.demo.service;
+
+import com.jtxt.demo.model.TagToResources;
+import com.jtxt.demo.repository.TagToResourcesRepository;
+import com.jtxt.demo.util.CacheStore;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+
+import static com.jtxt.demo.util.Constants.CHINA_ZONE;
+
+@Service
+public class TagToResourcesServiceImpl implements TagToResourcesService {
+    @Autowired
+    private TagToResourcesRepository tagToResourcesRepository;
+    @Autowired
+    private LearningMaterialService learningMaterialService;
+    @Autowired
+    private QuestionService questionService;
+    @Autowired
+    private CacheStore<TagToResources> tagCache;
+    @Override
+    public TagToResources findResourcesByTag(String tag) {
+        if (!isTagValid(tag)) return null;
+        TagToResources rst;
+        if (tagCache.get(tag) == null) {
+            rst = tagToResourcesRepository.findByName(tag);
+            if (rst == null) return null;
+            tagCache.add(tag, rst);
+        } else {
+            rst = tagCache.get(tag);
+        }
+        return rst;
+    }
+
+    @Override
+    public boolean addMaterialToTag(String tag, String material) {
+        if (!isTagValid(tag)) return false;
+        TagToResources resources = findResourcesByTag(tag);
+        if (resources == null) {
+            resources = new TagToResources(tag);
+        }
+        resources.addMaterial(material);
+        learningMaterialService.addTag(tag, material);
+        tagCache.add(tag, resources);
+        tagToResourcesRepository.save(resources);
+        return true;
+    }
+
+    @Override
+    public boolean addQuestionToTag(String tag, String question) {
+        if (!isTagValid(tag)) return false;
+        TagToResources resources = findResourcesByTag(tag);
+        if (resources == null) {
+            resources = new TagToResources(tag);
+        }
+        resources.addQuestion(question);
+        questionService.addTag(tag, question);
+        tagCache.add(tag, resources);
+        tagToResourcesRepository.save(resources);
+        return true;
+    }
+
+    @Override
+    public boolean removeMaterialFromTag(String tag, String materialId) {
+        if (!isTagValid(tag)) return false;
+        TagToResources resources = findResourcesByTag(tag);
+        if (resources == null) {
+            return false;
+        }
+        resources.removeMaterial(materialId);
+        learningMaterialService.removeTag(tag, materialId);
+        tagCache.add(tag, resources);
+        tagToResourcesRepository.save(resources);
+        return true;
+    }
+
+    @Override
+    public boolean removeQuestionFromTag(String tag, String questionId) {
+        if (!isTagValid(tag)) return false;
+        TagToResources resources = findResourcesByTag(tag);
+        if (resources == null) {
+            return false;
+        }
+        resources.removeQuestion(questionId);
+        questionService.removeTag(tag, questionId);
+        tagCache.add(tag, resources);
+        tagToResourcesRepository.save(resources);
+        return true;
+    }
+
+    private boolean isTagValid(String tag) {
+        try {
+            int dateInt = Integer.parseInt(tag);
+            if (tag.length() != 8) return false;
+            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd");
+            LocalDate localDate = LocalDate.now(CHINA_ZONE);
+            int todayInt = Integer.parseInt(dtf.format(localDate));
+            if (dateInt > todayInt + 1000) return false; // 最多可设置未来1000天的每日必读必练
+            if (dateInt < todayInt - 1) return false;
+            return true;
+        } catch (NumberFormatException nfe) {
+            // 但凡不是全部数字,一律判断为合法tag
+            return true;
+        }
+    }
+}