Compare commits

...

5 Commits

Author SHA1 Message Date
kkunkka
82e1f2deb4 add: 目标表增加根节点id和父节点id 2025-10-10 14:53:19 +08:00
kkunkka
affb754ae4 add: 习惯curd 2025-10-10 14:49:34 +08:00
kkunkka
79c0e37b87 refactor: 目标,任务,习惯都放一张表中 2025-10-10 12:40:31 +08:00
kkunkka
b9cb079139 sql 2025-10-10 11:41:50 +08:00
kkunkka
f86462bb24 add: 目标crud 2025-10-10 11:40:34 +08:00
15 changed files with 268 additions and 17 deletions

View File

@@ -1,16 +1,28 @@
drop table if exists `goals`; drop table if exists `goal`;
CREATE TABLE if not exists `goals` CREATE TABLE if not exists `goal`
( (
id INT auto_increment NOT NULL PRIMARY KEY, id INT unsigned auto_increment PRIMARY KEY,
status tinyint default 0 COMMENT '0进行中1已完成2已取消', status enum ('DOING', 'DONE', 'CANCEL') default 'DOING' not null,
create_time DATETIME NULL, create_time DATETIME NULL,
update_time DATETIME NULL, update_time DATETIME NULL,
name varchar(100) NOT NULL, type enum ('GOAL', 'TASK', 'HABIT') default 'TASK' NOT NULL,
tag varchar(100) NULL, name varchar(100) NOT NULL,
detail varchar(100) NULL tag varchar(100) NULL,
detail varchar(100) NULL,
root_id int unsigned null,
parent_id int unsigned null
) )
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT ='目标管理表'; COMMENT ='目标管理表';
drop table if exists `habit_record`;
create table habit_record
(
id int unsigned auto_increment
primary key,
create_time datetime null,
update_time datetime null,
goal_id int unsigned not null
)
comment '习惯记录表';

View File

@@ -0,0 +1,64 @@
package com.dota.nexus.controller;
import com.dota.nexus.entity.Goal;
import com.dota.nexus.entity.R;
import com.dota.nexus.service.GoalService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController("goal")
public class GoalController {
GoalService goalService;
public GoalController(GoalService goalService) {
this.goalService = goalService;
}
/**
* 新增目标
*/
@GetMapping
public R addGoal(Goal goal) {
goalService.save(goal);
return R.ok();
}
/**
* 删除目标
*/
@DeleteMapping
public R deleteGoal(Integer id) {
goalService.removeById(id);
return R.ok();
}
/**
* 完成目标
*/
@PostMapping("done")
public R doneGoal(Integer id) {
goalService.doneGoal(id);
return R.ok();
}
/**
* 目标进行中
*/
@PostMapping("doing")
public R doingGoal(Integer id) {
goalService.doingGoal(id);
return R.ok();
}
/**
* 取消目标
*/
@PostMapping("cancel")
public R cancelGoal(Integer id) {
goalService.cancelGoal(id);
return R.ok();
}
}

View File

@@ -0,0 +1,29 @@
package com.dota.nexus.controller;
import com.dota.nexus.entity.HabitRecord;
import com.dota.nexus.entity.R;
import com.dota.nexus.service.HabitRecordService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController("habit_record")
public class HabitRecordController {
HabitRecordService habitRecordService;
public HabitRecordController(HabitRecordService habitRecordService) {
this.habitRecordService = habitRecordService;
}
@PostMapping()
public R addHabitRecord(Integer id) {
var habit = new HabitRecord();
habit.setCreateTime(LocalDateTime.now());
habit.setUpdateTime(LocalDateTime.now());
habit.setGoalId(id);
habitRecordService.save(habit);
return R.ok();
}
}

View File

