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