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
public R get() {
return R.ok(anniversaryService.getAll());
@GetMapping("future")
public R getFuture() {
return R.ok(anniversaryService.getFuture());
}
/**
* 获取过去的纪念日
*/
@GetMapping("past")
public R getPast() {
return R.ok(anniversaryService.getPast());
}
@PostMapping
public R add(Anniversary anniversary) {
anniversaryService.save(anniversary);
anniversaryService.saveOrUpdate(anniversary);
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;
import java.util.List;
public record AnniversaryVO (List<AnniversaryRecord> anniversaryList, List<AnniversaryRecord> commemorationList){
public record AnniversaryVO(String name, String date, int distance) {
}

View File

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