From 79db32c215457e441d58256d839d5aa2e4d10ebf Mon Sep 17 00:00:00 2001
From: winter <2436197699@qq.com>
Date: Mon, 11 Dec 2023 11:52:40 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=83=A8=E5=88=86?=
=?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E6=8E=A5=E5=8F=A3=E4=BB=A5=E5=8F=8A?=
=?UTF-8?q?=E6=8F=90=E5=8D=87=E7=A8=B3=E5=AE=9A=E6=80=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 36 +++++++--
src/main/java/cn/teammodel/common/PageVo.java | 18 +++++
.../admin/controller/IndexController.java | 11 +--
.../admin/service/AdminAppraiseService.java | 5 +-
.../impl/AdminAppraiseServiceImpl.java | 50 ++++++++-----
.../dao/AppraiseRecordRepository.java | 2 +-
.../cn/teammodel/dao/SchoolRepository.java | 2 +
.../teammodel/model/vo/admin/IndexData.java | 2 +-
.../teammodel/model/vo/appraise/RecordVo.java | 10 ++-
.../service/impl/EvaluationServiceImpl.java | 8 +-
.../cn/teammodel/utils/SchoolDateUtil.java | 75 ++++++++++++++++---
.../TeamModelExtensionApplicationTests.java | 67 +++++++++--------
.../java/cn/teammodel/TestWithoutSpring.java | 18 ++++-
13 files changed, 216 insertions(+), 88 deletions(-)
create mode 100644 src/main/java/cn/teammodel/common/PageVo.java
diff --git a/pom.xml b/pom.xml
index 9e20422..55b1d49 100644
--- a/pom.xml
+++ b/pom.xml
@@ -39,16 +39,17 @@
-
- com.azure.spring
- spring-cloud-azure-starter
-
+
+
+
+
com.github.xiaoymin
knife4j-spring-boot-starter
3.0.3
+
com.azure.spring
spring-cloud-azure-starter-data-cosmos
@@ -73,11 +74,12 @@
- io.jsonwebtoken
- jjwt
- 0.9.1
+ jsonwebtoken
+ habook-jjwt
+ 0.0.1
+
org.springframework.boot
spring-boot-starter-test
@@ -92,6 +94,7 @@
+
@@ -104,6 +107,25 @@
+
+
+ gitea
+ http://163.228.224.105:3000/api/packages/winteach/maven
+
+
+
+
+
+ gitea
+ http://163.228.224.105:3000/api/packages/winteach/maven
+
+
+
+ gitea
+ http://163.228.224.105:3000/api/packages/winteach/maven
+
+
+
diff --git a/src/main/java/cn/teammodel/common/PageVo.java b/src/main/java/cn/teammodel/common/PageVo.java
new file mode 100644
index 0000000..17a148b
--- /dev/null
+++ b/src/main/java/cn/teammodel/common/PageVo.java
@@ -0,0 +1,18 @@
+package cn.teammodel.common;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author winter
+ * @create 2023-12-08 17:30
+ */
+@Data
+@AllArgsConstructor
+public class PageVo {
+ private Integer totalPages;
+ private Long totalItems;
+ private List content;
+}
diff --git a/src/main/java/cn/teammodel/controller/admin/controller/IndexController.java b/src/main/java/cn/teammodel/controller/admin/controller/IndexController.java
index f4dc0fd..55b7b9f 100644
--- a/src/main/java/cn/teammodel/controller/admin/controller/IndexController.java
+++ b/src/main/java/cn/teammodel/controller/admin/controller/IndexController.java
@@ -1,5 +1,6 @@
package cn.teammodel.controller.admin.controller;
+import cn.teammodel.common.PageVo;
import cn.teammodel.common.R;
import cn.teammodel.controller.admin.service.AdminAppraiseService;
import cn.teammodel.model.dto.admin.TimeRangeDto;
@@ -27,17 +28,17 @@ public class IndexController {
private AdminAppraiseService adminAppraiseService;
- @GetMapping("/{academicYearId}")
+ @GetMapping("/{periodId}")
@ApiOperation("获取首页数据")
- public R index(@PathVariable String academicYearId){
- IndexData indexData = adminAppraiseService.getIndexData(academicYearId);
+ public R index(@PathVariable String periodId){
+ IndexData indexData = adminAppraiseService.getIndexData(periodId);
return R.success(indexData);
}
@PostMapping("latestRecord")
@ApiOperation("查询最新评价(可通过时间范围分页)")
- public R> latestRecord(@Valid @RequestBody TimeRangeDto timeRangeDto) {
- List res = adminAppraiseService.conditionLatestRecord(timeRangeDto);
+ public R> latestRecord(@Valid @RequestBody TimeRangeDto timeRangeDto) {
+ PageVo res = adminAppraiseService.conditionLatestRecord(timeRangeDto);
return R.success(res);
}
diff --git a/src/main/java/cn/teammodel/controller/admin/service/AdminAppraiseService.java b/src/main/java/cn/teammodel/controller/admin/service/AdminAppraiseService.java
index eb54621..1f55397 100644
--- a/src/main/java/cn/teammodel/controller/admin/service/AdminAppraiseService.java
+++ b/src/main/java/cn/teammodel/controller/admin/service/AdminAppraiseService.java
@@ -1,5 +1,6 @@
package cn.teammodel.controller.admin.service;
+import cn.teammodel.common.PageVo;
import cn.teammodel.model.dto.admin.TimeRangeDto;
import cn.teammodel.model.vo.admin.IndexData;
import cn.teammodel.model.vo.admin.RankPo;
@@ -14,12 +15,12 @@ import java.util.List;
* @create 2023-12-06 14:45
*/
public interface AdminAppraiseService {
- IndexData getIndexData(String academicYearId);
+ IndexData getIndexData(String periodId);
/**
* 按时期分页获取最新的评价数据
*/
- List conditionLatestRecord(TimeRangeDto timeRangeDto);
+ PageVo conditionLatestRecord(TimeRangeDto timeRangeDto);
List classRank(TimeRangeDto timeRangeDto);
diff --git a/src/main/java/cn/teammodel/controller/admin/service/impl/AdminAppraiseServiceImpl.java b/src/main/java/cn/teammodel/controller/admin/service/impl/AdminAppraiseServiceImpl.java
index b4eae77..a73bb12 100644
--- a/src/main/java/cn/teammodel/controller/admin/service/impl/AdminAppraiseServiceImpl.java
+++ b/src/main/java/cn/teammodel/controller/admin/service/impl/AdminAppraiseServiceImpl.java
@@ -1,12 +1,16 @@
package cn.teammodel.controller.admin.service.impl;
import cn.hutool.core.lang.Pair;
+import cn.teammodel.common.ErrorCode;
import cn.teammodel.common.PK;
+import cn.teammodel.common.PageVo;
+import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.controller.admin.service.AdminAppraiseService;
import cn.teammodel.dao.*;
import cn.teammodel.model.dto.admin.TimeRangeDto;
import cn.teammodel.model.entity.User;
import cn.teammodel.model.entity.school.ClassInfo;
+import cn.teammodel.model.entity.school.School;
import cn.teammodel.model.entity.school.Student;
import cn.teammodel.model.entity.school.Teacher;
import cn.teammodel.model.vo.admin.IndexData;
@@ -15,26 +19,30 @@ import cn.teammodel.model.vo.admin.RankVo;
import cn.teammodel.model.vo.admin.StudentRankVo;
import cn.teammodel.model.vo.appraise.RecordVo;
import cn.teammodel.security.utils.SecurityUtil;
+import cn.teammodel.utils.SchoolDateUtil;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
-import java.time.Instant;
+import java.time.LocalDate;
import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.time.temporal.WeekFields;
import java.util.*;
import java.util.stream.Collectors;
+import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum;
+
/**
* @author winter
* @create 2023-12-06 14:46
*/
@Service
public class AdminAppraiseServiceImpl implements AdminAppraiseService {
+ @Resource
+ private SchoolRepository schoolRepository;
@Resource
private ClassRepository classRepository;
@Resource
@@ -48,19 +56,32 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
private AppraiseRecordRepository appraiseRecordRepository;
@Override
- public IndexData getIndexData(String academicYearId) {
+ public IndexData getIndexData(String periodId) {
+ final int SLICE_SIZE = 100;
+ if (StringUtils.isBlank(periodId)) {
+ throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "不能为空");
+ }
+
final IndexData indexData = new IndexData();
int totalCount = 0;
int criticalCount = 0;
Set creatorIdSet = new HashSet<>();
-
User loginUser = SecurityUtil.getLoginUser();
String schoolId = loginUser.getSchoolId();
+ // 获取学期起止时间
+ List semesters = schoolRepository.findSemestersById(schoolId, periodId);
+ SchoolDateUtil.semesterModel semesterModel = SchoolDateUtil.getSemesterByNow(semesters, LocalDate.now());
+ String academicYearId = semesterModel.getAcademicYearId();
+ LocalDateTime startDatetime = semesterModel.getStartDatetime();
+ LocalDateTime endDatetime = semesterModel.getEndDatetime();
+ if (startDatetime == null || endDatetime == null) throw new ServiceException(ErrorCode.PARAMS_ERROR);
+ long totalWeek = calculateWeekNum(startDatetime, endDatetime, null);
+
// slice 分段读取
- CosmosPageRequest pageRequest = new CosmosPageRequest(0, 100, null);
+ CosmosPageRequest pageRequest = new CosmosPageRequest(0, SLICE_SIZE, null);
Slice slice;
- Map countByWeek = new HashMap<>();
+ Map countByWeek = SchoolDateUtil.createEmptyWeekMap(totalWeek);
do {
slice = appraiseRecordRepository.findAllByAcademicYearId(String.format(PK.PK_APPRAISE_RECORD, schoolId), academicYearId, pageRequest);
@@ -72,7 +93,7 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
// 分批次计算
for (RecordVo item : content) {
// 处理每周的评价数
- int weekNum = calculateWeekNum(item.getCreateTime());
+ long weekNum = calculateWeekNum(startDatetime, endDatetime, item.getCreateTime());
countByWeek.put(weekNum, countByWeek.getOrDefault(weekNum, 0) + 1);
// 处理总评价数
totalCount++;
@@ -99,7 +120,7 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
}
@Override
- public List conditionLatestRecord(TimeRangeDto timeRangeDto) {
+ public PageVo conditionLatestRecord(TimeRangeDto timeRangeDto) {
Long startTime = timeRangeDto.getStartTime();
Long endTime = timeRangeDto.getEndTime();
String academicYearId = timeRangeDto.getAcademicYearId();
@@ -115,7 +136,7 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
endTime,
pageRequest);
- return page.getContent();
+ return new PageVo<>(page.getTotalPages(), page.getTotalElements(), page.getContent());
}
@Override
@@ -272,13 +293,4 @@ public class AdminAppraiseServiceImpl implements AdminAppraiseService {
return res;
}
- private int calculateWeekNum(Long timeStamp) {
- // second 时间戳转 LocalDateTime
- LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeStamp), ZoneOffset.UTC);
- // 获取周字段 todo: 时区或者周的计算
- WeekFields weekFields = WeekFields.of(Locale.getDefault());
- // 获取当前日期时间所在年的周数
- return localDateTime.get(weekFields.weekOfWeekBasedYear());
- }
-
}
diff --git a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java
index 9f2ce5f..f4fcbbb 100644
--- a/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java
+++ b/src/main/java/cn/teammodel/dao/AppraiseRecordRepository.java
@@ -67,7 +67,7 @@ public interface AppraiseRecordRepository extends CosmosRepository= @startTime) and " +
diff --git a/src/main/java/cn/teammodel/dao/SchoolRepository.java b/src/main/java/cn/teammodel/dao/SchoolRepository.java
index 571957c..53b9357 100644
--- a/src/main/java/cn/teammodel/dao/SchoolRepository.java
+++ b/src/main/java/cn/teammodel/dao/SchoolRepository.java
@@ -21,4 +21,6 @@ public interface SchoolRepository extends CosmosRepository {
List findPeriodById(@Param("schoolId") String schoolId, @Param("periodId") String periodId);
@Query("select value p.semesters from School as s join p in s.period where s.id = @schoolId and s.code = 'Base' and p.id = @periodId")
List findSemestersById(@Param("schoolId") String schoolId, @Param("periodId") String periodId);
+
+ School findSchoolByIdAndCode(String schoolId, String code);
}
diff --git a/src/main/java/cn/teammodel/model/vo/admin/IndexData.java b/src/main/java/cn/teammodel/model/vo/admin/IndexData.java
index abd30fd..2012dd2 100644
--- a/src/main/java/cn/teammodel/model/vo/admin/IndexData.java
+++ b/src/main/java/cn/teammodel/model/vo/admin/IndexData.java
@@ -14,5 +14,5 @@ public class IndexData {
private Integer praiseCount;
private Integer criticalCount;
private Integer teacherCount;
- private Map countByWeek;
+ private Map countByWeek;
}
diff --git a/src/main/java/cn/teammodel/model/vo/appraise/RecordVo.java b/src/main/java/cn/teammodel/model/vo/appraise/RecordVo.java
index ca1af08..82fdd3a 100644
--- a/src/main/java/cn/teammodel/model/vo/appraise/RecordVo.java
+++ b/src/main/java/cn/teammodel/model/vo/appraise/RecordVo.java
@@ -9,12 +9,14 @@ import lombok.Data;
*/
@Data
public class RecordVo {
- private String recordId;
- private String targetName;
+ private String recordId; // 文档 id
+ private String recordNodeId; // 文档中的评价记录 id
+ private String targetName; // 评价对象的名字
private String avatar;
+ private String className;
private String appraiseName;
- private String targetId;
- private String targetType;
+ private String targetId; // 评价对象 id
+ private String targetType; // 评价对象类型
private String creatorId;
private String creator;
private Long createTime;
diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java
index 14baa57..da80af5 100644
--- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java
+++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java
@@ -180,7 +180,7 @@ public class EvaluationServiceImpl implements EvaluationService {
newNode.setId(UUID.randomUUID().toString());
newNode.setCreatorId(loginUser.getId());
newNode.setCreator(loginUser.getName());
- newNode.setCreateTime(Instant.now().getEpochSecond());
+ newNode.setCreateTime(Instant.now().toEpochMilli());
originNodes.add(newNode);
return buildTree(appraiseRepository.save(appraise));
@@ -277,7 +277,7 @@ public class EvaluationServiceImpl implements EvaluationService {
List semesters = period.getSemesters();
// 获取当前学年学期组合 ID
- String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now());
+ String academicYearId = SchoolDateUtil.calculateAcademicYearId(semesters, LocalDate.now());
// 查询是否存在记录
AppraiseRecord record = appraiseRecordRepository.findAppraiseRecordByTargetIdAndClassIdAndAcademicYearIdAndCode(
@@ -293,7 +293,7 @@ public class EvaluationServiceImpl implements EvaluationService {
item.setAppraiseNode(appraiseTreeNode);
item.setCreator(loginUser.getName());
item.setCreatorId(loginUser.getId());
- item.setCreateTime(Instant.now().getEpochSecond());
+ item.setCreateTime(Instant.now().toEpochMilli());
// 处理学校与学生的差异
if (targetType.equals(TARGET_CLASS)){
item.setSpread(spread);
@@ -345,7 +345,7 @@ public class EvaluationServiceImpl implements EvaluationService {
String schoolId = loginUser.getSchoolId();
List semesters = schoolRepository.findSemestersById(schoolId, periodId);
- String academicYearId = SchoolDateUtil.generateAcademicId(semesters, LocalDate.now());
+ String academicYearId = SchoolDateUtil.calculateAcademicYearId(semesters, LocalDate.now());
// 分页
Sort sort = Sort.by("createTime").descending();
final CosmosPageRequest pageRequest = new CosmosPageRequest(findVoteRecordDto.getCurrent(), findVoteRecordDto.getSize(), null, sort);
diff --git a/src/main/java/cn/teammodel/utils/SchoolDateUtil.java b/src/main/java/cn/teammodel/utils/SchoolDateUtil.java
index be0fca8..29009b7 100644
--- a/src/main/java/cn/teammodel/utils/SchoolDateUtil.java
+++ b/src/main/java/cn/teammodel/utils/SchoolDateUtil.java
@@ -3,11 +3,18 @@ package cn.teammodel.utils;
import cn.teammodel.common.CommonConstant;
import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.model.entity.school.School;
-import org.apache.commons.lang3.tuple.Pair;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import java.time.Instant;
import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.temporal.ChronoUnit;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* 学校中年级,学年,日期相关的工具类
@@ -41,7 +48,7 @@ public class SchoolDateUtil {
* 注意: 处理跨年问题 -> 2024.1.1, 应该也是 2023学年上学期,同时你需要注意,semesters 可能不止两个学期
*
*/
- public static Pair getSemesterByNow(List semesters, LocalDate date) {
+ public static semesterModel getSemesterByNow(List semesters, LocalDate date) {
if (semesters == null || semesters.isEmpty() || date == null) {
throw new ServiceException("semesters and date must not be null or empty.");
}
@@ -53,6 +60,8 @@ public class SchoolDateUtil {
int size = semesters.size();
// 拿到最晚开学的日期,将年份 - 1后,与头节点组成可跨年区间
School.Semester lastSemester = semesters.get(size - 1);
+ School.Semester firstSemester = semesters.get(0);
+ // 动态替换
LocalDate lastSemesterStart = LocalDate.of(date.getYear() - 1, lastSemester.getMonth(), lastSemester.getDay());
String lastSemesterId = lastSemester.getId();
for (School.Semester semester : semesters) {
@@ -60,25 +69,71 @@ public class SchoolDateUtil {
// 2022.9.1 - 2023.3.1, 2023.3.1 - 2023.9.1, 2023.9.1 - 2024.3.1
LocalDate curSemesterStart = LocalDate.of(date.getYear(), semester.getMonth(), semester.getDay());
if (date.isEqual(lastSemesterStart) || date.isAfter(lastSemesterStart) && date.isBefore(curSemesterStart)) {
- return Pair.of(lastSemesterId, lastSemesterStart);
+ String academicYearId = generateCurAcademicId(lastSemesterStart.getYear(), lastSemesterId);
+ return new semesterModel(academicYearId, lastSemesterId, lastSemesterStart.atStartOfDay(), semester.getId(), curSemesterStart.atStartOfDay());
}
lastSemesterStart = curSemesterStart;
lastSemesterId = semester.getId();
}
- // 剩下的时间段, 学年应该就是最后一个学年
- return Pair.of(lastSemester.getId(), LocalDate.of(date.getYear(), lastSemester.getMonth(), lastSemester.getDay()));
+ // 剩下的时间段, 学年应该就是最后一个学年. 学期末就应该是下一年(year + 1)的第一个节点
+ String academicYearId = generateCurAcademicId(date.getYear(), lastSemester.getId());
+ return new semesterModel(
+ academicYearId,
+ lastSemester.getId(),
+ LocalDate.of(date.getYear(), lastSemester.getMonth(), lastSemester.getDay()).atStartOfDay(),
+ firstSemester.getId(),
+ LocalDate.of(date.getYear() + 1, firstSemester.getMonth(), firstSemester.getDay()).atStartOfDay()
+ );
}
/**
* 返回 ID -> 学年(组合 id: 学年-semesterId -> 2023-{semesterId})
*/
- public static String generateAcademicId(List semesters, LocalDate date) {
- Pair pair = getSemesterByNow(semesters, date);
- String semesterId = pair.getLeft();
- LocalDate academicYear = pair.getRight();
- return academicYear.getYear() + CommonConstant.DASH + semesterId;
+ private static String generateCurAcademicId(int academicYear, String semesterId) {
+ return academicYear + CommonConstant.DASH + semesterId;
}
+ public static String calculateAcademicYearId(List semesters, LocalDate date) {
+ return getSemesterByNow(semesters, date).getAcademicYearId();
+ }
+ /**
+ * 计算一学期相对的周数
+ * 如果 timeStamp 为 null, 则返回当前学期的总周数
+ */
+ public static long calculateWeekNum(LocalDateTime startDateTime, LocalDateTime endDateTime, Long timeStamp) {
+ if (timeStamp == null) {
+ return ChronoUnit.WEEKS.between(startDateTime, endDateTime) + 1;
+ } else {
+ LocalDateTime curDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneOffset.UTC);
+ // 如果传参没在学期范围内, 返回 -1
+ if (curDateTime.isBefore(startDateTime) || curDateTime.isAfter(endDateTime)) {
+ return -1;
+ }
+ return ChronoUnit.WEEKS.between(startDateTime, curDateTime) + 1;
+ }
+ }
+
+ /**
+ * 创建初始化的 Map
+ */
+ public static Map createEmptyWeekMap(long totalWeek) {
+ Map initialMap = new HashMap<>();
+ for (long i = 1; i <= totalWeek; i++) {
+ initialMap.put(i, 0);
+ }
+ return initialMap;
+ }
+
+
+ @Data
+ @AllArgsConstructor
+ public static class semesterModel {
+ private String academicYearId;
+ private String startSemesterId;
+ private LocalDateTime startDatetime;
+ private String endSemesterId;
+ private LocalDateTime endDatetime;
+ }
}
diff --git a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java
index c5722a6..7062405 100644
--- a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java
+++ b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java
@@ -10,24 +10,25 @@ import cn.teammodel.manager.DingAlertNotifier;
import cn.teammodel.model.dto.admin.TimeRangeDto;
import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseRecord;
+import cn.teammodel.model.entity.appraise.AppraiseRecordItem;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.model.vo.admin.RankPo;
-import cn.teammodel.model.vo.appraise.RecordVo;
import cn.teammodel.service.EvaluationService;
import cn.teammodel.service.impl.EvaluationServiceImpl;
import com.azure.cosmos.models.PartitionKey;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
-import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.data.domain.Slice;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.WeekFields;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.UUID;
@SpringBootTest
class TeamModelExtensionApplicationTests {
@@ -150,24 +151,24 @@ class TeamModelExtensionApplicationTests {
@Test
public void testSlice() {
- CosmosPageRequest pageRequest = new CosmosPageRequest(0, 10, null);
- Slice slice;
- Map countByWeek = new HashMap<>();
-
- do {
- slice = appraiseRecordRepository.findAllByAcademicYearId("AppraiseRecord-habook", "2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", pageRequest);
-
- slice.getContent().forEach(item -> {
- int weekNum = calculateWeekNum(item.getCreateTime());
- countByWeek.put(weekNum, countByWeek.getOrDefault(weekNum, 0) + 1);
- });
-
- if (slice.hasNext()) {
- pageRequest = (CosmosPageRequest) slice.nextPageable();
- }
- } while (slice.hasNext());
-
- countByWeek.forEach((item, count) -> System.out.println(item + ": " + count));
+// CosmosPageRequest pageRequest = new CosmosPageRequest(0, 10, null);
+// Slice slice;
+// Map countByWeek = new HashMap<>();
+//
+// do {
+// slice = appraiseRecordRepository.findAllByAcademicYearId("AppraiseRecord-habook", "2023-71fbd0bd-9a46-0490-f6b3-7d16cba4c017", pageRequest);
+//
+// slice.getContent().forEach(item -> {
+// int weekNum = calculateWeekNum(item.getCreateTime());
+// countByWeek.put(weekNum, countByWeek.getOrDefault(weekNum, 0) + 1);
+// });
+//
+// if (slice.hasNext()) {
+// pageRequest = (CosmosPageRequest) slice.nextPageable();
+// }
+// } while (slice.hasNext());
+//
+// countByWeek.forEach((item, count) -> System.out.println(item + ": " + count));
}
private int calculateWeekNum(LocalDateTime localDateTime) {
@@ -188,17 +189,19 @@ class TeamModelExtensionApplicationTests {
@Test
public void batchUpdateTimeFormat() {
// 批量更改所有 appraiseRecord 的时间格式为标准的 utf
- List appraiseRecords = appraiseRecordRepository.findByCode(String.format(PK.PK_APPRAISE_RECORD, "hbcn"));
+ List appraiseRecords = appraiseRecordRepository.findByCode(String.format(PK.PK_APPRAISE_RECORD, "habook"));
for (AppraiseRecord appraiseRecord : appraiseRecords) {
-// List nodes = appraiseRecord.getNodes();
-// // 处理 node 中的时间
-// for (AppraiseRecordItem node : nodes) {
-// String createTime = node.getTime();
-// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
-// // 解析字符串
-// LocalDateTime dateTime = LocalDateTime.parse(createTime, formatter);
-// node.setTime(String.valueOf(dateTime.toEpochSecond(ZoneOffset.UTC)));
-// }
+ List nodes = appraiseRecord.getNodes();
+ // 处理 node 中的时间
+ for (AppraiseRecordItem node : nodes) {
+ Long createTime = node.getCreateTime();
+ int length = String.valueOf(createTime).length();
+ // 让所有十位的时间戳转为十三位
+ if (length == 10) {
+ createTime = createTime * 1000L;
+ }
+ node.setCreateTime(createTime);
+ }
cosmosTemplate.upsert(appraiseRecord);
System.out.println("更新完毕");
}
diff --git a/src/test/java/cn/teammodel/TestWithoutSpring.java b/src/test/java/cn/teammodel/TestWithoutSpring.java
index 6bd60b0..6d45973 100644
--- a/src/test/java/cn/teammodel/TestWithoutSpring.java
+++ b/src/test/java/cn/teammodel/TestWithoutSpring.java
@@ -4,6 +4,7 @@ import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.service.impl.EvaluationServiceImpl;
+import cn.teammodel.utils.SchoolDateUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
@@ -12,7 +13,10 @@ import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
+import java.time.Instant;
import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -145,9 +149,17 @@ public class TestWithoutSpring {
new School.Semester("2", "下学期", 0, 5, 1)
);
- LocalDate currentDate = LocalDate.of(2024, 1, 1);
+ LocalDate currentDate = LocalDate.of(2024, 11, 1);
- //String currentSemester = SchoolDateUtil.getSemesterByNow(semesters, currentDate);
- //System.out.println(currentSemester);
+ String id = SchoolDateUtil.calculateAcademicYearId(semesters, currentDate);
+ System.out.println(id);
+ }
+
+ @Test
+ public void testWeek() {
+ LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochSecond(1690819200L), ZoneOffset.UTC);
+ LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochSecond(1708099200L), ZoneOffset.UTC);
+ long l = SchoolDateUtil.calculateWeekNum(start, end, null);
+ System.out.println(l);
}
}