supple.js
2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 补充数据
// 数据分组
export function groupData(data) {
let groupObject = {}
data.forEach(item => {
groupObject[item.mallId]
? groupObject[item.mallId].push(item)
: groupObject[item.mallId] = [item]
})
return groupObject
}
// 计算开始结束时间总天数
export function caleCountDay(startDate, endDate) {
let s_stamp = (
typeof(startDate) === 'string'
? new Date(startDate.replace(/-/g, '/'))
: new Date(startDate)
).getTime()
let e_stamp = (
typeof(startDate) === 'string'
? new Date(endDate.replace(/-/g, '/'))
: new Date(endDate)
).getTime()
let dateObj = {}
Array.from(
{ length: (e_stamp - s_stamp) / (24 * 60 * 60 * 1000) + 1 },
(v, k) => s_stamp + (k * 24 * 60 * 60 * 1000)
).forEach(item => {
dateObj[item] = null
})
return dateObj
}
// 格式化年月日时分秒
export function formatStamp(stamp) {
let time = new Date(stamp)
return [
[
time.getFullYear(),
time.getMonth() + 1,
time.getDate()].join('-'),
[
time.getHours(),
time.getMinutes(),
time.getSeconds()
].join(':')
].join(' ').replace(/(?=\b\d\b)/g, '0')
}
/**
* 补全数据
* @param {number} currentStamp
* @param {number} lastStamp
* @param {object} keyExample
* @param {string} needKey
* @returns {array}
*/
export function suppleHandle(stamp, keyExample, needKey = 'countdate') {
let temp = {}
Object.keys(keyExample).forEach(key => {
if (key !== 'mall') {
temp[key] = '--'
} else {
temp[key] = keyExample[key]
}
temp[needKey] = formatStamp(stamp)
})
return temp
}
/**
* complate data
* @param {array} data
* @param {string} startDate
* @param {string} endDate
* @returns {array}
*/
export function complateData(data, startDate, endDate) {
let resultData = []
let groups = groupData(data)
let countDayObj = caleCountDay(startDate, endDate)
Object.keys(groups).forEach(key => {
if (groups[key].length < Object.keys(countDayObj).length) {
let completeData = []
// 遍历开始结束日期对象, 补全数据
for (let [k, v] of Object.entries(countDayObj)) {
let filterExist = groups[key].filter((item, index) => {
let curStamp = new Date(item.countdate.split(' ')[0].replace(/-/g, '/')).getTime()
return curStamp == k
})
if (!filterExist.length) {
countDayObj[k] = suppleHandle(k - 0, groups[key][0])
} else {
countDayObj[k] = filterExist[0]
}
}
Object.keys(countDayObj).forEach(k => {
resultData.push(countDayObj[k])
})
} else {
resultData.push(...groups[key])
}
})
return resultData
}