Răsfoiți Sursa

修改和测试状态记录接口,增加接口注释

feick 5 ani în urmă
părinte
comite
533bfff942

+ 33 - 13
src/main/java/com/yaoxiang/diagnosis/controller/LearnRecordController.java

@@ -5,6 +5,7 @@ import com.yaoxiang.diagnosis.model.Result;
 import com.yaoxiang.diagnosis.service.LearnRecordService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
@@ -12,7 +13,7 @@ import org.springframework.web.bind.annotation.*;
 import java.util.List;
 import java.util.Map;
 
-@Api(tags = "学习")
+@Api(tags = "学习记录")
 @RestController
 @RequestMapping("learn")
 public class LearnRecordController {
@@ -22,50 +23,69 @@ public class LearnRecordController {
 
     @ApiOperation("增加")
     @PostMapping("add")
-    public Result add(Long uid, String content, Long beginTime, Long endTime) {
+    public Result add(Long uid, String content, String status,
+                      @RequestParam(required = false) Long beginTime,
+                      @RequestParam(required = false) Long endTime) {
         if (beginTime == null) {
             beginTime = System.currentTimeMillis();
         }
-        boolean result = learnRecordService.add(uid, content, beginTime, endTime);
-        return Result.ok(result);
+        boolean result = learnRecordService.add(uid, content, status, beginTime, endTime);
+        return new Result(result);
     }
 
     @ApiOperation("更新")
     @PostMapping("update")
     public Result update(@RequestBody LearnRecord record) {
         boolean result = learnRecordService.update(record);
-        return Result.ok(result);
+        return new Result(result);
     }
 
     @ApiOperation("删除")
     @PostMapping("delete")
     public Result delete(Long id) {
         boolean result = learnRecordService.delete(id);
-        return Result.ok(result);
+        return new Result(result);
     }
 
+    @GetMapping("monthStatus")
     @ApiOperation("获取当月的记录情况")
-    @ApiImplicitParam(name = "time", value = "月份时间,毫秒", paramType = "query")
-    public Map<Integer, Long> monthStatus(Long uid, Long time) {
+    @ApiImplicitParam(name = "time", value = "月份时间,单位至毫秒", paramType = "query")
+    public Map<String, Long> monthStatus(Long uid, @RequestParam(required = false) Long time) {
         if (time == null) {
             time = System.currentTimeMillis();
         }
         return learnRecordService.monthStatus(uid, time);
     }
 
-    @ApiOperation("获取列表")
+    @GetMapping("listByMonth")
+    @ApiOperation("获取当月的记录情况")
+    @ApiImplicitParam(name = "time", value = "月份时间,单位至毫秒", paramType = "query")
+    public List<LearnRecord> listByMonth(Long uid, @RequestParam(required = false) Long time) {
+        if (time == null) {
+            time = System.currentTimeMillis();
+        }
+        return learnRecordService.listByMonth(uid, time);
+    }
+
+    @ApiOperation("根据用户Id获取列表")
     @GetMapping("list")
-    public List<LearnRecord> list(Long uid, String category,
-                                  @RequestParam(defaultValue = "0") Long startTime, Long endTime) {
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "startTime", value = "开始时间,单位至毫秒", paramType = "query"),
+            @ApiImplicitParam(name = "endTime", value = "结束时间,单位至毫秒", paramType = "query")
+    })
+    public List<LearnRecord> list(Long uid, @RequestParam(required = false) String category,
+                                  @RequestParam(defaultValue = "0") Long startTime,
+                                  @RequestParam(required = false) Long endTime) {
         if (endTime == null) {
             endTime = System.currentTimeMillis();
         }
         return learnRecordService.list(uid, category, startTime, endTime);
     }
 
-    @ApiOperation("获取本周的记录")
+    @ApiOperation("获取time(毫秒)所在周的记录,time为null时,获取本周记录")
     @GetMapping("listByWeek")
-    public List<LearnRecord> listByWeek(Long uid, Long time) {
+    @ApiImplicitParam(name = "time", value = "周时间,单位至毫秒", paramType = "query")
+    public List<LearnRecord> listByWeek(Long uid, @RequestParam(required = false) Long time) {
         if (time == null) {
             time = System.currentTimeMillis();
         }

+ 11 - 9
src/main/java/com/yaoxiang/diagnosis/controller/LearnTagController.java

@@ -19,35 +19,37 @@ public class LearnTagController {
     private LearnTagService learnTagService;
 
     @GetMapping("list")
-    public List<LearnTag> list(Long uid, String category) {
+    public List<LearnTag> list(@RequestParam(required = false) Long uid,
+                               @RequestParam(required = false) String category) {
         return learnTagService.list(uid, category);
     }
 
     @GetMapping("exists")
-    @ApiOperation("检测是否已存在标签")
-    public Result exists(Long uid, String content) {
-        boolean result = learnTagService.exists(uid, content);
-        return Result.ok(result);
+    @ApiOperation("检测是否已存在标签,管理端不用传uid")
+    public Result exists(@RequestParam(required = false) Long uid, String content) {
+        boolean exists = learnTagService.exists(uid, uid != null, content);
+        return new Result(exists);
     }
 
     @PostMapping("add")
     public Result add(@RequestBody LearnTag learnTag) {
-        if (learnTagService.exists(learnTag.getUid(), learnTag.getContent())) {
+        boolean exists = learnTagService.exists(learnTag.getUid(), learnTag.getUid() != null, learnTag.getContent());
+        if (exists) {
             return Result.fail("该标签已存在");
         }
         boolean result = learnTagService.add(learnTag);
-        return Result.ok(result);
+        return new Result(result);
     }
 
     @PostMapping("update")
     public Result update(@RequestBody LearnTag learnTag) {
         boolean result = learnTagService.update(learnTag);
-        return Result.ok(result);
+        return new Result(result);
     }
 
     @PostMapping("delete")
     public Result delete(Long id) {
         boolean result = learnTagService.delete(id);
-        return Result.ok(result);
+        return new Result(result);
     }
 }

+ 10 - 0
src/main/java/com/yaoxiang/diagnosis/entity/LearnTag.java

@@ -16,6 +16,8 @@ public class LearnTag {
     private Long id;
 
     private String category;
+    @Column(nullable = false)
+    private Boolean self;
     private Long uid;
     @Column(nullable = false)
     private String content;
@@ -40,6 +42,14 @@ public class LearnTag {
         this.category = category;
     }
 
+    public Boolean getSelf() {
+        return self;
+    }
+
+    public void setSelf(Boolean self) {
+        this.self = self;
+    }
+
     public Long getUid() {
         return uid;
     }

+ 21 - 5
src/main/java/com/yaoxiang/diagnosis/service/LearnRecordService.java

@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.persistence.criteria.Predicate;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -18,6 +19,8 @@ public class LearnRecordService {
     private LearnRecordRepo learnRecordRepo;
 
     public List<LearnRecord> list(Long uid, String category, Long startTime, Long endTime) {
+//        SimpleDateFormat sdfmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
         return learnRecordRepo.findAll((root, query, builder) -> {
             List<Predicate> ps = new ArrayList<>();
             if (uid != null) {
@@ -26,23 +29,28 @@ public class LearnRecordService {
             if (StringUtils.isNotBlank(category)) {
                 ps.add(builder.like(root.get("category"), "%" + category + "%"));
             }
+//            ps.add(builder.between(root.get("createtime"),
+//                    sdfmat.parse()));
+
             if (startTime != null) {
-                ps.add(builder.greaterThan(root.get("createtime"), startTime));
+                ps.add(builder.greaterThanOrEqualTo(root.get("createtime").as(Date.class), new Date(startTime)));
             }
             if (endTime != null) {
-                ps.add(builder.lessThan(root.get("createtime"), endTime));
+                ps.add(builder.lessThanOrEqualTo(root.get("createtime").as(Date.class), new Date(endTime)));
             }
+
             Predicate[] p = new Predicate[ps.size()];
             return builder.and(ps.toArray(p));
         });
     }
 
-    public Map<Integer, Long> monthStatus(Long uid, Long time) {
+    public Map<String, Long> monthStatus(Long uid, Long time) {
         Date date = new Date(time);
         Date startTime = DateUtil.getFirstDayOfMonth(date);
         Date endTime = DateUtil.getLastDayOfMonth(date);
         List<LearnRecord> list = list(uid, null, startTime.getTime(), endTime.getTime());
-        return list.stream().collect(Collectors.groupingBy(a -> DateUtil.getDay(a.getCreatetime()),
+        SimpleDateFormat format = new SimpleDateFormat("dd");
+        return list.stream().collect(Collectors.groupingBy(a -> format.format(a.getCreatetime()),
                 Collectors.counting()));
     }
 
@@ -54,9 +62,10 @@ public class LearnRecordService {
     }
 
 
-    public boolean add(Long uid, String content, Long beginTime, Long endTime) {
+    public boolean add(Long uid, String content, String status, Long beginTime, Long endTime) {
         LearnRecord record = new LearnRecord();
         record.setUid(uid);
+        record.setStatus(status);
         record.setContent(content);
         record.setBeginTime(new Date(beginTime));
         if (endTime != null) {
@@ -79,4 +88,11 @@ public class LearnRecordService {
         learnRecordRepo.deleteById(id);
         return true;
     }
+
+    public List<LearnRecord> listByMonth(Long uid, Long time) {
+        Date date = new Date(time);
+        Date startTime = DateUtil.getFirstDayOfMonth(date);
+        Date endTime = DateUtil.getLastDayOfMonth(date);
+        return list(uid, null, startTime.getTime(), endTime.getTime());
+    }
 }

+ 18 - 18
src/main/java/com/yaoxiang/diagnosis/service/LearnTagService.java

@@ -1,18 +1,18 @@
 package com.yaoxiang.diagnosis.service;
 
+import com.google.common.base.Predicates;
 import com.yaoxiang.diagnosis.dao.LearnTagRepo;
 import com.yaoxiang.diagnosis.entity.LearnTag;
 import com.yaoxiang.diagnosis.util.CommonUtil;
 import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Example;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
 
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
+import javax.persistence.criteria.*;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -26,26 +26,26 @@ public class LearnTagService {
     @Autowired
     private LearnTagRepo learnTagRepo;
 
+    private static final Logger logger = LoggerFactory.getLogger(LearnTagService.class);
+
     public boolean add(LearnTag learnTag) {
+        learnTag.setSelf(learnTag.getUid() != null);
         learnTagRepo.save(learnTag);
         return true;
     }
 
-    public boolean exists(Long uid, String content) {
+    public boolean exists(String content) {
+        return exists(null, false, content);
+    }
+
+    public boolean exists(Long uid, boolean self, String content) {
         LearnTag tag = new LearnTag();
         tag.setContent(content);
-        Example<LearnTag> example = Example.of(tag);
-        if (exists(example)) {
-            return false;
+        tag.setSelf(self);
+        if (self) {
+            tag.setUid(uid);
         }
-        if (uid == null) {
-            return true;
-        }
-        tag.setUid(uid);
-        return exists(example);
-    }
-
-    private boolean exists(Example<LearnTag> example) {
+        Example<LearnTag> example = Example.of(tag);
         List<LearnTag> list = learnTagRepo.findAll(example);
         return CommonUtil.notEmpty(list);
     }
@@ -72,9 +72,9 @@ public class LearnTagService {
             List<Predicate> ps = new ArrayList<>();
             if (uid != null) {
                 ps.add(builder.or(builder.equal(root.get("uid"), uid),
-                        builder.equal(root.get("uid"), null)));
+                        builder.isNull(root.get("uid"))));
             } else {
-                ps.add(builder.equal(root.get("uid"), null));
+                ps.add(builder.isNull(root.get("uid")));
             }
             if (StringUtils.isNotBlank(category)) {
                 ps.add(builder.like(root.get("category"), "%" + category + "%"));

+ 1 - 1
src/main/java/com/yaoxiang/diagnosis/util/DateUtil.java

@@ -183,7 +183,7 @@ public class DateUtil {
         calendar.setTime(date);
         calendar.setFirstDayOfWeek(Calendar.SUNDAY);
         int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
-        calendar.add(Calendar.DATE, dayofweek - 1);
+        calendar.add(Calendar.DATE, 1 - dayofweek);
         calendar.set(Calendar.HOUR_OF_DAY, 0);
         calendar.set(Calendar.MINUTE, 0);
         calendar.set(Calendar.SECOND, 1);