feat:增加健康模块睡眠时间存储
This commit is contained in:
@@ -9,6 +9,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -21,12 +22,23 @@ public class HealthRecordDto {
|
||||
@Schema(description = "运行项目")
|
||||
private String sportProject;
|
||||
|
||||
@Schema(description = "运行时长")
|
||||
@Schema(description = "运动时长")
|
||||
private Integer sportDuration;
|
||||
|
||||
@Schema(description = "体重")
|
||||
private Double weight;
|
||||
|
||||
@Schema(description = "睡眠开始时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime sleepStartTime;
|
||||
|
||||
@Schema(description = "睡眠结束时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime sleepEndTime;
|
||||
|
||||
@Schema(description = "睡眠时长")
|
||||
private Double sleepDuration;
|
||||
|
||||
@Schema(description = "记录日期")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
|
||||
private LocalDate date;
|
||||
|
||||
@@ -7,6 +7,7 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@@ -21,6 +22,15 @@ public class HealthRecord extends AbstractEntity {
|
||||
@TableField("weight")
|
||||
private Double weight;
|
||||
|
||||
@TableField("sleep_start_time")
|
||||
private LocalDateTime sleepStartTime;
|
||||
|
||||
@TableField("sleep_end_time")
|
||||
private LocalDateTime sleepEndTime;
|
||||
|
||||
@TableField("sleep_duration")
|
||||
private Double sleepDuration;
|
||||
|
||||
@TableField("date")
|
||||
private LocalDate date;
|
||||
}
|
||||
|
||||
@@ -9,10 +9,11 @@ import com.cxx.home.mapper.HealthRecordMapper;
|
||||
import com.cxx.home.service.HealthService;
|
||||
import com.cxx.home.vo.HealthQueryVo;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -38,20 +39,28 @@ public class HealthServiceImpl implements HealthService {
|
||||
|
||||
@Override
|
||||
public Boolean update(Long id, HealthRecordDto dto) {
|
||||
HealthRecord healthRecord = new HealthRecord();
|
||||
|
||||
if (id == 0) {
|
||||
BeanUtils.copyProperties(dto, healthRecord);
|
||||
return healthRecordMapper.insert(healthRecord) == 1;
|
||||
setSleepDuration(dto);
|
||||
return healthRecordMapper.insert(healthConverter.toEntity(dto)) == 1;
|
||||
} else {
|
||||
if ((dto.getSportProject() == null || dto.getSportProject().isEmpty())
|
||||
&& dto.getSleepStartTime() == null && dto.getSleepEndTime() == null
|
||||
&& dto.getSportDuration() == 0 && dto.getWeight() == 0) {
|
||||
return healthRecordMapper.deleteById(id) == 1;
|
||||
} else {
|
||||
BeanUtils.copyProperties(dto, healthRecord);
|
||||
setSleepDuration(dto);
|
||||
HealthRecord healthRecord = healthConverter.toEntity(dto);
|
||||
healthRecord.setId(id);
|
||||
return healthRecordMapper.updateById(healthRecord) == 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setSleepDuration(HealthRecordDto dto) {
|
||||
LocalDateTime startTime = dto.getSleepStartTime();
|
||||
LocalDateTime endTime = dto.getSleepEndTime();
|
||||
if (startTime != null && endTime != null) {
|
||||
dto.setSleepDuration(Duration.between(startTime, endTime).toMinutes() / 60.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user