|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|