完成系统公告模块

This commit is contained in:
lulz1 2024-04-16 15:38:10 +08:00
parent 502774614e
commit 6748fe647e
5 changed files with 70 additions and 11 deletions

View File

@ -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; // 公告的优先级用于排序
}

View File

@ -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<String> 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<SystemAnnouncement> queryLastAnnouncement() {
@ -42,13 +58,19 @@ public class SystemController {
@ApiOperation("查询有效公告")
@PostMapping("/announcement/query/effective")
public Result<List<SystemAnnouncement>> queryEffectiveAnnouncement() {
// List<SystemAnnouncement> effective = systemAnnouncementRepository.findEffective();
//
// effective.forEach(it->{
//
// });
//
// return Result.Success(SUCCESS, systemAnnouncementRepository.findNew().orElseThrow(() -> new NotFoundException(NOT_FOUND)));
return null;
List<SystemAnnouncement> effective = systemAnnouncementRepository.findEffective();
effective.forEach(it->{
LocalDateTime expiryDate = it.getExpiryDate();
if(expiryDate.isBefore(LocalDateTime.now())) {
it.setStatus("0");
systemAnnouncementRepository.save(it);
}
});
List<SystemAnnouncement> systemAnnouncements = effective
.stream()
.filter(it -> it.getStatus().equals("1"))
.sorted(Comparator.comparingInt(SystemAnnouncement::getPriority))
.toList();
return Result.Success(SUCCESS, systemAnnouncements);
}
}

View File

@ -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;
}
}

View File

@ -8,6 +8,8 @@ import java.util.Optional;
public interface SystemAnnouncementRepository {
SystemAnnouncement save(SystemAnnouncement systemAnnouncement);
Optional<SystemAnnouncement> findById(Long id);
Optional<SystemAnnouncement> findNew();
List<SystemAnnouncement> findAll();

View File

@ -22,6 +22,11 @@ public class SystemAnnouncementRepositoryImpl implements SystemAnnouncementRepos
return jdbcSystemAnnouncementRepository.save(systemAnnouncement);
}
@Override
public Optional<SystemAnnouncement> findById(Long id) {
return jdbcSystemAnnouncementRepository.findById(id);
}
@Override
public Optional<SystemAnnouncement> findNew() {
return jdbcSystemAnnouncementRepository.findTopByOrderByAnnouncementIdDesc();