@ -34,6 +34,63 @@ class WarehousePanelService extends Service {
return ` ${ PANEL _CACHE _KEY _PREFIX } :lastChangeAt: ${ warehouseId } ` ;
return ` ${ PANEL _CACHE _KEY _PREFIX } :lastChangeAt: ${ warehouseId } ` ;
}
}
/ * *
* 读取仓库自然日开始时间 ( wms _store . subject _config . day _start _time ) 。
*
* 配置缺失 / 非法 / 查询异常时回退默认 06 : 00 。
*
* @ param { number } warehouseId 仓库 ID
* @ return { Promise < string > } HH : mm 格式时间 ( 如 06 : 00 )
* /
async getWarehouseDayStartTime ( warehouseId ) {
try {
const store = await this . ctx . model . Store . findOne ( {
where : { id : warehouseId , project : 'warehouse' } ,
attributes : [ 'subject_config' ] ,
raw : true ,
} ) ;
let config = store && store . subject _config ? store . subject _config : { } ;
if ( typeof config === 'string' ) {
try {
config = JSON . parse ( config ) ;
} catch ( parseError ) {
config = { } ;
}
}
const raw = config . day _start _time ;
if ( typeof raw === 'string' && /^([01]\d|2[0-3]):[0-5]\d$/ . test ( raw ) ) {
return raw ;
}
return '06:00' ;
} catch ( error ) {
const logger = this . ctx . logger ;
if ( logger && typeof logger . warn === 'function' ) {
logger . warn ( ` [WarehousePanel] 读取自然日开始时间失败,回退 06:00: ${ error . message } ` ) ;
}
return '06:00' ;
}
}
/ * *
* 解析业务日窗口 [ businessStart , businessEnd ) 。
*
* 业务日从配置的 day _start _time 开始持续 24 小时 ; 当前时间早于今日该时间点时 ,
* 窗口从昨日该时间点开始 ( 昨日该时间点后的订单视为今日 ) 。
*
* @ param { number } warehouseId 仓库 ID
* @ param { Date } now 当前时间
* @ return { Promise < Array < Date >> } [ 业务日开始 , 业务日结束 ]
* /
async resolveBusinessDayRange ( warehouseId , now ) {
const dayStartTime = await this . getWarehouseDayStartTime ( warehouseId ) ;
const [ hh , mm ] = dayStartTime . split ( ':' ) . map ( Number ) ;
let businessStart = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) , hh , mm , 0 , 0 ) ;
if ( now < businessStart ) {
businessStart = new Date ( businessStart . getTime ( ) - 86400000 ) ;
}
return [ businessStart , new Date ( businessStart . getTime ( ) + 86400000 ) ] ;
}
/ * *
/ * *
* 获取面板数据 。
* 获取面板数据 。
*
*
@ -101,7 +158,8 @@ class WarehousePanelService extends Service {
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 tomorrowStart = new Date ( todayStart . getTime ( ) + 86400000 ) ;
// 业务日窗口(仓库配置 day_start_time, 默认 06:00) : 昨日 06:00 后的订单视为今日
const [ businessStart , businessEnd ] = await this . resolveBusinessDayRange ( warehouseId , now ) ;
const factory = model . OrderDeliveryFactory ;
const factory = model . OrderDeliveryFactory ;
const [ currentModel , dispatchModel ] = await Promise . all ( [
const [ currentModel , dispatchModel ] = await Promise . all ( [
@ -118,24 +176,27 @@ class WarehousePanelService extends Service {
} ;
} ;
const blocks = await Promise . all ( [
const blocks = await Promise . all ( [
this . queryFulfillment ( currentModel , pendingBase ) . catch ( error => this . logBlockError ( 'fulfillment' , error ) ) ,
this . queryFulfillment ( currentModel , dispatchModel , pendingBase , warehouseId , businessStart , businessEnd )
this . queryStageOverview ( warehouseId , currentModel , pendingBase , todayStart , tomorrowStart )
. catch ( error => this . logBlockError ( 'fulfillment' , error ) ) ,
this . queryStageOverview ( warehouseId , currentModel , dispatchModel , pendingBase , businessStart , businessEnd )
. catch ( error => this . logBlockError ( 'stageOverview' , error ) ) ,
. catch ( error => this . logBlockError ( 'stageOverview' , error ) ) ,
this . queryB2b ( currentModel , dispatchModel , todayStart, tomorrowStart )
this . queryB2b ( currentModel , dispatchModel , businessStart, businessEnd )
. catch ( error => this . logBlockError ( 'b2b' , error ) ) ,
. catch ( error => this . logBlockError ( 'b2b' , error ) ) ,
this . queryPickingBlock ( warehouseId , currentTableName , dispatchTableName , [ 1 , 5 ] , todayStart , tomorrowStart )
this . queryPickingBlock ( warehouseId , currentTableName , dispatchTableName , true , businessStart , businessEnd )
. catch ( error => this . logBlockError ( 'groupPicking' , error ) ) ,
. catch ( error => this . logBlockError ( 'groupPicking' , error ) ) ,
this . queryPickingBlock ( warehouseId , currentTableName , dispatchTableName , [ 2 , 3 , 4 ] , todayStart , tomorrowStart )
this . queryPickingBlock ( warehouseId , currentTableName , dispatchTableName , false , businessStart , businessEnd )
. catch ( error => this . logBlockError ( 'scatterPicking' , error ) ) ,
. catch ( error => this . logBlockError ( 'scatterPicking' , error ) ) ,
this . queryPackingPersonal ( warehouseId , todayStart , tomorrowStart )
this . queryPackingBlock ( warehouseId , businessStart , businessEnd , true )
. catch ( error => this . logBlockError ( 'packingRank' , error ) ) ,
. catch ( error => this . logBlockError ( 'groupPacking' , error ) ) ,
this . queryPackingBlock ( warehouseId , businessStart , businessEnd , false )
. catch ( error => this . logBlockError ( 'scatterPacking' , error ) ) ,
this . queryTrend ( currentModel , pendingBase , todayStart )
this . queryTrend ( currentModel , pendingBase , todayStart )
. catch ( error => this . logBlockError ( 'trend' , error ) ) ,
. catch ( error => this . logBlockError ( 'trend' , error ) ) ,
this . queryWeeklyIntercept ( currentModel , dispatchModel , todayStart )
this . queryWeeklyIntercept ( currentModel , dispatchModel , todayStart )
. catch ( error => this . logBlockError ( 'weeklyEfficiency' , error ) ) ,
. catch ( error => this . logBlockError ( 'weeklyEfficiency' , error ) ) ,
] ) ;
] ) ;
const [ fulfillment , stageOverview , b2b , groupPicking , scatterPicking , packingRank , trend , weeklyEfficiency ] = blocks ;
const [ fulfillment , stageOverview , b2b , groupPicking , scatterPicking , groupPacking, scatterPacking , trend , weeklyEfficiency ] = blocks ;
const data = {
const data = {
title : ctx . user ? . store _name || '' ,
title : ctx . user ? . store _name || '' ,
@ -144,7 +205,8 @@ class WarehousePanelService extends Service {
b2b ,
b2b ,
groupPicking ,
groupPicking ,
scatterPicking ,
scatterPicking ,
packingRank ,
groupPacking ,
scatterPacking ,
trend ,
trend ,
weeklyEfficiency ,
weeklyEfficiency ,
} ;
} ;
@ -168,18 +230,95 @@ class WarehousePanelService extends Service {
}
}
/ * *
/ * *
* 订单履约链路 : 6 个环节 × 未 / 已 + 分母 。
* 订单履约链路 : 6 个环节 × 未 / 已 + 分母 + 今日退回指标 。
*
* 配送表补数 : 已出库订单会从常用表迁移到配送分表 ( outbound _status = fin ) ;
* 各环节未 * = 池内未完成 + 配送表今日出库未完成 ( 真实状态 ) ;
* 各环节已 * 口径 : 分配 / 打印 / 拣货从订单表状态获取 ( 池内 fin + 配送表今日出库 fin ,
* 保证已分配 ≥ 已打印 ≥ 已拣货 ) , 复核 = 今日复核时间 ( recheck _time ) ,
* 打包 = 今日打包任务订单数 、 出库 = 今日出库数 。
*
* 今日退回收件数 / 今日拆包件数 : 按 wms _return _received / wms _return _unpack
* 的 created _at 落在今日 ( 含今天 00 : 00 ) 且仓库匹配统计 , 以两项追加在
* items 数组末尾 ( label / icon / value 与环节项同构 ) 。
*
*
* @ param { Object } currentModel 常用分表模型
* @ param { Object } currentModel 常用分表模型
* @ param { Object } dispatchModel 配送分表模型
* @ param { Object } pendingBase 待处理基础过滤
* @ param { Object } pendingBase 待处理基础过滤
* @ return { Promise < Object > } { total , items }
* @ param { number } warehouseId 仓库 ID
* @ param { Date } todayStart 今日开始
* @ param { Date } tomorrowStart 明日开始
* @ return { Promise < Object > } { total , items } ( items 末尾追加今日退件 / 今日拆包两项 )
* /
* /
async queryFulfillment ( currentModel , pendingBase ) {
async queryFulfillment ( currentModel , dispatchModel , pendingBase , warehouseId , todayStart , tomorrowStart ) {
const { model } = this . ctx ;
const { Op } = this . app . Sequelize ;
const { Op } = this . app . Sequelize ;
const todayRange = { [ Op . gte ] : todayStart , [ Op . lt ] : tomorrowStart } ;
// 配送表今日出库且未取消:出库环节已*(今日出库数)与总数叠加(已迁移订单)
const dispatchTodayBase = {
status : { [ Op . ne ] : 'cancel' } ,
outbound _status : 'fin' ,
delivery _time : todayRange ,
dispatch _lane : { [ Op . in ] : [ 'm4' , 'm5' ] } ,
} ;
const totalCountPromise = currentModel . count ( { where : pendingBase } ) ;
const totalCountPromise = currentModel . count ( { where : pendingBase } ) ;
const dispatchTodayCountPromise = dispatchModel . count ( { where : dispatchTodayBase } ) ;
// 今日打包任务订单数( pack_mode=2 团购模式按 pack_quantity 求和 + 散单每任务 1 单,与打包两块同口径),
// 用于打包环节已*(避免跨天打包出库窗口错位导致已打包与打包两块对不上)
const packTaskOrdersTodayPromise = model . query (
` SELECT COALESCE(SUM(CASE WHEN pack_mode = 2 THEN pack_quantity ELSE 1 END), 0) AS cnt
FROM wms _pack _task
WHERE warehouse _id = : warehouseId
AND create _time >= : start AND create _time < : end
AND status != 3 AND deleted _at IS NULL ` ,
{ replacements : { warehouseId , start : todayStart , end : tomorrowStart } , type : model . QueryTypes . SELECT }
) . then ( rows => {
const first = Array . isArray ( rows ) && rows . length > 0 ? rows [ 0 ] : null ;
return Number ( first && first . cnt || 0 ) ;
} ) ;
// 各环节已* 口径:分配/打印/拣货从订单表状态获取(池内 fin + 配送表今日出库 fin) ,
// 保证已分配 ≥ 已打印 ≥ 已拣货 的流程约束;复核=今日复核时间;打包=今日打包任务;出库=今日出库
const todayAllocOrdersPromise = Promise . all ( [
currentModel . count ( { where : { ... pendingBase , post _status : 'fin' } } ) ,
dispatchModel . count ( { where : { ... dispatchTodayBase , post _status : 'fin' } } ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ;
const todayPrintOrdersPromise = Promise . all ( [
currentModel . count ( { where : { ... pendingBase , print _status : 'fin' } } ) ,
dispatchModel . count ( { where : { ... dispatchTodayBase , print _status : 'fin' } } ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ;
const todayPickOrdersPromise = Promise . all ( [
currentModel . count ( { where : { ... pendingBase , pick _status : 'fin' } } ) ,
dispatchModel . count ( {
where : {
status : { [ Op . ne ] : 'cancel' } ,
outbound _status : 'fin' ,
pick _status : 'fin' ,
delivery _time : todayRange ,
dispatch _lane : { [ Op . in ] : [ 'm4' , 'm5' ] } ,
} ,
} ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ;
const todayRecheckOrdersPromise = Promise . all ( [
currentModel . count ( { where : { status : { [ Op . ne ] : 'cancel' } , recheck _time : todayRange } } ) ,
dispatchModel . count ( {
where : {
status : { [ Op . ne ] : 'cancel' } ,
recheck _time : todayRange ,
dispatch _lane : { [ Op . in ] : [ 'm4' , 'm5' ] } ,
} ,
} ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ;
const returnReceivedTodayPromise = model . ReturnReceived . count ( {
where : { warehouse _id : warehouseId , created _at : todayRange } ,
} ) ;
const returnUnpackTodayPromise = model . ReturnUnpack . count ( {
where : { warehouse _id : warehouseId , created _at : todayRange } ,
} ) ;
const stageCounts = await Promise . all ( FULFILLMENT _STAGES . map ( async stage => {
const stageCounts = await Promise . all ( FULFILLMENT _STAGES . map ( async stage => {
const undone = await currentModel . count ( {
const [ undoneCurrent , dispatchFin , dispatchTodayOutbound , todayAlloc , todayPrint , todayPick , todayRecheck , packTaskOrdersToday ] = await Promise . all ( [
currentModel . count ( {
where : {
where : {
... pendingBase ,
... pendingBase ,
[ Op . or ] : [
[ Op . or ] : [
@ -187,22 +326,50 @@ class WarehousePanelService extends Service {
{ [ stage . field ] : { [ Op . is ] : null } } ,
{ [ stage . field ] : { [ Op . is ] : null } } ,
] ,
] ,
} ,
} ,
} ) ;
} ) ,
const done = await currentModel . count ( {
dispatchModel . count ( {
where : { ... pendingBase , [ stage . field ] : 'fin' } ,
where : { ... dispatchTodayBase , [ stage . field ] : 'fin' } ,
} ) ;
} ) ,
return { undone , done } ;
dispatchTodayCountPromise ,
todayAllocOrdersPromise ,
todayPrintOrdersPromise ,
todayPickOrdersPromise ,
todayRecheckOrdersPromise ,
packTaskOrdersTodayPromise ,
] ) ;
// 已* 按环节取今日作业口径数据源;出库环节=今日出库数;
// 未* = 池内未完成 + 配送表今日出库未完成(真实状态)
const doneTodayMap = {
post _status : todayAlloc ,
print _status : todayPrint ,
pick _status : todayPick ,
recheck _status : todayRecheck ,
pack _status : packTaskOrdersToday ,
outbound _status : dispatchTodayOutbound ,
} ;
const doneToday = doneTodayMap [ stage . field ] || 0 ;
return {
undone : undoneCurrent + ( dispatchTodayOutbound - dispatchFin ) ,
done : doneToday ,
} ;
} ) ) ;
} ) ) ;
const total = await totalCountPromise ;
const [ total , dispatchTodayOutbound , returnReceivedToday , returnUnpackToday ] = await Promise . all ( [
totalCountPromise ,
dispatchTodayCountPromise ,
returnReceivedTodayPromise ,
returnUnpackTodayPromise ,
] ) ;
const items = [ ] ;
const items = [ ] ;
FULFILLMENT _STAGES . forEach ( ( stage , index ) => {
FULFILLMENT _STAGES . forEach ( ( stage , index ) => {
const counts = stageCounts [ index ] || { undone : 0 , done : 0 } ;
const counts = stageCounts [ index ] || { undone : 0 , done : 0 } ;
items . push ( { label : stage . undone , icon : stage . icon , value : counts . undone || 0 } ) ;
items . push ( { label : stage . undone , icon : stage . icon , value : counts . undone || 0 } ) ;
items . push ( { label : stage . done , icon : stage . icon , value : counts . done || 0 } ) ;
items . push ( { label : stage . done , icon : stage . icon , value : counts . done || 0 } ) ;
} ) ;
} ) ;
items . push ( { label : '今日退件' , icon : 'returnReceived' , value : returnReceivedToday || 0 } ) ;
items . push ( { label : '今日拆包' , icon : 'returnUnpack' , value : returnUnpackToday || 0 } ) ;
return { total : total || 0 , items } ;
return { total : ( total || 0 ) + dispatchTodayOutbound , items } ;
}
}
/ * *
/ * *
@ -226,12 +393,13 @@ class WarehousePanelService extends Service {
*
*
* @ param { number } warehouseId 仓库 ID
* @ param { number } warehouseId 仓库 ID
* @ param { Object } currentModel 常用分表模型
* @ param { Object } currentModel 常用分表模型
* @ param { Object } dispatchModel 配送分表模型 ( 已出库订单迁移目标 , 今日出库双表合计 )
* @ param { Object } pendingBase 待处理基础过滤
* @ param { Object } pendingBase 待处理基础过滤
* @ param { Date } todayStart 今日开始
* @ param { Date } todayStart 今日开始
* @ param { Date } tomorrowStart 明日开始
* @ param { Date } tomorrowStart 明日开始
* @ return { Promise < Object > } { totalOrders , interceptOrders , stages }
* @ return { Promise < Object > } { totalOrders , interceptOrders , stages }
* /
* /
async queryStageOverview ( warehouseId , currentModel , pendingBase, todayStart , tomorrowStart ) {
async queryStageOverview ( warehouseId , currentModel , dispatchModel, pendingBase, todayStart , tomorrowStart ) {
const { model } = this . ctx ;
const { model } = this . ctx ;
const { Op } = this . app . Sequelize ;
const { Op } = this . app . Sequelize ;
const todayRange = { [ Op . gte ] : todayStart , [ Op . lt ] : tomorrowStart } ;
const todayRange = { [ Op . gte ] : todayStart , [ Op . lt ] : tomorrowStart } ;
@ -254,9 +422,19 @@ class WarehousePanelService extends Service {
packAbnormal ,
packAbnormal ,
] = await Promise . all ( [
] = await Promise . all ( [
currentModel . count ( { where : pendingBase } ) ,
currentModel . count ( { where : pendingBase } ) ,
Promise . all ( [
currentModel . count ( {
currentModel . count ( {
where : { step : 4 , outbound _status : 'fin' , status : { [ Op . ne ] : 'cancel' } , delivery _time : todayRange } ,
where : { step : 4 , outbound _status : 'fin' , status : { [ Op . ne ] : 'cancel' } , delivery _time : todayRange } ,
} ) ,
} ) ,
dispatchModel . count ( {
where : {
status : { [ Op . ne ] : 'cancel' } ,
outbound _status : 'fin' ,
delivery _time : todayRange ,
dispatch _lane : { [ Op . in ] : [ 'm4' , 'm5' ] } ,
} ,
} ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ,
currentModel . count ( {
currentModel . count ( {
where : { ... pendingBase , intercept _status : { [ Op . not ] : null , [ Op . ne ] : 'none' } } ,
where : { ... pendingBase , intercept _status : { [ Op . not ] : null , [ Op . ne ] : 'none' } } ,
} ) ,
} ) ,
@ -265,19 +443,34 @@ class WarehousePanelService extends Service {
model . Receiving . count ( {
model . Receiving . count ( {
where : { warehouse _id : warehouseId , storage _status : { [ Op . in ] : [ 'waiting' , 'partial' ] } } ,
where : { warehouse _id : warehouseId , storage _status : { [ Op . in ] : [ 'waiting' , 'partial' ] } } ,
} ) ,
} ) ,
this . countSql (
Promise . all ( [
` SELECT COUNT(*) AS cnt FROM wms_wave_pick_task
currentModel . count ( { where : { ... pendingBase , pick _status : 'fin' } } ) ,
WHERE warehouse _id = : warehouseId AND status IN ( : statuses )
dispatchModel . count ( {
AND finish _time >= : start AND finish _time < : end ` ,
where : {
{ warehouseId , statuses : [ 2 , 3 ] , start : todayStart , end : tomorrowStart }
status : { [ Op . ne ] : 'cancel' } ,
) ,
outbound _status : 'fin' ,
pick _status : 'fin' ,
delivery _time : todayRange ,
dispatch _lane : { [ Op . in ] : [ 'm4' , 'm5' ] } ,
} ,
} ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ,
this . countSql (
this . countSql (
` SELECT COUNT(*) AS cnt FROM wms_wave_pick_task
` SELECT COUNT(*) AS cnt FROM wms_wave_pick_task
WHERE warehouse _id = : warehouseId AND status = 1 ` ,
WHERE warehouse _id = : warehouseId AND status = 1 ` ,
{ warehouseId }
{ warehouseId }
) ,
) ,
currentModel . count ( { where : { ... pendingBase , pick _status : 'error' } } ) ,
currentModel . count ( { where : { ... pendingBase , pick _status : 'error' } } ) ,
Promise . all ( [
currentModel . count ( { where : { status : { [ Op . ne ] : 'cancel' } , recheck _time : todayRange } } ) ,
currentModel . count ( { where : { status : { [ Op . ne ] : 'cancel' } , recheck _time : todayRange } } ) ,
dispatchModel . count ( {
where : {
status : { [ Op . ne ] : 'cancel' } ,
recheck _time : todayRange ,
dispatch _lane : { [ Op . in ] : [ 'm4' , 'm5' ] } ,
} ,
} ) ,
] ) . then ( ( [ currentCount , dispatchCount ] ) => currentCount + dispatchCount ) ,
model . WaveBatch . count ( {
model . WaveBatch . count ( {
where : {
where : {
warehouse _id : warehouseId ,
warehouse _id : warehouseId ,
@ -286,7 +479,14 @@ class WarehousePanelService extends Service {
} ,
} ,
} ) ,
} ) ,
currentModel . count ( { where : { ... pendingBase , recheck _status : 'error' } } ) ,
currentModel . count ( { where : { ... pendingBase , recheck _status : 'error' } } ) ,
model . PackTask . count ( { where : { warehouse _id : warehouseId , create _time : todayRange } } ) ,
this . countSql (
` SELECT COALESCE(SUM(CASE WHEN pack_mode = 2 THEN pack_quantity ELSE 1 END), 0) AS cnt
FROM wms _pack _task
WHERE warehouse _id = : warehouseId
AND create _time >= : start AND create _time < : end
AND status != 3 AND deleted _at IS NULL ` ,
{ warehouseId , start : todayStart , end : tomorrowStart }
) ,
model . PackTask . count ( { where : { warehouse _id : warehouseId , status : 1 } } ) ,
model . PackTask . count ( { where : { warehouse _id : warehouseId , status : 1 } } ) ,
model . PackTask . count ( { where : { warehouse _id : warehouseId , status : 3 } } ) ,
model . PackTask . count ( { where : { warehouse _id : warehouseId , status : 3 } } ) ,
] ) ;
] ) ;
@ -380,24 +580,26 @@ class WarehousePanelService extends Service {
}
}
/ * *
/ * *
* 拣货统计块 ( 团购 pick_mode = 1 , 5 ; 散单 pick _mode = 2 , 3 , 4 ) 。
* 拣货统计块 ( 团购 wave_scope IN ( 1 , 3 ) ; 散单其余 ) 。
*
*
* 窗口: 今日创建 ( create _time 今日 ) 且未取消的任务 ;
* orders/ goods / sku / wave 汇总按订单表状态口径 ( 池内 pick fin + 配送表今日出库 pick fin ,
* orders/ wave 取任务快照 ( order _count / wave _count ) ;
* 与 fulfillment 已拣货一致 , 保证团购 + 散单 = 已拣货 ) ;
* goods/ sku 经 wms _wave _pick _task _order 关联订单分表快照 ( items _total / items _count ) 。
* top 按今日创建任务 ( pick _mode 1 , 5 团购 / 2 , 3 , 4 散单 ) 按人聚合 ( 任务口径 ) 。
*
*
* @ param { number } warehouseId 仓库 ID
* @ param { number } warehouseId 仓库 ID
* @ param { string } currentTableName 常用分表名
* @ param { string } currentTableName 常用分表名
* @ param { string } dispatchTableName 配送分表名
* @ param { string } dispatchTableName 配送分表名
* @ param { Array < number > } pickModes 拣货模式集合
* @ param { boolean } isGroup true = 团购 ( wave _scope 1 / 3 ) ; false = 散单 ( 其余 )
* @ param { Date } todayStart 今日开始
* @ param { Date } todayStart 今日开始
* @ param { Date } tomorrowStart 明日开始
* @ param { Date } tomorrowStart 明日开始
* @ return { Promise < Object > } { orders , goods , sku , wave , top }
* @ return { Promise < Object > } { orders , goods , sku , wave , top }
* /
* /
async queryPickingBlock ( warehouseId , currentTableName , dispatchTableName , p ickMode s, todayStart , tomorrowStart ) {
async queryPickingBlock ( warehouseId , currentTableName , dispatchTableName , isGroup , todayStart , tomorrowStart ) {
const { model } = this . ctx ;
const { model } = this . ctx ;
const { QueryTypes } = model ;
const { QueryTypes } = model ;
const scopeWhere = isGroup ? 'wave_scope IN (1, 3)' : 'wave_scope NOT IN (1, 3)' ;
const pickModes = isGroup ? [ 1 , 5 ] : [ 2 , 3 , 4 ] ;
const replacements = {
const replacements = {
warehouseId ,
warehouseId ,
pickModes ,
pickModes ,
@ -406,18 +608,33 @@ class WarehousePanelService extends Service {
end : tomorrowStart ,
end : tomorrowStart ,
} ;
} ;
// top: 今日创建任务按人聚合( 任务口径)
const taskWindowWhere = `
const taskWindowWhere = `
t . warehouse _id = : warehouseId AND t . pick _mode IN ( : pickModes )
t . warehouse _id = : warehouseId AND t . pick _mode IN ( : pickModes )
AND t . create _time >= : start AND t . create _time < : end
AND t . create _time >= : start AND t . create _time < : end
AND t . status IN ( : activeStatuses )
AND t . status IN ( : activeStatuses )
` ;
` ;
const [ summaryRows , topRows ] = await Promise . all ( [
// 汇总:订单表状态口径(池内 pick fin + 配送表今日出库 pick fin, 与已拣货一致)
model . query ( `
const summarySql = ( tableName , whereFragment ) => `
SELECT COUNT ( * ) AS task _count , COALESCE ( SUM ( order _count ) , 0 ) AS orders , COALESCE ( SUM ( wave _count ) , 0 ) AS waves
SELECT COUNT ( * ) AS orders , COALESCE ( SUM ( items _total ) , 0 ) AS goods ,
FROM wms _wave _pick _task t
COALESCE ( SUM ( items _count ) , 0 ) AS sku ,
WHERE $ { taskWindowWhere }
COUNT ( DISTINCT CASE WHEN wave _id > 0 THEN wave _id END ) AS wave
` , { replacements, type: QueryTypes.SELECT }),
FROM \ ` ${ tableName } \` WHERE ${ whereFragment }
` ;
const poolWhere = `
step IN ( 1 , 2 , 3 ) AND outbound _status != 'padding' AND status != 'cancel'
AND pick _status = 'fin' AND $ { scopeWhere }
` ;
const dispatchWhere = `
status != 'cancel' AND outbound _status = 'fin' AND pick _status = 'fin'
AND dispatch _lane IN ( 'm4' , 'm5' )
AND delivery _time >= : start AND delivery _time < : end AND $ { scopeWhere }
` ;
const [ currentRows , dispatchRows , topRows ] = await Promise . all ( [
model . query ( summarySql ( currentTableName , poolWhere ) , { replacements , type : QueryTypes . SELECT } ) ,
model . query ( summarySql ( dispatchTableName , dispatchWhere ) , { replacements , type : QueryTypes . SELECT } ) ,
model . query ( `
model . query ( `
SELECT t . pick _user _id AS user _id , t . pick _user _name AS name ,
SELECT t . pick _user _id AS user _id , t . pick _user _name AS name ,
COALESCE ( SUM ( t . order _count ) , 0 ) AS orders , COALESCE ( SUM ( t . wave _count ) , 0 ) AS waves
COALESCE ( SUM ( t . order _count ) , 0 ) AS orders , COALESCE ( SUM ( t . wave _count ) , 0 ) AS waves
@ -429,11 +646,20 @@ class WarehousePanelService extends Service {
` , { replacements, type: QueryTypes.SELECT }),
` , { replacements, type: QueryTypes.SELECT }),
] ) ;
] ) ;
const [ goodsSkuRows , topGoodsSkuRows ] = await this . queryPickingGoodsSku (
const current = Array . isArray ( currentRows ) && currentRows . length > 0 ? currentRows [ 0 ] : { } ;
const dispatch = Array . isArray ( dispatchRows ) && dispatchRows . length > 0 ? dispatchRows [ 0 ] : { } ;
const summary = {
orders : Number ( current . orders || 0 ) + Number ( dispatch . orders || 0 ) ,
goods : Number ( current . goods || 0 ) + Number ( dispatch . goods || 0 ) ,
sku : Number ( current . sku || 0 ) + Number ( dispatch . sku || 0 ) ,
wave : Number ( current . wave || 0 ) + Number ( dispatch . wave || 0 ) ,
} ;
// top 的 goods/sku 仍按任务口径(订单表无拣货人,无法按人拆分)
const [ , topGoodsSkuRows ] = await this . queryPickingGoodsSku (
currentTableName , dispatchTableName , taskWindowWhere , replacements
currentTableName , dispatchTableName , taskWindowWhere , replacements
) ;
) ;
const summary = Array . isArray ( summaryRows ) && summaryRows . length > 0 ? summaryRows [ 0 ] : { } ;
const topGoodsSkuMap = new Map ( ) ;
const topGoodsSkuMap = new Map ( ) ;
( topGoodsSkuRows || [ ] ) . forEach ( row => {
( topGoodsSkuRows || [ ] ) . forEach ( row => {
topGoodsSkuMap . set ( String ( row . user _id ) , { goods : Number ( row . goods || 0 ) , sku : Number ( row . sku || 0 ) } ) ;
topGoodsSkuMap . set ( String ( row . user _id ) , { goods : Number ( row . goods || 0 ) , sku : Number ( row . sku || 0 ) } ) ;
@ -452,10 +678,10 @@ class WarehousePanelService extends Service {
} ) ;
} ) ;
return {
return {
orders : Number ( summary . orders || 0 ) ,
orders : summary . orders ,
goods : Number ( goodsSkuRows . goods || 0 ) ,
goods : summary . goods ,
sku : Number ( goodsSkuRows . sku || 0 ) ,
sku : summary . sku ,
wave : Number ( summary . wave s || 0 ) ,
wave : summary . wave ,
top ,
top ,
} ;
} ;
}
}
@ -473,18 +699,50 @@ class WarehousePanelService extends Service {
const { model } = this . ctx ;
const { model } = this . ctx ;
const { QueryTypes } = model ;
const { QueryTypes } = model ;
const goodsSkuSelect = `
// 任务订单关联表缺失的任务(如 pick_mode=1 团购任务,实测 60 个任务 3862 单无 pto 记录)
SELECT COALESCE ( SUM ( od . items _total ) , 0 ) AS goods , COALESCE ( SUM ( od . items _count ) , 0 ) AS sku
// 按 wave_id 从订单分表统计,避免 goods/sku 漏算;有 pto 记录的仍走 pto 精确统计
const taskWindowWhere2 = taskWindowWhere . replace ( /\bt\./g , 't2.' ) ;
const goodsSkuSelect = tableName => `
SELECT COALESCE ( SUM ( x . items _total ) , 0 ) AS goods , COALESCE ( SUM ( x . items _count ) , 0 ) AS sku
FROM (
SELECT od . items _total , od . items _count
FROM wms _wave _pick _task t
FROM wms _wave _pick _task t
JOIN wms _wave _pick _task _order pto ON pto . task _id = t . id
JOIN wms _wave _pick _task _order pto ON pto . task _id = t . id
JOIN \ ` ${ currentTableName } \` od ON od.id = pto.order_id AND od.status != 'cancel'
JOIN \ ` ${ tableName} \` od ON od.id = pto.order_id AND od.status != 'cancel'
WHERE $ { taskWindowWhere }
WHERE $ { taskWindowWhere }
UNION ALL
SELECT od2 . items _total , od2 . items _count
FROM wms _wave _pick _task t2
JOIN \ ` ${ tableName } \` od2 ON od2.wave_id = t2.wave_id AND od2.status != 'cancel'
WHERE $ { taskWindowWhere2 } AND t2 . wave _id > 0
AND NOT EXISTS ( SELECT 1 FROM wms _wave _pick _task _order pto2 WHERE pto2 . task _id = t2 . id )
) x
` ;
` ;
const goodsSkuSelectDispatch = goodsSkuSelect . replace ( ` \` ${ currentTableName } \` ` , ` \` ${ dispatchTableName } \` ` ) ;
const [ currentRows , dispatchRows ] = await Promise . all ( [
const topGoodsSkuSelect = tableName => `
model . query ( goodsSkuSelect , { replacements , type : QueryTypes . SELECT } ) ,
SELECT t . pick _user _id AS user _id , COALESCE ( SUM ( g . items _total ) , 0 ) AS goods , COALESCE ( SUM ( g . items _count ) , 0 ) AS sku
model . query ( goodsSkuSelectDispatch , { replacements , type : QueryTypes . SELECT } ) ,
FROM wms _wave _pick _task t
JOIN (
SELECT pto . task _id AS task _id , od . items _total , od . items _count
FROM wms _wave _pick _task _order pto
JOIN \ ` ${ tableName } \` od ON od.id = pto.order_id AND od.status != 'cancel'
UNION ALL
SELECT t2 . id AS task _id , od2 . items _total , od2 . items _count
FROM wms _wave _pick _task t2
JOIN \ ` ${ tableName } \` od2 ON od2.wave_id = t2.wave_id AND od2.status != 'cancel'
WHERE $ { taskWindowWhere2 } AND t2 . wave _id > 0
AND NOT EXISTS ( SELECT 1 FROM wms _wave _pick _task _order pto2 WHERE pto2 . task _id = t2 . id )
) g ON g . task _id = t . id
WHERE $ { taskWindowWhere }
GROUP BY t . pick _user _id
` ;
const [ currentRows , dispatchRows , topCurrentRows , topDispatchRows ] = await Promise . all ( [
model . query ( goodsSkuSelect ( currentTableName ) , { replacements , type : QueryTypes . SELECT } ) ,
model . query ( goodsSkuSelect ( dispatchTableName ) , { replacements , type : QueryTypes . SELECT } ) ,
model . query ( topGoodsSkuSelect ( currentTableName ) , { replacements , type : QueryTypes . SELECT } ) ,
model . query ( topGoodsSkuSelect ( dispatchTableName ) , { replacements , type : QueryTypes . SELECT } ) ,
] ) ;
] ) ;
const current = Array . isArray ( currentRows ) && currentRows . length > 0 ? currentRows [ 0 ] : { } ;
const current = Array . isArray ( currentRows ) && currentRows . length > 0 ? currentRows [ 0 ] : { } ;
@ -494,21 +752,6 @@ class WarehousePanelService extends Service {
sku : Number ( current . sku || 0 ) + Number ( dispatch . sku || 0 ) ,
sku : Number ( current . sku || 0 ) + Number ( dispatch . sku || 0 ) ,
} ;
} ;
const topGoodsSkuSelect = `
SELECT t . pick _user _id AS user _id , COALESCE ( SUM ( od . items _total ) , 0 ) AS goods , COALESCE ( SUM ( od . items _count ) , 0 ) AS sku
FROM wms _wave _pick _task t
JOIN wms _wave _pick _task _order pto ON pto . task _id = t . id
JOIN \ ` ${ currentTableName } \` od ON od.id = pto.order_id AND od.status != 'cancel'
WHERE $ { taskWindowWhere }
GROUP BY t . pick _user _id
` ;
const topGoodsSkuSelectDispatch = topGoodsSkuSelect . replace ( ` \` ${ currentTableName } \` ` , ` \` ${ dispatchTableName } \` ` ) ;
const [ topCurrentRows , topDispatchRows ] = await Promise . all ( [
model . query ( topGoodsSkuSelect , { replacements , type : QueryTypes . SELECT } ) ,
model . query ( topGoodsSkuSelectDispatch , { replacements , type : QueryTypes . SELECT } ) ,
] ) ;
const topGoodsSkuMap = new Map ( ) ;
const topGoodsSkuMap = new Map ( ) ;
[ ... ( topCurrentRows || [ ] ) , ... ( topDispatchRows || [ ] ) ] . forEach ( row => {
[ ... ( topCurrentRows || [ ] ) , ... ( topDispatchRows || [ ] ) ] . forEach ( row => {
const key = String ( row . user _id ) ;
const key = String ( row . user _id ) ;
@ -523,36 +766,51 @@ class WarehousePanelService extends Service {
}
}
/ * *
/ * *
* 打包 个人排名( 散单 + 团购任务合并 , 按打包员姓名聚合 ) 。
* 打包 统计块( 按波次范围区分 : wave _scope 1 大团购 / 3 小团购为团购块 , 其余为散单块 ) 。
*
*
* 口径 : 今日创建 ( create _time 今日 ) 且非异常 ( status != 3 ) 的任务 ;
* 口径 : 今日创建 ( create _time 今日 ) 且非异常 ( status != 3 ) 且未删除的任务 ;
* 团购任务多人打包时同一任务计入多人 ( 不折算 ) 。
* orders 按打包模式统计 — — pack _mode = 2 ( 团购模式 ) 按 pack _quantity ( 波次订单数快照 )
* 求和 , pack _mode = 1 散单每任务 1 单 ;
* 团购块 top 按打包台 ( wms _workbench . name ) 聚合 ; 散单块 top 按打包员
* ( pack _task _packer . account _name ) 聚合 。
*
*
* @ param { number } warehouseId 仓库 ID
* @ param { number } warehouseId 仓库 ID
* @ param { Date } todayStart 今日开始
* @ param { Date } todayStart 今日开始
* @ param { Date } tomorrowStart 明日开始
* @ param { Date } tomorrowStart 明日开始
* @ param { boolean } isGroup true = 团购 ( wave _scope 1 / 3 , top 按打包台 ) ; false = 散单 ( wave _scope 非 1 / 3 , top 按打包员 )
* @ return { Promise < Object > } { orders , goods , sku , wave , top }
* @ return { Promise < Object > } { orders , goods , sku , wave , top }
* /
* /
async queryPacking Personal ( warehouseId , todayStart , tomorrowStart ) {
async queryPacking Block ( warehouseId , todayStart , tomorrowStart , isGroup ) {
const { model } = this . ctx ;
const { model } = this . ctx ;
const { QueryTypes } = model ;
const { QueryTypes } = model ;
const scopeWhere = isGroup ? 'pt.wave_scope IN (1, 3)' : 'pt.wave_scope NOT IN (1, 3)' ;
// 订单数口径: pack_mode=2( 团购模式, 含 wave_scope=3 小团购)按 pack_quantity 求和;散单每任务 1 单
const ordersExpr = 'SUM(CASE WHEN pt.pack_mode = 2 THEN pt.pack_quantity ELSE 1 END) AS orders' ;
const replacements = { warehouseId , start : todayStart , end : tomorrowStart } ;
const replacements = { warehouseId , start : todayStart , end : tomorrowStart } ;
const taskWindowWhere = `
const taskWindowWhere = `
pt . warehouse _id = : warehouseId AND pt . create _time >= : start AND pt . create _time < : end
pt . warehouse _id = : warehouseId AND $ { scopeWhere }
AND pt . create _time >= : start AND pt . create _time < : end
AND pt . status != 3 AND pt . deleted _at IS NULL
AND pt . status != 3 AND pt . deleted _at IS NULL
` ;
` ;
const [ summaryRows , topRows ] = await Promise . all ( [
// 团购块按打包台聚合;散单块按打包员聚合
model . query ( `
const topSql = isGroup
SELECT COUNT ( * ) AS orders , COALESCE ( SUM ( pt . item _quantity ) , 0 ) AS goods ,
? `
SELECT COALESCE ( w . name , '未指定打包台' ) AS name , $ { ordersExpr } ,
COALESCE ( SUM ( pt . item _quantity ) , 0 ) AS goods ,
COALESCE ( SUM ( pt . sku _count ) , 0 ) AS sku ,
COALESCE ( SUM ( pt . sku _count ) , 0 ) AS sku ,
COUNT ( DISTINCT CASE WHEN pt . wave _id > 0 THEN pt . wave _id END ) AS wave
COUNT ( DISTINCT CASE WHEN pt . wave _id > 0 THEN pt . wave _id END ) AS wave
FROM wms _pack _task pt
FROM wms _pack _task pt
LEFT JOIN wms _workbench w ON w . id = pt . workbench _id AND w . deleted _at IS NULL
WHERE $ { taskWindowWhere }
WHERE $ { taskWindowWhere }
` , { replacements, type: QueryTypes.SELECT }),
GROUP BY pt . workbench _id , w . name
model . query ( `
ORDER BY orders DESC
SELECT pp . account _name AS name , COUNT ( * ) AS orders , COALESCE ( SUM ( pt . item _quantity ) , 0 ) AS goods ,
LIMIT 5
`
: `
SELECT pp . account _name AS name , $ { ordersExpr } ,
COALESCE ( SUM ( pt . item _quantity ) , 0 ) AS goods ,
COALESCE ( SUM ( pt . sku _count ) , 0 ) AS sku ,
COALESCE ( SUM ( pt . sku _count ) , 0 ) AS sku ,
COUNT ( DISTINCT CASE WHEN pt . wave _id > 0 THEN pt . wave _id END ) AS wave
COUNT ( DISTINCT CASE WHEN pt . wave _id > 0 THEN pt . wave _id END ) AS wave
FROM wms _pack _task pt
FROM wms _pack _task pt
@ -561,7 +819,17 @@ class WarehousePanelService extends Service {
GROUP BY pp . account _name
GROUP BY pp . account _name
ORDER BY orders DESC
ORDER BY orders DESC
LIMIT 5
LIMIT 5
` ;
const [ summaryRows , topRows ] = await Promise . all ( [
model . query ( `
SELECT $ { ordersExpr } , COALESCE ( SUM ( pt . item _quantity ) , 0 ) AS goods ,
COALESCE ( SUM ( pt . sku _count ) , 0 ) AS sku ,
COUNT ( DISTINCT CASE WHEN pt . wave _id > 0 THEN pt . wave _id END ) AS wave
FROM wms _pack _task pt
WHERE $ { taskWindowWhere }
` , { replacements, type: QueryTypes.SELECT }),
` , { replacements, type: QueryTypes.SELECT }),
model . query ( topSql , { replacements , type : QueryTypes . SELECT } ) ,
] ) ;
] ) ;
const summary = Array . isArray ( summaryRows ) && summaryRows . length > 0 ? summaryRows [ 0 ] : { } ;
const summary = Array . isArray ( summaryRows ) && summaryRows . length > 0 ? summaryRows [ 0 ] : { } ;