|
@@ -1,13 +1,18 @@
|
|
|
package com.yaoxiang.diagnosis.service;
|
|
|
|
|
|
+import com.yaoxiang.diagnosis.config.Constants;
|
|
|
import com.yaoxiang.diagnosis.dao.LearnRecordRepo;
|
|
|
import com.yaoxiang.diagnosis.entity.LearnRecord;
|
|
|
import com.yaoxiang.diagnosis.util.DateUtil;
|
|
|
+import io.swagger.models.auth.In;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
+import javax.transaction.Transactional;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -18,7 +23,7 @@ public class LearnRecordService {
|
|
|
@Autowired
|
|
|
private LearnRecordRepo learnRecordRepo;
|
|
|
|
|
|
- public List<LearnRecord> list(Long uid, String category, Long startTime, Long endTime) {
|
|
|
+ public List<LearnRecord> list(Long uid, String category, String status, Long startTime, Long endTime) {
|
|
|
|
|
|
|
|
|
return learnRecordRepo.findAll((root, query, builder) -> {
|
|
@@ -31,7 +36,9 @@ public class LearnRecordService {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+ if (StringUtils.isNotBlank(status)) {
|
|
|
+ ps.add(builder.equal(root.get("status"), status));
|
|
|
+ }
|
|
|
if (startTime != null) {
|
|
|
ps.add(builder.greaterThanOrEqualTo(root.get("createtime").as(Date.class), new Date(startTime)));
|
|
|
}
|
|
@@ -44,28 +51,43 @@ public class LearnRecordService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public Map<String, Long> monthStatus(Long uid, Long time) {
|
|
|
+ public Map<String, Pair> 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());
|
|
|
+ List<LearnRecord> list = list(uid, null, null, startTime.getTime(), endTime.getTime());
|
|
|
+ Map<String, List<LearnRecord>> map = groupByDay(list);
|
|
|
+ Map<String, Pair> result = new LinkedHashMap<>();
|
|
|
+ map.keySet().stream().sorted(Comparator.comparingInt(Integer::parseInt))
|
|
|
+ .forEach(k -> {
|
|
|
+ List<LearnRecord> records = map.get(k);
|
|
|
+ boolean finished = records.stream().allMatch(r -> Constants.LEARN_STATUS_FINISH.equals(r.getStatus()));
|
|
|
+ Pair<Integer, Boolean> pair = ImmutablePair.of(records.size(), finished);
|
|
|
+ result.put(k, pair);
|
|
|
+ });
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, List<LearnRecord>> groupByDay(List<LearnRecord> list) {
|
|
|
SimpleDateFormat format = new SimpleDateFormat("dd");
|
|
|
- return list.stream().collect(Collectors.groupingBy(a -> format.format(a.getCreatetime()),
|
|
|
- Collectors.counting()));
|
|
|
+ return list.stream().collect(Collectors.groupingBy(a -> format.format(a.getCreatetime())));
|
|
|
}
|
|
|
|
|
|
public List<LearnRecord> listByWeek(Long uid, Long time) {
|
|
|
Date date = new Date(time);
|
|
|
Date startTime = DateUtil.getFirstDayOfWeek(date);
|
|
|
Date endTime = DateUtil.getLastDayOfWeek(date);
|
|
|
- return list(uid, null, startTime.getTime(), endTime.getTime());
|
|
|
+ return list(uid, null, null, startTime.getTime(), endTime.getTime());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public boolean add(Long uid, String content, String status, Long beginTime, Long endTime) {
|
|
|
+ @Transactional
|
|
|
+ public boolean add(Long uid, String content, Long subjectId, Long planId, Integer step, Long beginTime, Long endTime) {
|
|
|
LearnRecord record = new LearnRecord();
|
|
|
record.setUid(uid);
|
|
|
- record.setStatus(status);
|
|
|
+ record.setSubjectId(subjectId);
|
|
|
+ record.setPlanId(planId);
|
|
|
+ record.setStep(step);
|
|
|
+ record.setStatus(Constants.LEARN_STATUS_TODO);
|
|
|
record.setContent(content);
|
|
|
record.setBeginTime(new Date(beginTime));
|
|
|
if (endTime != null) {
|
|
@@ -75,6 +97,7 @@ public class LearnRecordService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
public boolean update(LearnRecord learnRecord) {
|
|
|
Optional<LearnRecord> optional = learnRecordRepo.findById(learnRecord.getId());
|
|
|
if (!optional.isPresent()) {
|
|
@@ -84,6 +107,7 @@ public class LearnRecordService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
public boolean delete(Long id) {
|
|
|
learnRecordRepo.deleteById(id);
|
|
|
return true;
|
|
@@ -93,6 +117,20 @@ public class LearnRecordService {
|
|
|
Date date = new Date(time);
|
|
|
Date startTime = DateUtil.getFirstDayOfMonth(date);
|
|
|
Date endTime = DateUtil.getLastDayOfMonth(date);
|
|
|
- return list(uid, null, startTime.getTime(), endTime.getTime());
|
|
|
+ return list(uid, null, null, startTime.getTime(), endTime.getTime());
|
|
|
}
|
|
|
+
|
|
|
+ public List<LearnRecord> listByDay(Long uid, Long time) {
|
|
|
+ Date date = new Date(time);
|
|
|
+ Date startTime = DateUtil.getFirstTimeOfDay(date);
|
|
|
+ Date endTime = DateUtil.getLastTimeOfDay(date);
|
|
|
+ return list(uid, null, null, startTime.getTime(), endTime.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, List<LearnRecord>> groupByStatus(Long uid) {
|
|
|
+ List<String> statuses = Arrays.asList(Constants.LEARN_STATUS_TODO, Constants.LEARN_STATUS_DOING);
|
|
|
+ List<LearnRecord> list = learnRecordRepo.findAllByUidAndStatusIn(uid, statuses);
|
|
|
+ return list.stream().collect(Collectors.groupingBy(LearnRecord::getStatus));
|
|
|
+ }
|
|
|
+
|
|
|
}
|