@@ -1,15 +1,16 @@
package com.dota.nexus.entity; package com.dota.nexus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data; import lombok.Data;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Data @Data
public class Entity { public class Entity {
@TableId(type = IdType.AUTO)
Integer id; Integer id;
Integer status;
LocalDateTime createTime; LocalDateTime createTime;
LocalDateTime updateTime; LocalDateTime updateTime;

View File

@@ -6,9 +6,17 @@ import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Data @Data
public class Goal extends Entity { public class Goal extends Entity {
private GoalStatusEnum status;
private GoalTypeEnum type;
private String name; private String name;
private String tag; private String tag;
private String detail; private String detail;
private Integer rootId;
private Integer parentId;
} }

View File

@@ -0,0 +1,16 @@
package com.dota.nexus.entity;
import lombok.Getter;
@Getter
public enum GoalStatusEnum {
DOING("进行中"),
DONE("完成"),
CANCEL("已取消");
private final String value;
GoalStatusEnum(String value) {
this.value = value;
}
}

View File

@@ -0,0 +1,13 @@
package com.dota.nexus.entity;
public enum GoalTypeEnum {
GOAL("目标"),
TASK("任务"),
HABIT("习惯");
private String value;
GoalTypeEnum(String value) {
this.value = value;
}
}

View File

@@ -0,0 +1,10 @@
package com.dota.nexus.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class HabitRecord extends Entity {
private Integer goalId;
}

View File

@@ -0,0 +1,22 @@
package com.dota.nexus.entity;
import lombok.Data;
@Data
public class R {
private Integer code;
private String msg;
private Object data;
public static R ok() {
return ok(null);
}
public static R ok(Object data) {
var r = new R();
r.setCode(200);
r.setMsg("ok");
r.setData(data);
return r;
}
}

View File

@@ -0,0 +1,9 @@
package com.dota.nexus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dota.nexus.entity.HabitRecord;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface HabitRecordMapper extends BaseMapper<HabitRecord> {
}

View File

@@ -4,4 +4,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.dota.nexus.entity.Goal; import com.dota.nexus.entity.Goal;
public interface GoalService extends IService<Goal> { public interface GoalService extends IService<Goal> {
void delGoal(Integer id);
void doneGoal(Integer id);
void doingGoal(Integer id);
void cancelGoal(Integer id);
} }

View File

@@ -0,0 +1,7 @@
package com.dota.nexus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.dota.nexus.entity.HabitRecord;
public interface HabitRecordService extends IService<HabitRecord> {
}

View File

@@ -1,10 +1,53 @@
package com.dota.nexus.service.impl; package com.dota.nexus.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dota.nexus.entity.Goal; import com.dota.nexus.entity.Goal;
import com.dota.nexus.entity.GoalStatusEnum;
import com.dota.nexus.entity.HabitRecord;
import com.dota.nexus.mapper.GoalMapper; import com.dota.nexus.mapper.GoalMapper;
import com.dota.nexus.mapper.HabitRecordMapper;
import com.dota.nexus.service.GoalService; import com.dota.nexus.service.GoalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class GoalServiceImpl extends ServiceImpl<GoalMapper, Goal> implements GoalService { public class GoalServiceImpl extends ServiceImpl<GoalMapper, Goal> implements GoalService {
HabitRecordMapper habitRecordMapper;
public GoalServiceImpl(HabitRecordMapper habitRecordMapper) {
this.habitRecordMapper = habitRecordMapper;
}
@Override
@Transactional
public void delGoal(Integer id) {
removeById(id);
var q = new LambdaQueryWrapper<HabitRecord>();
q.eq(HabitRecord::getGoalId, id);
habitRecordMapper.delete(q);
}
@Override
public void doneGoal(Integer id) {
updateStatus(id, GoalStatusEnum.DONE);
}
@Override
public void doingGoal(Integer id) {
updateStatus(id, GoalStatusEnum.DOING);
}
@Override
public void cancelGoal(Integer id) {
updateStatus(id, GoalStatusEnum.CANCEL);
}
private void updateStatus(Integer id, GoalStatusEnum status) {
var update = new LambdaUpdateWrapper<Goal>();
update.eq(Goal::getId, id);
update.set(Goal::getStatus, status);
this.baseMapper.update(update);
}
} }

View File

@@ -0,0 +1,11 @@
package com.dota.nexus.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dota.nexus.entity.HabitRecord;
import com.dota.nexus.mapper.HabitRecordMapper;
import com.dota.nexus.service.HabitRecordService;
import org.springframework.stereotype.Service;
@Service
public class HabitRecordServiceImpl extends ServiceImpl<HabitRecordMapper, HabitRecord> implements HabitRecordService {
}

View File

@@ -1,10 +1,9 @@
package com.dota.nexus.mapper; package com.dota.nexus.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.dota.nexus.entity.Goal; import com.dota.nexus.entity.Goal;
import com.dota.nexus.entity.GoalStatusEnum;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@@ -21,7 +20,7 @@ public class GoalMapperTest {
@Test @Test
public void testInsert() { public void testInsert() {
var g = new Goal(); var g = new Goal();
g.setStatus(1); g.setStatus(GoalStatusEnum.DONE);
g.setCreateTime(LocalDateTime.now()); g.setCreateTime(LocalDateTime.now());
g.setTag("test"); g.setTag("test");
g.setName("测试"); g.setName("测试");