优化代码结构
This commit is contained in:
1 parent
8c7a9040d2
commit
cbcd6b4c7f
97 files changed
+4350
-1183
No files matched your search
+49
-1
@@ -40,6 +40,11 @@
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||
<version>3.5.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
@@ -48,6 +53,11 @@
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-mysql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@@ -56,13 +66,29 @@
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>${project.basedir}/../sql/migration</directory>
|
||||
<targetPath>db/migration</targetPath>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.10.1</version>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<release>${java.version}</release>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.34</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
@@ -74,6 +100,28 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-artifacts</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<target>
|
||||
<mkdir dir="${project.basedir}/../deploy/artifacts/server"/>
|
||||
<copy file="${project.build.directory}/${project.build.finalName}.jar"
|
||||
todir="${project.basedir}/../deploy/artifacts/server"
|
||||
overwrite="true"/>
|
||||
</target>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.babytracker;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.babytracker.module.**.mapper")
|
||||
public class BabyTrackerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BabyTrackerApplication.class, args);
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.babytracker.common.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class BusinessException extends RuntimeException {
|
||||
private final String code;
|
||||
|
||||
@@ -7,9 +10,4 @@ public class BusinessException extends RuntimeException {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-1
@@ -1,6 +1,7 @@
|
||||
package com.babytracker.common.exception;
|
||||
|
||||
import com.babytracker.common.response.ApiResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
@@ -8,24 +9,28 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ApiResponse<Void> handleBusiness(BusinessException ex) {
|
||||
log.warn("业务异常: code={}, message={}", ex.getCode(), ex.getMessage());
|
||||
return ApiResponse.fail(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ApiResponse<Void> handleValidation(Exception ex) {
|
||||
log.warn("参数校验异常: {}", ex.getMessage());
|
||||
return ApiResponse.fail("VALIDATION_ERROR", "参数不合法");
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public ApiResponse<Void> handleException(Exception ex) {
|
||||
log.error("系统内部异常: ", ex);
|
||||
return ApiResponse.fail("INTERNAL_ERROR", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.babytracker.common.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public abstract class BaseEntity implements Serializable {
|
||||
|
||||
@TableField("remark")
|
||||
private String remark;
|
||||
|
||||
@TableLogic
|
||||
@TableField(value = "deleted", fill = FieldFill.INSERT)
|
||||
private Integer deleted;
|
||||
|
||||
@TableField(value = "created_by", fill = FieldFill.INSERT)
|
||||
private String createdBy;
|
||||
|
||||
@TableField(value = "created_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime createdTime;
|
||||
|
||||
@TableField(value = "updated_by", fill = FieldFill.INSERT_UPDATE)
|
||||
private String updatedBy;
|
||||
|
||||
@TableField(value = "updated_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.babytracker.common.mybatis;
|
||||
|
||||
import com.babytracker.common.model.BaseEntity;
|
||||
import com.babytracker.common.security.UserContext;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AuditFieldFillInterceptor implements InnerInterceptor {
|
||||
|
||||
@Override
|
||||
public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
|
||||
if (parameter == null) {
|
||||
return;
|
||||
}
|
||||
SqlCommandType commandType = ms.getSqlCommandType();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String user = UserContext.getCurrentUser();
|
||||
|
||||
if (parameter instanceof BaseEntity entity) {
|
||||
fillAuditFields(entity, commandType, now, user);
|
||||
} else if (parameter instanceof Map<?, ?> map) {
|
||||
if (map.containsKey(Constants.ENTITY) && map.get(Constants.ENTITY) instanceof BaseEntity entity) {
|
||||
fillAuditFields(entity, commandType, now, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fillAuditFields(BaseEntity entity, SqlCommandType commandType, LocalDateTime now, String user) {
|
||||
if (commandType == SqlCommandType.INSERT) {
|
||||
if (entity.getCreatedTime() == null) {
|
||||
entity.setCreatedTime(now);
|
||||
}
|
||||
if (entity.getUpdatedTime() == null) {
|
||||
entity.setUpdatedTime(now);
|
||||
}
|
||||
if (entity.getCreatedBy() == null || entity.getCreatedBy().trim().isEmpty()) {
|
||||
entity.setCreatedBy(user);
|
||||
}
|
||||
if (entity.getUpdatedBy() == null || entity.getUpdatedBy().trim().isEmpty()) {
|
||||
entity.setUpdatedBy(user);
|
||||
}
|
||||
if (entity.getDeleted() == null) {
|
||||
entity.setDeleted(0);
|
||||
}
|
||||
} else if (commandType == SqlCommandType.UPDATE) {
|
||||
entity.setUpdatedTime(now);
|
||||
entity.setUpdatedBy(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.babytracker.common.mybatis;
|
||||
|
||||
import com.babytracker.common.security.UserContext;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AuditMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String user = UserContext.getCurrentUser();
|
||||
|
||||
this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, now);
|
||||
this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, now);
|
||||
this.strictInsertFill(metaObject, "createdBy", String.class, user);
|
||||
this.strictInsertFill(metaObject, "updatedBy", String.class, user);
|
||||
this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
|
||||
|
||||
// 兜底补填(防止由于直接赋值 null 导致 strictInsertFill 忽略)
|
||||
if (this.getFieldValByName("createdBy", metaObject) == null) {
|
||||
this.setFieldValByName("createdBy", user, metaObject);
|
||||
}
|
||||
if (this.getFieldValByName("updatedBy", metaObject) == null) {
|
||||
this.setFieldValByName("updatedBy", user, metaObject);
|
||||
}
|
||||
if (this.getFieldValByName("createdTime", metaObject) == null) {
|
||||
this.setFieldValByName("createdTime", now, metaObject);
|
||||
}
|
||||
if (this.getFieldValByName("updatedTime", metaObject) == null) {
|
||||
this.setFieldValByName("updatedTime", now, metaObject);
|
||||
}
|
||||
if (this.getFieldValByName("deleted", metaObject) == null) {
|
||||
this.setFieldValByName("deleted", 0, metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String user = UserContext.getCurrentUser();
|
||||
|
||||
this.setFieldValByName("updatedTime", now, metaObject);
|
||||
this.setFieldValByName("updatedBy", user, metaObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.babytracker.common.mybatis;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(AuditFieldFillInterceptor auditFieldFillInterceptor) {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// 注入统一审计字段拦截器
|
||||
interceptor.addInnerInterceptor(auditFieldFillInterceptor);
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,13 @@
|
||||
package com.babytracker.common.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ApiError {
|
||||
private String code;
|
||||
private String message;
|
||||
|
||||
public ApiError() {
|
||||
}
|
||||
|
||||
public ApiError(String code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.babytracker.common.response;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class ApiResponse<T> {
|
||||
private boolean success;
|
||||
private T data;
|
||||
@@ -18,29 +23,4 @@ public class ApiResponse<T> {
|
||||
response.error = new ApiError(code, message);
|
||||
return response;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ApiError getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(ApiError error) {
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.babytracker.common.security;
|
||||
|
||||
public final class UserContext {
|
||||
private static final ThreadLocal<String> CURRENT_USER = new ThreadLocal<>();
|
||||
|
||||
private UserContext() {}
|
||||
|
||||
public static String getCurrentUser() {
|
||||
String user = CURRENT_USER.get();
|
||||
return (user != null && !user.trim().isEmpty()) ? user.trim() : "admin";
|
||||
}
|
||||
|
||||
public static void setCurrentUser(String username) {
|
||||
CURRENT_USER.set(username);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
CURRENT_USER.remove();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,59 @@
|
||||
package com.babytracker.module.baby;
|
||||
|
||||
import com.babytracker.common.response.ApiResponse;
|
||||
import com.babytracker.module.baby.ro.CreateBabyRO;
|
||||
import com.babytracker.module.baby.ro.SetDefaultBabyRO;
|
||||
import com.babytracker.module.baby.ro.UpdateBabyRO;
|
||||
import com.babytracker.module.baby.vo.BabyVO;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/v1/babies")
|
||||
public class BabyController {
|
||||
private final BabyService babyService;
|
||||
|
||||
public BabyController(BabyService babyService) {
|
||||
this.babyService = babyService;
|
||||
}
|
||||
|
||||
@GetMapping("/current")
|
||||
public ApiResponse<BabyProfile> current() {
|
||||
public ApiResponse<BabyVO> current() {
|
||||
return ApiResponse.ok(babyService.getCurrentBaby());
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ApiResponse<List<BabyProfile>> list() {
|
||||
public ApiResponse<List<BabyVO>> list() {
|
||||
return ApiResponse.ok(babyService.listBabies());
|
||||
}
|
||||
|
||||
@PutMapping("/current")
|
||||
public ApiResponse<BabyProfile> update(@Valid @RequestBody UpdateBabyRequest request) {
|
||||
@PutMapping
|
||||
public ApiResponse<BabyVO> update(@Valid @RequestBody UpdateBabyRO request) {
|
||||
if (request.getBabyId() != null && !request.getBabyId().trim().isEmpty()) {
|
||||
return ApiResponse.ok(babyService.updateBaby(request.getBabyId().trim(), request));
|
||||
}
|
||||
return ApiResponse.ok(babyService.updateCurrentBaby(request));
|
||||
}
|
||||
|
||||
@PutMapping("/{babyId}")
|
||||
public ApiResponse<BabyProfile> updateById(@PathVariable String babyId, @Valid @RequestBody UpdateBabyRequest request) {
|
||||
return ApiResponse.ok(babyService.updateBaby(babyId, request));
|
||||
@PutMapping("/current")
|
||||
public ApiResponse<BabyVO> updateCurrent(@Valid @RequestBody UpdateBabyRO request) {
|
||||
return ApiResponse.ok(babyService.updateCurrentBaby(request));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ApiResponse<BabyVO> create(@Valid @RequestBody CreateBabyRO request) {
|
||||
return ApiResponse.ok(babyService.createBaby(request));
|
||||
}
|
||||
|
||||
@PostMapping("/set-default")
|
||||
public ApiResponse<BabyVO> setDefault(@Valid @RequestBody SetDefaultBabyRO request) {
|
||||
return ApiResponse.ok(babyService.setDefaultBaby(request.getBabyId().trim()));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,45 @@
|
||||
package com.babytracker.module.baby;
|
||||
|
||||
import com.babytracker.common.model.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class BabyProfile {
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("baby_profile")
|
||||
public class BabyProfile extends BaseEntity {
|
||||
|
||||
@TableId(value = "id", type = IdType.INPUT)
|
||||
private String babyId;
|
||||
|
||||
@TableField("family_id")
|
||||
private String familyId;
|
||||
|
||||
@TableField("name")
|
||||
private String name;
|
||||
|
||||
@TableField("gender")
|
||||
private String gender;
|
||||
|
||||
@TableField("birth_date")
|
||||
private LocalDate birthDate;
|
||||
|
||||
@TableField("delivery_type")
|
||||
private String deliveryType;
|
||||
|
||||
@TableField("is_default")
|
||||
private Integer isDefault;
|
||||
|
||||
@TableField("status")
|
||||
private String status;
|
||||
|
||||
public static BabyProfile defaultBaby() {
|
||||
BabyProfile baby = new BabyProfile();
|
||||
baby.babyId = "00000000000000361915955447599104";
|
||||
@@ -19,62 +48,19 @@ public class BabyProfile {
|
||||
baby.gender = "boy";
|
||||
baby.birthDate = LocalDate.now().minusDays(3);
|
||||
baby.deliveryType = "natural";
|
||||
baby.isDefault = 1;
|
||||
baby.status = "active";
|
||||
return baby;
|
||||
}
|
||||
|
||||
public long getAgeDays() {
|
||||
if (birthDate == null) {
|
||||
return 0;
|
||||
}
|
||||
return Math.max(0, ChronoUnit.DAYS.between(birthDate, LocalDate.now()));
|
||||
}
|
||||
|
||||
public String getAgeText() {
|
||||
return "出生第 " + getAgeDays() + " 天";
|
||||
}
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public String getFamilyId() {
|
||||
return familyId;
|
||||
}
|
||||
|
||||
public void setFamilyId(String familyId) {
|
||||
this.familyId = familyId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
public void setBirthDate(LocalDate birthDate) {
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String getDeliveryType() {
|
||||
return deliveryType;
|
||||
}
|
||||
|
||||
public void setDeliveryType(String deliveryType) {
|
||||
this.deliveryType = deliveryType;
|
||||
}
|
||||
}
|
||||
@@ -1,90 +1,143 @@
|
||||
package com.babytracker.module.baby;
|
||||
|
||||
import com.babytracker.common.id.SnowflakeIdGenerator;
|
||||
import com.babytracker.module.baby.dto.BabyConvert;
|
||||
import com.babytracker.module.baby.mapper.BabyMapper;
|
||||
import com.babytracker.module.baby.ro.CreateBabyRO;
|
||||
import com.babytracker.module.baby.ro.UpdateBabyRO;
|
||||
import com.babytracker.module.baby.vo.BabyVO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BabyService {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final BabyMapper babyMapper;
|
||||
private final SnowflakeIdGenerator idGenerator;
|
||||
|
||||
public BabyService(JdbcTemplate jdbcTemplate, SnowflakeIdGenerator idGenerator) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
public BabyProfile getCurrentBaby() {
|
||||
List<BabyProfile> babies = jdbcTemplate.query(
|
||||
"SELECT id, family_id, name, gender, birth_date, delivery_type FROM baby_profile WHERE status = 'active' ORDER BY is_default DESC, id ASC LIMIT 1",
|
||||
(rs, rowNum) -> mapBaby(rs)
|
||||
public BabyProfile getCurrentBabyEntity() {
|
||||
BabyProfile baby = babyMapper.selectOne(
|
||||
new LambdaQueryWrapper<BabyProfile>()
|
||||
.eq(BabyProfile::getStatus, "active")
|
||||
.orderByDesc(BabyProfile::getIsDefault)
|
||||
.orderByAsc(BabyProfile::getBabyId)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
if (!babies.isEmpty()) {
|
||||
return babies.get(0);
|
||||
if (baby != null) {
|
||||
return baby;
|
||||
}
|
||||
|
||||
BabyProfile baby = BabyProfile.defaultBaby();
|
||||
baby.setBabyId(idGenerator.nextId());
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO baby_profile (id, family_id, name, gender, birth_date, delivery_type, is_default, status) VALUES (?, ?, ?, ?, ?, ?, 1, 'active')",
|
||||
baby.getBabyId(), baby.getFamilyId(), baby.getName(), baby.getGender(), Date.valueOf(baby.getBirthDate()), baby.getDeliveryType()
|
||||
);
|
||||
return baby;
|
||||
BabyProfile defaultBaby = BabyProfile.defaultBaby();
|
||||
defaultBaby.setBabyId(idGenerator.nextId());
|
||||
babyMapper.insert(defaultBaby);
|
||||
return defaultBaby;
|
||||
}
|
||||
|
||||
public List<BabyProfile> listBabies() {
|
||||
List<BabyProfile> babies = jdbcTemplate.query(
|
||||
"SELECT id, family_id, name, gender, birth_date, delivery_type FROM baby_profile WHERE status = 'active' ORDER BY is_default DESC, id ASC",
|
||||
(rs, rowNum) -> mapBaby(rs)
|
||||
public BabyVO getCurrentBaby() {
|
||||
return BabyConvert.toVO(getCurrentBabyEntity());
|
||||
}
|
||||
|
||||
public List<BabyVO> listBabies() {
|
||||
List<BabyProfile> babies = babyMapper.selectList(
|
||||
new LambdaQueryWrapper<BabyProfile>()
|
||||
.eq(BabyProfile::getStatus, "active")
|
||||
.orderByDesc(BabyProfile::getIsDefault)
|
||||
.orderByAsc(BabyProfile::getBabyId)
|
||||
);
|
||||
if (babies.isEmpty()) {
|
||||
return List.of(getCurrentBaby());
|
||||
}
|
||||
return babies;
|
||||
return BabyConvert.toVOList(babies);
|
||||
}
|
||||
|
||||
public BabyProfile updateCurrentBaby(UpdateBabyRequest request) {
|
||||
BabyProfile currentBaby = getCurrentBaby();
|
||||
public BabyVO updateCurrentBaby(UpdateBabyRO request) {
|
||||
BabyProfile currentBaby = getCurrentBabyEntity();
|
||||
return updateBaby(currentBaby.getBabyId(), request);
|
||||
}
|
||||
|
||||
public BabyProfile updateBaby(String babyId, UpdateBabyRequest request) {
|
||||
jdbcTemplate.update(
|
||||
"UPDATE baby_profile SET name = ?, gender = ?, birth_date = ?, delivery_type = ? WHERE id = ?",
|
||||
request.getName().trim(),
|
||||
request.getGender() == null || "unknown".equals(request.getGender()) ? "boy" : request.getGender(),
|
||||
Date.valueOf(request.getBirthDate()),
|
||||
request.getDeliveryType() == null ? "natural" : request.getDeliveryType(),
|
||||
babyId
|
||||
public BabyVO updateBaby(String babyId, UpdateBabyRO request) {
|
||||
String gender = request.getGender() == null || "unknown".equals(request.getGender()) ? "boy" : request.getGender();
|
||||
String deliveryType = request.getDeliveryType() == null ? "natural" : request.getDeliveryType();
|
||||
LocalDate birthDate = request.getBirthDate() != null ? LocalDate.parse(request.getBirthDate()) : null;
|
||||
|
||||
LambdaUpdateWrapper<BabyProfile> wrapper = new LambdaUpdateWrapper<BabyProfile>()
|
||||
.eq(BabyProfile::getBabyId, babyId)
|
||||
.set(BabyProfile::getName, request.getName().trim())
|
||||
.set(BabyProfile::getGender, gender)
|
||||
.set(BabyProfile::getDeliveryType, deliveryType);
|
||||
if (birthDate != null) {
|
||||
wrapper.set(BabyProfile::getBirthDate, birthDate);
|
||||
}
|
||||
if (request.getRemark() != null) {
|
||||
wrapper.set(BabyProfile::getRemark, request.getRemark().trim());
|
||||
}
|
||||
babyMapper.update(null, wrapper);
|
||||
return BabyConvert.toVO(findByBabyId(babyId));
|
||||
}
|
||||
|
||||
public BabyVO createBaby(CreateBabyRO request) {
|
||||
String familyId = request.getFamilyId();
|
||||
if (familyId == null || familyId.trim().isEmpty()) {
|
||||
BabyProfile current = getCurrentBabyEntity();
|
||||
familyId = current.getFamilyId();
|
||||
}
|
||||
|
||||
boolean isDef = Boolean.TRUE.equals(request.getIsDefault());
|
||||
if (isDef) {
|
||||
babyMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<BabyProfile>()
|
||||
.eq(BabyProfile::getFamilyId, familyId)
|
||||
.set(BabyProfile::getIsDefault, 0)
|
||||
);
|
||||
}
|
||||
|
||||
BabyProfile baby = new BabyProfile();
|
||||
baby.setBabyId(idGenerator.nextId());
|
||||
baby.setFamilyId(familyId);
|
||||
baby.setName(request.getName().trim());
|
||||
baby.setGender(request.getGender() == null || "unknown".equals(request.getGender()) ? "boy" : request.getGender());
|
||||
if (request.getBirthDate() != null && !request.getBirthDate().trim().isEmpty()) {
|
||||
baby.setBirthDate(LocalDate.parse(request.getBirthDate().trim()));
|
||||
} else {
|
||||
baby.setBirthDate(LocalDate.now());
|
||||
}
|
||||
baby.setDeliveryType(request.getDeliveryType() == null ? "natural" : request.getDeliveryType());
|
||||
baby.setIsDefault(isDef ? 1 : 0);
|
||||
baby.setStatus("active");
|
||||
baby.setRemark(request.getRemark());
|
||||
babyMapper.insert(baby);
|
||||
return BabyConvert.toVO(baby);
|
||||
}
|
||||
|
||||
public BabyVO setDefaultBaby(String babyId) {
|
||||
BabyProfile baby = findByBabyId(babyId);
|
||||
babyMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<BabyProfile>()
|
||||
.eq(BabyProfile::getFamilyId, baby.getFamilyId())
|
||||
.set(BabyProfile::getIsDefault, 0)
|
||||
);
|
||||
return findByBabyId(babyId);
|
||||
babyMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<BabyProfile>()
|
||||
.eq(BabyProfile::getBabyId, babyId)
|
||||
.set(BabyProfile::getIsDefault, 1)
|
||||
);
|
||||
baby.setIsDefault(1);
|
||||
return BabyConvert.toVO(baby);
|
||||
}
|
||||
|
||||
public BabyProfile findByBabyId(String babyId) {
|
||||
List<BabyProfile> babies = jdbcTemplate.query(
|
||||
"SELECT id, family_id, name, gender, birth_date, delivery_type FROM baby_profile WHERE id = ? LIMIT 1",
|
||||
(rs, rowNum) -> mapBaby(rs),
|
||||
babyId
|
||||
);
|
||||
if (babies.isEmpty()) {
|
||||
return getCurrentBaby();
|
||||
BabyProfile baby = babyMapper.selectById(babyId);
|
||||
if (baby == null) {
|
||||
return getCurrentBabyEntity();
|
||||
}
|
||||
return babies.get(0);
|
||||
}
|
||||
|
||||
private BabyProfile mapBaby(ResultSet rs) throws SQLException {
|
||||
BabyProfile baby = new BabyProfile();
|
||||
baby.setBabyId(rs.getString("id"));
|
||||
baby.setFamilyId(rs.getString("family_id"));
|
||||
baby.setName(rs.getString("name"));
|
||||
baby.setGender(rs.getString("gender"));
|
||||
baby.setBirthDate(rs.getDate("birth_date").toLocalDate());
|
||||
baby.setDeliveryType(rs.getString("delivery_type"));
|
||||
return baby;
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.babytracker.module.baby;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class UpdateBabyRequest {
|
||||
@NotBlank
|
||||
private String name;
|
||||
private String gender;
|
||||
@NotNull
|
||||
private LocalDate birthDate;
|
||||
private String deliveryType;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
public void setBirthDate(LocalDate birthDate) {
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String getDeliveryType() {
|
||||
return deliveryType;
|
||||
}
|
||||
|
||||
public void setDeliveryType(String deliveryType) {
|
||||
this.deliveryType = deliveryType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.babytracker.module.baby.dto;
|
||||
|
||||
import com.babytracker.module.baby.BabyProfile;
|
||||
import com.babytracker.module.baby.vo.BabyVO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class BabyConvert {
|
||||
|
||||
private BabyConvert() {}
|
||||
|
||||
public static BabyVO toVO(BabyProfile entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
BabyVO vo = new BabyVO();
|
||||
vo.setBabyId(entity.getBabyId());
|
||||
vo.setFamilyId(entity.getFamilyId());
|
||||
vo.setName(entity.getName());
|
||||
vo.setGender(entity.getGender());
|
||||
vo.setBirthDate(entity.getBirthDate());
|
||||
vo.setDeliveryType(entity.getDeliveryType());
|
||||
vo.setIsDefault(entity.getIsDefault());
|
||||
vo.setStatus(entity.getStatus());
|
||||
vo.setAgeDays(entity.getAgeDays());
|
||||
vo.setAgeText(entity.getAgeText());
|
||||
vo.setRemark(entity.getRemark());
|
||||
vo.setCreatedBy(entity.getCreatedBy());
|
||||
vo.setCreatedTime(entity.getCreatedTime());
|
||||
vo.setUpdatedBy(entity.getUpdatedBy());
|
||||
vo.setUpdatedTime(entity.getUpdatedTime());
|
||||
return vo;
|
||||
}
|
||||
|
||||
public static List<BabyVO> toVOList(List<BabyProfile> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<BabyVO> voList = new ArrayList<>(list.size());
|
||||
for (BabyProfile entity : list) {
|
||||
voList.add(toVO(entity));
|
||||
}
|
||||
return voList;
|
||||
}
|
||||
|
||||
public static BabyDTO toDTO(BabyProfile entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
BabyDTO dto = new BabyDTO();
|
||||
dto.setBabyId(entity.getBabyId());
|
||||
dto.setFamilyId(entity.getFamilyId());
|
||||
dto.setName(entity.getName());
|
||||
dto.setGender(entity.getGender());
|
||||
dto.setBirthDate(entity.getBirthDate());
|
||||
dto.setDeliveryType(entity.getDeliveryType());
|
||||
dto.setIsDefault(entity.getIsDefault());
|
||||
dto.setStatus(entity.getStatus());
|
||||
dto.setAgeDays(entity.getAgeDays());
|
||||
dto.setAgeText(entity.getAgeText());
|
||||
dto.setRemark(entity.getRemark());
|
||||
dto.setCreatedBy(entity.getCreatedBy());
|
||||
dto.setCreatedTime(entity.getCreatedTime());
|
||||
dto.setUpdatedBy(entity.getUpdatedBy());
|
||||
dto.setUpdatedTime(entity.getUpdatedTime());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.babytracker.module.baby.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class BabyDTO {
|
||||
private String babyId;
|
||||
private String familyId;
|
||||
private String name;
|
||||
private String gender;
|
||||
private LocalDate birthDate;
|
||||
private String deliveryType;
|
||||
private Integer isDefault;
|
||||
private String status;
|
||||
private long ageDays;
|
||||
private String ageText;
|
||||
private String remark;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.babytracker.module.baby.mapper;
|
||||
|
||||
import com.babytracker.module.baby.BabyProfile;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface BabyMapper extends BaseMapper<BabyProfile> {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.babytracker.module.baby.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CreateBabyRO {
|
||||
private String familyId;
|
||||
|
||||
@NotBlank(message = "宝宝姓名不能为空")
|
||||
private String name;
|
||||
|
||||
private String gender;
|
||||
|
||||
private String birthDate;
|
||||
|
||||
private String deliveryType;
|
||||
|
||||
private Boolean isDefault;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.babytracker.module.baby.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SetDefaultBabyRO {
|
||||
@NotBlank(message = "宝宝ID不能为空")
|
||||
private String babyId;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.babytracker.module.baby.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateBabyRO {
|
||||
private String babyId;
|
||||
|
||||
@NotBlank(message = "宝宝姓名不能为空")
|
||||
private String name;
|
||||
|
||||
private String gender;
|
||||
|
||||
private String birthDate;
|
||||
|
||||
private String deliveryType;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.babytracker.module.baby.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class BabyVO {
|
||||
private String babyId;
|
||||
private String familyId;
|
||||
private String name;
|
||||
private String gender;
|
||||
private LocalDate birthDate;
|
||||
private String deliveryType;
|
||||
private Integer isDefault;
|
||||
private String status;
|
||||
private long ageDays;
|
||||
private String ageText;
|
||||
private String remark;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdatedAt() {
|
||||
return updatedTime;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.babytracker.module.family;
|
||||
|
||||
import com.babytracker.module.baby.BabyProfile;
|
||||
import java.util.List;
|
||||
|
||||
public class FamilyContext {
|
||||
private FamilyInfo family;
|
||||
private List<BabyProfile> babies;
|
||||
private List<FamilyMember> members;
|
||||
|
||||
public FamilyInfo getFamily() {
|
||||
return family;
|
||||
}
|
||||
|
||||
public void setFamily(FamilyInfo family) {
|
||||
this.family = family;
|
||||
}
|
||||
|
||||
public List<BabyProfile> getBabies() {
|
||||
return babies;
|
||||
}
|
||||
|
||||
public void setBabies(List<BabyProfile> babies) {
|
||||
this.babies = babies;
|
||||
}
|
||||
|
||||
public List<FamilyMember> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(List<FamilyMember> members) {
|
||||
this.members = members;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,53 @@
|
||||
package com.babytracker.module.family;
|
||||
|
||||
import com.babytracker.common.response.ApiResponse;
|
||||
import com.babytracker.module.family.ro.CreateMemberRO;
|
||||
import com.babytracker.module.family.ro.DeleteMemberRO;
|
||||
import com.babytracker.module.family.ro.UpdateFamilyRO;
|
||||
import com.babytracker.module.family.ro.UpdateMemberRO;
|
||||
import com.babytracker.module.family.vo.FamilyContextVO;
|
||||
import com.babytracker.module.family.vo.FamilyMemberVO;
|
||||
import com.babytracker.module.family.vo.FamilyVO;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/v1/families")
|
||||
public class FamilyController {
|
||||
private final FamilyService familyService;
|
||||
|
||||
public FamilyController(FamilyService familyService) {
|
||||
this.familyService = familyService;
|
||||
}
|
||||
|
||||
@GetMapping("/current-context")
|
||||
public ApiResponse<FamilyContext> currentContext() {
|
||||
public ApiResponse<FamilyContextVO> currentContext() {
|
||||
return ApiResponse.ok(familyService.getCurrentContext());
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ApiResponse<FamilyVO> updateFamily(@Valid @RequestBody UpdateFamilyRO request) {
|
||||
return ApiResponse.ok(familyService.updateFamily(request));
|
||||
}
|
||||
|
||||
@PostMapping("/members")
|
||||
public ApiResponse<FamilyMemberVO> createMember(@Valid @RequestBody CreateMemberRO request) {
|
||||
return ApiResponse.ok(familyService.createMember(request));
|
||||
}
|
||||
|
||||
@PutMapping("/members")
|
||||
public ApiResponse<FamilyMemberVO> updateMember(@Valid @RequestBody UpdateMemberRO request) {
|
||||
return ApiResponse.ok(familyService.updateMember(request));
|
||||
}
|
||||
|
||||
@PostMapping("/members/delete")
|
||||
public ApiResponse<Void> deleteMember(@Valid @RequestBody DeleteMemberRO request) {
|
||||
familyService.deleteMember(request.getFamilyId(), request.getMemberId());
|
||||
return ApiResponse.ok(null);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,27 @@
|
||||
package com.babytracker.module.family;
|
||||
|
||||
public class FamilyInfo {
|
||||
import com.babytracker.common.model.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("family")
|
||||
public class FamilyInfo extends BaseEntity {
|
||||
|
||||
@TableId(value = "id", type = IdType.INPUT)
|
||||
private String familyId;
|
||||
|
||||
@TableField("name")
|
||||
private String name;
|
||||
|
||||
@TableField("invite_code")
|
||||
private String inviteCode;
|
||||
|
||||
public String getFamilyId() {
|
||||
return familyId;
|
||||
}
|
||||
|
||||
public void setFamilyId(String familyId) {
|
||||
this.familyId = familyId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getInviteCode() {
|
||||
return inviteCode;
|
||||
}
|
||||
|
||||
public void setInviteCode(String inviteCode) {
|
||||
this.inviteCode = inviteCode;
|
||||
}
|
||||
@TableField("status")
|
||||
private String status;
|
||||
}
|
||||
@@ -1,58 +1,36 @@
|
||||
package com.babytracker.module.family;
|
||||
|
||||
public class FamilyMember {
|
||||
import com.babytracker.common.model.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("family_member")
|
||||
public class FamilyMember extends BaseEntity {
|
||||
|
||||
@TableId(value = "id", type = IdType.INPUT)
|
||||
private String memberId;
|
||||
|
||||
@TableField("family_id")
|
||||
private String familyId;
|
||||
|
||||
@TableField("user_id")
|
||||
private String userId;
|
||||
|
||||
@TableField("role_name")
|
||||
private String roleName;
|
||||
|
||||
@TableField("role_type")
|
||||
private String roleType;
|
||||
|
||||
@TableField("is_default_operator")
|
||||
private boolean defaultOperator;
|
||||
|
||||
public String getMemberId() {
|
||||
return memberId;
|
||||
}
|
||||
|
||||
public void setMemberId(String memberId) {
|
||||
this.memberId = memberId;
|
||||
}
|
||||
|
||||
public String getFamilyId() {
|
||||
return familyId;
|
||||
}
|
||||
|
||||
public void setFamilyId(String familyId) {
|
||||
this.familyId = familyId;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getRoleName() {
|
||||
return roleName;
|
||||
}
|
||||
|
||||
public void setRoleName(String roleName) {
|
||||
this.roleName = roleName;
|
||||
}
|
||||
|
||||
public String getRoleType() {
|
||||
return roleType;
|
||||
}
|
||||
|
||||
public void setRoleType(String roleType) {
|
||||
this.roleType = roleType;
|
||||
}
|
||||
|
||||
public boolean isDefaultOperator() {
|
||||
return defaultOperator;
|
||||
}
|
||||
|
||||
public void setDefaultOperator(boolean defaultOperator) {
|
||||
this.defaultOperator = defaultOperator;
|
||||
}
|
||||
@TableField("status")
|
||||
private String status;
|
||||
}
|
||||
@@ -1,65 +1,164 @@
|
||||
package com.babytracker.module.family;
|
||||
|
||||
import com.babytracker.module.baby.BabyProfile;
|
||||
import com.babytracker.common.exception.BusinessException;
|
||||
import com.babytracker.common.id.SnowflakeIdGenerator;
|
||||
import com.babytracker.module.baby.BabyService;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import com.babytracker.module.family.dto.FamilyConvert;
|
||||
import com.babytracker.module.family.mapper.FamilyMapper;
|
||||
import com.babytracker.module.family.mapper.FamilyMemberMapper;
|
||||
import com.babytracker.module.family.ro.CreateMemberRO;
|
||||
import com.babytracker.module.family.ro.UpdateFamilyRO;
|
||||
import com.babytracker.module.family.ro.UpdateMemberRO;
|
||||
import com.babytracker.module.family.vo.FamilyContextVO;
|
||||
import com.babytracker.module.family.vo.FamilyMemberVO;
|
||||
import com.babytracker.module.family.vo.FamilyVO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FamilyService {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final FamilyMapper familyMapper;
|
||||
private final FamilyMemberMapper familyMemberMapper;
|
||||
private final BabyService babyService;
|
||||
private final SnowflakeIdGenerator idGenerator;
|
||||
|
||||
public FamilyService(JdbcTemplate jdbcTemplate, BabyService babyService) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.babyService = babyService;
|
||||
}
|
||||
|
||||
public FamilyContext getCurrentContext() {
|
||||
FamilyContext context = new FamilyContext();
|
||||
context.setFamily(getDefaultFamily());
|
||||
public FamilyContextVO getCurrentContext() {
|
||||
FamilyInfo family = getDefaultFamily();
|
||||
FamilyContextVO context = new FamilyContextVO();
|
||||
context.setFamily(FamilyConvert.toVO(family));
|
||||
context.setBabies(babyService.listBabies());
|
||||
context.setMembers(listMembers(context.getFamily().getFamilyId()));
|
||||
context.setMembers(FamilyConvert.toMemberVOList(listMembers(family.getFamilyId())));
|
||||
return context;
|
||||
}
|
||||
|
||||
private FamilyInfo getDefaultFamily() {
|
||||
List<FamilyInfo> families = jdbcTemplate.query(
|
||||
"SELECT id, name, invite_code FROM family ORDER BY id ASC LIMIT 1",
|
||||
(rs, rowNum) -> {
|
||||
FamilyInfo family = new FamilyInfo();
|
||||
family.setFamilyId(rs.getString("id"));
|
||||
family.setName(rs.getString("name"));
|
||||
family.setInviteCode(rs.getString("invite_code"));
|
||||
return family;
|
||||
}
|
||||
);
|
||||
if (!families.isEmpty()) {
|
||||
return families.get(0);
|
||||
public FamilyVO updateFamily(UpdateFamilyRO request) {
|
||||
String familyId = request.getFamilyId();
|
||||
if (familyId == null || familyId.trim().isEmpty()) {
|
||||
familyId = getDefaultFamily().getFamilyId();
|
||||
}
|
||||
FamilyInfo family = new FamilyInfo();
|
||||
family.setFamilyId("00000000000000361915955439210496");
|
||||
family.setName("新生儿照护家庭");
|
||||
family.setInviteCode("BT2026");
|
||||
return family;
|
||||
FamilyInfo family = familyMapper.selectById(familyId);
|
||||
if (family == null) {
|
||||
throw new BusinessException("FAMILY_NOT_FOUND", "家庭不存在");
|
||||
}
|
||||
family.setName(request.getName().trim());
|
||||
if (request.getInviteCode() != null) {
|
||||
family.setInviteCode(request.getInviteCode().trim());
|
||||
}
|
||||
if (request.getRemark() != null) {
|
||||
family.setRemark(request.getRemark().trim());
|
||||
}
|
||||
familyMapper.updateById(family);
|
||||
return FamilyConvert.toVO(family);
|
||||
}
|
||||
|
||||
private List<FamilyMember> listMembers(String familyId) {
|
||||
List<FamilyMember> members = jdbcTemplate.query(
|
||||
"SELECT id, family_id, user_id, role_name, role_type, is_default_operator FROM family_member WHERE family_id = ? AND status = 'active' ORDER BY is_default_operator DESC, id ASC",
|
||||
(rs, rowNum) -> {
|
||||
FamilyMember member = new FamilyMember();
|
||||
member.setMemberId(rs.getString("id"));
|
||||
member.setFamilyId(rs.getString("family_id"));
|
||||
member.setUserId(rs.getString("user_id"));
|
||||
member.setRoleName(rs.getString("role_name"));
|
||||
member.setRoleType(rs.getString("role_type"));
|
||||
member.setDefaultOperator(rs.getBoolean("is_default_operator"));
|
||||
return member;
|
||||
},
|
||||
familyId
|
||||
public FamilyMemberVO createMember(CreateMemberRO request) {
|
||||
String familyId = request.getFamilyId();
|
||||
if (familyId == null || familyId.trim().isEmpty()) {
|
||||
familyId = getDefaultFamily().getFamilyId();
|
||||
}
|
||||
|
||||
boolean isDef = Boolean.TRUE.equals(request.getDefaultOperator());
|
||||
if (isDef) {
|
||||
familyMemberMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<FamilyMember>()
|
||||
.eq(FamilyMember::getFamilyId, familyId)
|
||||
.set(FamilyMember::isDefaultOperator, false)
|
||||
);
|
||||
}
|
||||
|
||||
FamilyMember member = new FamilyMember();
|
||||
member.setMemberId(idGenerator.nextId());
|
||||
member.setFamilyId(familyId);
|
||||
member.setRoleName(request.getRoleName().trim());
|
||||
member.setRoleType(request.getRoleType() == null || request.getRoleType().trim().isEmpty() ? "caregiver" : request.getRoleType().trim());
|
||||
member.setDefaultOperator(isDef);
|
||||
member.setStatus("active");
|
||||
member.setRemark(request.getRemark());
|
||||
familyMemberMapper.insert(member);
|
||||
return FamilyConvert.toVO(member);
|
||||
}
|
||||
|
||||
public FamilyMemberVO updateMember(UpdateMemberRO request) {
|
||||
String memberId = request.getMemberId();
|
||||
FamilyMember member = familyMemberMapper.selectById(memberId);
|
||||
if (member == null) {
|
||||
throw new BusinessException("MEMBER_NOT_FOUND", "家庭成员不存在");
|
||||
}
|
||||
String familyId = request.getFamilyId() != null && !request.getFamilyId().trim().isEmpty()
|
||||
? request.getFamilyId() : member.getFamilyId();
|
||||
|
||||
boolean isDef = Boolean.TRUE.equals(request.getDefaultOperator());
|
||||
if (isDef) {
|
||||
familyMemberMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<FamilyMember>()
|
||||
.eq(FamilyMember::getFamilyId, familyId)
|
||||
.set(FamilyMember::isDefaultOperator, false)
|
||||
);
|
||||
}
|
||||
|
||||
member.setRoleName(request.getRoleName().trim());
|
||||
if (request.getRoleType() != null && !request.getRoleType().trim().isEmpty()) {
|
||||
member.setRoleType(request.getRoleType().trim());
|
||||
}
|
||||
member.setDefaultOperator(isDef);
|
||||
if (request.getRemark() != null) {
|
||||
member.setRemark(request.getRemark().trim());
|
||||
}
|
||||
familyMemberMapper.updateById(member);
|
||||
return FamilyConvert.toVO(member);
|
||||
}
|
||||
|
||||
public void deleteMember(String familyId, String memberId) {
|
||||
if (familyId == null || familyId.trim().isEmpty()) {
|
||||
FamilyMember member = familyMemberMapper.selectById(memberId);
|
||||
if (member == null) {
|
||||
throw new BusinessException("MEMBER_NOT_FOUND", "家庭成员不存在");
|
||||
}
|
||||
familyId = member.getFamilyId();
|
||||
}
|
||||
List<FamilyMember> members = listMembers(familyId);
|
||||
if (members.size() <= 1) {
|
||||
throw new BusinessException("CANNOT_DELETE_LAST_MEMBER", "家庭中至少需保留一名成员");
|
||||
}
|
||||
FamilyMember member = familyMemberMapper.selectById(memberId);
|
||||
if (member == null || !familyId.equals(member.getFamilyId())) {
|
||||
throw new BusinessException("MEMBER_NOT_FOUND", "家庭成员不存在");
|
||||
}
|
||||
member.setStatus("left");
|
||||
familyMemberMapper.deleteById(memberId);
|
||||
}
|
||||
|
||||
public FamilyInfo getDefaultFamily() {
|
||||
FamilyInfo family = familyMapper.selectOne(
|
||||
new LambdaQueryWrapper<FamilyInfo>()
|
||||
.orderByAsc(FamilyInfo::getFamilyId)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
if (family != null) {
|
||||
return family;
|
||||
}
|
||||
FamilyInfo defaultFamily = new FamilyInfo();
|
||||
defaultFamily.setFamilyId("00000000000000361915955439210496");
|
||||
defaultFamily.setName("新生儿照护家庭");
|
||||
defaultFamily.setInviteCode("BT2026");
|
||||
familyMapper.insert(defaultFamily);
|
||||
return defaultFamily;
|
||||
}
|
||||
|
||||
public List<FamilyMember> listMembers(String familyId) {
|
||||
List<FamilyMember> members = familyMemberMapper.selectList(
|
||||
new LambdaQueryWrapper<FamilyMember>()
|
||||
.eq(FamilyMember::getFamilyId, familyId)
|
||||
.eq(FamilyMember::getStatus, "active")
|
||||
.orderByDesc(FamilyMember::isDefaultOperator)
|
||||
.orderByAsc(FamilyMember::getMemberId)
|
||||
);
|
||||
if (!members.isEmpty()) {
|
||||
return members;
|
||||
@@ -76,5 +175,4 @@ public class FamilyService {
|
||||
mom.setDefaultOperator(true);
|
||||
return List.of(mom);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.babytracker.module.family.dto;
|
||||
|
||||
import com.babytracker.module.family.FamilyInfo;
|
||||
import com.babytracker.module.family.FamilyMember;
|
||||
import com.babytracker.module.family.vo.FamilyMemberVO;
|
||||
import com.babytracker.module.family.vo.FamilyVO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class FamilyConvert {
|
||||
|
||||
private FamilyConvert() {}
|
||||
|
||||
public static FamilyVO toVO(FamilyInfo entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
FamilyVO vo = new FamilyVO();
|
||||
vo.setFamilyId(entity.getFamilyId());
|
||||
vo.setName(entity.getName());
|
||||
vo.setInviteCode(entity.getInviteCode());
|
||||
vo.setStatus(entity.getStatus());
|
||||
vo.setRemark(entity.getRemark());
|
||||
vo.setCreatedBy(entity.getCreatedBy());
|
||||
vo.setCreatedTime(entity.getCreatedTime());
|
||||
vo.setUpdatedBy(entity.getUpdatedBy());
|
||||
vo.setUpdatedTime(entity.getUpdatedTime());
|
||||
return vo;
|
||||
}
|
||||
|
||||
public static FamilyDTO toDTO(FamilyInfo entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
FamilyDTO dto = new FamilyDTO();
|
||||
dto.setFamilyId(entity.getFamilyId());
|
||||
dto.setName(entity.getName());
|
||||
dto.setInviteCode(entity.getInviteCode());
|
||||
dto.setStatus(entity.getStatus());
|
||||
dto.setRemark(entity.getRemark());
|
||||
dto.setCreatedBy(entity.getCreatedBy());
|
||||
dto.setCreatedTime(entity.getCreatedTime());
|
||||
dto.setUpdatedBy(entity.getUpdatedBy());
|
||||
dto.setUpdatedTime(entity.getUpdatedTime());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public static FamilyMemberVO toVO(FamilyMember entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
FamilyMemberVO vo = new FamilyMemberVO();
|
||||
vo.setMemberId(entity.getMemberId());
|
||||
vo.setFamilyId(entity.getFamilyId());
|
||||
vo.setUserId(entity.getUserId());
|
||||
vo.setRoleName(entity.getRoleName());
|
||||
vo.setRoleType(entity.getRoleType());
|
||||
vo.setDefaultOperator(entity.isDefaultOperator());
|
||||
vo.setStatus(entity.getStatus());
|
||||
vo.setRemark(entity.getRemark());
|
||||
vo.setCreatedBy(entity.getCreatedBy());
|
||||
vo.setCreatedTime(entity.getCreatedTime());
|
||||
vo.setUpdatedBy(entity.getUpdatedBy());
|
||||
vo.setUpdatedTime(entity.getUpdatedTime());
|
||||
return vo;
|
||||
}
|
||||
|
||||
public static List<FamilyMemberVO> toMemberVOList(List<FamilyMember> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<FamilyMemberVO> voList = new ArrayList<>(list.size());
|
||||
for (FamilyMember entity : list) {
|
||||
voList.add(toVO(entity));
|
||||
}
|
||||
return voList;
|
||||
}
|
||||
|
||||
public static FamilyMemberDTO toDTO(FamilyMember entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
FamilyMemberDTO dto = new FamilyMemberDTO();
|
||||
dto.setMemberId(entity.getMemberId());
|
||||
dto.setFamilyId(entity.getFamilyId());
|
||||
dto.setUserId(entity.getUserId());
|
||||
dto.setRoleName(entity.getRoleName());
|
||||
dto.setRoleType(entity.getRoleType());
|
||||
dto.setDefaultOperator(entity.isDefaultOperator());
|
||||
dto.setStatus(entity.getStatus());
|
||||
dto.setRemark(entity.getRemark());
|
||||
dto.setCreatedBy(entity.getCreatedBy());
|
||||
dto.setCreatedTime(entity.getCreatedTime());
|
||||
dto.setUpdatedBy(entity.getUpdatedBy());
|
||||
dto.setUpdatedTime(entity.getUpdatedTime());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.babytracker.module.family.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class FamilyDTO {
|
||||
private String familyId;
|
||||
private String name;
|
||||
private String inviteCode;
|
||||
private String status;
|
||||
private String remark;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.babytracker.module.family.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class FamilyMemberDTO {
|
||||
private String memberId;
|
||||
private String familyId;
|
||||
private String userId;
|
||||
private String roleName;
|
||||
private String roleType;
|
||||
private boolean defaultOperator;
|
||||
private String status;
|
||||
private String remark;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.babytracker.module.family.mapper;
|
||||
|
||||
import com.babytracker.module.family.FamilyInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface FamilyMapper extends BaseMapper<FamilyInfo> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.babytracker.module.family.mapper;
|
||||
|
||||
import com.babytracker.module.family.FamilyMember;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface FamilyMemberMapper extends BaseMapper<FamilyMember> {
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.babytracker.module.family.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CreateMemberRO {
|
||||
private String familyId;
|
||||
|
||||
@NotBlank(message = "角色称谓不能为空")
|
||||
private String roleName;
|
||||
|
||||
private String roleType; // owner, caregiver, viewer
|
||||
|
||||
private Boolean defaultOperator;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.babytracker.module.family.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DeleteMemberRO {
|
||||
private String familyId;
|
||||
|
||||
@NotBlank(message = "成员ID不能为空")
|
||||
private String memberId;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.babytracker.module.family.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateFamilyRO {
|
||||
private String familyId;
|
||||
|
||||
@NotBlank(message = "家庭名称不能为空")
|
||||
private String name;
|
||||
|
||||
private String inviteCode;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.babytracker.module.family.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateMemberRO {
|
||||
private String familyId;
|
||||
|
||||
@NotBlank(message = "成员ID不能为空")
|
||||
private String memberId;
|
||||
|
||||
@NotBlank(message = "角色称谓不能为空")
|
||||
private String roleName;
|
||||
|
||||
private String roleType; // owner, caregiver, viewer
|
||||
|
||||
private Boolean defaultOperator;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.babytracker.module.family.vo;
|
||||
|
||||
import com.babytracker.module.baby.vo.BabyVO;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class FamilyContextVO {
|
||||
private FamilyVO family;
|
||||
private List<BabyVO> babies;
|
||||
private List<FamilyMemberVO> members;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.babytracker.module.family.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class FamilyMemberVO {
|
||||
private String memberId;
|
||||
private String familyId;
|
||||
private String userId;
|
||||
private String roleName;
|
||||
private String roleType;
|
||||
private boolean defaultOperator;
|
||||
private String status;
|
||||
private String remark;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdatedAt() {
|
||||
return updatedTime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.babytracker.module.family.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class FamilyVO {
|
||||
private String familyId;
|
||||
private String name;
|
||||
private String inviteCode;
|
||||
private String status;
|
||||
private String remark;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdatedAt() {
|
||||
return updatedTime;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,59 @@
|
||||
package com.babytracker.module.record;
|
||||
|
||||
import com.babytracker.common.model.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class CareRecord {
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("care_record")
|
||||
public class CareRecord extends BaseEntity {
|
||||
|
||||
@TableId(value = "id", type = IdType.INPUT)
|
||||
private String recordId;
|
||||
|
||||
@TableField("family_id")
|
||||
private String familyId;
|
||||
|
||||
@TableField("baby_id")
|
||||
private String babyId;
|
||||
|
||||
@TableField("record_type")
|
||||
private String recordType;
|
||||
|
||||
@TableField("start_time")
|
||||
private LocalDateTime startTime;
|
||||
|
||||
@TableField("end_time")
|
||||
private LocalDateTime endTime;
|
||||
|
||||
@TableField("duration_minutes")
|
||||
private Integer durationMinutes;
|
||||
|
||||
@TableField("feeding_method")
|
||||
private String feedingMethod;
|
||||
|
||||
@TableField("amount_ml")
|
||||
private Integer amountMl;
|
||||
|
||||
@TableField("excretion_type")
|
||||
private String excretionType;
|
||||
|
||||
@TableField("operator_name")
|
||||
private String operatorName;
|
||||
private String note;
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@TableField("operator_user_id")
|
||||
private String operatorUserId;
|
||||
|
||||
@TableField("source")
|
||||
private String source = "h5";
|
||||
|
||||
public void recalculateDuration() {
|
||||
if (startTime != null && endTime != null) {
|
||||
@@ -24,108 +61,11 @@ public class CareRecord {
|
||||
}
|
||||
}
|
||||
|
||||
public String getRecordId() {
|
||||
return recordId;
|
||||
}
|
||||
|
||||
public void setRecordId(String recordId) {
|
||||
this.recordId = recordId;
|
||||
}
|
||||
|
||||
public String getFamilyId() {
|
||||
return familyId;
|
||||
}
|
||||
|
||||
public void setFamilyId(String familyId) {
|
||||
this.familyId = familyId;
|
||||
}
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public String getRecordType() {
|
||||
return recordType;
|
||||
}
|
||||
|
||||
public void setRecordType(String recordType) {
|
||||
this.recordType = recordType;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(LocalDateTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(LocalDateTime endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public Integer getDurationMinutes() {
|
||||
return durationMinutes;
|
||||
}
|
||||
|
||||
public void setDurationMinutes(Integer durationMinutes) {
|
||||
this.durationMinutes = durationMinutes;
|
||||
}
|
||||
|
||||
public String getFeedingMethod() {
|
||||
return feedingMethod;
|
||||
}
|
||||
|
||||
public void setFeedingMethod(String feedingMethod) {
|
||||
this.feedingMethod = feedingMethod;
|
||||
}
|
||||
|
||||
public Integer getAmountMl() {
|
||||
return amountMl;
|
||||
}
|
||||
|
||||
public void setAmountMl(Integer amountMl) {
|
||||
this.amountMl = amountMl;
|
||||
}
|
||||
|
||||
public String getExcretionType() {
|
||||
return excretionType;
|
||||
}
|
||||
|
||||
public void setExcretionType(String excretionType) {
|
||||
this.excretionType = excretionType;
|
||||
}
|
||||
|
||||
public String getOperatorName() {
|
||||
return operatorName;
|
||||
}
|
||||
|
||||
public void setOperatorName(String operatorName) {
|
||||
this.operatorName = operatorName;
|
||||
}
|
||||
|
||||
public String getNote() {
|
||||
return note;
|
||||
return getRemark();
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
setRemark(note);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,37 @@
|
||||
package com.babytracker.module.record;
|
||||
|
||||
import com.babytracker.common.response.ApiResponse;
|
||||
import com.babytracker.module.record.ro.CreateFeedingRO;
|
||||
import com.babytracker.module.record.ro.CreateManualRecordRO;
|
||||
import com.babytracker.module.record.ro.DeleteCareRecordRO;
|
||||
import com.babytracker.module.record.ro.ExcretionRO;
|
||||
import com.babytracker.module.record.ro.SleepRO;
|
||||
import com.babytracker.module.record.ro.UpdateCareRecordRO;
|
||||
import com.babytracker.module.record.vo.CareRecordVO;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/v1/records")
|
||||
public class CareRecordController {
|
||||
private final CareRecordService careRecordService;
|
||||
|
||||
public CareRecordController(CareRecordService careRecordService) {
|
||||
this.careRecordService = careRecordService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ApiResponse<List<CareRecord>> list(
|
||||
public ApiResponse<List<CareRecordVO>> list(
|
||||
@RequestParam String babyId,
|
||||
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date
|
||||
) {
|
||||
@@ -35,43 +39,53 @@ public class CareRecordController {
|
||||
}
|
||||
|
||||
@PostMapping("/feeding")
|
||||
public ApiResponse<CareRecord> feeding(@Valid @RequestBody CreateFeedingRequest request) {
|
||||
public ApiResponse<CareRecordVO> feeding(@Valid @RequestBody CreateFeedingRO request) {
|
||||
return ApiResponse.ok(careRecordService.createFeeding(request));
|
||||
}
|
||||
|
||||
@PostMapping("/feeding/start")
|
||||
public ApiResponse<CareRecord> startFeeding(@Valid @RequestBody CreateFeedingRequest request) {
|
||||
public ApiResponse<CareRecordVO> startFeeding(@Valid @RequestBody CreateFeedingRO request) {
|
||||
return ApiResponse.ok(careRecordService.startFeeding(request));
|
||||
}
|
||||
|
||||
@PostMapping("/feeding/end")
|
||||
public ApiResponse<CareRecord> endFeeding(@Valid @RequestBody CreateFeedingRequest request) {
|
||||
public ApiResponse<CareRecordVO> endFeeding(@Valid @RequestBody CreateFeedingRO request) {
|
||||
return ApiResponse.ok(careRecordService.endFeeding(request));
|
||||
}
|
||||
|
||||
@PostMapping("/sleep/start")
|
||||
public ApiResponse<CareRecord> startSleep(@Valid @RequestBody SleepRequest request) {
|
||||
public ApiResponse<CareRecordVO> startSleep(@Valid @RequestBody SleepRO request) {
|
||||
return ApiResponse.ok(careRecordService.startSleep(request));
|
||||
}
|
||||
|
||||
@PostMapping("/sleep/end")
|
||||
public ApiResponse<CareRecord> endSleep(@Valid @RequestBody SleepRequest request) {
|
||||
public ApiResponse<CareRecordVO> endSleep(@Valid @RequestBody SleepRO request) {
|
||||
return ApiResponse.ok(careRecordService.endSleep(request));
|
||||
}
|
||||
|
||||
@PostMapping("/pee")
|
||||
public ApiResponse<CareRecord> pee(@Valid @RequestBody ExcretionRequest request) {
|
||||
public ApiResponse<CareRecordVO> pee(@Valid @RequestBody ExcretionRO request) {
|
||||
return ApiResponse.ok(careRecordService.createPee(request));
|
||||
}
|
||||
|
||||
@PostMapping("/poop")
|
||||
public ApiResponse<CareRecord> poop(@Valid @RequestBody ExcretionRequest request) {
|
||||
public ApiResponse<CareRecordVO> poop(@Valid @RequestBody ExcretionRO request) {
|
||||
return ApiResponse.ok(careRecordService.createPoop(request));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{recordId}")
|
||||
public ApiResponse<Void> delete(@PathVariable String recordId) {
|
||||
careRecordService.delete(recordId);
|
||||
@PostMapping
|
||||
public ApiResponse<CareRecordVO> createManual(@Valid @RequestBody CreateManualRecordRO request) {
|
||||
return ApiResponse.ok(careRecordService.createManual(request));
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ApiResponse<CareRecordVO> update(@Valid @RequestBody UpdateCareRecordRO request) {
|
||||
return ApiResponse.ok(careRecordService.update(request));
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public ApiResponse<Void> delete(@Valid @RequestBody DeleteCareRecordRO request) {
|
||||
careRecordService.delete(request.getRecordId());
|
||||
return ApiResponse.ok(null);
|
||||
}
|
||||
}
|
||||
@@ -2,75 +2,72 @@ package com.babytracker.module.record;
|
||||
|
||||
import com.babytracker.common.exception.BusinessException;
|
||||
import com.babytracker.common.id.SnowflakeIdGenerator;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import com.babytracker.module.baby.BabyProfile;
|
||||
import com.babytracker.module.baby.mapper.BabyMapper;
|
||||
import com.babytracker.module.record.dto.CareRecordConvert;
|
||||
import com.babytracker.module.record.mapper.CareRecordMapper;
|
||||
import com.babytracker.module.record.ro.CreateFeedingRO;
|
||||
import com.babytracker.module.record.ro.CreateManualRecordRO;
|
||||
import com.babytracker.module.record.ro.ExcretionRO;
|
||||
import com.babytracker.module.record.ro.SleepRO;
|
||||
import com.babytracker.module.record.ro.UpdateCareRecordRO;
|
||||
import com.babytracker.module.record.vo.CareRecordVO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CareRecordService {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final CareRecordMapper careRecordMapper;
|
||||
private final BabyMapper babyMapper;
|
||||
private final SnowflakeIdGenerator idGenerator;
|
||||
|
||||
public CareRecordService(JdbcTemplate jdbcTemplate, SnowflakeIdGenerator idGenerator) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
public List<CareRecord> list(String babyId, LocalDate date) {
|
||||
public List<CareRecordVO> list(String babyId, LocalDate date) {
|
||||
LocalDate targetDate = date == null ? LocalDate.now() : date;
|
||||
return jdbcTemplate.query(
|
||||
"SELECT * FROM care_record WHERE baby_id = ? AND deleted_at IS NULL AND start_time >= ? AND start_time < ? ORDER BY start_time DESC, id DESC",
|
||||
(rs, rowNum) -> mapRecord(rs),
|
||||
babyId,
|
||||
Timestamp.valueOf(targetDate.atStartOfDay()),
|
||||
Timestamp.valueOf(targetDate.plusDays(1).atStartOfDay())
|
||||
LocalDateTime dayStart = targetDate.atStartOfDay();
|
||||
LocalDateTime dayEnd = targetDate.plusDays(1).atStartOfDay();
|
||||
|
||||
List<CareRecord> records = careRecordMapper.selectList(
|
||||
new LambdaQueryWrapper<CareRecord>()
|
||||
.eq(CareRecord::getBabyId, babyId)
|
||||
.ge(CareRecord::getStartTime, dayStart)
|
||||
.lt(CareRecord::getStartTime, dayEnd)
|
||||
.orderByDesc(CareRecord::getStartTime)
|
||||
.orderByDesc(CareRecord::getRecordId)
|
||||
);
|
||||
return CareRecordConvert.toVOList(records);
|
||||
}
|
||||
|
||||
public List<CareRecord> allByBaby(String babyId) {
|
||||
return jdbcTemplate.query(
|
||||
"SELECT * FROM care_record WHERE baby_id = ? AND deleted_at IS NULL ORDER BY start_time ASC, id ASC",
|
||||
(rs, rowNum) -> mapRecord(rs),
|
||||
babyId
|
||||
);
|
||||
}
|
||||
|
||||
public CareRecord createFeeding(CreateFeedingRequest request) {
|
||||
CareRecord record = baseRecord(request.getBabyId(), "feeding", request.getFeedingTime(), request.getOperatorName(), buildFeedingNote(request));
|
||||
public CareRecordVO createFeeding(CreateFeedingRO request) {
|
||||
CareRecord record = baseRecord(request.getBabyId(), "feeding", request.getFeedingTime(), request.getOperatorName(), request.getRemark());
|
||||
record.setFeedingMethod(request.getFeedingMethod());
|
||||
record.setAmountMl(request.getAmountMl());
|
||||
LocalDateTime endTime = request.getEndTime() == null ? record.getStartTime() : request.getEndTime();
|
||||
if (endTime != null) {
|
||||
if (endTime.isBefore(record.getStartTime())) {
|
||||
throw new BusinessException("INVALID_FEEDING_TIME", "喂奶结束时间不能早于开始时间");
|
||||
}
|
||||
record.setEndTime(endTime);
|
||||
record.setEndTime(request.getEndTime());
|
||||
if (record.getStartTime() != null && record.getEndTime() != null) {
|
||||
record.recalculateDuration();
|
||||
}
|
||||
insert(record);
|
||||
return record;
|
||||
careRecordMapper.insert(record);
|
||||
return CareRecordConvert.toVO(record);
|
||||
}
|
||||
|
||||
public CareRecord startFeeding(CreateFeedingRequest request) {
|
||||
CareRecord activeFeeding = findActiveFeeding(request.getBabyId());
|
||||
public CareRecordVO startFeeding(CreateFeedingRO request) {
|
||||
CareRecord activeFeeding = findActiveFeedingEntity(request.getBabyId());
|
||||
if (activeFeeding != null) {
|
||||
throw new BusinessException("FEEDING_ALREADY_STARTED", "当前已有进行中的亲喂记录");
|
||||
}
|
||||
CareRecord record = baseRecord(request.getBabyId(), "feeding", request.getFeedingTime(), request.getOperatorName(), buildFeedingNote(request));
|
||||
CareRecord record = baseRecord(request.getBabyId(), "feeding", request.getFeedingTime(), request.getOperatorName(), buildFeedingRemark(request));
|
||||
record.setFeedingMethod("breastfeed");
|
||||
insert(record);
|
||||
return record;
|
||||
careRecordMapper.insert(record);
|
||||
return CareRecordConvert.toVO(record);
|
||||
}
|
||||
|
||||
public CareRecord endFeeding(CreateFeedingRequest request) {
|
||||
CareRecord activeFeeding = findActiveFeeding(request.getBabyId());
|
||||
public CareRecordVO endFeeding(CreateFeedingRO request) {
|
||||
CareRecord activeFeeding = findActiveFeedingEntity(request.getBabyId());
|
||||
if (activeFeeding == null) {
|
||||
throw new BusinessException("NO_ACTIVE_FEEDING", "当前没有进行中的亲喂记录");
|
||||
}
|
||||
@@ -79,30 +76,26 @@ public class CareRecordService {
|
||||
throw new BusinessException("INVALID_FEEDING_TIME", "喂奶结束时间不能早于开始时间");
|
||||
}
|
||||
activeFeeding.setEndTime(end);
|
||||
activeFeeding.setNote(request.getNote() == null ? activeFeeding.getNote() : buildFeedingNote(request));
|
||||
if (request.getRemark() != null) {
|
||||
activeFeeding.setRemark(buildFeedingRemark(request));
|
||||
}
|
||||
activeFeeding.recalculateDuration();
|
||||
jdbcTemplate.update(
|
||||
"UPDATE care_record SET end_time = ?, duration_minutes = ?, note = ? WHERE id = ?",
|
||||
Timestamp.valueOf(activeFeeding.getEndTime()),
|
||||
activeFeeding.getDurationMinutes(),
|
||||
activeFeeding.getNote(),
|
||||
activeFeeding.getRecordId()
|
||||
);
|
||||
return activeFeeding;
|
||||
careRecordMapper.updateById(activeFeeding);
|
||||
return CareRecordConvert.toVO(activeFeeding);
|
||||
}
|
||||
|
||||
public CareRecord startSleep(SleepRequest request) {
|
||||
CareRecord activeSleep = findActiveSleep(request.getBabyId());
|
||||
public CareRecordVO startSleep(SleepRO request) {
|
||||
CareRecord activeSleep = findActiveSleepEntity(request.getBabyId());
|
||||
if (activeSleep != null) {
|
||||
throw new BusinessException("SLEEP_ALREADY_STARTED", "当前已有进行中的睡眠记录");
|
||||
}
|
||||
CareRecord record = baseRecord(request.getBabyId(), "sleep", request.getTime(), request.getOperatorName(), request.getNote());
|
||||
insert(record);
|
||||
return record;
|
||||
CareRecord record = baseRecord(request.getBabyId(), "sleep", request.getTime(), request.getOperatorName(), request.getRemark());
|
||||
careRecordMapper.insert(record);
|
||||
return CareRecordConvert.toVO(record);
|
||||
}
|
||||
|
||||
public CareRecord endSleep(SleepRequest request) {
|
||||
CareRecord activeSleep = findActiveSleep(request.getBabyId());
|
||||
public CareRecordVO endSleep(SleepRO request) {
|
||||
CareRecord activeSleep = findActiveSleepEntity(request.getBabyId());
|
||||
if (activeSleep == null) {
|
||||
throw new BusinessException("NO_ACTIVE_SLEEP", "当前没有进行中的睡眠记录");
|
||||
}
|
||||
@@ -111,72 +104,153 @@ public class CareRecordService {
|
||||
throw new BusinessException("INVALID_SLEEP_TIME", "睡眠结束时间不能早于开始时间");
|
||||
}
|
||||
activeSleep.setEndTime(end);
|
||||
activeSleep.setNote(request.getNote() == null ? activeSleep.getNote() : request.getNote());
|
||||
if (request.getRemark() != null) {
|
||||
activeSleep.setRemark(request.getRemark());
|
||||
}
|
||||
activeSleep.recalculateDuration();
|
||||
jdbcTemplate.update(
|
||||
"UPDATE care_record SET end_time = ?, duration_minutes = ?, note = ? WHERE id = ?",
|
||||
Timestamp.valueOf(activeSleep.getEndTime()),
|
||||
activeSleep.getDurationMinutes(),
|
||||
activeSleep.getNote(),
|
||||
activeSleep.getRecordId()
|
||||
);
|
||||
return activeSleep;
|
||||
careRecordMapper.updateById(activeSleep);
|
||||
return CareRecordConvert.toVO(activeSleep);
|
||||
}
|
||||
|
||||
public CareRecord createPee(ExcretionRequest request) {
|
||||
public CareRecordVO createPee(ExcretionRO request) {
|
||||
return createExcretion(request, "pee");
|
||||
}
|
||||
|
||||
public CareRecord createPoop(ExcretionRequest request) {
|
||||
public CareRecordVO createPoop(ExcretionRO request) {
|
||||
return createExcretion(request, "poop");
|
||||
}
|
||||
|
||||
public void delete(String recordId) {
|
||||
int affected = jdbcTemplate.update("UPDATE care_record SET deleted_at = NOW() WHERE id = ? AND deleted_at IS NULL", recordId);
|
||||
if (affected == 0) {
|
||||
public CareRecordVO createManual(CreateManualRecordRO request) {
|
||||
if (request.getEndTime() != null && request.getEndTime().isBefore(request.getStartTime())) {
|
||||
throw new BusinessException("INVALID_TIME", "结束时间不能早于开始时间");
|
||||
}
|
||||
CareRecord record = new CareRecord();
|
||||
record.setRecordId(idGenerator.nextId());
|
||||
record.setFamilyId(resolveFamilyId(request.getBabyId()));
|
||||
record.setBabyId(request.getBabyId());
|
||||
record.setRecordType(request.getRecordType());
|
||||
record.setStartTime(request.getStartTime());
|
||||
record.setEndTime(request.getEndTime());
|
||||
if (request.getDurationMinutes() != null) {
|
||||
record.setDurationMinutes(request.getDurationMinutes());
|
||||
} else if (record.getStartTime() != null && record.getEndTime() != null) {
|
||||
record.recalculateDuration();
|
||||
}
|
||||
record.setFeedingMethod(request.getFeedingMethod());
|
||||
record.setAmountMl(request.getAmountMl());
|
||||
record.setExcretionType(request.getExcretionType());
|
||||
record.setOperatorName(request.getOperatorName() == null || request.getOperatorName().trim().isEmpty() ? "宝妈" : request.getOperatorName().trim());
|
||||
record.setRemark(request.getRemark());
|
||||
record.setSource("h5");
|
||||
careRecordMapper.insert(record);
|
||||
return CareRecordConvert.toVO(record);
|
||||
}
|
||||
|
||||
public CareRecordVO update(UpdateCareRecordRO request) {
|
||||
String recordId = request.getRecordId();
|
||||
CareRecord record = careRecordMapper.selectById(recordId);
|
||||
if (record == null) {
|
||||
throw new BusinessException("RECORD_NOT_FOUND", "记录不存在");
|
||||
}
|
||||
|
||||
if (request.getStartTime() != null) {
|
||||
record.setStartTime(request.getStartTime());
|
||||
}
|
||||
if (request.getEndTime() != null) {
|
||||
if (record.getStartTime() != null && request.getEndTime().isBefore(record.getStartTime())) {
|
||||
throw new BusinessException("INVALID_TIME", "结束时间不能早于开始时间");
|
||||
}
|
||||
record.setEndTime(request.getEndTime());
|
||||
}
|
||||
if (request.getDurationMinutes() != null) {
|
||||
record.setDurationMinutes(request.getDurationMinutes());
|
||||
} else if (record.getStartTime() != null && record.getEndTime() != null) {
|
||||
record.recalculateDuration();
|
||||
}
|
||||
if (request.getRecordType() != null) {
|
||||
record.setRecordType(request.getRecordType());
|
||||
}
|
||||
if (request.getFeedingMethod() != null) {
|
||||
record.setFeedingMethod(request.getFeedingMethod());
|
||||
}
|
||||
if (request.getAmountMl() != null) {
|
||||
record.setAmountMl(request.getAmountMl());
|
||||
}
|
||||
if (request.getExcretionType() != null) {
|
||||
record.setExcretionType(request.getExcretionType());
|
||||
}
|
||||
if (request.getOperatorName() != null) {
|
||||
record.setOperatorName(request.getOperatorName().trim());
|
||||
}
|
||||
if (request.getRemark() != null) {
|
||||
record.setRemark(request.getRemark());
|
||||
}
|
||||
careRecordMapper.updateById(record);
|
||||
return CareRecordConvert.toVO(record);
|
||||
}
|
||||
|
||||
public CareRecord findActiveSleep(String babyId) {
|
||||
List<CareRecord> active = jdbcTemplate.query(
|
||||
"SELECT * FROM care_record WHERE baby_id = ? AND record_type = 'sleep' AND end_time IS NULL AND deleted_at IS NULL ORDER BY start_time DESC, id DESC LIMIT 1",
|
||||
(rs, rowNum) -> mapRecord(rs),
|
||||
babyId
|
||||
public void delete(String recordId) {
|
||||
CareRecord record = careRecordMapper.selectById(recordId);
|
||||
if (record == null) {
|
||||
throw new BusinessException("RECORD_NOT_FOUND", "记录不存在");
|
||||
}
|
||||
careRecordMapper.deleteById(recordId);
|
||||
}
|
||||
|
||||
public CareRecord findActiveSleepEntity(String babyId) {
|
||||
return careRecordMapper.selectOne(
|
||||
new LambdaQueryWrapper<CareRecord>()
|
||||
.eq(CareRecord::getBabyId, babyId)
|
||||
.eq(CareRecord::getRecordType, "sleep")
|
||||
.isNull(CareRecord::getEndTime)
|
||||
.orderByDesc(CareRecord::getStartTime)
|
||||
.orderByDesc(CareRecord::getRecordId)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
return active.isEmpty() ? null : active.get(0);
|
||||
}
|
||||
|
||||
public CareRecord findActiveFeeding(String babyId) {
|
||||
List<CareRecord> active = jdbcTemplate.query(
|
||||
"SELECT * FROM care_record WHERE baby_id = ? AND record_type = 'feeding' AND feeding_method = 'breastfeed' AND end_time IS NULL AND deleted_at IS NULL ORDER BY start_time DESC, id DESC LIMIT 1",
|
||||
(rs, rowNum) -> mapRecord(rs),
|
||||
babyId
|
||||
public CareRecordVO findActiveSleep(String babyId) {
|
||||
return CareRecordConvert.toVO(findActiveSleepEntity(babyId));
|
||||
}
|
||||
|
||||
public CareRecord findActiveFeedingEntity(String babyId) {
|
||||
return careRecordMapper.selectOne(
|
||||
new LambdaQueryWrapper<CareRecord>()
|
||||
.eq(CareRecord::getBabyId, babyId)
|
||||
.eq(CareRecord::getRecordType, "feeding")
|
||||
.eq(CareRecord::getFeedingMethod, "breastfeed")
|
||||
.isNull(CareRecord::getEndTime)
|
||||
.orderByDesc(CareRecord::getStartTime)
|
||||
.orderByDesc(CareRecord::getRecordId)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
return active.isEmpty() ? null : active.get(0);
|
||||
}
|
||||
|
||||
private String buildFeedingNote(CreateFeedingRequest request) {
|
||||
public CareRecordVO findActiveFeeding(String babyId) {
|
||||
return CareRecordConvert.toVO(findActiveFeedingEntity(babyId));
|
||||
}
|
||||
|
||||
private String buildFeedingRemark(CreateFeedingRO request) {
|
||||
String side = request.getBreastSide();
|
||||
String note = request.getNote();
|
||||
String remark = request.getRemark();
|
||||
if (side == null || side.trim().isEmpty()) {
|
||||
return note;
|
||||
return remark;
|
||||
}
|
||||
String sideText = "left".equals(side) ? "左侧" : "right".equals(side) ? "右侧" : side.trim();
|
||||
if (note == null || note.trim().isEmpty()) {
|
||||
if (remark == null || remark.trim().isEmpty()) {
|
||||
return "亲喂侧: " + sideText;
|
||||
}
|
||||
return "亲喂侧: " + sideText + ";" + note.trim();
|
||||
return "亲喂侧: " + sideText + ";" + remark.trim();
|
||||
}
|
||||
|
||||
private CareRecord createExcretion(ExcretionRequest request, String excretionType) {
|
||||
CareRecord record = baseRecord(request.getBabyId(), "excretion", request.getTime(), request.getOperatorName(), request.getNote());
|
||||
private CareRecordVO createExcretion(ExcretionRO request, String excretionType) {
|
||||
CareRecord record = baseRecord(request.getBabyId(), "excretion", request.getTime(), request.getOperatorName(), request.getRemark());
|
||||
record.setExcretionType(excretionType);
|
||||
insert(record);
|
||||
return record;
|
||||
careRecordMapper.insert(record);
|
||||
return CareRecordConvert.toVO(record);
|
||||
}
|
||||
|
||||
private CareRecord baseRecord(String babyId, String type, LocalDateTime time, String operatorName, String note) {
|
||||
private CareRecord baseRecord(String babyId, String type, LocalDateTime time, String operatorName, String remark) {
|
||||
CareRecord record = new CareRecord();
|
||||
record.setRecordId(idGenerator.nextId());
|
||||
record.setFamilyId(resolveFamilyId(babyId));
|
||||
@@ -184,62 +258,22 @@ public class CareRecordService {
|
||||
record.setRecordType(type);
|
||||
record.setStartTime(time == null ? LocalDateTime.now() : time);
|
||||
record.setOperatorName(operatorName == null || operatorName.trim().isEmpty() ? "宝妈" : operatorName.trim());
|
||||
record.setNote(note);
|
||||
record.setCreatedAt(LocalDateTime.now());
|
||||
record.setRemark(remark);
|
||||
record.setSource("h5");
|
||||
return record;
|
||||
}
|
||||
|
||||
private String resolveFamilyId(String babyId) {
|
||||
List<String> familyIds = jdbcTemplate.query(
|
||||
"SELECT family_id FROM baby_profile WHERE id = ? AND status = 'active' LIMIT 1",
|
||||
(rs, rowNum) -> rs.getString("family_id"),
|
||||
babyId
|
||||
BabyProfile baby = babyMapper.selectOne(
|
||||
new LambdaQueryWrapper<BabyProfile>()
|
||||
.select(BabyProfile::getFamilyId)
|
||||
.eq(BabyProfile::getBabyId, babyId)
|
||||
.eq(BabyProfile::getStatus, "active")
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
if (familyIds.isEmpty()) {
|
||||
if (baby == null || baby.getFamilyId() == null) {
|
||||
throw new BusinessException("BABY_NOT_FOUND", "宝宝档案不存在");
|
||||
}
|
||||
return familyIds.get(0);
|
||||
}
|
||||
|
||||
private void insert(CareRecord record) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO care_record (id, family_id, baby_id, record_type, start_time, end_time, duration_minutes, feeding_method, amount_ml, excretion_type, operator_name, note, source, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'h5', ?)",
|
||||
record.getRecordId(),
|
||||
record.getFamilyId(),
|
||||
record.getBabyId(),
|
||||
record.getRecordType(),
|
||||
Timestamp.valueOf(record.getStartTime()),
|
||||
record.getEndTime() == null ? null : Timestamp.valueOf(record.getEndTime()),
|
||||
record.getDurationMinutes(),
|
||||
record.getFeedingMethod(),
|
||||
record.getAmountMl(),
|
||||
record.getExcretionType(),
|
||||
record.getOperatorName(),
|
||||
record.getNote(),
|
||||
Timestamp.valueOf(record.getCreatedAt())
|
||||
);
|
||||
}
|
||||
|
||||
private CareRecord mapRecord(ResultSet rs) throws SQLException {
|
||||
CareRecord record = new CareRecord();
|
||||
record.setRecordId(rs.getString("id"));
|
||||
record.setFamilyId(rs.getString("family_id"));
|
||||
record.setBabyId(rs.getString("baby_id"));
|
||||
record.setRecordType(rs.getString("record_type"));
|
||||
record.setStartTime(rs.getTimestamp("start_time").toLocalDateTime());
|
||||
Timestamp endTime = rs.getTimestamp("end_time");
|
||||
record.setEndTime(endTime == null ? null : endTime.toLocalDateTime());
|
||||
record.setDurationMinutes((Integer) rs.getObject("duration_minutes"));
|
||||
record.setFeedingMethod(rs.getString("feeding_method"));
|
||||
record.setAmountMl((Integer) rs.getObject("amount_ml"));
|
||||
record.setExcretionType(rs.getString("excretion_type"));
|
||||
record.setOperatorName(rs.getString("operator_name"));
|
||||
record.setNote(rs.getString("note"));
|
||||
Timestamp createdAt = rs.getTimestamp("created_at");
|
||||
record.setCreatedAt(createdAt == null ? record.getStartTime() : createdAt.toLocalDateTime());
|
||||
if ("sleep".equals(record.getRecordType()) && record.getEndTime() != null && record.getDurationMinutes() == null) {
|
||||
record.setDurationMinutes((int) Math.max(0, Duration.between(record.getStartTime(), record.getEndTime()).toMinutes()));
|
||||
}
|
||||
return record;
|
||||
return baby.getFamilyId();
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.babytracker.module.record;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class CreateFeedingRequest {
|
||||
@NotBlank
|
||||
private String babyId;
|
||||
@NotBlank
|
||||
private String feedingMethod;
|
||||
private LocalDateTime feedingTime;
|
||||
private LocalDateTime endTime;
|
||||
private Integer amountMl;
|
||||
private String breastSide;
|
||||
private String operatorName;
|
||||
private String note;
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public String getFeedingMethod() {
|
||||
return feedingMethod;
|
||||
}
|
||||
|
||||
public void setFeedingMethod(String feedingMethod) {
|
||||
this.feedingMethod = feedingMethod;
|
||||
}
|
||||
|
||||
public LocalDateTime getFeedingTime() {
|
||||
return feedingTime;
|
||||
}
|
||||
|
||||
public void setFeedingTime(LocalDateTime feedingTime) {
|
||||
this.feedingTime = feedingTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(LocalDateTime endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public Integer getAmountMl() {
|
||||
return amountMl;
|
||||
}
|
||||
|
||||
public void setAmountMl(Integer amountMl) {
|
||||
this.amountMl = amountMl;
|
||||
}
|
||||
|
||||
public String getBreastSide() {
|
||||
return breastSide;
|
||||
}
|
||||
|
||||
public void setBreastSide(String breastSide) {
|
||||
this.breastSide = breastSide;
|
||||
}
|
||||
|
||||
public String getOperatorName() {
|
||||
return operatorName;
|
||||
}
|
||||
|
||||
public void setOperatorName(String operatorName) {
|
||||
this.operatorName = operatorName;
|
||||
}
|
||||
|
||||
public String getNote() {
|
||||
return note;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.note = note;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.babytracker.module.record;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class ExcretionRequest {
|
||||
@NotBlank
|
||||
private String babyId;
|
||||
private LocalDateTime time;
|
||||
private String operatorName;
|
||||
private String note;
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public LocalDateTime getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(LocalDateTime time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getOperatorName() {
|
||||
return operatorName;
|
||||
}
|
||||
|
||||
public void setOperatorName(String operatorName) {
|
||||
this.operatorName = operatorName;
|
||||
}
|
||||
|
||||
public String getNote() {
|
||||
return note;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.note = note;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.babytracker.module.record;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class SleepRequest {
|
||||
@NotBlank
|
||||
private String babyId;
|
||||
private LocalDateTime time;
|
||||
private String operatorName;
|
||||
private String note;
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public LocalDateTime getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(LocalDateTime time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getOperatorName() {
|
||||
return operatorName;
|
||||
}
|
||||
|
||||
public void setOperatorName(String operatorName) {
|
||||
this.operatorName = operatorName;
|
||||
}
|
||||
|
||||
public String getNote() {
|
||||
return note;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.note = note;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.babytracker.module.record.dto;
|
||||
|
||||
import com.babytracker.module.record.CareRecord;
|
||||
import com.babytracker.module.record.vo.CareRecordVO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class CareRecordConvert {
|
||||
|
||||
private CareRecordConvert() {}
|
||||
|
||||
public static CareRecordVO toVO(CareRecord entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
CareRecordVO vo = new CareRecordVO();
|
||||
vo.setRecordId(entity.getRecordId());
|
||||
vo.setFamilyId(entity.getFamilyId());
|
||||
vo.setBabyId(entity.getBabyId());
|
||||
vo.setRecordType(entity.getRecordType());
|
||||
vo.setStartTime(entity.getStartTime());
|
||||
vo.setEndTime(entity.getEndTime());
|
||||
vo.setDurationMinutes(entity.getDurationMinutes());
|
||||
vo.setFeedingMethod(entity.getFeedingMethod());
|
||||
vo.setAmountMl(entity.getAmountMl());
|
||||
vo.setExcretionType(entity.getExcretionType());
|
||||
vo.setOperatorName(entity.getOperatorName());
|
||||
vo.setOperatorUserId(entity.getOperatorUserId());
|
||||
vo.setRemark(entity.getRemark());
|
||||
vo.setNote(entity.getRemark());
|
||||
vo.setSource(entity.getSource());
|
||||
vo.setDeleted(entity.getDeleted());
|
||||
vo.setCreatedBy(entity.getCreatedBy());
|
||||
vo.setCreatedTime(entity.getCreatedTime());
|
||||
vo.setUpdatedBy(entity.getUpdatedBy());
|
||||
vo.setUpdatedTime(entity.getUpdatedTime());
|
||||
return vo;
|
||||
}
|
||||
|
||||
public static List<CareRecordVO> toVOList(List<CareRecord> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<CareRecordVO> voList = new ArrayList<>(list.size());
|
||||
for (CareRecord entity : list) {
|
||||
voList.add(toVO(entity));
|
||||
}
|
||||
return voList;
|
||||
}
|
||||
|
||||
public static CareRecordDTO toDTO(CareRecord entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
CareRecordDTO dto = new CareRecordDTO();
|
||||
dto.setRecordId(entity.getRecordId());
|
||||
dto.setFamilyId(entity.getFamilyId());
|
||||
dto.setBabyId(entity.getBabyId());
|
||||
dto.setRecordType(entity.getRecordType());
|
||||
dto.setStartTime(entity.getStartTime());
|
||||
dto.setEndTime(entity.getEndTime());
|
||||
dto.setDurationMinutes(entity.getDurationMinutes());
|
||||
dto.setFeedingMethod(entity.getFeedingMethod());
|
||||
dto.setAmountMl(entity.getAmountMl());
|
||||
dto.setExcretionType(entity.getExcretionType());
|
||||
dto.setOperatorName(entity.getOperatorName());
|
||||
dto.setOperatorUserId(entity.getOperatorUserId());
|
||||
dto.setRemark(entity.getRemark());
|
||||
dto.setSource(entity.getSource());
|
||||
dto.setDeleted(entity.getDeleted());
|
||||
dto.setCreatedBy(entity.getCreatedBy());
|
||||
dto.setCreatedTime(entity.getCreatedTime());
|
||||
dto.setUpdatedBy(entity.getUpdatedBy());
|
||||
dto.setUpdatedTime(entity.getUpdatedTime());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.babytracker.module.record.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class CareRecordDTO {
|
||||
private String recordId;
|
||||
private String familyId;
|
||||
private String babyId;
|
||||
private String recordType;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private Integer durationMinutes;
|
||||
private String feedingMethod;
|
||||
private Integer amountMl;
|
||||
private String excretionType;
|
||||
private String operatorName;
|
||||
private String operatorUserId;
|
||||
private String remark;
|
||||
private String source;
|
||||
private Integer deleted;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.babytracker.module.record.mapper;
|
||||
|
||||
import com.babytracker.module.record.CareRecord;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface CareRecordMapper extends BaseMapper<CareRecord> {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.babytracker.module.record.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class CreateFeedingRO {
|
||||
@NotBlank(message = "宝宝ID不能为空")
|
||||
private String babyId;
|
||||
|
||||
@NotBlank(message = "喂养方式不能为空")
|
||||
private String feedingMethod;
|
||||
|
||||
private LocalDateTime feedingTime;
|
||||
private LocalDateTime endTime;
|
||||
private Integer amountMl;
|
||||
private String breastSide;
|
||||
private String operatorName;
|
||||
private String remark;
|
||||
|
||||
public String getNote() {
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.remark = note;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.babytracker.module.record.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class CreateManualRecordRO {
|
||||
@NotBlank(message = "宝宝ID不能为空")
|
||||
private String babyId;
|
||||
|
||||
@NotBlank(message = "记录类型不能为空")
|
||||
private String recordType;
|
||||
|
||||
@NotNull(message = "开始时间不能为空")
|
||||
private LocalDateTime startTime;
|
||||
|
||||
private LocalDateTime endTime;
|
||||
private Integer durationMinutes;
|
||||
private String feedingMethod;
|
||||
private Integer amountMl;
|
||||
private String excretionType;
|
||||
private String operatorName;
|
||||
private String remark;
|
||||
|
||||
public String getNote() {
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.remark = note;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.babytracker.module.record.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DeleteCareRecordRO {
|
||||
@NotBlank(message = "记录ID不能为空")
|
||||
private String recordId;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.babytracker.module.record.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class ExcretionRO {
|
||||
@NotBlank(message = "宝宝ID不能为空")
|
||||
private String babyId;
|
||||
|
||||
private LocalDateTime time;
|
||||
private String operatorName;
|
||||
private String remark;
|
||||
|
||||
public String getNote() {
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.remark = note;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.babytracker.module.record.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class SleepRO {
|
||||
@NotBlank(message = "宝宝ID不能为空")
|
||||
private String babyId;
|
||||
|
||||
private LocalDateTime time;
|
||||
private String operatorName;
|
||||
private String remark;
|
||||
|
||||
public String getNote() {
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.remark = note;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.babytracker.module.record.ro;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class UpdateCareRecordRO {
|
||||
@NotBlank(message = "记录ID不能为空")
|
||||
private String recordId;
|
||||
|
||||
private String recordType;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private Integer durationMinutes;
|
||||
private String feedingMethod;
|
||||
private Integer amountMl;
|
||||
private String excretionType;
|
||||
private String operatorName;
|
||||
private String remark;
|
||||
|
||||
public String getNote() {
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setNote(String note) {
|
||||
this.remark = note;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.babytracker.module.record.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class CareRecordVO {
|
||||
private String recordId;
|
||||
private String familyId;
|
||||
private String babyId;
|
||||
private String recordType;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private Integer durationMinutes;
|
||||
private String feedingMethod;
|
||||
private Integer amountMl;
|
||||
private String excretionType;
|
||||
private String operatorName;
|
||||
private String operatorUserId;
|
||||
private String remark;
|
||||
private String note;
|
||||
private String source;
|
||||
private Integer deleted;
|
||||
private String createdBy;
|
||||
private LocalDateTime createdTime;
|
||||
private String updatedBy;
|
||||
private LocalDateTime updatedTime;
|
||||
|
||||
public String getNote() {
|
||||
return remark != null ? remark : note;
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package com.babytracker.module.summary;
|
||||
|
||||
import com.babytracker.module.record.CareRecord;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DailySummary {
|
||||
private String babyId;
|
||||
private String date;
|
||||
private int feedingCount;
|
||||
private Map<String, Integer> feedingMethodCounts;
|
||||
private int sleepTotalMinutes;
|
||||
private int peeCount;
|
||||
private int poopCount;
|
||||
private CareRecord lastFeeding;
|
||||
private CareRecord activeFeeding;
|
||||
private CareRecord activeSleep;
|
||||
private List<CareRecord> timeline;
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public int getFeedingCount() {
|
||||
return feedingCount;
|
||||
}
|
||||
|
||||
public void setFeedingCount(int feedingCount) {
|
||||
this.feedingCount = feedingCount;
|
||||
}
|
||||
|
||||
public Map<String, Integer> getFeedingMethodCounts() {
|
||||
return feedingMethodCounts;
|
||||
}
|
||||
|
||||
public void setFeedingMethodCounts(Map<String, Integer> feedingMethodCounts) {
|
||||
this.feedingMethodCounts = feedingMethodCounts;
|
||||
}
|
||||
|
||||
public int getSleepTotalMinutes() {
|
||||
return sleepTotalMinutes;
|
||||
}
|
||||
|
||||
public void setSleepTotalMinutes(int sleepTotalMinutes) {
|
||||
this.sleepTotalMinutes = sleepTotalMinutes;
|
||||
}
|
||||
|
||||
public int getPeeCount() {
|
||||
return peeCount;
|
||||
}
|
||||
|
||||
public void setPeeCount(int peeCount) {
|
||||
this.peeCount = peeCount;
|
||||
}
|
||||
|
||||
public int getPoopCount() {
|
||||
return poopCount;
|
||||
}
|
||||
|
||||
public void setPoopCount(int poopCount) {
|
||||
this.poopCount = poopCount;
|
||||
}
|
||||
|
||||
public CareRecord getLastFeeding() {
|
||||
return lastFeeding;
|
||||
}
|
||||
|
||||
public void setLastFeeding(CareRecord lastFeeding) {
|
||||
this.lastFeeding = lastFeeding;
|
||||
}
|
||||
|
||||
public CareRecord getActiveSleep() {
|
||||
return activeSleep;
|
||||
}
|
||||
|
||||
public void setActiveSleep(CareRecord activeSleep) {
|
||||
this.activeSleep = activeSleep;
|
||||
}
|
||||
|
||||
public CareRecord getActiveFeeding() {
|
||||
return activeFeeding;
|
||||
}
|
||||
|
||||
public void setActiveFeeding(CareRecord activeFeeding) {
|
||||
this.activeFeeding = activeFeeding;
|
||||
}
|
||||
|
||||
public List<CareRecord> getTimeline() {
|
||||
return timeline;
|
||||
}
|
||||
|
||||
public void setTimeline(List<CareRecord> timeline) {
|
||||
this.timeline = timeline;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,39 @@
|
||||
package com.babytracker.module.summary;
|
||||
|
||||
import com.babytracker.common.response.ApiResponse;
|
||||
import com.babytracker.module.summary.ro.SummaryQueryRO;
|
||||
import com.babytracker.module.summary.vo.DailySummaryVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/v1/summary")
|
||||
public class SummaryController {
|
||||
private final SummaryService summaryService;
|
||||
|
||||
public SummaryController(SummaryService summaryService) {
|
||||
this.summaryService = summaryService;
|
||||
}
|
||||
|
||||
@GetMapping("/daily")
|
||||
public ApiResponse<DailySummary> daily(
|
||||
@RequestParam String babyId,
|
||||
public ApiResponse<DailySummaryVO> daily(
|
||||
@RequestParam(required = false) String babyId,
|
||||
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date
|
||||
) {
|
||||
return ApiResponse.ok(summaryService.daily(babyId, date));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/daily")
|
||||
public ApiResponse<DailySummaryVO> dailyByPost(@RequestBody(required = false) SummaryQueryRO request) {
|
||||
String babyId = request != null ? request.getBabyId() : null;
|
||||
LocalDate date = request != null ? request.getDate() : null;
|
||||
return ApiResponse.ok(summaryService.daily(babyId, date));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.babytracker.module.summary;
|
||||
|
||||
import com.babytracker.module.record.CareRecord;
|
||||
import com.babytracker.module.record.CareRecordService;
|
||||
import com.babytracker.module.record.vo.CareRecordVO;
|
||||
import com.babytracker.module.summary.vo.DailySummaryVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@@ -10,31 +12,28 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SummaryService {
|
||||
private final CareRecordService careRecordService;
|
||||
|
||||
public SummaryService(CareRecordService careRecordService) {
|
||||
this.careRecordService = careRecordService;
|
||||
}
|
||||
|
||||
public DailySummary daily(String babyId, LocalDate date) {
|
||||
public DailySummaryVO daily(String babyId, LocalDate date) {
|
||||
LocalDate targetDate = date == null ? LocalDate.now() : date;
|
||||
List<CareRecord> timeline = careRecordService.list(babyId, targetDate);
|
||||
DailySummary summary = new DailySummary();
|
||||
List<CareRecordVO> timeline = careRecordService.list(babyId, targetDate);
|
||||
DailySummaryVO summary = new DailySummaryVO();
|
||||
summary.setBabyId(babyId);
|
||||
summary.setDate(targetDate.toString());
|
||||
summary.setTimeline(timeline);
|
||||
summary.setActiveSleep(careRecordService.findActiveSleep(babyId));
|
||||
summary.setActiveFeeding(careRecordService.findActiveFeeding(babyId));
|
||||
|
||||
Map<String, Integer> methodCounts = new LinkedHashMap<String, Integer>();
|
||||
Map<String, Integer> methodCounts = new LinkedHashMap<>();
|
||||
int feedingCount = 0;
|
||||
int sleepTotal = 0;
|
||||
int peeCount = 0;
|
||||
int poopCount = 0;
|
||||
CareRecord lastFeeding = null;
|
||||
CareRecordVO lastFeeding = null;
|
||||
|
||||
for (CareRecord record : timeline) {
|
||||
for (CareRecordVO record : timeline) {
|
||||
if ("feeding".equals(record.getRecordType())) {
|
||||
feedingCount++;
|
||||
String method = record.getFeedingMethod() == null ? "other" : record.getFeedingMethod();
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.babytracker.module.summary.dto;
|
||||
|
||||
import com.babytracker.module.record.dto.CareRecordDTO;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class DailySummaryDTO {
|
||||
private String babyId;
|
||||
private String date;
|
||||
private int feedingCount;
|
||||
private Map<String, Integer> feedingMethodCounts;
|
||||
private int sleepTotalMinutes;
|
||||
private int peeCount;
|
||||
private int poopCount;
|
||||
private CareRecordDTO lastFeeding;
|
||||
private CareRecordDTO activeFeeding;
|
||||
private CareRecordDTO activeSleep;
|
||||
private List<CareRecordDTO> timeline;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.babytracker.module.summary.dto;
|
||||
|
||||
import com.babytracker.module.summary.vo.DailySummaryVO;
|
||||
|
||||
public final class SummaryConvert {
|
||||
|
||||
private SummaryConvert() {}
|
||||
|
||||
public static DailySummaryVO toVO(DailySummaryDTO dto) {
|
||||
if (dto == null) {
|
||||
return null;
|
||||
}
|
||||
DailySummaryVO vo = new DailySummaryVO();
|
||||
vo.setBabyId(dto.getBabyId());
|
||||
vo.setDate(dto.getDate());
|
||||
vo.setFeedingCount(dto.getFeedingCount());
|
||||
vo.setFeedingMethodCounts(dto.getFeedingMethodCounts());
|
||||
vo.setSleepTotalMinutes(dto.getSleepTotalMinutes());
|
||||
vo.setPeeCount(dto.getPeeCount());
|
||||
vo.setPoopCount(dto.getPoopCount());
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.babytracker.module.summary.ro;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
public class SummaryQueryRO {
|
||||
private String babyId;
|
||||
|
||||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
|
||||
private LocalDate date;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.babytracker.module.summary.vo;
|
||||
|
||||
import com.babytracker.module.record.vo.CareRecordVO;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class DailySummaryVO {
|
||||
private String babyId;
|
||||
private String date;
|
||||
private int feedingCount;
|
||||
private Map<String, Integer> feedingMethodCounts;
|
||||
private int sleepTotalMinutes;
|
||||
private int peeCount;
|
||||
private int poopCount;
|
||||
private CareRecordVO lastFeeding;
|
||||
private CareRecordVO activeFeeding;
|
||||
private CareRecordVO activeSleep;
|
||||
private List<CareRecordVO> timeline;
|
||||
}
|
||||
@@ -22,6 +22,17 @@ spring:
|
||||
time-zone: Asia/Shanghai
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: input
|
||||
logic-delete-field: deleted
|
||||
logic-delete-value: 1
|
||||
logic-not-delete-value: 0
|
||||
|
||||
app:
|
||||
cors:
|
||||
allowed-origins:
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
SET NAMES utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `app_user` (
|
||||
`id` CHAR(32) NOT NULL COMMENT '用户ID,雪花ID字符串',
|
||||
`openid` VARCHAR(128) NULL COMMENT '微信小程序 openid',
|
||||
`unionid` VARCHAR(128) NULL COMMENT '微信 unionid',
|
||||
`phone` VARCHAR(32) NULL COMMENT '手机号',
|
||||
`nickname` VARCHAR(50) NOT NULL DEFAULT '看护人' COMMENT '用户昵称',
|
||||
`avatar_url` VARCHAR(500) NULL COMMENT '头像地址',
|
||||
`status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '状态: active, disabled',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_app_user_openid` (`openid`),
|
||||
UNIQUE KEY `uk_app_user_phone` (`phone`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `family` (
|
||||
`id` CHAR(32) NOT NULL COMMENT '家庭ID,雪花ID字符串',
|
||||
`name` VARCHAR(100) NOT NULL COMMENT '家庭名称',
|
||||
`invite_code` VARCHAR(32) NULL COMMENT '家庭邀请码',
|
||||
`creator_user_id` CHAR(32) NULL COMMENT '创建人用户ID',
|
||||
`status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '状态: active, archived',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_family_invite_code` (`invite_code`),
|
||||
KEY `idx_family_creator` (`creator_user_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='家庭表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `family_member` (
|
||||
`id` CHAR(32) NOT NULL COMMENT '成员ID,雪花ID字符串',
|
||||
`family_id` CHAR(32) NOT NULL COMMENT '家庭ID',
|
||||
`user_id` CHAR(32) NULL COMMENT '用户ID',
|
||||
`role_name` VARCHAR(50) NOT NULL COMMENT '看护人称谓',
|
||||
`role_type` VARCHAR(30) NOT NULL DEFAULT 'caregiver' COMMENT '角色类型: owner, caregiver, viewer',
|
||||
`is_default_operator` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否默认记录人',
|
||||
`status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '状态: active, left, disabled',
|
||||
`joined_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_family_member_family` (`family_id`),
|
||||
KEY `idx_family_member_user` (`user_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='家庭成员表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `baby_profile` (
|
||||
`id` CHAR(32) NOT NULL COMMENT '宝宝ID,雪花ID字符串',
|
||||
`family_id` CHAR(32) NOT NULL COMMENT '家庭ID',
|
||||
`name` VARCHAR(50) NOT NULL COMMENT '宝宝昵称',
|
||||
`gender` VARCHAR(20) NOT NULL DEFAULT 'boy' COMMENT '性别: boy, girl, unknown',
|
||||
`birth_date` DATE NOT NULL COMMENT '出生日期',
|
||||
`delivery_type` VARCHAR(20) NOT NULL DEFAULT 'natural' COMMENT '分娩方式: natural, c_section, unknown',
|
||||
`is_default` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否默认宝宝',
|
||||
`status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '状态: active, archived',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_baby_profile_family` (`family_id`),
|
||||
KEY `idx_baby_profile_default` (`family_id`, `is_default`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='宝宝档案表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `care_record` (
|
||||
`id` CHAR(32) NOT NULL COMMENT '记录ID,雪花ID字符串',
|
||||
`family_id` CHAR(32) NOT NULL COMMENT '家庭ID',
|
||||
`baby_id` CHAR(32) NOT NULL COMMENT '宝宝ID',
|
||||
`record_type` VARCHAR(30) NOT NULL COMMENT '记录类型: feeding, sleep, excretion',
|
||||
`start_time` DATETIME NOT NULL COMMENT '开始/发生时间',
|
||||
`end_time` DATETIME NULL COMMENT '结束时间',
|
||||
`duration_minutes` INT NULL COMMENT '持续分钟数',
|
||||
`feeding_method` VARCHAR(30) NULL COMMENT '喂奶方式',
|
||||
`amount_ml` INT NULL COMMENT '奶量 ml,可选',
|
||||
`excretion_type` VARCHAR(20) NULL COMMENT '排泄类型: pee, poop',
|
||||
`operator_name` VARCHAR(50) NOT NULL DEFAULT '看护人' COMMENT '记录人称谓',
|
||||
`operator_user_id` CHAR(32) NULL COMMENT '记录人用户ID',
|
||||
`note` VARCHAR(500) NULL COMMENT '备注',
|
||||
`source` VARCHAR(30) NOT NULL DEFAULT 'h5' COMMENT '来源: h5, miniapp, admin, system',
|
||||
`deleted_at` DATETIME NULL COMMENT '软删除时间',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_care_record_baby_time` (`baby_id`, `start_time`),
|
||||
KEY `idx_care_record_family_time` (`family_id`, `start_time`),
|
||||
KEY `idx_care_record_type_time` (`record_type`, `start_time`),
|
||||
KEY `idx_care_record_deleted` (`deleted_at`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='照护记录表';
|
||||
|
||||
INSERT IGNORE INTO `app_user` (`id`, `nickname`, `status`)
|
||||
VALUES ('00000000000000361915955435016192', '默认看护人', 'active');
|
||||
|
||||
INSERT IGNORE INTO `family` (`id`, `name`, `invite_code`, `creator_user_id`, `status`)
|
||||
VALUES ('00000000000000361915955439210496', '新生儿照护家庭', 'BT2026', '00000000000000361915955435016192', 'active');
|
||||
|
||||
INSERT IGNORE INTO `family_member` (`id`, `family_id`, `user_id`, `role_name`, `role_type`, `is_default_operator`, `status`)
|
||||
VALUES
|
||||
('00000000000000361915955443404800', '00000000000000361915955439210496', '00000000000000361915955435016192', '宝妈', 'owner', 1, 'active'),
|
||||
('00000000000000361915955451793408', '00000000000000361915955439210496', NULL, '宝爸', 'caregiver', 0, 'active');
|
||||
|
||||
INSERT IGNORE INTO `baby_profile` (`id`, `family_id`, `name`, `gender`, `birth_date`, `delivery_type`, `is_default`, `status`)
|
||||
VALUES ('00000000000000361915955447599104', '00000000000000361915955439210496', '宝宝', 'boy', DATE_SUB(CURDATE(), INTERVAL 3 DAY), 'natural', 1, 'active');
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.babytracker;
|
||||
|
||||
import com.babytracker.module.baby.BabyService;
|
||||
import com.babytracker.module.baby.vo.BabyVO;
|
||||
import com.babytracker.module.family.FamilyService;
|
||||
import com.babytracker.module.family.vo.FamilyContextVO;
|
||||
import com.babytracker.module.record.CareRecordService;
|
||||
import com.babytracker.module.record.ro.CreateFeedingRO;
|
||||
import com.babytracker.module.record.vo.CareRecordVO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
class BabyTrackerApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private BabyService babyService;
|
||||
|
||||
@Autowired
|
||||
private FamilyService familyService;
|
||||
|
||||
@Autowired
|
||||
private CareRecordService careRecordService;
|
||||
|
||||
@Test
|
||||
void testMyBatisPlusServicesAndAuditFields() {
|
||||
FamilyContextVO context = familyService.getCurrentContext();
|
||||
assertNotNull(context);
|
||||
assertNotNull(context.getFamily());
|
||||
assertNotNull(context.getMembers());
|
||||
|
||||
BabyVO currentBaby = babyService.getCurrentBaby();
|
||||
assertNotNull(currentBaby);
|
||||
assertNotNull(currentBaby.getBabyId());
|
||||
|
||||
List<CareRecordVO> records = careRecordService.list(currentBaby.getBabyId(), LocalDate.now());
|
||||
assertNotNull(records);
|
||||
|
||||
// 测试自动填充拦截器:创建一条记录并验证审计字段
|
||||
CreateFeedingRO feedingRO = new CreateFeedingRO();
|
||||
feedingRO.setBabyId(currentBaby.getBabyId());
|
||||
feedingRO.setFeedingMethod("bottle");
|
||||
feedingRO.setFeedingTime(LocalDateTime.now());
|
||||
feedingRO.setAmountMl(60);
|
||||
feedingRO.setRemark("自动化测试喂养");
|
||||
CareRecordVO created = careRecordService.createFeeding(feedingRO);
|
||||
|
||||
assertNotNull(created);
|
||||
assertNotNull(created.getRecordId());
|
||||
assertEquals("admin", created.getCreatedBy());
|
||||
assertEquals("admin", created.getUpdatedBy());
|
||||
assertNotNull(created.getCreatedTime());
|
||||
assertNotNull(created.getUpdatedTime());
|
||||
assertEquals("自动化测试喂养", created.getRemark());
|
||||
assertEquals(0, created.getDeleted());
|
||||
|
||||
// 测试逻辑删除
|
||||
careRecordService.delete(created.getRecordId());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user