spendStatistics.vue
8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<template>
<div>
<CardGroupIndex :data="cardData"></CardGroupIndex>
<div class="g-card">
<div class="g-card-title">支出趋势</div>
<div>
<el-button-group>
<el-button type="primary" :plain="timeRange != 7" @click="setTime(7)">近7日</el-button>
<el-button type="primary" :plain="timeRange != 30" @click="setTime(30)">近30日</el-button>
</el-button-group>
<el-date-picker
v-model="dateRange"
class="ml20"
style="width: 260px"
value-format="yyyy-MM-dd"
type="daterange"
:clearable="false"
range-separator="-"
start-placeholder="开始日期"
end-placeholder="结束日期"
@change="getList"></el-date-picker>
</div>
<div v-loading="loadingChart">
<div id="chart" ref="chart" style="width: 90%; height: 400px"></div>
</div>
</div>
<div class="g-card">
<div class="g-card-title">支出记录</div>
<el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" max-height="600">
<el-table-column label="员工名称" align="center" prop="userName" show-overflow-tooltip />
<el-table-column label="发送类型" align="center" prop="receiveType">
<template slot-scope="{ row }">
{{ { 1: '好友客户', 2: '群成员' }[row.receiveType] }}
</template>
</el-table-column>
<el-table-column label="发送时间" align="center" prop="createTime"></el-table-column>
<el-table-column label="发送金额(元)" align="center" prop="totalMoney" width="180"></el-table-column>
</el-table>
<pagination
:total="total"
:page.sync="query.pageNum"
:limit.sync="query.pageSize"
@pagination="getRecordList()" />
</div>
</div>
</template>
<script>
import * as echarts from 'echarts'
import { parseTime } from '@/utils/common'
// import TabContent from './components/TabContent'
import { getAnalysis, getChartList, getRecordList } from '@/api/redPacketTool/spendStatistics'
function generateMockData() {
// 7天和30天的假数据
let now = new Date().valueOf()
const oneDay = 1000 * 60 * 60 * 24
let xAxis = []
let payNums = []
let amount = []
new Array(30).fill(1).forEach((i, idx) => {
payNums.push(parseInt(Math.random() * 100 + 60))
amount.push(parseInt(Math.random() * 120 + 10))
xAxis.push(parseTime(now - idx * oneDay, '{y}-{m}-{d}'))
})
return {
7: {
data: [payNums.slice(0, 8), amount.slice(0, 8)],
xAxis: xAxis.slice(0, 8),
},
30: {
data: [payNums, amount],
xAxis,
},
}
}
export default {
name: 'total-data',
components: {},
data() {
return {
timeRange: 7,
dateRange: [],
loading: false,
loadingChart: false,
query: {
pageNum: 1,
pageSize: 10,
},
list: [],
total: 0,
cardData: [],
// mockData: null
}
},
mounted() {
this.$store.dispatch(
'app/setBusininessDesc',
`
<div>统计分析企业支出金额及笔数情况</div>
`,
)
// this.mockData = generateMockData()
this.getList()
this.setTime(7)
this.getRecordList()
},
methods: {
getList() {
this.loading = true
getAnalysis()
.then(({ data }) => {
this.cardData = [
{
title: '累计支出金额(元)',
value: data.totalMoney / 100,
noArrow: true,
},
{
title: '累计支出笔数',
value: data.totalNum,
noArrow: true,
},
{
title: '今日支出金额(元)',
value: data.currentMoney / 100,
noArrow: true,
},
{
title: '今日支出笔数',
value: data.currentNum,
noArrow: true,
},
]
})
.catch((e) => {
console.log(e)
})
.finally(() => {
this.loading = false
})
},
getChartList() {
this.loadingChart = true
if (this.dateRange) {
this.query.beginTime = this.dateRange[0]
this.query.endTime = this.dateRange[1]
} else {
this.query.beginTime = ''
this.query.endTime = ''
}
getChartList(this.query)
.then(({ data }) => {
let seriesData = [] // this.mockData[this.timeRange]
seriesData[0] = data.map((e) => e.totalMoney / 100)
seriesData[1] = data.map((e) => e.totalNum)
let xAxisData = data.map((e) => e.createTime.slice(0, 10))
this.setEchart(xAxisData, seriesData)
})
.catch((e) => {
console.log(e)
})
.finally(() => {
this.loadingChart = false
})
},
getRecordList(page) {
page && (this.query.pageNum = page)
this.loading = true
getRecordList(this.query)
.then(({ rows, total }) => {
rows.forEach((element) => {
element.totalMoney /= 100
})
this.list = rows
this.total = +total
})
.catch((e) => {
console.log(e)
})
.finally(() => {
this.loading = false
})
},
handleSelectionChange() {},
setTime(days) {
this.timeRange = days
let date = new Date()
date.setDate(date.getDate() - days)
this.dateRange = [this.getTime(date), this.getTime()]
this.getChartList()
},
getTime(datePar) {
const d = datePar ? new Date(datePar) : new Date()
const year = d.getFullYear()
const month = this.getHandledValue(d.getMonth() + 1)
const date = this.getHandledValue(d.getDate())
return year + '-' + month + '-' + date
},
getHandledValue(num) {
return num < 10 ? '0' + num : num
},
setEchart(xAxisData, seriesData) {
setTimeout(() => {
this.drawLine('chart', ['支出金额', '支出笔数'], xAxisData, seriesData, ['#5AD2D2', '#637BC0'])
}, 0)
},
drawLine(id, legend, xAxisData, seriesData, color) {
const dom = document.getElementById(id)
let width = dom.parentNode.offsetWidth || window.innerWidth - 100
dom.style.width = width + 'px'
dom.style.height = '400px'
const charts = echarts.init(dom)
let series = []
let yAxis = []
seriesData.forEach((iData, index) => {
let max = iData.slice().sort((a, b) => b - a)[0]
// 最大值上取整最近的10的正数倍
max = Math.ceil(max / 10) * 10
yAxis.push({
name: legend[index],
type: 'value',
min: 0,
max,
interval: max / 5,
axisLine: {
// show: false,
lineStyle: {
color: '#ccc',
},
},
})
series.push({
name: legend[index],
type: index === 0 ? 'line' : 'bar',
yAxisIndex: index,
smooth: true,
data: iData,
itemStyle: {
normal: {
lineStyle: {
color: color[index],
},
},
},
textStyle: {
color: '#fff', // 主标题文字的颜色。
},
})
})
let chartOption = {
color: color,
tooltip: {
trigger: 'axis',
},
legend: {
data: legend,
bottom: '0%',
},
grid: {
left: '3%',
right: '4%',
bottom: '40px',
containLabel: true,
},
xAxis: {
type: 'category',
// boundaryGap: false,
data: xAxisData,
axisLine: {
lineStyle: {
color: '#ccc',
},
},
},
yAxis,
series,
}
charts.setOption(chartOption)
new ResizeObserver((entries) => {
charts.resize()
}).observe(dom)
},
},
}
</script>
<style lang="scss" scoped></style>