diff --git a/src/main/java/com/chint/application/dtos/SystemAnnouncementDTO.java b/src/main/java/com/chint/application/dtos/SystemAnnouncementDTO.java index 15c7b065..18bd548d 100644 --- a/src/main/java/com/chint/application/dtos/SystemAnnouncementDTO.java +++ b/src/main/java/com/chint/application/dtos/SystemAnnouncementDTO.java @@ -6,10 +6,11 @@ import java.time.LocalDateTime; @Data public class SystemAnnouncementDTO { + private Long announcementId; // 公告的唯一标识符 private String title; // 公告的标题 private String content; // 公告的详细内容 private String postedBy; // 发布公告的用户或管理员的用户名 - private LocalDateTime postDate; // 公告发布的日期和时间 - private LocalDateTime expiryDate; // 公告的过期日期 + private String postDate; // 公告发布的日期和时间 + private String expiryDate; // 公告的过期日期 private Integer priority; // 公告的优先级,用于排序 } diff --git a/src/main/java/com/chint/application/system/SystemController.java b/src/main/java/com/chint/application/system/SystemController.java index 7da758d4..09d79ec8 100644 --- a/src/main/java/com/chint/application/system/SystemController.java +++ b/src/main/java/com/chint/application/system/SystemController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.time.LocalDateTime; +import java.util.Comparator; import java.util.List; import static com.chint.infrastructure.constant.CommonMessageConstant.NOT_FOUND; @@ -33,6 +35,20 @@ public class SystemController { return Result.Success(SUCCESS, systemAnnouncementRepository.save(systemAnnouncement)); } + @ApiOperation("更新公告") + @PostMapping("/announcement/update") + public Result updateAnnouncement(@RequestBody SystemAnnouncementDTO systemAnnouncementDTO) { + systemAnnouncementRepository.findById(systemAnnouncementDTO.getAnnouncementId()) + .ifPresent(systemAnnouncement -> { + systemAnnouncement.setTitle(systemAnnouncementDTO.getTitle()); + systemAnnouncement.setContent(systemAnnouncementDTO.getContent()); + systemAnnouncement.setPriority(systemAnnouncementDTO.getPriority()); + systemAnnouncement.updateTime(systemAnnouncementDTO); + systemAnnouncementRepository.save(systemAnnouncement); + }); + return Result.Success(SUCCESS); + } + @ApiOperation("查询最新公告") @PostMapping("/announcement/query/last") public Result queryLastAnnouncement() { @@ -42,13 +58,19 @@ public class SystemController { @ApiOperation("查询有效公告") @PostMapping("/announcement/query/effective") public Result> queryEffectiveAnnouncement() { -// List effective = systemAnnouncementRepository.findEffective(); -// -// effective.forEach(it->{ -// -// }); -// -// return Result.Success(SUCCESS, systemAnnouncementRepository.findNew().orElseThrow(() -> new NotFoundException(NOT_FOUND))); - return null; + List effective = systemAnnouncementRepository.findEffective(); + effective.forEach(it->{ + LocalDateTime expiryDate = it.getExpiryDate(); + if(expiryDate.isBefore(LocalDateTime.now())) { + it.setStatus("0"); + systemAnnouncementRepository.save(it); + } + }); + List systemAnnouncements = effective + .stream() + .filter(it -> it.getStatus().equals("1")) + .sorted(Comparator.comparingInt(SystemAnnouncement::getPriority)) + .toList(); + return Result.Success(SUCCESS, systemAnnouncements); } } diff --git a/src/main/java/com/chint/domain/aggregates/system/SystemAnnouncement.java b/src/main/java/com/chint/domain/aggregates/system/SystemAnnouncement.java index c88006a7..eabb9238 100644 --- a/src/main/java/com/chint/domain/aggregates/system/SystemAnnouncement.java +++ b/src/main/java/com/chint/domain/aggregates/system/SystemAnnouncement.java @@ -4,13 +4,16 @@ import cn.hutool.core.bean.BeanUtil; import com.chint.application.dtos.SystemAnnouncementDTO; import com.chint.domain.aggregates.user.User; import com.chint.infrastructure.util.BaseContext; +import com.chint.infrastructure.util.DateTimeUtil; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; import java.io.Serial; import java.io.Serializable; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; @Data @Table("system_announcements") @@ -25,15 +28,23 @@ public class SystemAnnouncement implements Serializable { private LocalDateTime postDate; // 公告发布的日期和时间 private LocalDateTime expiryDate; // 公告的过期日期 private String status; // 公告的状态 - private Integer priority; // 公告的优先级,用于排序、 + private Integer priority; // 公告的优先级,用于排序 public static SystemAnnouncement of(SystemAnnouncementDTO dto) { SystemAnnouncement systemAnnouncement = BeanUtil.copyProperties(dto, SystemAnnouncement.class); if (systemAnnouncement.getPostDate() == null) { systemAnnouncement.setPostDate(LocalDateTime.now()); + } else { + LocalDateTime postDate = LocalDateTime.of(LocalDate.parse(dto.getPostDate(), DateTimeUtil.formatterDate), + LocalTime.MIN); + systemAnnouncement.setPostDate(postDate); } if (systemAnnouncement.getExpiryDate() == null) { systemAnnouncement.setExpiryDate(LocalDateTime.now().plusDays(15L)); + } else { + LocalDateTime expiryTime = LocalDateTime.of(LocalDate.parse(dto.getExpiryDate(), DateTimeUtil.formatterDate), + LocalTime.MAX); + systemAnnouncement.setPostDate(expiryTime); } systemAnnouncement.setStatus("1"); systemAnnouncement.setPriority(0); @@ -46,4 +57,22 @@ public class SystemAnnouncement implements Serializable { } return systemAnnouncement; } + + public SystemAnnouncement updateTime(SystemAnnouncementDTO dto) { + if (dto.getPostDate() != null) { + this.setPostDate(LocalDateTime.of(LocalDate.parse(dto.getPostDate(), DateTimeUtil.formatterDate), + LocalTime.MIN)); + } + if (dto.getExpiryDate() != null) { + this.setExpiryDate(LocalDateTime.of(LocalDate.parse(dto.getExpiryDate(), DateTimeUtil.formatterDate), + LocalTime.MAX)); + } + LocalDateTime expiryDate = this.getExpiryDate(); + if (expiryDate.isBefore(LocalDateTime.now())) { + this.setStatus("0"); + } + + return this; + } + } diff --git a/src/main/java/com/chint/domain/repository/SystemAnnouncementRepository.java b/src/main/java/com/chint/domain/repository/SystemAnnouncementRepository.java index 458664d2..85b10ebe 100644 --- a/src/main/java/com/chint/domain/repository/SystemAnnouncementRepository.java +++ b/src/main/java/com/chint/domain/repository/SystemAnnouncementRepository.java @@ -8,6 +8,8 @@ import java.util.Optional; public interface SystemAnnouncementRepository { SystemAnnouncement save(SystemAnnouncement systemAnnouncement); + Optional findById(Long id); + Optional findNew(); List findAll(); diff --git a/src/main/java/com/chint/infrastructure/repository/SystemAnnouncementRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/SystemAnnouncementRepositoryImpl.java index ce0d5ca8..2fc58010 100644 --- a/src/main/java/com/chint/infrastructure/repository/SystemAnnouncementRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/SystemAnnouncementRepositoryImpl.java @@ -22,6 +22,11 @@ public class SystemAnnouncementRepositoryImpl implements SystemAnnouncementRepos return jdbcSystemAnnouncementRepository.save(systemAnnouncement); } + @Override + public Optional findById(Long id) { + return jdbcSystemAnnouncementRepository.findById(id); + } + @Override public Optional findNew() { return jdbcSystemAnnouncementRepository.findTopByOrderByAnnouncementIdDesc();