|
|
@ -194,24 +194,48 @@ const CommonUtil = {
|
|
|
|
sleepArray.forEach((item,index) => {
|
|
|
|
sleepArray.forEach((item,index) => {
|
|
|
|
// 判断是否是同一天的数据
|
|
|
|
// 判断是否是同一天的数据
|
|
|
|
let isSameDay = this.timeComparison(compareDate,compareMinute, item.yyyyMMdd, item.minute);
|
|
|
|
let isSameDay = this.timeComparison(compareDate,compareMinute, item.yyyyMMdd, item.minute);
|
|
|
|
|
|
|
|
// if(compareDate== "20250128"){
|
|
|
|
|
|
|
|
// console.log("是否是同一天:",isSameDay)
|
|
|
|
|
|
|
|
// }
|
|
|
|
// 如果状态发生变化,且日期不同,就表示开始新的睡眠数据
|
|
|
|
// 如果状态发生变化,且日期不同,就表示开始新的睡眠数据
|
|
|
|
if (item.status !== firstSleepData.status) {
|
|
|
|
// if (item.status !== firstSleepData.status) {
|
|
|
|
if (isSameDay) {
|
|
|
|
// if (isSameDay) {
|
|
|
|
resItem.sleepList.push(item);
|
|
|
|
// resItem.sleepList.push(item);
|
|
|
|
} else {
|
|
|
|
// } else {
|
|
|
|
// 取出数据
|
|
|
|
// // 取出数据
|
|
|
|
let lastItem = sleepArray[index - 1];
|
|
|
|
// let lastItem = sleepArray[index - 1];
|
|
|
|
lastItem.minute = lastItem.minute + 1;//加1分钟作为起床点
|
|
|
|
// lastItem.minute = lastItem.minute + 1;//加1分钟作为起床点
|
|
|
|
resItem.sleepList.push(lastItem);
|
|
|
|
// resItem.sleepList.push(lastItem);
|
|
|
|
res.push(resItem); // 否则,将当天的数据推入结果数组
|
|
|
|
// res.push(resItem); // 否则,将当天的数据推入结果数组
|
|
|
|
resItem = { // 新的一天开始
|
|
|
|
// resItem = { // 新的一天开始
|
|
|
|
date: item.yyyyMMdd,
|
|
|
|
// date: item.yyyyMMdd,
|
|
|
|
sleepList: [item] // 将当前项作为新的睡眠数据开始
|
|
|
|
// sleepList: [item] // 将当前项作为新的睡眠数据开始
|
|
|
|
};
|
|
|
|
// };
|
|
|
|
compareDate = item.yyyyMMdd; // 更新对比日期,用于比对后续数据
|
|
|
|
// compareDate = item.yyyyMMdd; // 更新对比日期,用于比对后续数据
|
|
|
|
compareMinute = item.minute;
|
|
|
|
// compareMinute = item.minute;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// firstSleepData = item; // 更新当前比对的睡眠数据
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(isSameDay && item.status !== firstSleepData.status){
|
|
|
|
|
|
|
|
// 如果是一天的睡眠
|
|
|
|
|
|
|
|
resItem.sleepList.push(item);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// 如果不是同一天的睡眠
|
|
|
|
|
|
|
|
if(!isSameDay){
|
|
|
|
|
|
|
|
let lastItem = sleepArray[index - 1];
|
|
|
|
|
|
|
|
// console.log("lastItem:",lastItem)
|
|
|
|
|
|
|
|
lastItem.minute = lastItem.minute + 1;//加1分钟作为起床点
|
|
|
|
|
|
|
|
resItem.sleepList.push(lastItem);
|
|
|
|
|
|
|
|
res.push(resItem); // 否则,将当天的数据推入结果数组
|
|
|
|
|
|
|
|
resItem = { // 新的一天开始
|
|
|
|
|
|
|
|
date: item.yyyyMMdd,
|
|
|
|
|
|
|
|
sleepList: [item] // 将当前项作为新的睡眠数据开始
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
compareDate = item.yyyyMMdd; // 更新对比日期,用于比对后续数据
|
|
|
|
|
|
|
|
compareMinute = item.minute;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
firstSleepData = item; // 更新当前比对的睡眠数据
|
|
|
|
firstSleepData = item; // 更新当前比对的睡眠数据
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -229,23 +253,12 @@ const CommonUtil = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 计算出一天的时间范围
|
|
|
|
* 计算出一天的时间范围,手表计算睡眠的时间为20:00~11:00(即晚上20点至第二天的11点)
|
|
|
|
* @param {对比日期} compareDate
|
|
|
|
* @param {对比日期} compareDate
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
getDayTimeRange(compareDate,compareMinute) {
|
|
|
|
getDayTimeRange(compareDate,compareMinute) {
|
|
|
|
// 将 "YYYYMMDD" 格式的日期转换为 "YYYY-MM-DD" 格式,方便解析
|
|
|
|
// 将 "YYYYMMDD" 格式的日期转换为 "YYYY-MM-DD" 格式,方便解析
|
|
|
|
const formattedDateStr = compareDate.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3');
|
|
|
|
const formattedDateStr = compareDate.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3');
|
|
|
|
// 创建目标日期对象
|
|
|
|
|
|
|
|
// const date = new Date(formattedDateStr); // 例如:2024-12-12
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取对比日期的Date,前一天的20:00:00
|
|
|
|
|
|
|
|
// const prevDayStart = new Date(date);
|
|
|
|
|
|
|
|
// prevDayStart.setDate(date.getDate() - 1); //设置为前一天
|
|
|
|
|
|
|
|
// prevDayStart.setHours(20, 0, 0, 0); // 设置为20:00:00
|
|
|
|
|
|
|
|
// // 获取对比日期的Date,后一天的10:00:00
|
|
|
|
|
|
|
|
// const nextDayEnd = new Date(date);
|
|
|
|
|
|
|
|
// nextDayEnd.setDate(date.getDate()); // 设置为后一天
|
|
|
|
|
|
|
|
// nextDayEnd.setHours(10, 0, 0, 0); // 设置为10:00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取对比日期的Date,前一天的20:00:00
|
|
|
|
// 获取对比日期的Date,前一天的20:00:00
|
|
|
|
const date1 = new Date(formattedDateStr)
|
|
|
|
const date1 = new Date(formattedDateStr)
|
|
|
@ -256,15 +269,15 @@ const CommonUtil = {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
prevDayStart.setDate(date1.getDate() - 1);
|
|
|
|
prevDayStart.setDate(date1.getDate() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevDayStart.setHours(20, 0, 0, 0); // 设置为20:00:00
|
|
|
|
prevDayStart.setHours(20, 0, 0, 0);
|
|
|
|
// 获取对比日期的Date,后一天的10:00:00
|
|
|
|
// 获取对比日期的Date,后一天的11:00:00
|
|
|
|
const nextDayEnd = new Date(date2);
|
|
|
|
const nextDayEnd = new Date(date2);
|
|
|
|
if(this.isEveningTime(compareMinute)){
|
|
|
|
if(this.isEveningTime(compareMinute)){
|
|
|
|
nextDayEnd.setDate(date2.getDate() + 1);
|
|
|
|
nextDayEnd.setDate(date2.getDate() + 1);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
nextDayEnd.setDate(date2.getDate());
|
|
|
|
nextDayEnd.setDate(date2.getDate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextDayEnd.setHours(10, 0, 0, 0); // 设置为10:00:00
|
|
|
|
nextDayEnd.setHours(11, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 返回时间戳
|
|
|
|
// 返回时间戳
|
|
|
|
return {
|
|
|
|
return {
|
|
|
@ -311,6 +324,10 @@ const CommonUtil = {
|
|
|
|
|
|
|
|
|
|
|
|
let dateRange = this.getDayTimeRange(compareDate,compareMinute);
|
|
|
|
let dateRange = this.getDayTimeRange(compareDate,compareMinute);
|
|
|
|
let time = this.getTimestampFromDateAndMinutes(comparisonValue,minute);
|
|
|
|
let time = this.getTimestampFromDateAndMinutes(comparisonValue,minute);
|
|
|
|
|
|
|
|
// if(compareDate == "20250128"){
|
|
|
|
|
|
|
|
// console.log(`start:${dateRange.startTimestamp}-end:${dateRange.endTimestamp}-time${time}`)
|
|
|
|
|
|
|
|
// console.log(`${compareDate}-${compareMinute}-${comparisonValue}-${minute}`)
|
|
|
|
|
|
|
|
// }
|
|
|
|
if(time >= dateRange.startTimestamp && time <= dateRange.endTimestamp){
|
|
|
|
if(time >= dateRange.startTimestamp && time <= dateRange.endTimestamp){
|
|
|
|
// 在范围内,是同一天睡眠
|
|
|
|
// 在范围内,是同一天睡眠
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -320,15 +337,6 @@ const CommonUtil = {
|
|
|
|
|
|
|
|
|
|
|
|
sortSleep(data){
|
|
|
|
sortSleep(data){
|
|
|
|
data.forEach((item) =>{
|
|
|
|
data.forEach((item) =>{
|
|
|
|
// 排序算法
|
|
|
|
|
|
|
|
// item.sleepList.sort((a, b) => {
|
|
|
|
|
|
|
|
// // 先比较 yyyyMMdd
|
|
|
|
|
|
|
|
// if (a.yyyyMMdd !== b.yyyyMMdd) {
|
|
|
|
|
|
|
|
// return a.yyyyMMdd.localeCompare(b.yyyyMMdd);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // 再比较 minute
|
|
|
|
|
|
|
|
// return a.minute - b.minute;
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// 因为后端的接口处理方式为如果第一个数据为清醒,不会作为睡眠时间,故将第一个数据作为浅睡
|
|
|
|
// 因为后端的接口处理方式为如果第一个数据为清醒,不会作为睡眠时间,故将第一个数据作为浅睡
|
|
|
|
item.sleepList[0].status = 1
|
|
|
|
item.sleepList[0].status = 1
|
|
|
|
item.date = item.sleepList[item.sleepList.length - 1].yyyyMMdd
|
|
|
|
item.date = item.sleepList[item.sleepList.length - 1].yyyyMMdd
|
|
|
@ -337,7 +345,7 @@ const CommonUtil = {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 睡眠计算时间是晚上的20:00~到第二天上午11:00,即上午11点~晚上20点间隔540分钟
|
|
|
|
* @param {补全数据} data
|
|
|
|
* @param {补全数据} data
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
completeMissingData(data){
|
|
|
|
completeMissingData(data){
|
|
|
@ -360,7 +368,7 @@ const CommonUtil = {
|
|
|
|
if (i < data.length - 1) {
|
|
|
|
if (i < data.length - 1) {
|
|
|
|
let currentMinute = data[i].minute;
|
|
|
|
let currentMinute = data[i].minute;
|
|
|
|
let nextMinute = data[i + 1].minute;
|
|
|
|
let nextMinute = data[i + 1].minute;
|
|
|
|
if(data[i].yyyyMMdd == data[i + 1].yyyyMMdd && nextMinute - currentMinute < 600){
|
|
|
|
if(data[i].yyyyMMdd == data[i + 1].yyyyMMdd && nextMinute - currentMinute < 540){
|
|
|
|
// 如果不连续,补全缺失的数据
|
|
|
|
// 如果不连续,补全缺失的数据
|
|
|
|
if (nextMinute > currentMinute + 1) {
|
|
|
|
if (nextMinute > currentMinute + 1) {
|
|
|
|
for (let j = currentMinute + 1; j < nextMinute; j++) {
|
|
|
|
for (let j = currentMinute + 1; j < nextMinute; j++) {
|
|
|
@ -378,10 +386,10 @@ const CommonUtil = {
|
|
|
|
|
|
|
|
|
|
|
|
if(CommonUtil.isNextDay(currentDate,nextDate) &&
|
|
|
|
if(CommonUtil.isNextDay(currentDate,nextDate) &&
|
|
|
|
currentMinute > 1200 && nextDate > 480 ){
|
|
|
|
currentMinute > 1200 && nextDate > 480 ){
|
|
|
|
console.log(currentDate,currentMinute);
|
|
|
|
// console.log(currentDate,currentMinute);
|
|
|
|
console.log(nextDate,nextMinute);
|
|
|
|
// console.log(nextDate,nextMinute);
|
|
|
|
console.log("相减的差值:",nextMinute - currentMinute);
|
|
|
|
// console.log("相减的差值:",nextMinute - currentMinute);
|
|
|
|
console.log("是否跨天:",CommonUtil.isNextDay(currentDate,nextDate));
|
|
|
|
// console.log("是否跨天:",CommonUtil.isNextDay(currentDate,nextDate));
|
|
|
|
if(currentMinute < 1439){
|
|
|
|
if(currentMinute < 1439){
|
|
|
|
for (let j = currentMinute + 1; j < 1440; j++) {
|
|
|
|
for (let j = currentMinute + 1; j < 1440; j++) {
|
|
|
|
result.push({
|
|
|
|
result.push({
|
|
|
@ -401,30 +409,7 @@ const CommonUtil = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if(nextMinute - currentMinute < 1){
|
|
|
|
|
|
|
|
// let currentDate = parseInt(data[i].yyyyMMdd);
|
|
|
|
|
|
|
|
// let nextDate = parseInt(data[i + 1].yyyyMMdd);
|
|
|
|
|
|
|
|
// if((nextDate - currentDate) == 1){
|
|
|
|
|
|
|
|
// for (let j = currentMinute + 1; j <= 1440; j++) {
|
|
|
|
|
|
|
|
// result.push({
|
|
|
|
|
|
|
|
// minute: j,
|
|
|
|
|
|
|
|
// status: 3,
|
|
|
|
|
|
|
|
// yyyyMMdd: data[i].yyyyMMdd
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (let j = 0; j < nextMinute; j++) {
|
|
|
|
|
|
|
|
// result.push({
|
|
|
|
|
|
|
|
// minute: j,
|
|
|
|
|
|
|
|
// status: 3,
|
|
|
|
|
|
|
|
// yyyyMMdd: data[i].yyyyMMdd
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|