front/src/utils/workplace-care.ts
zzh f77c478981
Some checks failed
Build and Deploy Front / deploy (push) Failing after 8s
feat: 大包/主单管理打印面单,并优化价格表公式下拉
管理页支持选模板批量打印;标签设计增加大包/主单类型;价格表计算公式改为轻量选项菜单。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-07 17:56:13 +08:00

282 lines
7.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { OrderWorkplaceStats } from '@/api/workplace'
import {
getCarePeriod,
getSpringFestivalDate,
type CarePeriod,
} from '@/utils/admin-login-care'
/** 工作台「今日关照」:按角色挑选一条待办引导,避免堆指标 */
export type CarePendingKind =
| 'pendingPayment'
| 'pendingShip'
| 'problematic'
| 'storeOrderPending'
export type CarePendingGuide = {
kind: CarePendingKind
count: number
path: string
query?: Record<string, string>
}
export type CareRoleTone = 'finance' | 'cs' | 'ops' | 'track' | 'sales' | 'general'
export type CareWeekdayTone = 'monday' | 'friday' | null
const OVERDUE_CARE_THRESHOLD = 5
export function roleHints(hasRole: (code: string) => boolean) {
const hit = (list: string[]) => list.some((c) => hasRole(c))
return {
finance: hit(['Finance', 'finance', '财务']),
cs: hit(['CS', 'CustomerService', '客服']),
ops: hit(['Ops', 'Operation', 'Warehouse', '操作', '仓储']),
track: hit(['Track', '轨迹']),
sales: hit(['Sales', '销售']),
}
}
export function resolveCareRoleTone(hasRole: (code: string) => boolean): CareRoleTone {
const h = roleHints(hasRole)
if (h.finance) return 'finance'
if (h.cs) return 'cs'
if (h.ops) return 'ops'
if (h.track) return 'track'
if (h.sales) return 'sales'
return 'general'
}
/** 员工工作台常用入口:按角色 3~4 个,避免与「今日待办」数字入口重复 */
export type AdminQuickLinkKey =
| 'training'
| 'warning'
| 'waybill'
| 'track'
| 'pickup'
| 'price'
export function resolveAdminQuickLinkKeys(
hasRole: (code: string) => boolean,
): AdminQuickLinkKey[] {
const tone = resolveCareRoleTone(hasRole)
let roleLinks: AdminQuickLinkKey[]
switch (tone) {
case 'finance':
roleLinks = ['waybill', 'price', 'track']
break
case 'cs':
roleLinks = ['waybill', 'track', 'pickup']
break
case 'ops':
roleLinks = ['waybill', 'pickup', 'track']
break
case 'track':
roleLinks = ['warning', 'track', 'waybill']
break
case 'sales':
roleLinks = ['waybill', 'price', 'track']
break
default:
roleLinks = ['waybill', 'track', 'pickup', 'price']
}
// 培训手册置顶,方便新人;总量仍控制在 4 个以内
return ['training', ...roleLinks.filter((k) => k !== 'training')].slice(0, 4)
}
/** 待办卡紧急度:有量时问题件 / 理赔优先 */
export type AdminOpsMetricKey =
| 'problematic'
| 'claimed'
| 'pendingShip'
| 'pendingPayment'
const OPS_URGENCY: Record<AdminOpsMetricKey, number> = {
problematic: 0,
claimed: 1,
pendingShip: 2,
pendingPayment: 3,
}
export function sortAdminOpsMetrics(
counts: Record<AdminOpsMetricKey, number>,
): AdminOpsMetricKey[] {
const keys = Object.keys(OPS_URGENCY) as AdminOpsMetricKey[]
return [...keys].sort((a, b) => {
const aActive = (counts[a] ?? 0) > 0 ? 0 : 1
const bActive = (counts[b] ?? 0) > 0 ? 0 : 1
if (aActive !== bActive) return aActive - bActive
return OPS_URGENCY[a] - OPS_URGENCY[b]
})
}
function pickMax(
candidates: Array<{
kind: CarePendingKind
count: number
path: string
query?: Record<string, string>
}>,
): CarePendingGuide | null {
const positive = candidates.filter((c) => c.count > 0)
if (!positive.length) return null
positive.sort((a, b) => b.count - a.count)
return positive[0]
}
/**
* 角色差异:优先匹配角色偏好;否则在「有数」的待办里挑最大的一条。
* 数据本身已受工作台 stats 数据权限过滤。
*/
export function resolveCarePendingGuide(options: {
hasRole: (code: string) => boolean
isCustomerPortal: boolean
stats: OrderWorkplaceStats | null
orderListPath: string
}): CarePendingGuide | null {
if (options.isCustomerPortal || !options.stats) return null
const s = options.stats
const listPath = options.orderListPath
const hints = roleHints(options.hasRole)
const pendingPayment: CarePendingGuide = {
kind: 'pendingPayment',
count: s.pendingPayment,
path: listPath,
query: { status: 'pendingPayment' },
}
const pendingShip: CarePendingGuide = {
kind: 'pendingShip',
count: s.pendingShip,
path: listPath,
query: { status: 'pendingShip' },
}
const problematic: CarePendingGuide = {
kind: 'problematic',
count: s.problematic,
path: listPath,
query: { status: 'problematic' },
}
const storePending: CarePendingGuide = {
kind: 'storeOrderPending',
count: s.storeOrderPending,
path: '/order/platformStoreOrder',
query: { fulfillStatus: 'pending' },
}
if (hints.finance) {
return pendingPayment.count > 0 ? pendingPayment : pickMax([problematic, pendingShip])
}
if (hints.cs) {
if (problematic.count > 0) return problematic
return pickMax([pendingPayment, pendingShip])
}
if (hints.ops) {
if (pendingShip.count > 0) return pendingShip
return pickMax([problematic, storePending])
}
if (hints.sales) {
return pickMax([storePending, pendingPayment, pendingShip, problematic])
}
if (hints.track) {
return pickMax([problematic, pendingShip])
}
return pickMax([problematic, pendingPayment, pendingShip, storePending])
}
export function isCarePressureHigh(
guide: CarePendingGuide | null,
threshold = OVERDUE_CARE_THRESHOLD,
) {
return (guide?.count ?? 0) >= threshold
}
/** 周一慢热 / 周五收口;周末不套用 */
export function getWeekdayTone(now = new Date()): CareWeekdayTone {
const day = now.getDay()
if (day === 1) return 'monday'
if (day === 5) return 'friday'
return null
}
/**
* 旺季窗口(P2 可配置前的前端常量):
* - 年末高峰 11/01–12/31
* - 春节窗口:当年春节公历日前 14 天~后 7 天(复用关怀节假日表)
*/
export function isPeakSeason(now = new Date()): boolean {
const m = now.getMonth() + 1
if (m === 11 || m === 12) return true
const year = now.getFullYear()
if (m === 1 || m === 2) {
const spring = getSpringFestivalDate(year)
if (spring) {
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime()
const start = spring.getTime() - 14 * 86400000
const end = spring.getTime() + 7 * 86400000
if (today >= start && today <= end) return true
}
}
return false
}
/**
* 开班交接轻提示:上午且有问题件时展示。
* 无交接班专用接口前,用问题件数量作「建议先看」引导,不伪称精确上一班统计。
*/
export function resolveHandoffHint(options: {
now?: Date
stats: OrderWorkplaceStats | null
orderListPath: string
}): CarePendingGuide | null {
const now = options.now ?? new Date()
const period: CarePeriod = getCarePeriod(now)
if (period !== 'morning') return null
const count = options.stats?.problematic ?? 0
if (count <= 0) return null
return {
kind: 'problematic',
count,
path: options.orderListPath,
query: { status: 'problematic' },
}
}
function pad2(n: number) {
return String(n).padStart(2, '0')
}
function toDateKey(date: Date) {
return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`
}
export const CARE_WARNING_PATH = '/track/warningOrder'
export const CARE_PANEL_COLLAPSED_KEY = 'oms-admin-care-panel-collapsed'
export const CARE_CLOCK_OUT_KEY_PREFIX = 'oms-admin-care-clock-out:'
export function getClockOutStorageKey(userId: string, now = new Date()) {
return `${CARE_CLOCK_OUT_KEY_PREFIX}${userId}:${toDateKey(now)}`
}
export function isClockedOutToday(userId: string, now = new Date()) {
if (!userId) return false
try {
return localStorage.getItem(getClockOutStorageKey(userId, now)) === '1'
} catch {
return false
}
}
export function setClockedOutToday(userId: string, done: boolean, now = new Date()) {
if (!userId) return
try {
const key = getClockOutStorageKey(userId, now)
if (done) localStorage.setItem(key, '1')
else localStorage.removeItem(key)
} catch {
// ignore
}
}