gradeData.js
4 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import NP from 'number-precision'
const gradeObj = {
gradeConfigList: [
{
servicePeriod: '',
maxPrice: '15',
minPrice: '0',
computeFlag: 0,
priceCoefficient: '',
standardShelfCost: '',
standardSalesCost: '',
shelfCost: '',
salesCost: '',
childSalePeriodData: []
},
{
servicePeriod: '',
maxPrice: '30',
minPrice: '15',
computeFlag: 0,
priceCoefficient: '',
standardShelfCost: '',
standardSalesCost: '',
shelfCost: '',
salesCost: '',
childSalePeriodData: []
},
{
servicePeriod: '',
maxPrice: '200',
minPrice: '30',
computeFlag: 0,
priceCoefficient: '',
standardShelfCost: '',
standardSalesCost: '',
shelfCost: '',
salesCost: '',
childSalePeriodData: []
}
],
salePeriodData: [],
serviceGrade: 'A',
serviceOverview: '',
serviceDetail: ''
}
const deepClone = obj => {
// Handle the 3 simple types, and null or undefined or function
if (obj == null || typeof obj !== 'object') return obj
// Handle Date
if (obj instanceof Date) {
const copy = new Date()
copy.setTime(obj.getTime())
return copy
}
// Handle Array or Object
if ((obj instanceof Array) | (obj instanceof Object)) {
const copy = obj instanceof Array ? [] : {}
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = deepClone(obj[attr])
}
}
return copy
}
throw new Error("Unable to clone obj! Its type isn't supported.")
}
export const addGradeItem = (grade = 'A', salePeriodList = []) => {
let _gradeObj = deepClone(gradeObj)
_gradeObj.serviceGrade = grade
salePeriodList = salePeriodList.sort(function(a, b) {
return a - b
})
_gradeObj.salePeriodData = salePeriodList.map(item => {
return { servicePeriod: item, standardShelfCost: '', standardSalesCost: '', shelfCost: '', salesCost: '' }
})
_gradeObj.gradeConfigList?.forEach(item => {
item.childSalePeriodData = deepClone(_gradeObj.salePeriodData)
})
console.log(_gradeObj)
return _gradeObj
}
export const handleGradeData = (dataFromApi, salePeriodList = []) => {
console.log(dataFromApi)
const data = deepClone(dataFromApi)
let arr = []
data.forEach(element => {
const { serviceOverview, serviceDetail, serviceGrade, gradeConfigList, id } = element
let _gradeObj = {
id,
serviceOverview,
serviceDetail,
serviceGrade,
gradeConfigList: []
}
// 根据区间去重
let intervalArr = gradeConfigList?.reduce((acc, current) => {
const exists = acc.some(
item => item.maxPrice === current.maxPrice && item.minPrice === current.minPrice
)
if (!exists) {
acc.push(current)
}
return acc
}, [])
intervalArr.forEach((item, index) => {
let { maxPrice, minPrice, priceCoefficient, computeFlag, id } = item || {}
_gradeObj.gradeConfigList.push({
maxPrice: NP.divide(maxPrice || 0, 1000000),
minPrice: NP.divide(minPrice || 0, 1000000),
priceCoefficient: priceCoefficient,
computeFlag,
id
})
})
salePeriodList = salePeriodList.sort(function(a, b) {
return a - b
})
_gradeObj.salePeriodData = salePeriodList.map(item => {
const _target = element.gradeConfigList.find(o => o.servicePeriod == item)
return { servicePeriod: item, standardShelfCost: NP.divide(_target?.standardShelfCost || 0, 100), standardSalesCost: NP.divide(_target?.standardSalesCost || 0, 100) }
})
_gradeObj.gradeConfigList?.forEach(item => {
let arr = []
salePeriodList?.forEach(it => {
let _obj = gradeConfigList.find(o => o.servicePeriod == it && NP.divide(o.minPrice || 0, 1000000) == item.minPrice && NP.divide(o.maxPrice || 0, 1000000) == item.maxPrice)
arr.push({ id: _obj?.id, servicePeriod: it, shelfCost: _obj?.shelfCost, salesCost: _obj?.salesCost })
})
item.childSalePeriodData = arr
})
arr.push(_gradeObj)
})
return arr
}