259 lines
10 KiB
TypeScript
259 lines
10 KiB
TypeScript
/** 客户端任务管理看板纯逻辑:下拉选项、状态/设备/店铺/进度展示映射、查询参数构建。
|
|
* 无框架依赖,可直接被 node 单测导入(对齐 tasks 域 image-video-model 的拆法)。 */
|
|
import { asinCountryLabel } from '../asin/asin-country.ts'
|
|
|
|
export interface TaskCenterTaskFilter {
|
|
username: string
|
|
moduleType: string
|
|
status: string
|
|
/** 店铺名精确匹配(后端按 LOWER(TRIM(...)) 比较),候选来自 /shop-options 与面板下钻。 */
|
|
shop: string
|
|
startTime: string
|
|
endTime: string
|
|
}
|
|
|
|
export interface TaskCenterVariantFilter {
|
|
/** 麦象状态码字符串(''=全部状态 / 0 待执行 / 1 处理中 / 2 成功 / 3 失败)。 */
|
|
status: string
|
|
}
|
|
|
|
export type TaskCenterQueryParams = Record<string, string | number>
|
|
|
|
export function createTaskFilter(): TaskCenterTaskFilter {
|
|
return { username: '', moduleType: '', status: '', shop: '', startTime: '', endTime: '' }
|
|
}
|
|
|
|
export function createVariantFilter(): TaskCenterVariantFilter {
|
|
return { status: '' }
|
|
}
|
|
|
|
/** 业务下拉:与后端 TaskModuleRegistry 的客户端模块(biz_file_task 12 个 + 品牌检测)对齐。 */
|
|
export const MODULE_OPTIONS: Array<{ value: string; label: string }> = [
|
|
{ value: '', label: '全部业务' },
|
|
{ value: 'PUBLISH', label: '上架' },
|
|
{ value: 'DELETE_BRAND', label: '删除ASIN' },
|
|
{ value: 'PRODUCT_RISK_RESOLVE', label: '商品风险解决' },
|
|
{ value: 'PRICE_TRACK', label: '跟价' },
|
|
{ value: 'SHOP_MATCH', label: '定时匹配' },
|
|
{ value: 'PATROL_DELETE', label: '巡店删除' },
|
|
{ value: 'QUERY_ASIN', label: '查询ASIN' },
|
|
{ value: 'WITHDRAW', label: '取款' },
|
|
{ value: 'APPEARANCE_PATENT', label: '外观专利检测' },
|
|
{ value: 'SIMILAR_ASIN', label: '货源查询' },
|
|
{ value: 'COLLECT_DATA', label: '采集数据' },
|
|
{ value: 'SHOP_DATA_CRAWL', label: '店铺数据抓取' },
|
|
{ value: 'BRAND', label: '品牌检测' },
|
|
]
|
|
|
|
/**
|
|
* 「在跑」= 待执行 ∪ 执行中 ∪ 已排期 的合并哨兵(后端展开成 IN 列表),不是表里的状态值。
|
|
* 看板默认就用它:客户端任务的日常问题是「现在有什么在跑」,历史状态保留但不用来看。
|
|
*/
|
|
export const ACTIVE_TASK_STATUS = 'ACTIVE'
|
|
|
|
/**
|
|
* 状态三态收敛(用户 2026-10-04 定案:任务状态只要 执行中/成功/失败,其他不用):
|
|
* 「执行中」= ACTIVE 合并哨兵(待执行/已排期并入,与默认视图、汇总卡同口径);
|
|
* 成功/失败的同义词展开(COMPLETED/ERROR/取消)在后端 TaskCenterQueryService.statusFilter。
|
|
*/
|
|
export const TASK_STATUS_OPTIONS: Array<{ value: string; label: string }> = [
|
|
{ value: '', label: '全部状态' },
|
|
{ value: ACTIVE_TASK_STATUS, label: '执行中' },
|
|
{ value: 'SUCCESS', label: '成功' },
|
|
{ value: 'FAILED', label: '失败' },
|
|
]
|
|
|
|
/**
|
|
* 变体采集状态四态(用户 2026-10-05 定案:在跑口径不要,就显示 待执行/处理中/成功/失败):
|
|
* 空值 = 全部状态(后端原样直查麦象,含历史终态),不再有「在跑」合并口径。
|
|
*/
|
|
export const VARIANT_STATUS_OPTIONS: Array<{ value: string; label: string }> = [
|
|
{ value: '', label: '全部状态' },
|
|
{ value: '0', label: '待执行' },
|
|
{ value: '1', label: '处理中' },
|
|
{ value: '2', label: '成功' },
|
|
{ value: '3', label: '失败' },
|
|
]
|
|
|
|
/** 三态展示映射(2026-10-04 收敛):进行中各态统一「执行中」,完成态统一「成功」,
|
|
* 失败/出错/取消统一「失败」(取消 = 没成功也不在跑,并入失败)。 */
|
|
const TASK_STATUS_LABELS: Record<string, string> = {
|
|
PENDING: '执行中',
|
|
RUNNING: '执行中',
|
|
SCHEDULED: '执行中',
|
|
SUCCESS: '成功',
|
|
COMPLETED: '成功',
|
|
FAILED: '失败',
|
|
ERROR: '失败',
|
|
CANCELLED: '失败',
|
|
CANCELED: '失败',
|
|
}
|
|
|
|
/** 麦象批量任务状态四态文案(2026-10-05 用户定案):0 待执行 / 1 处理中 / 2 成功 / 3 失败。 */
|
|
const VARIANT_STATUS_LABELS: Record<number, string> = {
|
|
0: '待执行',
|
|
1: '处理中',
|
|
2: '成功',
|
|
3: '失败',
|
|
}
|
|
|
|
export type TaskStatusType = 'pending' | 'running' | 'success' | 'failed' | 'cancelled'
|
|
|
|
export function taskStatusLabel(status: string | null | undefined): string {
|
|
const key = (status || '').toUpperCase()
|
|
return TASK_STATUS_LABELS[key] || '执行中'
|
|
}
|
|
|
|
export function taskStatusType(status: string | null | undefined): TaskStatusType {
|
|
const key = (status || '').toUpperCase()
|
|
if (key === 'SUCCESS' || key === 'COMPLETED') return 'success'
|
|
if (key === 'FAILED' || key === 'ERROR' || key === 'CANCELLED' || key === 'CANCELED') return 'failed'
|
|
return 'running'
|
|
}
|
|
|
|
/** 麦象批量任务状态:0 待执行 / 1 处理中 / 2 成功 / 3 失败(未识别不冒充成功)。 */
|
|
export function variantStatusLabel(status: number | string | null | undefined): string {
|
|
if (status == null || status === '') return '未知'
|
|
const code = Number(status)
|
|
if (!Number.isFinite(code)) return '未知'
|
|
return VARIANT_STATUS_LABELS[code] || '未知'
|
|
}
|
|
|
|
export function variantStatusType(status: number | string | null | undefined): TaskStatusType {
|
|
const code = Number(status)
|
|
if (code === 2) return 'success'
|
|
if (code === 3) return 'failed'
|
|
if (code === 0) return 'pending'
|
|
return 'running'
|
|
}
|
|
|
|
const DEVICE_ID_DISPLAY_MAX = 18
|
|
|
|
/** 设备展示名:优先设备名,缺失时截断设备号(任务侧没有主机名)。 */
|
|
export function deviceDisplayName(
|
|
deviceName: string | null | undefined,
|
|
deviceId: string | null | undefined,
|
|
): string {
|
|
const name = (deviceName || '').trim()
|
|
if (name) return name
|
|
const id = (deviceId || '').trim()
|
|
if (!id) return '未记录'
|
|
return id.length > DEVICE_ID_DISPLAY_MAX ? `${id.slice(0, DEVICE_ID_DISPLAY_MAX - 1)}…` : id
|
|
}
|
|
|
|
/** 店铺展示:主店铺名 + 多店铺数量(口径 = 主来源命中数)。 */
|
|
export function shopDisplayName(shopName: string | null | undefined, shopCount: number | null | undefined): string {
|
|
const name = (shopName || '').trim()
|
|
if (!name) return '—'
|
|
const count = Number(shopCount || 0)
|
|
return count > 1 ? `${name} 等${count}个` : name
|
|
}
|
|
|
|
/** 国家列展示:后端已归一成 `UK` / `UK/DE` 形状,逐段走共享国家字典转中文(用户 2026-10-04 要求);
|
|
* 字典外的国家码原样保留,取不到画「—」。 */
|
|
export function countryLabel(country: string | null | undefined): string {
|
|
const value = (country || '').trim()
|
|
if (!value) return '—'
|
|
return value
|
|
.split('/')
|
|
.map((part) => asinCountryLabel(part.trim()))
|
|
.filter((label) => label !== '')
|
|
.join('/')
|
|
}
|
|
|
|
/** 页签徽章用的条数:只有接口真的回了非负数才算一个数,否则回 null 画「—」。
|
|
* 麦象在结果表未注册时把 total 回成 -1 哨兵,`Number(x || 0)` 会把它读成 0 条。 */
|
|
export function tabCount(total: unknown): number | null {
|
|
if (total === null || total === undefined || total === '') return null
|
|
const value = Number(total)
|
|
return Number.isFinite(value) && value >= 0 ? value : null
|
|
}
|
|
|
|
/** 进度行:已完成数 = 成功 + 失败,与客户端一致(跑完一步就计入,不论成败)。
|
|
* 见 frontend-vue `BrandBrandTab.vue`「完成数 = 已处理品牌行(成功或查询失败都计入)」;
|
|
* 旧口径只数成功,会把「1 步全失败」的终态任务显示成 0 / 1。 */
|
|
export interface TaskCenterProgress {
|
|
progressSuccess: number
|
|
progressFailed: number
|
|
progressTotal: number
|
|
moduleType?: string | null
|
|
}
|
|
|
|
function progressDone(row: TaskCenterProgress): number {
|
|
return Math.max(0, Number(row.progressSuccess || 0)) + Math.max(0, Number(row.progressFailed || 0))
|
|
}
|
|
|
|
/** 采集数据在客户端写的是「关键词进度:第 N/总 个」(N = 已处理 + 1,封顶总个数),
|
|
* 照抄口径而不是这里另造一套说法:见 frontend-vue BrandCollectDataTab.vue taskKeywordProgress。 */
|
|
export function progressText(row: TaskCenterProgress): string {
|
|
const total = Number(row.progressTotal || 0)
|
|
if (total <= 0) return '—'
|
|
const done = progressDone(row)
|
|
if ((row.moduleType || '').toUpperCase() === 'COLLECT_DATA') {
|
|
return `关键词进度:第 ${Math.min(total, done + 1)}/${total} 个`
|
|
}
|
|
return `已完成 ${done} / 总 ${total}`
|
|
}
|
|
|
|
export function progressPercent(row: TaskCenterProgress): number {
|
|
const total = Number(row.progressTotal || 0)
|
|
if (total <= 0) return 0
|
|
return Math.min(100, Math.max(0, Math.round((progressDone(row) / total) * 100)))
|
|
}
|
|
|
|
/** datetime-local 原生值(YYYY-MM-DDTHH:mm) → 后端 LocalDateTime 兼容的秒级格式。 */
|
|
export function toApiTime(value: string | null | undefined): string | undefined {
|
|
const text = (value || '').trim().replace(' ', 'T')
|
|
const match = /^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(text)
|
|
if (!match) return undefined
|
|
return `${match[1]}T${match[2]}:${match[3]}:${match[4] ?? '00'}`
|
|
}
|
|
|
|
function text(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
/**
|
|
* 任务列表查询参数:空白项不下传(后端按缺省处理)。
|
|
* companyId = 公司 tab 选中的公司(超管专用,页面从 companyTab.requestCompanyId 取);
|
|
* 非超管恒 null ⇒ 这一支压根不发 company_id,后端的会话口径不受前端影响。
|
|
*/
|
|
export function buildTaskQuery(
|
|
filter: TaskCenterTaskFilter,
|
|
page: number,
|
|
pageSize: number,
|
|
companyId?: number | null,
|
|
): TaskCenterQueryParams {
|
|
const query: TaskCenterQueryParams = { page, pageSize }
|
|
if (companyId != null) query.company_id = companyId
|
|
const put = (key: string, value: string | undefined) => {
|
|
if (value) query[key] = value
|
|
}
|
|
put('username', text(filter.username))
|
|
put('moduleType', text(filter.moduleType))
|
|
put('status', text(filter.status))
|
|
put('shop', text(filter.shop))
|
|
put('startTime', toApiTime(filter.startTime))
|
|
put('endTime', toApiTime(filter.endTime))
|
|
return query
|
|
}
|
|
|
|
/** 变体采集查询参数:companyId 的口径与 buildTaskQuery 同一条(null 不发,不发=不筛)。 */
|
|
export function buildVariantQuery(
|
|
filter: TaskCenterVariantFilter,
|
|
page: number,
|
|
pageSize: number,
|
|
companyId?: number | null,
|
|
): TaskCenterQueryParams {
|
|
const query: TaskCenterQueryParams = { page, pageSize }
|
|
if (companyId != null) query.company_id = companyId
|
|
const status = text(filter.status)
|
|
if (status !== '') query.status = Number(status)
|
|
return query
|
|
}
|
|
|
|
/** 变体采集账号列:平台账号名缺失(历史伪 uid)时回落显示 uid。 */
|
|
export function variantAccountText(row: { username: string | null; uid: string | null }): string {
|
|
return (row.username || '').trim() || (row.uid || '').trim() || '—'
|
|
}
|