add: 周年纪念日

This commit is contained in:
jsh
2025-10-12 14:08:16 +08:00
parent ef224aa546
commit fa278dec01
8 changed files with 63 additions and 2 deletions

View File

@@ -34,6 +34,7 @@ create table habit_record
id int unsigned auto_increment,
create_time datetime null,
update_time datetime null,
name varchar(100) not null,
date date not null comment '重要的日期',
type enum ('anniversary', 'commemoration') default 'anniversary' not null
)

View File

@@ -27,7 +27,7 @@
<url/>
</scm>
<properties>
<java.version>21</java.version>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>

View File

@@ -14,6 +14,15 @@ public class AnniversaryController {
this.anniversaryService = anniversaryService;
}
/**
* 获取所有周年日和纪念日
* 周年日:还有几天
*/
@GetMapping
public R get() {
return R.ok(anniversaryService.getAll());
}
@PostMapping
public R add(Anniversary anniversary) {
anniversaryService.save(anniversary);

View File

@@ -1,5 +1,7 @@
package com.dota.nexus.entity;
import com.dota.nexus.entity.vo.AnniversaryRecord;
import com.dota.nexus.entity.vo.AnniversaryVO;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -8,7 +10,9 @@ import java.time.LocalDate;
@Data
@EqualsAndHashCode(callSuper = true)
public class Anniversary extends Entity{
private String name;
private LocalDate date;
private type;
private AnniversaryEnum type;
}

View File

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

View File

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

View File

@@ -2,6 +2,8 @@ package com.dota.nexus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.dota.nexus.entity.Anniversary;
import com.dota.nexus.entity.vo.AnniversaryVO;
public interface AnniversaryService extends IService<Anniversary> {
AnniversaryVO getAll();
}

View File

@@ -2,10 +2,45 @@ package com.dota.nexus.service.impl;
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;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
@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);
var anniversaries = this.baseMapper.selectList(null);
var now = LocalDate.now();
for (Anniversary anniversary : anniversaries) {
// 周年,变成今年或者明年
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(date, now)));
} else if (anniversary.getType() == AnniversaryEnum.COMMEMORATION) {
bList.add(new AnniversaryRecord(anniversary.getName(), anniversary.getDate().toString(), (int) ChronoUnit.DAYS.between(now, anniversary.getDate())));
}
}
return res;
}
}