添加了Announcement相关的类与方法,将时间相关序列化的时区改为了+8:00,修复了TimeUtils不能使用的bug,修改了yml中的转发处理配置

master
ArgonarioD 2022-07-07 15:10:48 +08:00
parent f715834af7
commit 2270bf6422
14 changed files with 300 additions and 24 deletions

View File

@ -34,14 +34,14 @@ public class SerializeConfig {
@Override @Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) { if (value != null) {
gen.writeNumber(value.toEpochSecond(ZoneOffset.UTC)); gen.writeNumber(value.toEpochSecond(ZoneOffset.of("+08:00")));
} }
} }
}); });
javaTimeModule.addDeserializer(LocalDateTime.class, new JsonDeserializer<>() { javaTimeModule.addDeserializer(LocalDateTime.class, new JsonDeserializer<>() {
@Override @Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Instant.ofEpochSecond(p.getValueAsLong(), 0).atOffset(ZoneOffset.UTC).toLocalDateTime(); return Instant.ofEpochSecond(p.getValueAsLong(), 0).atOffset(ZoneOffset.of("+08:00")).toLocalDateTime();
} }
}); });
@ -57,7 +57,7 @@ public class SerializeConfig {
javaTimeModule.addDeserializer(LocalDate.class, new JsonDeserializer<>() { javaTimeModule.addDeserializer(LocalDate.class, new JsonDeserializer<>() {
@Override @Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Instant.ofEpochSecond(p.getValueAsLong(), 0).atOffset(ZoneOffset.UTC).toLocalDate(); return Instant.ofEpochSecond(p.getValueAsLong(), 0).atOffset(ZoneOffset.of("+08:00")).toLocalDate();
} }
}); });
@ -66,14 +66,14 @@ public class SerializeConfig {
@Override @Override
public void serialize(LocalTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { public void serialize(LocalTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) { if (value != null) {
gen.writeNumber(value.toEpochSecond(LocalDate.of(1970, 1, 1), ZoneOffset.UTC)); gen.writeNumber(value.toEpochSecond(LocalDate.of(1970, 1, 1), ZoneOffset.of("+08:00")));
} }
} }
}); });
javaTimeModule.addDeserializer(LocalTime.class, new JsonDeserializer<>() { javaTimeModule.addDeserializer(LocalTime.class, new JsonDeserializer<>() {
@Override @Override
public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Instant.ofEpochSecond(p.getValueAsLong(), 0).atOffset(ZoneOffset.UTC).toLocalTime(); return Instant.ofEpochSecond(p.getValueAsLong(), 0).atOffset(ZoneOffset.of("+08:00")).toLocalTime();
} }
}); });

View File

@ -0,0 +1,83 @@
package cn.edu.hfut.rmdjzz.projectmanagement.controller;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDto;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IAnnouncementService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author
* @since 2022/7/7 11:29
*/
@RestController
@RequestMapping("/project/{projectId}/announcement")
public class AnnouncementController {
@Autowired
private IAnnouncementService announcementService;
@Autowired
private IProjectGroupService projectGroupService;
@SneakyThrows
@GetMapping
public ResponseList<AnnouncementDto> getAnnouncementList(@RequestHeader("Token") String token, @PathVariable Integer projectId) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new ForbiddenException("无该项目访问权限");
}
return ResponseList.ofSuccess("查询成功", announcementService.getAnnouncementList(projectId));
}
@SneakyThrows
@GetMapping("/{announcementId}")
public ResponseMap getAnnouncementById(
@RequestHeader("Token") String token,
@PathVariable Integer projectId,
@PathVariable Long announcementId
){
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new ForbiddenException("无该项目访问权限");
}
return ResponseMap.ofSuccess("查询成功");
}
@SneakyThrows
@PostMapping
public ResponseMap createAnnouncement(
@RequestHeader("Token") String token,
@PathVariable Integer projectId,
@RequestBody Announcement announcement
) {
if (projectGroupService.getProjectAccessLevel(token, projectId) > 2) {
throw new ForbiddenException("无该操作权限");
}
announcement.setProjectId(projectId);
announcement.setAnnouncementPublisherId(TokenUtils.getStaffId(token));
announcement.setAnnouncementPublishTime(null);
if (announcementService.save(announcement)) {
return ResponseMap.ofSuccess("创建成功");
}
throw new BadRequestException("创建失败");
}
//TODO: 查询权限高低
@PutMapping("/{announcementId}")
public ResponseMap modifyAnnouncement(
@RequestHeader("Token") String token,
@PathVariable Integer projectId,
@PathVariable Long announcementId,
@RequestBody Announcement announcement
){
return ResponseMap.ofSuccess("更新成功");
}
}

