Commit 132c602d by 汪志阳

fix:bug修改

parent aac25ad8
...@@ -18,11 +18,6 @@ public class SleepTimeAnalysisVO { ...@@ -18,11 +18,6 @@ public class SleepTimeAnalysisVO {
private String endTime; private String endTime;
/** /**
* 起床时间
*/
private String newEndTime;
/**
* 间隔时间(分钟) * 间隔时间(分钟)
*/ */
private Long interval; private Long interval;
......
package com.makeit.service.platform.elder.impl; package com.makeit.service.platform.elder.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
...@@ -9,13 +12,13 @@ import com.google.common.collect.Lists; ...@@ -9,13 +12,13 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.makeit.common.entity.BaseEntity; import com.makeit.common.entity.BaseEntity;
import com.makeit.entity.platform.device.PlatDevice; import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.elder.ElderSleepAnalysisVO;
import com.makeit.entity.platform.elder.PlatElder; import com.makeit.entity.platform.elder.PlatElder;
import com.makeit.entity.platform.elder.PlatElderSleep; import com.makeit.entity.platform.elder.PlatElderSleep;
import com.makeit.entity.platform.elder.PlatElderSleepAnalysis; import com.makeit.entity.platform.elder.PlatElderSleepAnalysis;
import com.makeit.entity.platform.space.PlatRoomBedDevice; import com.makeit.entity.platform.space.PlatRoomBedDevice;
import com.makeit.entity.saas.analysis.SaasSleepAnalysisModel; import com.makeit.entity.saas.analysis.SaasSleepAnalysisModel;
import com.makeit.entity.saas.analysis.SaasSleepEvaluateStandardReport; import com.makeit.entity.saas.analysis.SaasSleepEvaluateStandardReport;
import com.makeit.enums.report.ElderSleepType;
import com.makeit.enums.report.SleepTypeEnum; import com.makeit.enums.report.SleepTypeEnum;
import com.makeit.global.aspect.tenant.TenantIdIgnore; import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.mapper.platform.elder.PlatElderSleepMapper; import com.makeit.mapper.platform.elder.PlatElderSleepMapper;
...@@ -32,7 +35,6 @@ import com.makeit.utils.data.convert.StreamUtil; ...@@ -32,7 +35,6 @@ import com.makeit.utils.data.convert.StreamUtil;
import com.makeit.vo.platform.elder.report.day.PlatSleepRangeVO; import com.makeit.vo.platform.elder.report.day.PlatSleepRangeVO;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -61,12 +63,8 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -61,12 +63,8 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
@Autowired @Autowired
private SaasSleepEvaluateReportService saasSleepEvaluateReportService; private SaasSleepEvaluateReportService saasSleepEvaluateReportService;
@Autowired @Autowired
private SaasElderReportConfigService saasElderReportConfigService;
@Autowired
private SaasSleepEvaluateStandardReportService saasSleepEvaluateStandardReportService; private SaasSleepEvaluateStandardReportService saasSleepEvaluateStandardReportService;
@Autowired @Autowired
private SaasDiseaseModelService saasDiseaseModelService;
@Autowired
private IotProductDeviceService productDeviceService; private IotProductDeviceService productDeviceService;
@Autowired @Autowired
private PlatRoomBedDeviceService roomBedDeviceService; private PlatRoomBedDeviceService roomBedDeviceService;
...@@ -77,647 +75,779 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -77,647 +75,779 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
@Autowired @Autowired
private PlatElderSleepAnalysisService platElderSleepAnalysisService; private PlatElderSleepAnalysisService platElderSleepAnalysisService;
@Autowired @Autowired
private PlatElderBreatheDayStatService platElderBreatheDayStatService;
@Autowired
private PlatElderSleepService platElderSleepService; private PlatElderSleepService platElderSleepService;
@Autowired private TreeMap<String, AnalysisVO> getPerMinuteData(Map<String, List<DeviceInfoContentBreathe>> minuteMap, SaasSleepAnalysisModel analysisModel) {
private PlatElderReportMonthService platElderReportMonthService;
@Override
@Transactional(rollbackFor = Exception.class)
@TenantIdIgnore
public List<PlatElderSleepAnalysis> elderSleepSleepAnalysisTask(Integer month,Integer day) {
List<String> dayHourRangeList = getLastDayHourRange(month,day);
String currentDate = LocalDate.now().minusDays(1).toString();
if(month != null && day != null){
currentDate = LocalDate.of(2024, month, day).minusDays(1).toString();
}
SaasSleepAnalysisModel analysisModel = saasSleepAnalysisModelService.getOne(new QueryWrapper<SaasSleepAnalysisModel>().lambda()
.orderByDesc(BaseEntity::getCreateBy)
.last("limit 1"));
List<PlatRoomBedDevice> platRoomBedDeviceList = roomBedDeviceService.list(new QueryWrapper<PlatRoomBedDevice>()
.lambda().isNotNull(PlatRoomBedDevice::getBedId));
Integer actionThreshold = Integer.valueOf(analysisModel.getActionThreshold());
int turnedThreshold = Integer.parseInt(analysisModel.getTurnedThreshold()); int turnedThreshold = Integer.parseInt(analysisModel.getTurnedThreshold());
int sleepTimeActionThreshold = Integer.parseInt(analysisModel.getSleepTimeActionThreshold()); int sleepTimeActionThreshold = Integer.parseInt(analysisModel.getSleepTimeActionThreshold());
Integer actionThreshold = Integer.valueOf(analysisModel.getActionThreshold());
double sleepTimeActionDuration = Double.parseDouble(analysisModel.getSleepTimeActionDuration()); // 每分钟大于30秒体动值 大于(50)
int riseActionThreshold = Integer.parseInt(analysisModel.getRiseActionThreshold()); int riseActionThreshold = Integer.parseInt(analysisModel.getRiseActionThreshold());
int riseActionDuration = Integer.parseInt(analysisModel.getRiseActionDuration());
double riseLeaveThreshold = Double.parseDouble(analysisModel.getRiseLeaveThreshold());
int awakeThreshold = Integer.parseInt(analysisModel.getAwakeThreshold()); int awakeThreshold = Integer.parseInt(analysisModel.getAwakeThreshold());
TreeMap<String, AnalysisVO> statisticsMap = Maps.newTreeMap();
for (Map.Entry<String, List<DeviceInfoContentBreathe>> entry : minuteMap.entrySet()) {
List<DeviceInfoContentBreathe> deviceInfoContentBreathes = entry.getValue();
DeviceInfoContentBreathe.Properties breatheProperties;
int reportSize = deviceInfoContentBreathes.size();
// 记录总呼吸率和总心率
int maxBr = 0;
int minBr = 0;
int maxHr = 0;
int minHr = 0;
int hrBrCount = 0; // 呼吸率心率统计次数
int totalBr = 0;
int totalHr = 0;
Integer bodymoveCount = 0; // 体动次数
Integer turnoverCount = 0; // 翻身次数
boolean isAction = false; // 每分钟是否动过
AnalysisVO analysisVO = new AnalysisVO();
int getUpBodymoveCount = 0;
boolean isMoveBed = false;
// 清醒每分钟体动是否满足要求 (每分钟体动值大于60或无人)
boolean awakeMinuteActionFlag = false;
int noPersonCount = 0; // 无人跳过计数
for (DeviceInfoContentBreathe infoContentBreathe : deviceInfoContentBreathes) {
// 体动指数
breatheProperties = infoContentBreathe.getProperties();
Integer bodyMove = breatheProperties.getBodymove();
int br = breatheProperties.getBr();
int hr = breatheProperties.getHr();
Integer hasPerson = breatheProperties.getPerson(); // 0无人,1有人
if (hasPerson == 0) {
noPersonCount++;
isMoveBed = true;
awakeMinuteActionFlag = true;
continue;
}
// 0和255直接跳过
if (breatheProperties.getHr() == 255 || breatheProperties.getHr() == 0) {
noPersonCount++;
continue;
}
if (breatheProperties.getBr() == 255 || breatheProperties.getBr() == 0) {
noPersonCount++;
continue;
}
int sleepDeepActionThreshold = Integer.parseInt(analysisModel.getSleepDeepActionThreshold()); if (bodyMove > actionThreshold) {
int sleepDeepActionTimeBegin = Integer.parseInt(analysisModel.getSleepDeepActionTimeBegin()); bodymoveCount++;
}
if (bodyMove > turnedThreshold) {
turnoverCount++;
}
// 判断入睡时间的体动阈值
if (bodyMove >= sleepTimeActionThreshold) {
isAction = true;
}
// 起床每分钟体动次数
if (bodyMove > riseActionThreshold) {
getUpBodymoveCount++;
}
// 清醒
if (bodyMove > awakeThreshold) {
awakeMinuteActionFlag = true;
}
int sleepDeepBreatheMin = Integer.parseInt(analysisModel.getSleepDeepBreatheMin()); if (br > maxBr) {
int sleepDeepBreatheMax = Integer.parseInt(analysisModel.getSleepDeepBreatheMax()); maxBr = br;
}
if (minBr == 0) {
minBr = br;
} else if (br < minBr) {
minBr = br;
}
if (hr > maxHr) {
maxHr = hr;
}
int sleepModerateActionThreshold = Integer.parseInt(analysisModel.getSleepModerateActionThreshold()); if (minHr == 0) {
int sleepModerateActionTimeBegin = Integer.parseInt(analysisModel.getSleepModerateActionTimeBegin()); minHr = hr;
List<PlatElderSleepAnalysis> result = new ArrayList<>(); } else if (hr < minHr) {
minHr = hr;
}
for (PlatRoomBedDevice platRoomBedDevice : platRoomBedDeviceList) { if (hasPerson == 1) {
TreeMap<String, AnalysisVO> totalMap = new TreeMap<>(); totalBr += br;
totalHr += hr;
hrBrCount++;
}
}
if (reportSize == noPersonCount) {
continue;
}
// 每分钟的数据
analysisVO.setTotalBr(totalBr);
analysisVO.setAvgBr(totalBr / reportSize);
analysisVO.setTotalHr(totalHr);
analysisVO.setAvgHr(totalHr / reportSize);
analysisVO.setMaxBr(maxBr);
analysisVO.setMinBr(minBr);
analysisVO.setMaxHr(maxHr);
analysisVO.setMinHr(minHr);
analysisVO.setActionCount(bodymoveCount);
analysisVO.setTurnedCount(turnoverCount);
analysisVO.setIsAction(isAction);
analysisVO.setIsMoveBed(isMoveBed);
analysisVO.setIsMinuteActionFlag(getUpBodymoveCount > 30);
analysisVO.setAwakeMinuteActionFlag(awakeMinuteActionFlag);
statisticsMap.put(entry.getKey(), analysisVO);
}
return statisticsMap;
}
String bedId = platRoomBedDevice.getBedId(); /**
PlatElder elder = platElderService.getOne(new QueryWrapper<PlatElder>().lambda() * 获取满足入睡时间条件数据(体动值小于20且连续0.5小时)
.eq(PlatElder::getBedId, bedId)); *
if (elder == null) { * @param totalMap
* @param sleepTimeActionDuration 体动值持续时间 0.5h
* @param sleepTimeAnalysisVOList
* @return key:开始入睡时间,value:睡眠时间段内的数据
*/
private TreeMap<String, TreeMap<String, AnalysisVO>> getFallAsleepData(TreeMap<String, AnalysisVO> totalMap, double sleepTimeActionDuration,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
String startSleepTime = null;
int sleepMinute = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
AnalysisVO analysisVO = entry.getValue();
// 非离床且体动值<20
if (!analysisVO.getIsAction() && !analysisVO.getIsMoveBed()) {
if (StrUtil.isBlank(startSleepTime)) {
startSleepTime = entry.getKey();
}
sleepMinute++;
continue; continue;
} }
PlatDevice platDevice = platDeviceService.getById(platRoomBedDevice.getDeviceId()); if (StringUtils.isEmpty(startSleepTime) && sleepMinute == 0) {
if (platDevice == null) {
continue; continue;
} }
String tenantId = elder.getTenantId(); if (sleepMinute > sleepTimeActionDuration * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
for (String hourRange : dayHourRangeList) { sleepTimeAnalysisVO.setStartTime(startSleepTime);
String[] hourRangeArray = hourRange.split("~"); sleepTimeAnalysisVO.setEndTime(entry.getKey());
List<DeviceOperationLogEntity> deviceOperationLogEntities = productDeviceService.getDeviceLogByTimeRange(platDevice.getOriDeviceId(), "reportProperty", 5000, hourRangeArray[0], hourRangeArray[1]); sleepTimeAnalysisVO.setType(SleepTypeEnum.DAY_SLEEP.getCode());
if (CollectionUtils.isEmpty(deviceOperationLogEntities)) { sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
continue;
}
List<DeviceInfoContentBreathe> deviceInfoContentBreatheList = deviceOperationLogEntities.stream()
.filter(deviceOperationLogEntity -> deviceOperationLogEntity.getType().contains("reportProperty"))
.map(deviceOperationLogEntity -> {
DeviceInfoContentBreathe deviceInfoContentBreathe = JsonUtil.toObj((String) deviceOperationLogEntity.getContent(), DeviceInfoContentBreathe.class);
assert deviceInfoContentBreathe != null;
deviceInfoContentBreathe.setReportTime(formatLongTime(deviceInfoContentBreathe.getTimestamp()));
return deviceInfoContentBreathe;
})
.collect(Collectors.toList());
Map<String, List<DeviceInfoContentBreathe>> minuteMap = StreamUtil.groupBy(deviceInfoContentBreatheList, DeviceInfoContentBreathe::getReportTime);
deviceOperationLogEntities.clear();
// 统计每小时的体动和翻身
Map<String, AnalysisVO> statisticsMap = Maps.newHashMap();
List<DeviceInfoContentBreathe> deviceInfoContentBreathes;
for (Map.Entry<String, List<DeviceInfoContentBreathe>> entry : minuteMap.entrySet()) {
deviceInfoContentBreathes = entry.getValue();
DeviceInfoContentBreathe.Properties breatheProperties;
Integer bodymove;
int reportSize = deviceInfoContentBreathes.size();
// 记录总呼吸率和总心率
int maxBr = 0;
int minBr = 0;
int maxHr = 0;
int minHr = 0;
int hrBrCount = 0; // 呼吸率心率统计次数
int totalBr = 0;
int totalHr = 0;
Integer bodymoveCount = 0; // 体动次数
Integer turnoverCount = 0; // 翻身次数
boolean isAction = false; // 每分钟是否动过
AnalysisVO analysisVO = new AnalysisVO();
int getUpBodymoveCount = 0;
boolean isMoveBed = false;
boolean awakeMinuteActionFlag = false; // 清醒每分钟体动是否满足要求
int noPersonCount = 0; // 无人跳过计数
int sleepDeepMinuteCount = 0; // 深睡每分钟体动和翻身次数
int sleepModerateAMinuteCount = 0; // 中度睡每分钟体动和翻身次数
for (DeviceInfoContentBreathe infoContentBreathe : deviceInfoContentBreathes) {
// 体动指数
breatheProperties = infoContentBreathe.getProperties();
bodymove = breatheProperties.getBodymove();
int br = breatheProperties.getBr();
int hr = breatheProperties.getHr();
Integer hasPerson = breatheProperties.getPerson(); // 0无人,1有人
if (hasPerson == 0) {
noPersonCount++;
isMoveBed = true;
continue;
}
// 0和255直接跳过
if (breatheProperties.getHr() == 255 || breatheProperties.getHr() == 0 ) {
noPersonCount++;
continue;
}
if (breatheProperties.getBr() == 255 || breatheProperties.getBr() == 0) {
noPersonCount++;
continue;
}
if (bodymove > actionThreshold) {
bodymoveCount++;
}
if (bodymove > turnedThreshold) {
turnoverCount++;
}
// 判断入睡时间的体动阈值
if (bodymove >= sleepTimeActionThreshold) {
isAction = true;
}
// 起床每分钟体动次数
if (getUpBodymoveCount > riseActionThreshold) {
getUpBodymoveCount++;
}
// 清醒
if (bodymove > awakeThreshold) {
awakeMinuteActionFlag = true;
}
if (br > maxBr) {
maxBr = br;
}
if (br < minBr) {
minBr = br;
}
if (hr > maxHr) {
maxHr = hr;
}
if (hr < minHr) {
minHr = hr;
}
if (hasPerson == 1) {
totalBr += br;
totalHr += hr;
hrBrCount++;
}
}
if (reportSize == noPersonCount) {
continue;
}
analysisVO.setTotalBr(totalBr);
analysisVO.setAvgBr(totalBr / reportSize);
analysisVO.setTotalHr(totalHr);
analysisVO.setAvgHr(totalHr / reportSize);
analysisVO.setMaxBr(maxBr);
analysisVO.setMinBr(minBr);
analysisVO.setMaxHr(maxHr);
analysisVO.setMinHr(minHr);
analysisVO.setActionCount(bodymoveCount);
analysisVO.setTurnedCount(turnoverCount);
analysisVO.setIsAction(isAction);
analysisVO.setIsMoveBed(isMoveBed);
analysisVO.setIsMinuteActionFlag(getUpBodymoveCount > 30);
analysisVO.setAwakeMinuteActionFlag(awakeMinuteActionFlag);
statisticsMap.put(entry.getKey(), analysisVO);
}
TreeMap<String, AnalysisVO> treeMap = new TreeMap<>(statisticsMap);
totalMap.putAll(treeMap);
statisticsMap.clear();
treeMap.clear();
} }
startSleepTime = null;
if (totalMap.isEmpty()) { sleepMinute = 0;
}
TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap = new TreeMap<>();
if (CollUtil.isEmpty(sleepTimeAnalysisVOList)) {
return sleepTypeMap;
}
for (SleepTimeAnalysisVO timeAnalysisVO : sleepTimeAnalysisVOList) {
if (!timeAnalysisVO.getType().equals(SleepTypeEnum.DAY_SLEEP.getCode())) {
continue; continue;
} }
boolean startTimeFlag = false;
TreeMap<String, AnalysisVO> sleepTotalMap = new TreeMap<>();
int totalActionCount = 0;
int totalTurnedCount = 0;
// 记录长者不同类型的睡眠时间
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList = Lists.newArrayList();
SleepTimeAnalysisVO sleepTimeAnalysisVO;
// 判断睡觉时间
String startSleepTime = null; // 入睡时间开始
int sleepMinute = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) { for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
String reportTime = entry.getKey();
AnalysisVO analysisVO = entry.getValue(); AnalysisVO analysisVO = entry.getValue();
if (!analysisVO.getIsAction() && !analysisVO.getIsMoveBed()) { if (reportTime.equals(timeAnalysisVO.getStartTime())) {
if (StringUtils.isEmpty(startSleepTime)) { startTimeFlag = true;
startSleepTime = entry.getKey(); sleepTotalMap.put(reportTime, analysisVO);
}
sleepMinute++;
continue;
} }
if (StringUtils.isEmpty(startSleepTime) && sleepMinute == 0) { if (reportTime.equals(timeAnalysisVO.getEndTime())) {
continue; sleepTotalMap.put(reportTime, analysisVO);
startTimeFlag = false;
} }
if (sleepMinute > sleepTimeActionDuration * 60) { if (startTimeFlag) {
sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); sleepTotalMap.put(reportTime, analysisVO);
sleepTimeAnalysisVO.setStartTime(startSleepTime); }
sleepTypeMap.put(timeAnalysisVO.getStartTime(), sleepTotalMap);
}
}
return sleepTypeMap;
}
/**
* 获取起床数据(离床大于0.5小时,或者每分钟大于30秒体动值大于50持续10min,且在两小时内无再次入睡
*
* @param totalMap
* @param riseLeaveThreshold
* @param riseActionDuration
* @param sleepTimeAnalysisVOList
*/
private void getUp(TreeMap<String, AnalysisVO> totalMap, double riseLeaveThreshold, int riseActionDuration,
double repeatSleepThreshold, List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList,
SleepCountDTO sleepCountDTO) {
Integer actionCount = 0;
Integer turnedCount = 0;
String startGetupTime = null;
int getupMinute = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
AnalysisVO analysisVO = entry.getValue();
actionCount += analysisVO.getActionCount();
turnedCount += analysisVO.getTurnedCount();
if (analysisVO.getIsMoveBed()) {
if (StrUtil.isBlank(startGetupTime)) {
startGetupTime = entry.getKey();
}
getupMinute++;
continue;
}
if (StringUtils.isEmpty(startGetupTime) && getupMinute == 0) {
continue;
}
if (getupMinute > riseLeaveThreshold * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime);
sleepTimeAnalysisVO.setEndTime(entry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
}
startGetupTime = null;
getupMinute = 0;
}
sleepCountDTO.setTotalActionCount(actionCount);
sleepCountDTO.setTotalTurnedCount(turnedCount);
List<SleepTimeAnalysisVO> getUpList = sleepTimeAnalysisVOList.stream()
.filter(f -> SleepTypeEnum.GETUP.getCode().equals(f.getType())).collect(Collectors.toList());
List<SleepTimeAnalysisVO> sleepList = sleepTimeAnalysisVOList.stream()
.filter(f -> SleepTypeEnum.SLEEP.getCode().equals(f.getType())).collect(Collectors.toList());
if (CollUtil.isEmpty(sleepList)) {
return;
}
String startGetupTime2 = null;
int getupMinute2 = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
AnalysisVO analysisVO = entry.getValue();
if (analysisVO.getIsMinuteActionFlag()) {
if (StrUtil.isBlank(startGetupTime)) {
startGetupTime2 = entry.getKey();
}
getupMinute2++;
continue;
}
if (StringUtils.isEmpty(startGetupTime2) && getupMinute2 == 0) {
continue;
}
if (getupMinute2 > riseActionDuration && startGetupTime2 != null) {
String finalStartGetupTime = startGetupTime2;
boolean exist = getUpList.stream().anyMatch(f -> f.getStartTime().equals(finalStartGetupTime));
if (!exist) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime2);
sleepTimeAnalysisVO.setEndTime(entry.getKey()); sleepTimeAnalysisVO.setEndTime(entry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP.getCode()); sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
} }
startSleepTime = null;
sleepMinute = 0;
}
TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap = new TreeMap<>();
for (SleepTimeAnalysisVO timeAnalysisVO : sleepTimeAnalysisVOList) {
boolean startTimeFlag = false;
TreeMap<String, AnalysisVO> sleepTotalMap = new TreeMap<>();
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
if (entry.getKey().equals(timeAnalysisVO.getStartTime())) {
startTimeFlag = true;
sleepTotalMap.put(entry.getKey(), entry.getValue());
}
if (entry.getKey().equals(timeAnalysisVO.getEndTime())) {
sleepTotalMap.put(entry.getKey(), entry.getValue());
startTimeFlag = false;
}
if (startTimeFlag) {
sleepTotalMap.put(entry.getKey(), entry.getValue());
}
sleepTypeMap.put(timeAnalysisVO.getType(), sleepTotalMap);
}
} }
startGetupTime2 = null;
getupMinute2 = 0;
}
setWakeUpTime(sleepTimeAnalysisVOList);
}
// 判断起床时间 /**
String startGetupTime = null; // 起床时间开始 * 深度睡眠(体动和翻身次数10分钟内等于0次,且呼吸率在10-20范围内)
int getupMinute = 0; *
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) { * @param sleepTypeMap
AnalysisVO analysisVO = entry.getValue(); * @param analysisModel
if (analysisVO.getIsMoveBed()) { * @param sleepTimeAnalysisVOList
if (StringUtils.isEmpty(startGetupTime)) { */
startGetupTime = entry.getKey(); private void deepSleepData(TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap, SaasSleepAnalysisModel analysisModel,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
int sleepDeepMinute = 0;
String startSleepDeepTime = null;
int sleepDeepActionThreshold = Integer.parseInt(analysisModel.getSleepDeepActionThreshold());
int sleepDeepActionTimeBegin = Integer.parseInt(analysisModel.getSleepDeepActionTimeBegin());
int sleepDeepBreatheMin = Integer.parseInt(analysisModel.getSleepDeepBreatheMin());
int sleepDeepBreatheMax = Integer.parseInt(analysisModel.getSleepDeepBreatheMax());
if (CollUtil.isEmpty(sleepTypeMap)) {
return;
}
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) {
AnalysisVO analysisVO = voEntry.getValue();
boolean actionFlag = analysisVO.getActionCount() == sleepDeepActionThreshold;
boolean turnedFlag = analysisVO.getTurnedCount() == sleepDeepActionThreshold;
if (actionFlag && turnedFlag
&& sleepDeepBreatheMin <= analysisVO.getAvgBr() && sleepDeepBreatheMax >= analysisVO.getAvgBr()) {
if (StringUtils.isEmpty(startSleepDeepTime)) {
startSleepDeepTime = voEntry.getKey();
} }
getupMinute++; sleepDeepMinute++;
continue; continue;
} }
if (StringUtils.isEmpty(startGetupTime) && getupMinute == 0) { if (StringUtils.isEmpty(startSleepDeepTime) && sleepDeepMinute == 0) {
continue; continue;
} }
if (getupMinute > riseLeaveThreshold * 60) { if (sleepDeepMinute > sleepDeepActionTimeBegin) {
sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime); sleepTimeAnalysisVO.setStartTime(startSleepDeepTime);
sleepTimeAnalysisVO.setEndTime(entry.getKey()); sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode()); sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_DEEP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
} }
startGetupTime = null; startSleepDeepTime = null;
getupMinute = 0; sleepDeepMinute = 0;
} }
// 判断起床时间 }
String startGetupTime2 = null; // 起床时间开始 }
int getupMinute2 = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) { private SleepCountDTO deepSleepAndRestData(TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap, SaasSleepAnalysisModel analysisModel,
AnalysisVO analysisVO = entry.getValue(); List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
if (analysisVO.getIsMinuteActionFlag()) { String startSleepDeepTime;
if (StringUtils.isEmpty(startGetupTime2)) { int napThreshold = Integer.parseInt(analysisModel.getNapThreshold());
startGetupTime2 = entry.getKey(); int napDuration = Integer.parseInt(analysisModel.getNapDuration());
List<String> startTimeList = new ArrayList<>(sleepTypeMap.keySet());
List<LocalDateTime> timeList = new ArrayList<>();
startTimeList.forEach(t -> timeList.add(LocalDateTime.parse(t, DEFAULT_FORMATTER)));
TreeMap<String, TreeMap<String, AnalysisVO>> daySleepMap = new TreeMap<>();
TreeMap<String, TreeMap<String, AnalysisVO>> restSleepMap = new TreeMap<>();
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
// 每分钟的数据
int sleepMinute = sleepMap.size();
Map.Entry<String, AnalysisVO> lastEntry = sleepMap.lastEntry();
startSleepDeepTime = entry.getKey();
// 睡眠:深度睡眠和浅度睡眠>3小时
if (sleepMinute > napThreshold * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startSleepDeepTime);
sleepTimeAnalysisVO.setEndTime(lastEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
daySleepMap.put(startSleepDeepTime, sleepMap);
}
LocalDateTime startTime = LocalDateTime.parse(startSleepDeepTime, DEFAULT_FORMATTER);
// LocalDateTime repeatSleepTime = timeList.stream().filter(f -> Duration.between(startTime, f).toMinutes() <= napDuration * 60L).findFirst().orElse(null);
// 小憩:深度睡眠和浅度睡眠<=3小时,且2小时内无再次入睡
if (sleepMinute <= napThreshold * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startSleepDeepTime);
sleepTimeAnalysisVO.setEndTime(lastEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.RESTING.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
daySleepMap.put(startSleepDeepTime, sleepMap);
}
}
SleepCountDTO sleepCountDTO = new SleepCountDTO();
sleepCountDTO.setDaySleepMap(daySleepMap);
sleepCountDTO.setRestSleepMap(restSleepMap);
return sleepCountDTO;
}
/**
* 中度睡眠(体动和翻身次数10分钟内小于3次)
*
* @param sleepTypeMap
* @param analysisModel
* @param sleepTimeAnalysisVOList
*/
private void middleSleep(TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap, SaasSleepAnalysisModel analysisModel,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
int sleepModerateActionThreshold = Integer.parseInt(analysisModel.getSleepModerateActionThreshold());
int sleepModerateActionTimeBegin = Integer.parseInt(analysisModel.getSleepModerateActionTimeBegin());
int sleepMidMinute = 0;
String startSleepMidTime = null;
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) {
AnalysisVO analysisVO = voEntry.getValue();
if (analysisVO.getActionCount() < sleepModerateActionThreshold && analysisVO.getTurnedCount() < sleepModerateActionThreshold) {
if (StringUtils.isEmpty(startSleepMidTime)) {
startSleepMidTime = voEntry.getKey();
} }
getupMinute2++; sleepMidMinute++;
continue; continue;
} }
if (StringUtils.isEmpty(startGetupTime2) && getupMinute2 == 0) { if (StringUtils.isEmpty(startSleepMidTime) && sleepMidMinute == 0) {
continue; continue;
} }
if (getupMinute2 > riseActionDuration) { if (sleepMidMinute > sleepModerateActionTimeBegin) {
sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime2); sleepTimeAnalysisVO.setStartTime(startSleepMidTime);
sleepTimeAnalysisVO.setEndTime(entry.getKey()); sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode()); sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_MODERATE.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
} }
startGetupTime2 = null; startSleepMidTime = null;
getupMinute2 = 0; sleepMidMinute = 0;
} }
}
int sleepDeepMinute = 0; }
String startSleepDeepTime = null; // 入睡时间开始
// 判断深度睡眠时间 /**
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) { * 清醒(每分钟体动值大于60或无人)
TreeMap<String, AnalysisVO> sleepMap = entry.getValue(); *
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) { * @param sleepTypeMap
AnalysisVO analysisVO = voEntry.getValue(); * @param sleepTimeAnalysisVOList
totalActionCount += analysisVO.getActionCount(); */
totalTurnedCount += analysisVO.getTurnedCount(); private void sober(TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap,
if (analysisVO.getActionCount() == sleepDeepActionThreshold && analysisVO.getTurnedCount() == sleepDeepActionThreshold List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
&& sleepDeepBreatheMin <= analysisVO.getAvgBr() && sleepDeepBreatheMax >= analysisVO.getAvgBr()) { int awakeMinute = 0;
if (StringUtils.isEmpty(startSleepDeepTime)) { String startAwakeTime = null;
startSleepDeepTime = voEntry.getKey(); for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
} TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
sleepDeepMinute++; for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) {
continue; AnalysisVO analysisVO = voEntry.getValue();
} if (analysisVO.getAwakeMinuteActionFlag() || analysisVO.getIsMoveBed()) {
if (StringUtils.isEmpty(startSleepDeepTime) && sleepDeepMinute == 0) { if (StringUtils.isEmpty(startAwakeTime)) {
continue; startAwakeTime = voEntry.getKey();
}
if (sleepDeepMinute > sleepDeepActionTimeBegin) {
sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startSleepDeepTime);
sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_DEEP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
} }
startSleepDeepTime = null; awakeMinute++;
sleepDeepMinute = 0; continue;
}
if (StringUtils.isEmpty(startAwakeTime) && awakeMinute == 0) {
continue;
} }
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startAwakeTime);
sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SOBER.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
startAwakeTime = null;
awakeMinute = 0;
}
}
setRestTime(sleepTimeAnalysisVOList);
}
private SleepTimeDTO groupSleepData(List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
sleepTimeAnalysisVOList.forEach(s -> {
Long durationRange = getDurationRange(s.getStartTime(), s.getEndTime());
s.setInterval(durationRange);
});
Map<String, List<SleepTimeAnalysisVO>> sleepMap = StreamUtil.groupBy(sleepTimeAnalysisVOList, SleepTimeAnalysisVO::getType);
List<SleepTimeAnalysisVO> daySleepList = sleepMap.containsKey(SleepTypeEnum.DAY_SLEEP.getCode()) ? sleepMap.get(SleepTypeEnum.DAY_SLEEP.getCode()) : new ArrayList<>();
List<SleepTimeAnalysisVO> sleepList = sleepMap.containsKey(SleepTypeEnum.SLEEP.getCode()) ? sleepMap.get(SleepTypeEnum.SLEEP.getCode()) : new ArrayList<>();
List<SleepTimeAnalysisVO> deepList = sleepMap.containsKey(SleepTypeEnum.SLEEP_DEEP.getCode()) ? sleepMap.get(SleepTypeEnum.SLEEP_DEEP.getCode()) : new ArrayList<>();
List<SleepTimeAnalysisVO> moderateList = sleepMap.containsKey(SleepTypeEnum.SLEEP_MODERATE.getCode()) ? sleepMap.get(SleepTypeEnum.SLEEP_MODERATE.getCode()) : new ArrayList<>();
List<SleepTimeAnalysisVO> getUpList = sleepMap.containsKey(SleepTypeEnum.GETUP.getCode()) ? sleepMap.get(SleepTypeEnum.GETUP.getCode()) : new ArrayList<>();
List<SleepTimeAnalysisVO> soberList = sleepMap.containsKey(SleepTypeEnum.SOBER.getCode()) ? sleepMap.get(SleepTypeEnum.SOBER.getCode()) : new ArrayList<>();
SleepTimeDTO sleepTimeDTO = new SleepTimeDTO();
sleepTimeDTO.setDeepList(deepList);
sleepTimeDTO.setMidList(moderateList);
sleepTimeDTO.setSoberList(soberList);
sleepTimeDTO.setSleepList(sleepList);
sleepTimeDTO.setGetUpList(getUpList);
sleepTimeDTO.setDaySleepList(daySleepList);
List<SleepTimeAnalysisVO> lightSleepList = new ArrayList<>();
if (CollUtil.isNotEmpty(sleepList)) {
lightSleepList = sleepList;
if (CollUtil.isNotEmpty(deepList)) {
lightSleepList.removeAll(deepList);
} }
if (CollUtil.isNotEmpty(moderateList)) {
lightSleepList.removeAll(moderateList);
}
}
sleepTimeDTO.setLightList(lightSleepList);
return sleepTimeDTO;
}
// 中度睡眠 private void setWakeUpTime(List<SleepTimeAnalysisVO> list) {
int sleepMidMinute = 0; if (CollUtil.isEmpty(list)) {
String startSleepMidTime = null; // 入睡时间开始 return;
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) { }
TreeMap<String, AnalysisVO> sleepMap = entry.getValue(); List<SleepTimeAnalysisVO> getUpList = list.stream()
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) { .filter(f -> SleepTypeEnum.GETUP.getCode().equals(f.getType())).collect(Collectors.toList());
AnalysisVO analysisVO = voEntry.getValue(); List<SleepTimeAnalysisVO> sleepList = list.stream()
if (analysisVO.getActionCount() < sleepModerateActionThreshold && analysisVO.getTurnedCount() < sleepModerateActionThreshold) { .filter(f -> SleepTypeEnum.SLEEP.getCode().equals(f.getType())).collect(Collectors.toList());
if (StringUtils.isEmpty(startSleepMidTime)) { if (CollUtil.isEmpty(sleepList) || CollUtil.isEmpty(getUpList)) {
startSleepMidTime = voEntry.getKey(); return;
} }
sleepMidMinute++; sleepList.forEach(s -> {
continue; List<SleepTimeAnalysisVO> wakeList = getUpList.stream().filter(f -> LocalDateTime.parse(f.getStartTime(), DEFAULT_FORMATTER)
} .isAfter(LocalDateTime.parse(s.getStartTime(), DEFAULT_FORMATTER))).collect(Collectors.toList());
if (StringUtils.isEmpty(startSleepMidTime) && sleepMidMinute == 0) { // 查询最近的清醒时间,设置
continue; if (CollUtil.isNotEmpty(wakeList)) {
} final long[] minute = {24 * 60};
if (sleepMidMinute > sleepModerateActionTimeBegin) { wakeList.forEach(w -> {
sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); long between = Duration.between(LocalDateTime.parse(s.getStartTime(), DEFAULT_FORMATTER),
sleepTimeAnalysisVO.setStartTime(startSleepMidTime); LocalDateTime.parse(w.getStartTime(), DEFAULT_FORMATTER)).toMinutes();
sleepTimeAnalysisVO.setEndTime(voEntry.getKey()); if (between < minute[0]) {
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_MODERATE.getCode()); minute[0] = between;
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); s.setEndTime(w.getStartTime());
} }
startSleepMidTime = null; });
sleepMidMinute = 0;
}
} }
});
// 清醒 }
int awakeMinute = 0;
String startAwakeTime = null; // 入睡时间开始
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) { /**
TreeMap<String, AnalysisVO> sleepMap = entry.getValue(); * 小憩结束时间为清醒时间
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) { * @param list
AnalysisVO analysisVO = voEntry.getValue(); */
if (analysisVO.getAwakeMinuteActionFlag() || analysisVO.getIsMoveBed()) { private void setRestTime(List<SleepTimeAnalysisVO> list) {
if (StringUtils.isEmpty(startAwakeTime)) { if (CollUtil.isEmpty(list)) {
startAwakeTime = voEntry.getKey(); return;
} }
awakeMinute++; List<SleepTimeAnalysisVO> soberList = list.stream()
continue; .filter(f -> SleepTypeEnum.SOBER.getCode().equals(f.getType())).collect(Collectors.toList());
} List<SleepTimeAnalysisVO> restList = list.stream()
if (StringUtils.isEmpty(startAwakeTime) && awakeMinute == 0) { .filter(f -> SleepTypeEnum.RESTING.getCode().equals(f.getType())).collect(Collectors.toList());
continue; if (CollUtil.isEmpty(restList) || CollUtil.isEmpty(soberList)) {
return;
}
restList.forEach(s -> {
List<SleepTimeAnalysisVO> wakeList = soberList.stream().filter(f -> LocalDateTime.parse(f.getStartTime(), DEFAULT_FORMATTER)
.isAfter(LocalDateTime.parse(s.getStartTime(), DEFAULT_FORMATTER))).collect(Collectors.toList());
// 查询最近的清醒时间,设置
if (CollUtil.isNotEmpty(wakeList)) {
final long[] minute = {24 * 60};
wakeList.forEach(w -> {
long between = Duration.between(LocalDateTime.parse(s.getStartTime(), DEFAULT_FORMATTER),
LocalDateTime.parse(w.getStartTime(), DEFAULT_FORMATTER)).toMinutes();
if (between < minute[0]) {
minute[0] = between;
s.setEndTime(w.getStartTime());
} }
sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); });
sleepTimeAnalysisVO.setStartTime(startAwakeTime);
sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SOBER.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
startAwakeTime = null;
awakeMinute = 0;
}
} }
});
Map<String, List<SleepTimeAnalysisVO>> sleepMap = StreamUtil.groupBy(sleepTimeAnalysisVOList, SleepTimeAnalysisVO::getType); }
List<SleepTimeAnalysisVO> sleepList = sleepMap.get(SleepTypeEnum.SLEEP.getCode());
if (CollectionUtils.isEmpty(sleepList)) { private PlatElderSleepAnalysis savePlatElderSleepAnalysis(SleepTimeDTO sleepTimeDTO,
continue; SaasSleepEvaluateStandardReport evaluateStandardReport,
String elderId, String currentDate, SleepCountDTO sleepCountDTO,
String tenantId,
String oriDeviceId) {
SleepTimeMinuteDTO sleepTimeMinute = getSleepTimeMinute(sleepTimeDTO);
long daySleepTime = sleepTimeMinute.getDaySleepTime() != null ? sleepTimeMinute.getDaySleepTime() : 0L;
long deepTime = sleepTimeMinute.getDeepTime() != null ? sleepTimeMinute.getDeepTime() : 0L;
long dayRestTime = sleepTimeMinute.getDayRestTime() != null ? sleepTimeMinute.getDayRestTime() : 0L;
long soberTime = sleepTimeMinute.getSoberTime() != null ? sleepTimeMinute.getSoberTime() : 0L;
long lightTime = sleepTimeMinute.getLightTime() != null ? sleepTimeMinute.getLightTime() : 0L;
String score = calculateScores(daySleepTime, dayRestTime, deepTime, soberTime, lightTime, evaluateStandardReport);
EvaluateReportVO sleepReport = saasSleepEvaluateReportService.getByScore(Long.parseLong(score));
// 长者一天的睡眠分析
PlatElderSleepAnalysis elderSleepAnalysis = new PlatElderSleepAnalysis();
elderSleepAnalysis.setElderId(elderId);
elderSleepAnalysis.setActionCount(sleepCountDTO.getTotalActionCount());
elderSleepAnalysis.setTurnedCount(sleepCountDTO.getTotalTurnedCount());
elderSleepAnalysis.setHappenDate(currentDate);
elderSleepAnalysis.setSleepScore(score);
elderSleepAnalysis.setSleepTime(String.valueOf(daySleepTime));
elderSleepAnalysis.setRestTime(String.valueOf(dayRestTime));
elderSleepAnalysis.setSleepResult(sleepReport.getResult());
elderSleepAnalysis.setTenantId(tenantId);
elderSleepAnalysis.setSleepEvaluate(sleepReport.getEvaluate());
elderSleepAnalysis.setOriDeviceId(oriDeviceId);
// platElderSleepAnalysisService.save(elderSleepAnalysis);
return elderSleepAnalysis;
}
private List<PlatElderSleep> saveElderSleep(SleepTimeDTO sleepTimeDTO, String platElderId, String currentDate) {
List<PlatElderSleep> elderSleepList = new ArrayList<>();
List<SleepTimeAnalysisVO> deepList = sleepTimeDTO.getDeepList();
List<SleepTimeAnalysisVO> moderateList = sleepTimeDTO.getMidList();
List<SleepTimeAnalysisVO> lightList = sleepTimeDTO.getLightList();
List<SleepTimeAnalysisVO> soberList = sleepTimeDTO.getSoberList();
List<SleepTimeAnalysisVO> sleepList = sleepTimeDTO.getSleepList();
List<SleepTimeAnalysisVO> restList = sleepTimeDTO.getRestList();
List<PlatSleepRangeVO> sleepRangeList = new ArrayList<>();
if (CollUtil.isNotEmpty(deepList)) {
for (SleepTimeAnalysisVO analysisVO : deepList) {
PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(SleepTypeEnum.SLEEP_DEEP.getCode());
sleepRangeList.add(sleepRangeVO);
} }
// 遍历得出长者一天多次睡眠中包含的不同睡眠类型 }
List<SleepTimeAnalysisVO> finalSleepTimeAnalysisList = Lists.newArrayList(); // 记录多端睡眠记录 if (CollUtil.isNotEmpty(moderateList)) {
SleepTimeAnalysisVO sleepTimeAnalysis; for (SleepTimeAnalysisVO analysisVO : sleepTimeDTO.getMidList()) {
PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
for (int i = 0; i < sleepList.size(); i++) { sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepTimeAnalysis = new SleepTimeAnalysisVO(); sleepRangeVO.setSleepType(SleepTypeEnum.SLEEP_MODERATE.getCode());
List<SleepTimeAnalysisVO> deepList = Lists.newArrayList(); sleepRangeList.add(sleepRangeVO);
List<SleepTimeAnalysisVO> midList = Lists.newArrayList();
List<SleepTimeAnalysisVO> lightList = Lists.newArrayList();
List<SleepTimeAnalysisVO> soberList = Lists.newArrayList();
SleepTimeAnalysisVO timeAnalysisVO = sleepList.get(i);
BeanUtils.copyProperties(timeAnalysisVO, sleepTimeAnalysis);
String startTime = timeAnalysisVO.getStartTime();
String endTime = timeAnalysisVO.getEndTime();
Long durationRange = getDurationRange(timeAnalysisVO.getStartTime(), timeAnalysisVO.getEndTime());
timeAnalysisVO.setInterval(durationRange);
LocalDateTime startDateTime = LocalDateTime.parse(startTime, DEFAULT_FORMATTER);
LocalDateTime endDateTime = LocalDateTime.parse(endTime, DEFAULT_FORMATTER);
boolean lightSleepFlag = true; // 如果不满足下面条件,就默认都是浅睡
for (SleepTimeAnalysisVO otherSleepTime : sleepTimeAnalysisVOList) {
if (otherSleepTime.getType().equals(SleepTypeEnum.SLEEP.getCode()) || otherSleepTime.getType().equals(SleepTypeEnum.GETUP.getCode())) {
continue;
}
String deepStartTime = otherSleepTime.getStartTime();
String deepEndTime = otherSleepTime.getEndTime();
LocalDateTime startDeepDateTime = LocalDateTime.parse(deepStartTime, DEFAULT_FORMATTER);
LocalDateTime endDeepDateTime = LocalDateTime.parse(deepEndTime, DEFAULT_FORMATTER);
if ((startDateTime.isBefore(startDeepDateTime) || startDateTime.equals(startDeepDateTime)) &&
(endDateTime.isAfter(endDeepDateTime) || endDateTime.equals(endDeepDateTime))) {
if (otherSleepTime.getType().equals(SleepTypeEnum.SLEEP_DEEP.getCode())) {
deepList.add(otherSleepTime);
} else if (otherSleepTime.getType().equals(SleepTypeEnum.SLEEP_MODERATE.getCode())) {
midList.add(otherSleepTime);
} else if (timeAnalysisVO.getType().equals(SleepTypeEnum.SOBER.getCode())) {
soberList.add(otherSleepTime);
} else {
otherSleepTime.setType(SleepTypeEnum.SLEEP_LIGHTNESS.getCode());
lightList.add(otherSleepTime);
}
lightSleepFlag = false;
}
}
if (lightSleepFlag) {
SleepTimeAnalysisVO lightSleepTime = new SleepTimeAnalysisVO();
lightSleepTime.setType(SleepTypeEnum.SLEEP_LIGHTNESS.getCode());
lightSleepTime.setStartTime(startTime);
lightSleepTime.setEndTime(endTime);
lightList.add(lightSleepTime);
}
sleepTimeAnalysis.setDeepList(deepList);
sleepTimeAnalysis.setInterval(durationRange);
sleepTimeAnalysis.setMidList(midList);
sleepTimeAnalysis.setLightList(lightList);
sleepTimeAnalysis.setSoberList(soberList);
finalSleepTimeAnalysisList.add(sleepTimeAnalysis);
}
SaasSleepEvaluateStandardReport evaluateStandardReport = saasSleepEvaluateStandardReportService.getOne(new QueryWrapper<SaasSleepEvaluateStandardReport>().lambda()
.orderByDesc(BaseEntity::getCreateDate)
.last("limit 1"));
// 算出每一段睡觉的不同睡眠类型的时间段
SaasSleepEvaluateStandardReportVO saasSleepEvaluateStandardReportVO = new SaasSleepEvaluateStandardReportVO();
List<PlatElderSleep> elderSleepList = Lists.newArrayList();
PlatSleepRangeVO sleepRangeVO;
for (SleepTimeAnalysisVO timeAnalysisVO : finalSleepTimeAnalysisList) {
List<PlatSleepRangeVO> sleepRangeVOList = Lists.newArrayList();
PlatElderSleep elderSleep = new PlatElderSleep();
for (SleepTimeAnalysisVO analysisVO : timeAnalysisVO.getDeepList()) {
sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(analysisVO.getType());
sleepRangeVOList.add(sleepRangeVO);
}
for (SleepTimeAnalysisVO analysisVO : timeAnalysisVO.getMidList()) {
sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(analysisVO.getType());
sleepRangeVOList.add(sleepRangeVO);
}
for (SleepTimeAnalysisVO analysisVO : timeAnalysisVO.getLightList()) {
sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(analysisVO.getType());
sleepRangeVOList.add(sleepRangeVO);
}
for (SleepTimeAnalysisVO analysisVO : timeAnalysisVO.getSoberList()) {
sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(analysisVO.getType());
sleepRangeVOList.add(sleepRangeVO);
}
elderSleep.setStartSleep(LocalDateTime.parse(timeAnalysisVO.getStartTime(), DEFAULT_FORMATTER));
elderSleep.setEndSleep(LocalDateTime.parse(timeAnalysisVO.getEndTime(), DEFAULT_FORMATTER));
if (timeAnalysisVO.getInterval() / 60 > 3) {
elderSleep.setElderSleepType(ElderSleepType.SLEEP.getCode());
} else {
elderSleep.setElderSleepType(ElderSleepType.REST.getCode());
}
elderSleep.setInterval(timeAnalysisVO.getInterval());
elderSleep.setSleepRecord(sleepRangeVOList);
elderSleepList.add(elderSleep);
}
// 获取睡眠评估标准配置
String sleepDeepConfig = evaluateStandardReport.getSleepDeepConfig();
String soberConfig = evaluateStandardReport.getSoberConfig();
String sleepTimeConfig = evaluateStandardReport.getSleepTime();
String lightnessConfig = evaluateStandardReport.getSleepLightnessConfig();
List<SleepConfigVO> sleepDeepConfigList = JSON.parseArray(sleepDeepConfig, SleepConfigVO.class);
List<SleepConfigVO> soberConfigList = JSON.parseArray(soberConfig, SleepConfigVO.class);
List<SleepConfigVO> sleepTimeConfigList = JSON.parseArray(sleepTimeConfig, SleepConfigVO.class);
SleepConfigVO lightnessConfigVO = JSON.parseObject(lightnessConfig, SleepConfigVO.class);
long deepTime = 0;
long soberTime = 0;
long soberCount = 0;
long sleepTime = 0;
long lightTime = 0;
// 算出不同睡眠类型的总时长
for (SleepTimeAnalysisVO timeAnalysisVO : sleepTimeAnalysisVOList) {
Long durationRange = getDurationRange(timeAnalysisVO.getStartTime(), timeAnalysisVO.getEndTime());
timeAnalysisVO.setInterval(durationRange);
if (timeAnalysisVO.getType().equals(SleepTypeEnum.SLEEP_DEEP.getCode())) {
deepTime += durationRange;
}
if (timeAnalysisVO.getType().equals(SleepTypeEnum.SOBER.getCode())) {
soberTime += durationRange;
}
if (timeAnalysisVO.getType().equals(SleepTypeEnum.SLEEP_LIGHTNESS.getCode())) {
lightTime += durationRange;
soberCount++;
}
if (timeAnalysisVO.getType().equals(SleepTypeEnum.SLEEP.getCode())) {
sleepTime += durationRange;
}
} }
}
long deepScore = 0; if (CollUtil.isNotEmpty(lightList)) {
for (int i = 0; i < sleepDeepConfigList.size(); i++) { for (SleepTimeAnalysisVO analysisVO : lightList) {
long hour = deepTime / 60; PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
SleepConfigVO sleepConfigVO = sleepDeepConfigList.get(i); sleepRangeVO.setStartTime(analysisVO.getStartTime());
long configHour = Long.parseLong(sleepConfigVO.getHour()); sleepRangeVO.setEndTime(analysisVO.getEndTime());
if (i == 0 && Objects.equals(hour, configHour)) { sleepRangeVO.setSleepType(SleepTypeEnum.SLEEP_LIGHTNESS.getCode());
deepScore = Long.parseLong(sleepConfigVO.getScore()); sleepRangeList.add(sleepRangeVO);
}
if (i > 0 && hour > configHour) {
deepScore += Long.parseLong(sleepConfigVO.getScore());
}
} }
saasSleepEvaluateStandardReportVO.setDeepScore(deepScore); }
long soberScore = 0; if (CollUtil.isNotEmpty(soberList)) {
for (int i = 0; i < soberConfigList.size(); i++) { for (SleepTimeAnalysisVO analysisVO : soberList) {
SleepConfigVO sleepConfigVO = soberConfigList.get(i); PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
long configHour = Long.parseLong(sleepConfigVO.getHour()); sleepRangeVO.setStartTime(analysisVO.getStartTime());
if (i == 0 && soberCount != 0) { sleepRangeVO.setEndTime(analysisVO.getEndTime());
soberScore = Long.parseLong(sleepConfigVO.getScore()) * soberCount; sleepRangeVO.setSleepType(SleepTypeEnum.SOBER.getCode());
} sleepRangeList.add(sleepRangeVO);
if (i > 0 && soberTime > configHour) {
soberScore += Long.parseLong(sleepConfigVO.getScore());
}
} }
saasSleepEvaluateStandardReportVO.setSoberScore(soberScore); }
List<SleepTimeAnalysisVO> getUpList = sleepTimeDTO.getGetUpList();
if (CollUtil.isNotEmpty(getUpList)) {
for (SleepTimeAnalysisVO analysisVO : getUpList) {
PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(SleepTypeEnum.GETUP.getCode());
sleepRangeList.add(sleepRangeVO);
}
}
List<String> sleepTypes = Lists.newArrayList(SleepTypeEnum.SLEEP_DEEP.getCode(),
SleepTypeEnum.SLEEP_MODERATE.getCode(), SleepTypeEnum.SLEEP_LIGHTNESS.getCode(), SleepTypeEnum.GETUP.getCode());
if (CollUtil.isNotEmpty(sleepList)) {
for (SleepTimeAnalysisVO analysisVO : sleepList) {
PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(analysisVO.getType());
List<PlatSleepRangeVO> rangeList = sleepRangeList.stream().filter(f -> sleepTypes.contains(f.getSleepType())).collect(Collectors.toList());
rangeList.add(sleepRangeVO);
rangeList = rangeList.stream().sorted(Comparator.comparing(l -> LocalDateTime.parse(l.getStartTime(), DEFAULT_FORMATTER))).collect(Collectors.toList());
PlatElderSleep platElderSleep = new PlatElderSleep();
platElderSleep.setStartSleep(LocalDateTime.parse(analysisVO.getStartTime(), DEFAULT_FORMATTER));
platElderSleep.setEndSleep(LocalDateTime.parse(analysisVO.getEndTime(), DEFAULT_FORMATTER));
platElderSleep.setElderId(platElderId);
platElderSleep.setElderSleepType(SleepTypeEnum.SLEEP.getType());
platElderSleep.setSleepRecord(rangeList);
platElderSleep.setInterval(analysisVO.getInterval());
platElderSleep.setHappenDate(currentDate);
elderSleepList.add(platElderSleep);
}
}
if (CollUtil.isNotEmpty(restList)) {
for (SleepTimeAnalysisVO analysisVO : restList) {
PlatSleepRangeVO sleepRangeVO = new PlatSleepRangeVO();
sleepRangeVO.setStartTime(analysisVO.getStartTime());
sleepRangeVO.setEndTime(analysisVO.getEndTime());
sleepRangeVO.setSleepType(analysisVO.getType());
PlatElderSleep platElderSleep = new PlatElderSleep();
List<PlatSleepRangeVO> rangeList = sleepRangeList.stream().filter(f -> SleepTypeEnum.SOBER.getCode().equals(f.getSleepType())).collect(Collectors.toList());
rangeList.add(sleepRangeVO);
rangeList = rangeList.stream().sorted(Comparator.comparing(l -> LocalDateTime.parse(l.getStartTime(), DEFAULT_FORMATTER))).collect(Collectors.toList());
platElderSleep.setStartSleep(LocalDateTime.parse(analysisVO.getStartTime(), DEFAULT_FORMATTER));
platElderSleep.setEndSleep(LocalDateTime.parse(analysisVO.getEndTime(), DEFAULT_FORMATTER));
platElderSleep.setElderId(platElderId);
platElderSleep.setElderSleepType(SleepTypeEnum.RESTING.getType());
platElderSleep.setSleepRecord(rangeList);
platElderSleep.setInterval(analysisVO.getInterval());
platElderSleep.setHappenDate(currentDate);
elderSleepList.add(platElderSleep);
}
}
// platElderSleepService.saveBatch(elderSleepList);
return elderSleepList;
}
long lightScore = 0; private SleepTimeMinuteDTO getSleepTimeMinute(SleepTimeDTO sleepTimeDTO) {
SleepTimeMinuteDTO result = new SleepTimeMinuteDTO();
result.setDaySleepTime(CollUtil.isNotEmpty(sleepTimeDTO.getSleepList()) ? sleepTimeDTO.getSleepList().stream().mapToLong(SleepTimeAnalysisVO::getInterval).reduce(0L, Long::sum) : 0);
result.setDeepTime(CollUtil.isNotEmpty(sleepTimeDTO.getDeepList()) ? sleepTimeDTO.getDeepList().stream().mapToLong(SleepTimeAnalysisVO::getInterval).reduce(0L, Long::sum) : 0);
result.setLightTime(CollUtil.isNotEmpty(sleepTimeDTO.getLightList()) ? sleepTimeDTO.getLightList().stream().mapToLong(SleepTimeAnalysisVO::getInterval).reduce(0L, Long::sum) : 0);
result.setSoberTime(CollUtil.isNotEmpty(sleepTimeDTO.getSoberList()) ? sleepTimeDTO.getSoberList().stream().mapToLong(SleepTimeAnalysisVO::getInterval).reduce(0L, Long::sum) : 0);
return result;
}
long lightHour = lightTime / 60; @Override
long lightnessConfigHour = Long.parseLong(lightnessConfigVO.getHour()); @Transactional(rollbackFor = Exception.class)
if ( Objects.equals(lightHour,lightnessConfigHour)) { @TenantIdIgnore
lightScore = Long.parseLong(lightnessConfigVO.getScore()); public List<ElderSleepAnalysisVO> elderSleepSleepAnalysisTask(Integer month, Integer day) {
}
saasSleepEvaluateStandardReportVO.setLightScore(lightScore);
long sleepScore = 0; List<String> dayHourRangeList = getLastDayHourRange(month, day);
for (int i = 0; i < sleepTimeConfigList.size(); i++) { String currentDate = LocalDate.now().minusDays(1).toString();
long hour = sleepTime / 60; if (month != null && day != null) {
SleepConfigVO sleepConfigVO = sleepTimeConfigList.get(i); currentDate = LocalDate.of(2024, month, day).minusDays(1).toString();
long configHour = Long.parseLong(sleepConfigVO.getHour()); }
if (i == 0 && Objects.equals(hour, configHour)) { SaasSleepAnalysisModel analysisModel = saasSleepAnalysisModelService.getOne(new QueryWrapper<SaasSleepAnalysisModel>().lambda()
sleepScore = Long.parseLong(sleepConfigVO.getScore()); .orderByDesc(BaseEntity::getCreateBy)
} .last("limit 1"));
if (i > 0 && hour > configHour) { List<PlatRoomBedDevice> platRoomBedDeviceList = roomBedDeviceService.list(new QueryWrapper<PlatRoomBedDevice>()
sleepScore += Long.parseLong(sleepConfigVO.getScore()); .lambda().isNotNull(PlatRoomBedDevice::getBedId));
} SaasSleepEvaluateStandardReport evaluateStandardReport = saasSleepEvaluateStandardReportService.getOne(new QueryWrapper<SaasSleepEvaluateStandardReport>().lambda()
} .orderByDesc(BaseEntity::getCreateDate)
saasSleepEvaluateStandardReportVO.setSleepScore(sleepScore); .last("limit 1"));
long totalScore = sleepScore + deepScore + lightScore - soberScore; double sleepTimeActionDuration = Double.parseDouble(analysisModel.getSleepTimeActionDuration());
saasSleepEvaluateStandardReportVO.setTotalScore(totalScore); int riseActionDuration = Integer.parseInt(analysisModel.getRiseActionDuration());
double riseLeaveThreshold = Double.parseDouble(analysisModel.getRiseLeaveThreshold());
double repeatSleepThreshold = Double.parseDouble(analysisModel.getRiseRepeatThreshold());
List<ElderSleepAnalysisVO> result = new ArrayList<>();
// 记录长者一天睡眠的记录 for (PlatRoomBedDevice platRoomBedDevice : platRoomBedDeviceList) {
long daySleepTime = 0; TreeMap<String, AnalysisVO> totalMap = new TreeMap<>();
long dayRestTime = 0;
for (PlatElderSleep elderSleep : elderSleepList) {
elderSleep.setElderId(elder.getId());
elderSleep.setHappenDate(currentDate);
elderSleep.setTenantId(tenantId);
if (ElderSleepType.SLEEP.getCode() == elderSleep.getElderSleepType()) { String bedId = platRoomBedDevice.getBedId();
daySleepTime += elderSleep.getInterval(); PlatElder elder = platElderService.getOne(new QueryWrapper<PlatElder>().lambda()
.eq(PlatElder::getBedId, bedId));
if (elder == null) {
continue;
}
PlatDevice platDevice = platDeviceService.getById(platRoomBedDevice.getDeviceId());
if (platDevice == null) {
continue;
}
String tenantId = elder.getTenantId();
for (String hourRange : dayHourRangeList) {
String[] hourRangeArray = hourRange.split("~");
List<DeviceOperationLogEntity> deviceOperationLogEntities = productDeviceService.getDeviceLogByTimeRange(platDevice.getOriDeviceId(), "reportProperty", 5000, hourRangeArray[0], hourRangeArray[1]);
if (CollectionUtils.isEmpty(deviceOperationLogEntities)) {
continue;
} }
if (ElderSleepType.REST.getCode() == elderSleep.getElderSleepType()) { List<DeviceInfoContentBreathe> deviceInfoContentBreatheList = deviceOperationLogEntities.stream()
dayRestTime += elderSleep.getInterval(); .filter(deviceOperationLogEntity -> deviceOperationLogEntity.getType().contains("reportProperty"))
.map(deviceOperationLogEntity -> {
DeviceInfoContentBreathe deviceInfoContentBreathe = JsonUtil.toObj((String) deviceOperationLogEntity.getContent(), DeviceInfoContentBreathe.class);
assert deviceInfoContentBreathe != null;
deviceInfoContentBreathe.setReportTime(formatLongTime(deviceInfoContentBreathe.getTimestamp()));
return deviceInfoContentBreathe;
})
.collect(Collectors.toList());
Map<String, List<DeviceInfoContentBreathe>> minuteMap = StreamUtil.groupBy(deviceInfoContentBreatheList, DeviceInfoContentBreathe::getReportTime);
deviceOperationLogEntities.clear();
TreeMap<String, AnalysisVO> statisticsMap = getPerMinuteData(minuteMap, analysisModel);
if (CollUtil.isNotEmpty(statisticsMap)) {
totalMap.putAll(statisticsMap);
} }
} }
int riseRepeatThreshold = Integer.parseInt(analysisModel.getRiseRepeatThreshold()); if (CollUtil.isEmpty(totalMap)) {
platElderSleepService.saveBatch(elderSleepList); continue;
String score = calculateScores(daySleepTime, dayRestTime, deepTime, soberTime, lightTime, evaluateStandardReport); }
EvaluateReportVO sleepReport = saasSleepEvaluateReportService.getByScore(Long.parseLong(score)); // 记录长者不同类型的睡眠时间
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList = Lists.newArrayList();
// 长者一天的睡眠分析 // 入睡时间
PlatElderSleepAnalysis elderSleepAnalysis = new PlatElderSleepAnalysis(); TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap =
elderSleepAnalysis.setElderId(elder.getId()); getFallAsleepData(totalMap, sleepTimeActionDuration, sleepTimeAnalysisVOList);
elderSleepAnalysis.setActionCount(totalActionCount); // 获取睡眠和小憩
elderSleepAnalysis.setTurnedCount(totalTurnedCount); SleepCountDTO sleepCountDTO = deepSleepAndRestData(sleepTypeMap, analysisModel, sleepTimeAnalysisVOList);
elderSleepAnalysis.setHappenDate(currentDate); // 满足睡眠条件的map
elderSleepAnalysis.setSleepScore(score); TreeMap<String, TreeMap<String, AnalysisVO>> daySleepMap = sleepCountDTO.getDaySleepMap();
elderSleepAnalysis.setSleepTime(String.valueOf(daySleepTime)); // 深度睡眠
elderSleepAnalysis.setRestTime(String.valueOf(dayRestTime)); deepSleepData(daySleepMap, analysisModel, sleepTimeAnalysisVOList);
elderSleepAnalysis.setSleepResult(sleepReport.getResult()); // 中度睡眠
elderSleepAnalysis.setTenantId(tenantId); middleSleep(daySleepMap, analysisModel, sleepTimeAnalysisVOList);
elderSleepAnalysis.setSleepEvaluate(sleepReport.getEvaluate()); // 清醒
elderSleepAnalysis.setOriDeviceId(platDevice.getOriDeviceId()); sober(sleepTypeMap, sleepTimeAnalysisVOList);
result.add(elderSleepAnalysis); // 起床
platElderSleepAnalysisService.save(elderSleepAnalysis); getUp(totalMap, riseLeaveThreshold, riseActionDuration, repeatSleepThreshold, sleepTimeAnalysisVOList, sleepCountDTO);
//浅睡
SleepTimeDTO sleepTimeDTO = groupSleepData(sleepTimeAnalysisVOList);
List<PlatElderSleep> platElderSleeps = saveElderSleep(sleepTimeDTO, elder.getId(), currentDate);
PlatElderSleepAnalysis platElderSleepAnalysis = savePlatElderSleepAnalysis(sleepTimeDTO, evaluateStandardReport, elder.getId(), currentDate,
sleepCountDTO, tenantId, platDevice.getOriDeviceId());
ElderSleepAnalysisVO analysisVO = BeanUtil.copyProperties(platElderSleepAnalysis, ElderSleepAnalysisVO.class);
analysisVO.setPlatElderSleeps(platElderSleeps);
result.add(analysisVO);
} }
return result; return result;
} }
...@@ -760,6 +890,9 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -760,6 +890,9 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
lightScore = Integer.parseInt(lightnessConfig.getScore()) * multiplier; lightScore = Integer.parseInt(lightnessConfig.getScore()) * multiplier;
} }
Integer totalScore = sleepScore.get() + deepScore.get() + lightScore - soberScore.get(); Integer totalScore = sleepScore.get() + deepScore.get() + lightScore - soberScore.get();
if (totalScore < 0) {
totalScore = 0;
}
return String.valueOf(totalScore); return String.valueOf(totalScore);
} }
...@@ -777,7 +910,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -777,7 +910,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
return DEFAULT_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault())); return DEFAULT_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
} }
public List<String> getLastDayHourRange(Integer month,Integer day) { public List<String> getLastDayHourRange(Integer month, Integer day) {
int count = 24; int count = 24;
LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime = LocalDateTime.now();
if (month != null && day != null) { if (month != null && day != null) {
...@@ -794,8 +927,4 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -794,8 +927,4 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
return list; return list;
} }
public static LocalDateTime getDayStart(LocalDate time) {
return time.atTime(0, 0, 0);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment