dashboard 货主端自适应分支:storeStats/express_ratio/platform_distribution 按 store_type 区分仓库/货主查询,货主端跨仓汇总
This commit is contained in:
parent
9c6d984ebc
commit
4e20831386
@ -10,26 +10,61 @@ class dashboardService extends MainService {
|
|||||||
const _noNeedLog = '*';
|
const _noNeedLog = '*';
|
||||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog);
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
async storeStats() {
|
async storeStats() {
|
||||||
const { ctx } = this;
|
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) {
|
if (!warehouseId) {
|
||||||
return this.fail('缺少仓库上下文');
|
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 model = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
||||||
const { Sequelize } = ctx.app;
|
const { Sequelize } = ctx.app;
|
||||||
const { Op } = Sequelize;
|
const { Op } = Sequelize;
|
||||||
|
|
||||||
// 待处理订单基础条件:step in [1,2,3](排除待审核 step=0,包含待处理异常 step=3)
|
|
||||||
const pendingBase = {
|
const pendingBase = {
|
||||||
step: { [Op.in]: [ 1, 2, 3 ] },
|
step: { [Op.in]: [ 1, 2, 3 ] },
|
||||||
outbound_status: { [Op.ne]: 'padding' },
|
outbound_status: { [Op.ne]: 'padding' },
|
||||||
status: { [Op.ne]: 'cancel' },
|
status: { [Op.ne]: 'cancel' },
|
||||||
|
store_id: { [Op.in]: [] }, // 占位,后续填充
|
||||||
};
|
};
|
||||||
|
delete pendingBase.store_id;
|
||||||
|
|
||||||
// 今日时间范围
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
||||||
const todayEnd = new Date(todayStart.getTime() + 86400000);
|
const todayEnd = new Date(todayStart.getTime() + 86400000);
|
||||||
@ -59,42 +94,85 @@ class dashboardService extends MainService {
|
|||||||
model.count({ where: { ...pendingBase, step: 3 } }),
|
model.count({ where: { ...pendingBase, step: 3 } }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const orderTrend = yesterdayNew > 0
|
return {
|
||||||
? parseFloat((((todayNew - yesterdayNew) / yesterdayNew) * 100).toFixed(1))
|
warehouseId,
|
||||||
: (todayNew > 0 ? 100 : 0);
|
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
|
const outboundRate = totalForRate > 0
|
||||||
? parseFloat(((todayOutbound / totalForRate) * 100).toFixed(2))
|
? parseFloat(((merged.todayOutbound / totalForRate) * 100).toFixed(2))
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
const shipmentProgress = totalPending > 0
|
const shipmentProgress = merged.totalPending > 0
|
||||||
? parseFloat((((totalPending - pendingShipments) / totalPending) * 100).toFixed(0))
|
? parseFloat((((merged.totalPending - merged.pendingShipments) / merged.totalPending) * 100).toFixed(0))
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
const data = {
|
return {
|
||||||
code: 200,
|
code: 200,
|
||||||
message: '成功',
|
message: '成功',
|
||||||
data: {
|
data: {
|
||||||
todayOrders: totalPending + todayOutbound,
|
todayOrders: merged.totalPending + merged.todayOutbound,
|
||||||
orderTrend,
|
orderTrend,
|
||||||
|
unassignedOrders: merged.unassignedOrders,
|
||||||
unassignedOrders,
|
unprintedOrders: merged.unprintedOrders,
|
||||||
unprintedOrders,
|
unpickedOrders: merged.unpickedOrders,
|
||||||
unpickedOrders,
|
pendingShipments: merged.pendingShipments,
|
||||||
|
|
||||||
pendingShipments,
|
|
||||||
shipmentProgress,
|
shipmentProgress,
|
||||||
|
todayOutbound: merged.todayOutbound,
|
||||||
todayOutbound,
|
|
||||||
outboundRate,
|
outboundRate,
|
||||||
|
interceptedOrders: merged.interceptedOrders,
|
||||||
interceptedOrders,
|
exceptionOrders: merged.exceptionOrders,
|
||||||
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() {
|
async warehouseStats() {
|
||||||
const data = {
|
const data = {
|
||||||
@ -204,17 +282,46 @@ class dashboardService extends MainService {
|
|||||||
}
|
}
|
||||||
async express_ratio() {
|
async express_ratio() {
|
||||||
const { ctx } = this;
|
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) {
|
if (!warehouseId) {
|
||||||
return this.fail('缺少仓库上下文');
|
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 model = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
||||||
const { Sequelize } = ctx.app;
|
const { Sequelize } = ctx.app;
|
||||||
|
|
||||||
// 待处理订单条件:step in [1,2,3](排除待审核 step=0,包含待处理异常 step=3)
|
|
||||||
// 排除已取消的订单
|
|
||||||
const rows = await model.findAll({
|
const rows = await model.findAll({
|
||||||
attributes: [
|
attributes: [
|
||||||
'logistics_name',
|
'logistics_name',
|
||||||
@ -230,40 +337,96 @@ class dashboardService extends MainService {
|
|||||||
raw: true,
|
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 = [
|
const colorPalette = [
|
||||||
'#409EFF', '#67C23A', '#E6A23C', '#909399', '#67023A',
|
'#409EFF', '#67C23A', '#E6A23C', '#909399', '#67023A',
|
||||||
'#97293A', '#F56C6C', '#67913A', '#9370DB', '#67313A',
|
'#97293A', '#F56C6C', '#67913A', '#9370DB', '#67313A',
|
||||||
'#3AA1FF', '#36CFC9', '#FFC53D', '#FF7A45', '#597EF7',
|
'#3AA1FF', '#36CFC9', '#FFC53D', '#FF7A45', '#597EF7',
|
||||||
];
|
];
|
||||||
|
|
||||||
const data = {
|
const sorted = [ ...nameValueMap.entries() ].sort((a, b) => b[1] - a[1]);
|
||||||
|
|
||||||
|
return {
|
||||||
code: 200,
|
code: 200,
|
||||||
message: '成功',
|
message: '成功',
|
||||||
data: rows.map((row, index) => ({
|
data: sorted.map(([ name, value ], index) => ({
|
||||||
name: row.logistics_name || '未指定快递',
|
name,
|
||||||
value: Number(row.value),
|
value,
|
||||||
itemStyle: {
|
itemStyle: { color: colorPalette[index % colorPalette.length] },
|
||||||
color: colorPalette[index % colorPalette.length],
|
|
||||||
},
|
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return this.success('ok', data);
|
emptyChartData() {
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
message: '成功',
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
async platform_distribution() {
|
async platform_distribution() {
|
||||||
const { ctx } = this;
|
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) {
|
if (!warehouseId) {
|
||||||
return this.fail('缺少仓库上下文');
|
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 model = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
||||||
const { Sequelize } = ctx.app;
|
const { Sequelize } = ctx.app;
|
||||||
|
|
||||||
// 待处理订单条件:step in [1,2,3](排除待审核 step=0,包含待处理异常 step=3)
|
|
||||||
// 排除已取消的订单
|
|
||||||
const rows = await model.findAll({
|
const rows = await model.findAll({
|
||||||
attributes: [
|
attributes: [
|
||||||
'sys_platform_name',
|
'sys_platform_name',
|
||||||
@ -279,26 +442,11 @@ class dashboardService extends MainService {
|
|||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// 颜色调色板
|
const nameValueMap = new Map();
|
||||||
const colorPalette = [
|
for (const row of rows) {
|
||||||
'#409EFF', '#67C23A', '#E6A23C', '#909399', '#67023A',
|
nameValueMap.set(row.sys_platform_name || '未指定平台', Number(row.value) || 0);
|
||||||
'#97293A', '#F56C6C', '#67913A', '#9370DB', '#67313A',
|
}
|
||||||
'#3AA1FF', '#36CFC9', '#FFC53D', '#FF7A45', '#597EF7',
|
return nameValueMap;
|
||||||
];
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 订单趋势统计:按小时统计当日订单数与发货数
|
* 订单趋势统计:按小时统计当日订单数与发货数
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user