View File

@ -80,7 +80,6 @@ public class ProjectGroupController {
return ResponseMap.ofSuccess("更新成功"); return ResponseMap.ofSuccess("更新成功");
} }
//TODO: test
@SneakyThrows @SneakyThrows
@GetMapping("/stats") @GetMapping("/stats")
public ResponseMap getGroupPositionsStatistics( public ResponseMap getGroupPositionsStatistics(

View File

@ -0,0 +1,27 @@
package cn.edu.hfut.rmdjzz.projectmanagement.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @author
* @since 2022/7/7 11:22
*/
@Data
public class Announcement {
@TableId(type = IdType.AUTO)
private Long announcementId;
private Integer projectId;
private Integer announcementPublisherId;
private LocalDateTime announcementPublishTime;
private String announcementTitle;
private String announcementContent;
@TableField("is_deleted")
@TableLogic
private Boolean deleted;
}

View File

@ -48,13 +48,10 @@ public class Project {
private LocalDate projectClosedDate; private LocalDate projectClosedDate;
public Boolean checkProjectDate() { public Boolean checkProjectDate() {
if(!TimeUtils.validateTimeLine(projectStartDate, projectFirstTestDate, projectFinalTestDate, projectEndDate)) { if (!TimeUtils.validateDateLine(projectStartDate, projectFirstTestDate, projectFinalTestDate, projectEndDate)) {
return false; return false;
} }
if (!TimeUtils.validateTimeLine(projectStartDate, projectOnlineDate)) { return TimeUtils.validateDateLine(projectStartDate, projectOnlineDate);
return false;
}
return true;
} }
public Boolean checkModification(Project rawProject) { public Boolean checkModification(Project rawProject) {

View File

@ -0,0 +1,19 @@
package cn.edu.hfut.rmdjzz.projectmanagement.entity.dto;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @author
* @since 2022/7/7 11:33
*/
@Data
public class AnnouncementDto {
private Long announcementId;
private Integer announcementPublisherId;
private String announcementPublisherName;
private LocalDateTime announcementPublishTime;
private String announcementTitle;
private String announcementContent;
}

View File

@ -0,0 +1,18 @@
package cn.edu.hfut.rmdjzz.projectmanagement.mapper;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDto;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author
* @since 2022/7/7 11:26
*/
public interface AnnouncementMapper extends BaseMapper<Announcement> {
List<AnnouncementDto> selectAnnouncementList(@Param("projectId") Integer projectId);
AnnouncementDto selectAnnouncementById(@Param("announcementId") Long announcementId);
}

View File

@ -0,0 +1,17 @@
package cn.edu.hfut.rmdjzz.projectmanagement.service;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDto;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author
* @since 2022/7/7 11:27
*/
public interface IAnnouncementService extends IService<Announcement> {
List<AnnouncementDto> getAnnouncementList(Integer projectId);
AnnouncementDto getAnnouncementById(Long announcementId);
}

View File

@ -0,0 +1,28 @@
package cn.edu.hfut.rmdjzz.projectmanagement.service.impl;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDto;
import cn.edu.hfut.rmdjzz.projectmanagement.mapper.AnnouncementMapper;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IAnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author
* @since 2022/7/7 11:28
*/
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {
@Override
public List<AnnouncementDto> getAnnouncementList(Integer projectId) {
return baseMapper.selectAnnouncementList(projectId);
}
@Override
public AnnouncementDto getAnnouncementById(Long announcementId) {
return null;
}
}

View File

@ -1,7 +1,7 @@
package cn.edu.hfut.rmdjzz.projectmanagement.utils; package cn.edu.hfut.rmdjzz.projectmanagement.utils;
import java.time.Instant; import java.time.LocalDate;
import java.time.temporal.TemporalAccessor; import java.time.LocalDateTime;
/** /**
* @author * @author
@ -11,23 +11,51 @@ public class TimeUtils {
/** /**
* 线null * 线null
*/ */
public static boolean validateTimeLine(TemporalAccessor... temporalAccessors) { public static boolean validateDateLine(LocalDate... dates) {
int first; int first;
for (first = 0; first < temporalAccessors.length; first++) { for (first = 0; first < dates.length; first++) {
if (temporalAccessors[first] != null) { if (dates[first] != null) {
break; break;
} }
} }
if (first >= temporalAccessors.length - 1) { if (first >= dates.length - 1) {
return true; return true;
} }
Instant last = Instant.from(temporalAccessors[first]); LocalDate last = dates[first];
Instant current; LocalDate current;
for (int i = first + 1; i < temporalAccessors.length; i++) { for (int i = first + 1; i < dates.length; i++) {
if (temporalAccessors[i] == null) { if (dates[i] == null) {
continue; continue;
} }
current = Instant.from(temporalAccessors[i]); current = dates[i];
if (last.isAfter(current)) {
return false;
}
last = current;
}
return true;
}
/**
* 线null
*/
public static boolean validateDateTimeLine(LocalDateTime... dateTimes) {
int first;
for (first = 0; first < dateTimes.length; first++) {
if (dateTimes[first] != null) {
break;
}
}
if (first >= dateTimes.length - 1) {
return true;
}
LocalDateTime last = dateTimes[first];
LocalDateTime current;
for (int i = first + 1; i < dateTimes.length; i++) {
if (dateTimes[i] == null) {
continue;
}
current = dateTimes[i];
if (last.isAfter(current)) { if (last.isAfter(current)) {
return false; return false;
} }

View File

@ -29,5 +29,6 @@ server:
charset: UTF-8 charset: UTF-8
force: true force: true
enabled: true enabled: true
forward-headers-strategy: framework
springdoc: springdoc:
packages-to-scan: cn.edu.hfut.rmdjzz.projectmanagement packages-to-scan: cn.edu.hfut.rmdjzz.projectmanagement

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.edu.hfut.rmdjzz.projectmanagement.mapper.AnnouncementMapper">
<resultMap id="announcementDto" type="cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDto">
<id property="announcementId" column="announcement_id"/>
</resultMap>
<select id="selectAnnouncementList" resultMap="announcementDto">
SELECT announcement_id,
announcement_publisher_id,
s.staff_fullname AS announcement_publisher_name,
announcement_publish_time,
announcement_title
FROM announcement
JOIN (SELECT staff_id, staff_fullname FROM staff) s
ON staff_id = announcement.announcement_publisher_id
WHERE is_deleted = 0
AND project_id = #{projectId}
ORDER BY announcement_id DESC;
</select>
<select id="selectAnnouncementById" resultMap="announcementDto">
SELECT announcement_id,
announcement_publisher_id,
s.staff_fullname AS announcement_publisher_name,
announcement_publish_time,
announcement_title,
announcement_content
FROM announcement
JOIN (SELECT staff_id, staff_fullname FROM staff) s
ON staff_id = announcement.announcement_publisher_id
WHERE is_deleted = 0
AND announcement_id = #{announcementId};
</select>
</mapper>

View File

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
/** /**
* @author * @author
@ -21,6 +22,6 @@ public class SerializeTests {
@SneakyThrows @SneakyThrows
@Test @Test
public void serializeTime() { public void serializeTime() {
System.out.println(objectMapper.readValue("1656523481", LocalDate.class)); System.out.println(objectMapper.readValue("1657166400", LocalDateTime.class));
} }
} }

View File

@ -0,0 +1,24 @@
package cn.edu.hfut.rmdjzz.projectmanagement;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TimeUtils;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* @author
* @since 2022/7/7 14:36
*/
public class UtilTests {
@Test
public void timeTest() {
LocalDateTime a = LocalDateTime.now();
LocalDateTime b = LocalDateTime.now().minusDays(1);
LocalDateTime c = LocalDateTime.now();
LocalDate d = LocalDate.now();
LocalTime t = LocalTime.now();
System.out.println(TimeUtils.validateDateTimeLine(a, b, c));
}
}