add: 获取未来的重要日子,获取过去的重要日子

This commit is contained in:
kkunkka
2025-10-14 22:45:42 +08:00
parent 8439653082
commit 40c2da044a
5 changed files with 55 additions and 40 deletions

View File

@@ -15,16 +15,24 @@ public class AnniversaryController {
} }
/** /**
* 获取所有周年日和纪念日 * 获取未来所有周年日和纪念日
*/ */
@GetMapping @GetMapping("future")
public R get() { public R getFuture() {
return R.ok(anniversaryService.getAll()); return R.ok(anniversaryService.getFuture());
}
/**
* 获取过去的纪念日
*/
@GetMapping("past")
public R getPast() {
return R.ok(anniversaryService.getPast());
} }
@PostMapping @PostMapping
public R add(Anniversary anniversary) { public R add(Anniversary anniversary) {
anniversaryService.save(anniversary); anniversaryService.saveOrUpdate(anniversary);
return R.ok(); return R.ok();
} }

View File

@@ -1,4 +0,0 @@
package com.dota.nexus.entity.vo;
public record AnniversaryRecord(String name, String date, int distance) {
}

View File

@@ -1,6 +1,4 @@
package com.dota.nexus.entity.vo; package com.dota.nexus.entity.vo;
import java.util.List; public record AnniversaryVO(String name, String date, int distance) {
}
public record AnniversaryVO (List<AnniversaryRecord> anniversaryList, List<AnniversaryRecord> commemorationList){
}

View File

@@ -4,6 +4,10 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.dota.nexus.entity.Anniversary; import com.dota.nexus.entity.Anniversary;
import com.dota.nexus.entity.vo.AnniversaryVO; import com.dota.nexus.entity.vo.AnniversaryVO;
import java.util.List;
public interface AnniversaryService extends IService<Anniversary> { public interface AnniversaryService extends IService<Anniversary> {
AnniversaryVO getAll(); List<AnniversaryVO> getFuture();
List<AnniversaryVO> getPast();
} }

View File

@@ -1,9 +1,9 @@
package com.dota.nexus.service.impl; package com.dota.nexus.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dota.nexus.entity.Anniversary; import com.dota.nexus.entity.Anniversary;
import com.dota.nexus.entity.AnniversaryEnum; import com.dota.nexus.entity.AnniversaryEnum;
import com.dota.nexus.entity.vo.AnniversaryRecord;
import com.dota.nexus.entity.vo.AnniversaryVO; import com.dota.nexus.entity.vo.AnniversaryVO;
import com.dota.nexus.mapper.AnniversaryMapper; import com.dota.nexus.mapper.AnniversaryMapper;
import com.dota.nexus.service.AnniversaryService; import com.dota.nexus.service.AnniversaryService;
@@ -12,35 +12,44 @@ import org.springframework.stereotype.Service;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class AnniversaryServiceImpl extends ServiceImpl<AnniversaryMapper, Anniversary> implements AnniversaryService { public class AnniversaryServiceImpl extends ServiceImpl<AnniversaryMapper, Anniversary> implements AnniversaryService {
/** @Override
* public List<AnniversaryVO> getFuture() {
*/ var anniversaries = this.baseMapper.selectList(null);
@Override var list = new ArrayList<AnniversaryVO>();
public AnniversaryVO getAll() { var now = LocalDate.now();
var aList = new ArrayList<AnniversaryRecord>(); for (Anniversary anniversary : anniversaries) {
var bList = new ArrayList<AnniversaryRecord>(); // 周年,变成今年或者明年
var res = new AnniversaryVO(aList, bList); if (anniversary.getType() == AnniversaryEnum.ANNIVERSARY) {
var anniversaries = this.baseMapper.selectList(null); LocalDate date = anniversary.getDate();
var now = LocalDate.now(); date = date.withYear(now.getYear());
for (Anniversary anniversary : anniversaries) { if (date.isBefore(now)) {
// 周年,变成今年或者明年 date = date.withYear(now.getYear() + 1);
if (anniversary.getType() == AnniversaryEnum.ANNIVERSARY) { }
LocalDate date = anniversary.getDate();
date = date.withYear(now.getYear());
if (date.isBefore(now)) {
date = date.withYear(now.getYear() + 1);
}
aList.add(new AnniversaryRecord(anniversary.getName(), date.toString(), (int) ChronoUnit.DAYS.between(now, date))); list.add(new AnniversaryVO(anniversary.getName(), date.toString(), (int) ChronoUnit.DAYS.between(now, date)));
} else if (anniversary.getType() == AnniversaryEnum.COMMEMORATION) { } else if (anniversary.getType() == AnniversaryEnum.COMMEMORATION && !anniversary.getDate().isBefore(now)) {
bList.add(new AnniversaryRecord(anniversary.getName(), anniversary.getDate().toString(), (int) ChronoUnit.DAYS.between(anniversary.getDate(), now))); list.add(new AnniversaryVO(anniversary.getName(), anniversary.getDate().toString(), (int) ChronoUnit.DAYS.between(now, anniversary.getDate())));
} }
} }
list.sort(Comparator.comparingInt(AnniversaryVO::distance));
return res; return list;
} }
@Override
public List<AnniversaryVO> getPast() {
var q = new LambdaQueryWrapper<Anniversary>();
q.eq(Anniversary::getType, AnniversaryEnum.COMMEMORATION);
var anniversaries = this.baseMapper.selectList(q);
var now = LocalDate.now();
return anniversaries.stream().map(a -> new AnniversaryVO(a.getName(), a.getDate().toString(), (int) ChronoUnit.DAYS.between(now, a.getDate()))).sorted((a,b)->b.distance()-a.distance()).collect(Collectors.toList());
}
} }