5e0c6e5a
“wangming”
feat: 完善会员画像功能,优化...
|
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
</div>
</div>
<div ref="storeChart" class="chart-container"></div>
</div>
</div>
</template>
<script>
import request from '@/utils/request'
import dayjs from 'dayjs'
import * as echarts from 'echarts'
import { kpiDrillMixin } from './mixins'
export default {
name: 'TargetAnalysis',
mixins: [kpiDrillMixin],
props: {
filters: {
type: Object,
default: () => ({ startTime: null, endTime: null, storeIds: [], month: null })
},
extra: { type: Object, default: () => ({}) },
storeOptions: { type: Array, default: () => [] }
},
data() {
return {
loading: false,
list: [],
displayList: [],
overview: {
totalTarget: 0,
totalCompleted: 0,
completionRate: 0,
estimatedRate: 0,
dailyRequired: 0,
remainingDays: 0,
achievedCount: 0,
unachievedCount: 0,
lastMonthRate: null,
lastMonthComparison: 0,
dailyRequiredFor100: 0,
isCurrentMonth: true // 是否是当前月份
},
topStore: null,
topAchievedStores: [],
topUnachievedStores: []
}
},
watch: {
filters: {
deep: true,
handler() {
this.resetAndFetch()
}
}
},
mounted() {
this.fetchData()
},
methods: {
resetAndFetch() {
this.fetchData()
},
async fetchData() {
this.loading = true
try {
// 获取门店目标数据 - 不传分页参数,获取所有数据
const url = '/api/Extend/LqMdTarget'
const data = {
currentPage: 1,
pageSize: 1000 // 设置一个较大的值,确保获取所有门店
}
if (this.filters && this.filters.month) data.Month = this.filters.month
if (this.filters && this.filters.storeIds && this.filters.storeIds.length > 0) {
data.StoreId = this.filters.storeIds[0] // 如果只选了一个门店
// 如果选了多个门店,可能需要调整接口支持
}
const res = await request({ url, method: 'GET', data })
await this.handleResponse(res)
} catch (error) {
console.error('Target analysis load error:', error)
this.$message.error(error.message || '加载数据失败')
this.list = []
this.displayList = []
} finally {
this.loading = false
}
},
async handleResponse(res) {
let list = []
if (res && res.data) {
if (res.data.list && Array.isArray(res.data.list)) {
list = res.data.list
} else if (Array.isArray(res.data)) {
list = res.data
}
}
const fmtMonth = v => {
const s = (v || '').toString()
if (s.length === 6) return `${s.slice(0, 4)}-${s.slice(4)}`
return s
}
// 从storeOptions中查找门店名称
const getStoreName = (storeId) => {
if (!storeId) return '未知门店'
const store = this.storeOptions.find(s => s.id === storeId)
return store ? (store.fullName || store.dm || store.name || store.label || '未知门店') : '未知门店'
}
list = list.map(i => {
// 注意:接口返回的字段名可能是小写的 storeId, storeTarget
const storeId = i.StoreId || i.storeId || ''
const storeTarget = i.StoreTarget || i.storeTarget || i.storeTarget || 0
return {
storeId: storeId,
storeName: getStoreName(storeId),
month: i.Month || i.month || '',
monthText: fmtMonth(i.Month || i.month || ''),
storeTarget: Number(storeTarget)
}
})
// 过滤掉没有门店ID的数据
list = list.filter(item => item.storeId)
// 获取每个门店的实际业绩
const timeRange = this.buildDateRange()
const metrics = await Promise.all(list.map(async row => {
if (!row.storeId) return null
try {
const resKpi = await request({
url: '/api/Extend/LqReport/get-business-statistics',
method: 'POST',
data: {
startTime: timeRange ? `${timeRange.start} 00:00:00` : null,
endTime: timeRange ? `${timeRange.end} 23:59:59` : null,
storeIds: [row.storeId]
}
})
const d = resKpi.data || {}
return {
storeId: row.storeId,
billing: Number(d.TotalBillingAmount || d.billing_amount || 0),
refund: Number(d.TotalRefundAmount || d.refund_amount || 0),
net: Number(d.TotalBillingAmount || 0) - Number(d.TotalRefundAmount || 0)
}
} catch (error) {
console.error(`获取门店 ${row.storeId} 业绩失败:`, error)
return {
storeId: row.storeId,
billing: 0,
refund: 0,
net: 0
}
}
}))
const map = {}
metrics.filter(Boolean).forEach(m => { map[m.storeId] = m })
// 计算完成率和是否达成
list = list.map(row => {
const m = map[row.storeId] || { billing: 0, refund: 0, net: 0 }
const completionRate = (row.storeTarget || 0) > 0
? ((m.net || 0) / row.storeTarget * 100)
: 0
const achieved = (row.storeTarget || 0) > 0 ? ((m.net || 0) >= row.storeTarget ? '达成' : '未达成') : '未设置'
return {
...row,
actualBilling: m.billing || 0,
refundAmount: m.refund || 0,
netBilling: m.net || 0,
completionRate: completionRate,
achieved
}
})
this.list = list
// 不再需要displayList,因为列表已删除
// 计算整体统计
this.calculateOverview()
// 获取上月数据
await this.fetchLastMonthData()
// 渲染图表
this.$nextTick(() => {
this.renderStoreChart()
})
},
calculateOverview() {
const totalTarget = this.list.reduce((sum, row) => sum + (row.storeTarget || 0), 0)
const totalCompleted = this.list.reduce((sum, row) => sum + (row.netBilling || 0), 0)
const completionRate = totalTarget > 0 ? (totalCompleted / totalTarget * 100) : 0
// 判断是否是当前月份或未来月份
const now = dayjs()
const timeRange = this.buildDateRange()
// 获取查询的月份
const queryMonth = timeRange ? dayjs(timeRange.start) : now
const queryMonthStart = queryMonth.startOf('month')
const queryMonthEnd = queryMonth.endOf('month')
const currentMonthStart = now.startOf('month')
// 判断是否是当前月份(查询月份等于当前月份)
const isCurrentMonth = queryMonthStart.isSame(currentMonthStart, 'month')
// 判断月份是否已过完(查询月份结束时间小于当前时间)
const isMonthFinished = queryMonthEnd.isBefore(now, 'day')
let estimatedRate = 0
let dailyRequiredFor100 = 0
let remainingDays = 0
// 只有当前月份且未过完时才计算预计完成率和每日需完成
if (isCurrentMonth && !isMonthFinished) {
// 计算预计完成率
const monthStart = now.startOf('month')
const monthEnd = now.endOf('month')
// 已过天数:从月初到现在(包括今天)
const actualPassedDays = now.diff(monthStart, 'day') + 1
// 总天数:这个月的总天数
const actualTotalDays = monthEnd.diff(monthStart, 'day') + 1
remainingDays = Math.max(0, actualTotalDays - actualPassedDays)
// 预计完成率计算:已完成业绩 / 已过天数 = 平均每天业绩,然后 * 总天数 = 预计完成业绩,最后 / 目标 * 100
const avgDailyPerformance = actualPassedDays > 0 ? (totalCompleted / actualPassedDays) : 0
const estimatedCompleted = avgDailyPerformance * actualTotalDays
estimatedRate = totalTarget > 0 ? (estimatedCompleted / totalTarget * 100) : 0
// 计算每天需要达到的金额(用于达成100%)
const remainingAmount = Math.max(0, totalTarget - totalCompleted)
dailyRequiredFor100 = remainingDays > 0 ? (remainingAmount / remainingDays) : 0
}
// 统计达成情况
const achievedList = this.list.filter(row => row.achieved === '达成')
const unachievedList = this.list.filter(row => row.achieved === '未达成')
// 排名第一的门店
const sortedByNet = [...this.list].sort((a, b) => (b.netBilling || 0) - (a.netBilling || 0))
this.topStore = sortedByNet[0] || null
// 已完成门店排名(按完成率)
this.topAchievedStores = achievedList.sort((a, b) => (b.completionRate || 0) - (a.completionRate || 0))
// 未完成门店排名(按完成率,从高到低)
this.topUnachievedStores = unachievedList.sort((a, b) => (b.completionRate || 0) - (a.completionRate || 0))
this.overview = {
totalTarget,
totalCompleted,
completionRate,
estimatedRate,
dailyRequired: dailyRequiredFor100,
remainingDays,
achievedCount: achievedList.length,
unachievedCount: unachievedList.length,
lastMonthRate: null,
lastMonthComparison: 0,
dailyRequiredFor100,
isCurrentMonth: isCurrentMonth && !isMonthFinished
}
},
async fetchLastMonthData() {
try {
const timeRange = this.buildDateRange()
const currentMonth = timeRange ? dayjs(timeRange.start) : dayjs()
const lastMonthStart = currentMonth.subtract(1, 'month').startOf('month')
const lastMonthEnd = currentMonth.subtract(1, 'month').endOf('month')
const res = await request({
url: '/api/Extend/LqReport/get-business-statistics',
method: 'POST',
data: {
startTime: `${lastMonthStart.format('YYYY-MM-DD')} 00:00:00`,
endTime: `${lastMonthEnd.format('YYYY-MM-DD')} 23:59:59`,
storeIds: this.filters && this.filters.storeIds ? this.filters.storeIds : []
}
})
const data = res.data || {}
const lastMonthTarget = data.TargetBillingAmount || 0
const lastMonthCompleted = data.CompletedBillingAmount || 0
const lastMonthRate = lastMonthTarget > 0 ? (lastMonthCompleted / lastMonthTarget * 100) : 0
this.overview.lastMonthRate = lastMonthRate
this.overview.lastMonthComparison = this.overview.completionRate - lastMonthRate
} catch (error) {
console.error('获取上月数据失败:', error)
}
},
renderStoreChart() {
const dom = this.$refs.storeChart
if (!dom) return
const chart = echarts.init(dom)
// 使用所有门店数据,不限制数量
|
5e0c6e5a
“wangming”
feat: 完善会员画像功能,优化...
|
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
const targets = chartData.map(item => item.storeTarget || 0)
// 使用净业绩(开单-退卡),与目标对比
const completed = chartData.map(item => item.netBilling || 0)
chart.setOption({
tooltip: {
trigger: 'axis',
axisPointer: { type: 'shadow' },
formatter: params => {
const p = Array.isArray(params) ? params : [params]
let result = `${p[0].name}<br/>`
p.forEach(item => {
const value = Number(item.value || 0)
result += `${item.seriesName}: ¥${this.formatMoney(value)}<br/>`
})
return result
}
},
legend: {
data: ['目标业绩', '净业绩'],
top: 10,
textStyle: { color: '#606266' }
},
grid: {
left: '12%',
right: '4%',
top: '15%',
bottom: chartData.length > 20 ? '25%' : '15%',
containLabel: false
},
xAxis: {
type: 'category',
data: storeNames,
axisLabel: {
color: '#606266',
fontSize: chartData.length > 20 ? 9 : 11,
rotate: chartData.length > 15 ? 45 : 0,
interval: 0
},
axisLine: { lineStyle: { color: '#dcdfe6' } }
},
yAxis: {
type: 'value',
name: '金额',
axisLabel: { color: '#606266' },
splitLine: { lineStyle: { color: '#ebeef5' } }
},
series: [
{
name: '目标业绩',
type: 'bar',
data: targets,
itemStyle: { color: '#E6A23C' },
barWidth: chartData.length > 20 ? 8 : 12
},
{
name: '净业绩',
type: 'bar',
data: completed,
itemStyle: { color: '#67C23A' },
barWidth: chartData.length > 20 ? 8 : 12
}
]
})
}
}
}
</script>
<style lang="scss" scoped>
.target-wrapper {
width: 100%;
display: flex;
flex-direction: column;
gap: 16px;
|