diff --git a/app/service/systemMethods/dashboard.js b/app/service/systemMethods/dashboard.js index 1b16148..f78ac3e 100644 --- a/app/service/systemMethods/dashboard.js +++ b/app/service/systemMethods/dashboard.js @@ -10,26 +10,61 @@ class dashboardService extends MainService { const _noNeedLog = '*'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog); } + async storeStats() { const { ctx } = this; - const warehouseId = ctx.user?.store_id; + const storeType = String(ctx.user?.store_type || '').trim(); + if (storeType === 'store') { + return await this.storeSideStoreStats(); + } + + // 仓库端:单仓查询 + const warehouseId = Number(ctx.user?.store_id); if (!warehouseId) { return this.fail('缺少仓库上下文'); } + const stats = await this.queryWarehouseStats(warehouseId); + return this.success('ok', this.mergeStoreStats([stats])); + } + async storeSideStoreStats() { + const { ctx } = this; + const storeId = Number(ctx.user?.store_id); + if (!storeId) { + return this.fail('缺少货主上下文'); + } + + // 获取签约仓库 + const contracts = await ctx.model.StoreWarehouse.findAll({ + where: { store_id: storeId, auth_status: 2 }, + attributes: [ 'warehouse_id' ], + raw: true, + }); + const warehouseIds = (contracts || []).map(c => Number(c.warehouse_id)).filter(id => id > 0); + if (!warehouseIds.length) { + return this.success('ok', this.emptyStoreStats()); + } + + // 跨仓并行查询并汇总 + const results = await Promise.all(warehouseIds.map(wid => this.queryWarehouseStats(wid).catch(() => null))); + return this.success('ok', this.mergeStoreStats(results.filter(Boolean))); + } + + async queryWarehouseStats(warehouseId) { + const { ctx } = this; const model = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId); const { Sequelize } = ctx.app; const { Op } = Sequelize; - // 待处理订单基础条件:step in [1,2,3](排除待审核 step=0,包含待处理异常 step=3) const pendingBase = { step: { [Op.in]: [ 1, 2, 3 ] }, outbound_status: { [Op.ne]: 'padding' }, status: { [Op.ne]: 'cancel' }, + store_id: { [Op.in]: [] }, // 占位,后续填充 }; + delete pendingBase.store_id; - // 今日时间范围 const now = new Date(); const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const todayEnd = new Date(todayStart.getTime() + 86400000); @@ -59,42 +94,85 @@ class dashboardService extends MainService { model.count({ where: { ...pendingBase, step: 3 } }), ]); - const orderTrend = yesterdayNew > 0 - ? parseFloat((((todayNew - yesterdayNew) / yesterdayNew) * 100).toFixed(1)) - : (todayNew > 0 ? 100 : 0); + return { + warehouseId, + totalPending, + todayNew, + yesterdayNew, + unassignedOrders, + unprintedOrders, + unpickedOrders, + pendingShipments, + todayOutbound, + interceptedOrders, + exceptionOrders, + }; + } - const totalForRate = pendingShipments + todayOutbound; + mergeStoreStats(results) { + const merged = { + totalPending: 0, todayNew: 0, yesterdayNew: 0, + unassignedOrders: 0, unprintedOrders: 0, unpickedOrders: 0, + pendingShipments: 0, todayOutbound: 0, interceptedOrders: 0, exceptionOrders: 0, + }; + + for (const r of results) { + merged.totalPending += r.totalPending || 0; + merged.todayNew += r.todayNew || 0; + merged.yesterdayNew += r.yesterdayNew || 0; + merged.unassignedOrders += r.unassignedOrders || 0; + merged.unprintedOrders += r.unprintedOrders || 0; + merged.unpickedOrders += r.unpickedOrders || 0; + merged.pendingShipments += r.pendingShipments || 0; + merged.todayOutbound += r.todayOutbound || 0; + merged.interceptedOrders += r.interceptedOrders || 0; + merged.exceptionOrders += r.exceptionOrders || 0; + } + + const orderTrend = merged.yesterdayNew > 0 + ? parseFloat((((merged.todayNew - merged.yesterdayNew) / merged.yesterdayNew) * 100).toFixed(1)) + : (merged.todayNew > 0 ? 100 : 0); + + const totalForRate = merged.pendingShipments + merged.todayOutbound; const outboundRate = totalForRate > 0 - ? parseFloat(((todayOutbound / totalForRate) * 100).toFixed(2)) + ? parseFloat(((merged.todayOutbound / totalForRate) * 100).toFixed(2)) : 0; - const shipmentProgress = totalPending > 0 - ? parseFloat((((totalPending - pendingShipments) / totalPending) * 100).toFixed(0)) + const shipmentProgress = merged.totalPending > 0 + ? parseFloat((((merged.totalPending - merged.pendingShipments) / merged.totalPending) * 100).toFixed(0)) : 0; - const data = { + return { code: 200, message: '成功', data: { - todayOrders: totalPending + todayOutbound, + todayOrders: merged.totalPending + merged.todayOutbound, orderTrend, - - unassignedOrders, - unprintedOrders, - unpickedOrders, - - pendingShipments, + unassignedOrders: merged.unassignedOrders, + unprintedOrders: merged.unprintedOrders, + unpickedOrders: merged.unpickedOrders, + pendingShipments: merged.pendingShipments, shipmentProgress, - - todayOutbound, + todayOutbound: merged.todayOutbound, outboundRate, - - interceptedOrders, - exceptionOrders, + interceptedOrders: merged.interceptedOrders, + exceptionOrders: merged.exceptionOrders, }, }; + } - return this.success('ok', data); + emptyStoreStats() { + return { + code: 200, + message: '成功', + data: { + todayOrders: 0, orderTrend: 0, + unassignedOrders: 0, unprintedOrders: 0, unpickedOrders: 0, + pendingShipments: 0, shipmentProgress: 0, + todayOutbound: 0, outboundRate: 0, + interceptedOrders: 0, exceptionOrders: 0, + }, + }; } async warehouseStats() { const data = { @@ -204,17 +282,46 @@ class dashboardService extends MainService { } async express_ratio() { const { ctx } = this; - const warehouseId = ctx.user?.store_id; + const storeType = String(ctx.user?.store_type || '').trim(); + if (storeType === 'store') { + return await this.storeSideExpressRatio(); + } + + const warehouseId = Number(ctx.user?.store_id); if (!warehouseId) { return this.fail('缺少仓库上下文'); } + const nameValueMap = await this.queryWarehouseExpressRatio(warehouseId); + return this.success('ok', this.formatChartData(nameValueMap)); + } + async storeSideExpressRatio() { + const { ctx } = this; + const storeId = Number(ctx.user?.store_id); + if (!storeId) { + return this.fail('缺少货主上下文'); + } + + const contracts = await ctx.model.StoreWarehouse.findAll({ + where: { store_id: storeId, auth_status: 2 }, + attributes: [ 'warehouse_id' ], + raw: true, + }); + const warehouseIds = (contracts || []).map(c => Number(c.warehouse_id)).filter(id => id > 0); + if (!warehouseIds.length) { + return this.success('ok', this.emptyChartData()); + } + + const results = await Promise.all(warehouseIds.map(wid => this.queryWarehouseExpressRatio(wid).catch(() => null))); + return this.success('ok', this.mergeChartData(results.filter(Boolean))); + } + + async queryWarehouseExpressRatio(warehouseId) { + const { ctx } = this; const model = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId); const { Sequelize } = ctx.app; - // 待处理订单条件:step in [1,2,3](排除待审核 step=0,包含待处理异常 step=3) - // 排除已取消的订单 const rows = await model.findAll({ attributes: [ 'logistics_name', @@ -230,40 +337,96 @@ class dashboardService extends MainService { raw: true, }); - // 颜色调色板 + const nameValueMap = new Map(); + for (const row of rows) { + nameValueMap.set(row.logistics_name || '未指定快递', Number(row.value) || 0); + } + return nameValueMap; + } + + mergeChartData(results) { + const merged = new Map(); + for (const map of results) { + for (const [ name, value ] of map.entries()) { + merged.set(name, (merged.get(name) || 0) + value); + } + } + return this.formatChartData(merged); + } + + formatChartData(nameValueMap) { + if (!nameValueMap || nameValueMap.size === 0) { + return { code: 200, message: '成功', data: [] }; + } + const colorPalette = [ '#409EFF', '#67C23A', '#E6A23C', '#909399', '#67023A', '#97293A', '#F56C6C', '#67913A', '#9370DB', '#67313A', '#3AA1FF', '#36CFC9', '#FFC53D', '#FF7A45', '#597EF7', ]; - const data = { + const sorted = [ ...nameValueMap.entries() ].sort((a, b) => b[1] - a[1]); + + return { code: 200, message: '成功', - data: rows.map((row, index) => ({ - name: row.logistics_name || '未指定快递', - value: Number(row.value), - itemStyle: { - color: colorPalette[index % colorPalette.length], - }, + data: sorted.map(([ name, value ], index) => ({ + name, + value, + itemStyle: { color: colorPalette[index % colorPalette.length] }, })), }; + } - return this.success('ok', data); + emptyChartData() { + return { + code: 200, + message: '成功', + data: [], + }; } async platform_distribution() { const { ctx } = this; - const warehouseId = ctx.user?.store_id; + const storeType = String(ctx.user?.store_type || '').trim(); + if (storeType === 'store') { + return await this.storeSidePlatformDistribution(); + } + + const warehouseId = Number(ctx.user?.store_id); if (!warehouseId) { return this.fail('缺少仓库上下文'); } + const nameValueMap = await this.queryWarehousePlatformDistribution(warehouseId); + return this.success('ok', this.formatChartData(nameValueMap)); + } + async storeSidePlatformDistribution() { + const { ctx } = this; + const storeId = Number(ctx.user?.store_id); + if (!storeId) { + return this.fail('缺少货主上下文'); + } + + const contracts = await ctx.model.StoreWarehouse.findAll({ + where: { store_id: storeId, auth_status: 2 }, + attributes: [ 'warehouse_id' ], + raw: true, + }); + const warehouseIds = (contracts || []).map(c => Number(c.warehouse_id)).filter(id => id > 0); + if (!warehouseIds.length) { + return this.success('ok', this.emptyChartData()); + } + + const results = await Promise.all(warehouseIds.map(wid => this.queryWarehousePlatformDistribution(wid).catch(() => null))); + return this.success('ok', this.mergeChartData(results.filter(Boolean))); + } + + async queryWarehousePlatformDistribution(warehouseId) { + const { ctx } = this; const model = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId); const { Sequelize } = ctx.app; - // 待处理订单条件:step in [1,2,3](排除待审核 step=0,包含待处理异常 step=3) - // 排除已取消的订单 const rows = await model.findAll({ attributes: [ 'sys_platform_name', @@ -279,26 +442,11 @@ class dashboardService extends MainService { raw: true, }); - // 颜色调色板 - const colorPalette = [ - '#409EFF', '#67C23A', '#E6A23C', '#909399', '#67023A', - '#97293A', '#F56C6C', '#67913A', '#9370DB', '#67313A', - '#3AA1FF', '#36CFC9', '#FFC53D', '#FF7A45', '#597EF7', - ]; - - const data = { - code: 200, - message: '成功', - data: rows.map((row, index) => ({ - name: row.sys_platform_name || '未指定平台', - value: Number(row.value), - itemStyle: { - color: colorPalette[index % colorPalette.length], - }, - })), - }; - - return this.success('ok', data); + const nameValueMap = new Map(); + for (const row of rows) { + nameValueMap.set(row.sys_platform_name || '未指定平台', Number(row.value) || 0); + } + return nameValueMap; } /** * 订单趋势统计:按小时统计当日订单数与发货数