feat: 盘点任务按父子资产展示

This commit is contained in:
WuYongJie 2026-07-17 17:39:17 +08:00
parent 3e05835408
commit e1eb946506
12 changed files with 538 additions and 244 deletions

View File

@ -0,0 +1,17 @@
-- 冻结盘点任务创建时的父子关系,历史任务仅在父资产也在同一任务内时回填。
ALTER TABLE `inventory_task_item`
ADD COLUMN `snapshot_parent_equipment_id` INT NULL AFTER `equipment_id`;
CREATE INDEX `inventory_task_item_task_id_snapshot_parent_equipment_id_idx`
ON `inventory_task_item` (`task_id`, `snapshot_parent_equipment_id`);
-- 不能使用当前关系直接覆盖历史任务;只补齐同一任务内可确认的直接父资产。
UPDATE `inventory_task_item` AS `child_item`
INNER JOIN `equipment` AS `current_equipment`
ON `current_equipment`.`id` = `child_item`.`equipment_id`
INNER JOIN `inventory_task_item` AS `parent_item`
ON `parent_item`.`task_id` = `child_item`.`task_id`
AND `parent_item`.`equipment_id` = `current_equipment`.`parent_id`
SET `child_item`.`snapshot_parent_equipment_id` = `current_equipment`.`parent_id`
WHERE `child_item`.`snapshot_parent_equipment_id` IS NULL
AND `current_equipment`.`parent_id` IS NOT NULL;

View File

@ -517,6 +517,8 @@ model InventoryTaskItem {
task_id Int
participant_id Int?
equipment_id Int
// 任务创建时冻结的直接父资产;为空表示该项在本任务中按根资产展示。
snapshot_parent_equipment_id Int?
equipment_code String @db.VarChar(50)
equipment_name String @db.VarChar(100)
equipment_model String? @db.VarChar(100)
@ -551,6 +553,7 @@ model InventoryTaskItem {
@@unique([task_id, equipment_id])
@@index([task_id, participant_id, check_status])
@@index([task_id, equipment_id])
@@index([task_id, snapshot_parent_equipment_id])
@@index([task_id, is_unassigned])
@@index([task_id, check_status])
@@index([checked_by_participant_id])

View File

@ -6,16 +6,15 @@ import { successResponse, errorResponse } from '../utils/response'
const DEFAULT_SITE_NAME = '固定资产管理系统'
const DEFAULT_THEME_COLOR = '#10A37F'
const DEFAULT_LOGO_URL = '/branding/logo.svg'
const DEFAULT_LOGO_URL = '/branding/custom-logo.jpg'
const DEFAULT_FAVICON_URL = '/branding/favicon.svg'
const CUSTOM_LOGO_CANDIDATES = [
'custom-logo.svg',
'custom-logo.png',
'custom-logo.webp',
// 站点 Logo 固定优先使用后台已确认的 custom-logo.jpg,避免首屏或文件探测回退到旧 SVG。
const LOGO_CANDIDATES = [
'custom-logo.jpg',
'custom-logo.jpeg'
'custom-logo.jpeg',
'custom-logo.png',
'custom-logo.webp'
]
const DEFAULT_LOGO_CANDIDATES = ['logo.svg', 'logo.png', 'logo.webp', 'logo.jpg', 'logo.jpeg']
const CUSTOM_FAVICON_CANDIDATES = [
'custom-favicon.ico',
'custom-favicon.svg',
@ -58,7 +57,7 @@ const resolveBrandingAssetUrl = (candidates: string[], fallbackUrl: string) => {
const getResolvedLogoUrl = () =>
resolveBrandingAssetUrl(
[...CUSTOM_LOGO_CANDIDATES, ...DEFAULT_LOGO_CANDIDATES],
LOGO_CANDIDATES,
DEFAULT_LOGO_URL
)
@ -66,9 +65,8 @@ const getResolvedFaviconUrl = () =>
resolveBrandingAssetUrl(
[
...CUSTOM_FAVICON_CANDIDATES,
...CUSTOM_LOGO_CANDIDATES,
...DEFAULT_FAVICON_CANDIDATES,
...DEFAULT_LOGO_CANDIDATES
...LOGO_CANDIDATES,
...DEFAULT_FAVICON_CANDIDATES
],
getResolvedLogoUrl() || DEFAULT_FAVICON_URL
)

View File

@ -55,6 +55,7 @@ const sanitizeInventoryParticipant = <T extends { person_source_userid?: string
type FrozenItemDraft = {
participant_person_id: number | null
equipment_id: number
snapshot_parent_equipment_id: number | null
equipment_code: string
equipment_name: string
equipment_model: string | null
@ -485,6 +486,18 @@ const buildAssetTypeBreakdown = (items: FrozenItemDraft[]) =>
.values()
).sort((left, right) => right.total - left.total)
const buildInventoryTreeCounts = (items: FrozenItemDraft[]) => {
const equipmentIds = new Set(items.map((item) => item.equipment_id))
const rootItemsCount = items.filter((item) =>
!item.snapshot_parent_equipment_id || !equipmentIds.has(item.snapshot_parent_equipment_id)
).length
return {
root_items_count: rootItemsCount,
accessory_items_count: Math.max(0, items.length - rootItemsCount)
}
}
export const buildInventoryScope = async (
options: InventoryScopeOptions
): Promise<InventoryScopeBuildResult> => {
@ -555,10 +568,13 @@ export const buildInventoryScope = async (
const borrowAnchorId = resolveBorrowAnchorId(equipment.id, equipmentMap, activeBorrowRecordMap)
const activeBorrowRecords = activeBorrowRecordMap.get(borrowAnchorId) || []
const activeBorrowRecord = resolveUniqueActiveBorrowRecord(activeBorrowRecords)
const itemDraft = buildInventoryScopeItemDraft({
equipment,
activeBorrowRecord
})
const itemDraft: FrozenItemDraft = {
...buildInventoryScopeItemDraft({
equipment,
activeBorrowRecord
}),
snapshot_parent_equipment_id: equipment.parent_id || null
}
if (
normalizedScope.personIds.length > 0 &&
@ -603,6 +619,7 @@ export const buildInventoryScope = async (
export const previewInventoryTask = async (options: InventoryScopeOptions) => {
const scope = await buildInventoryScope(options)
const unassignedCount = scope.items.filter((item) => item.is_unassigned).length
const treeCounts = buildInventoryTreeCounts(scope.items)
return {
scope_department: scope.scopeDepartments.length === 1 ? scope.scopeDepartments[0] : null,
@ -613,6 +630,7 @@ export const previewInventoryTask = async (options: InventoryScopeOptions) => {
scope_people: scope.scopePeople,
participants_count: scope.participants.length,
items_count: scope.items.length,
...treeCounts,
unassigned_count: unassignedCount,
department_breakdown: buildDepartmentBreakdown(scope.items),
asset_type_breakdown: buildAssetTypeBreakdown(scope.items),
@ -643,10 +661,13 @@ export const getInventoryScopeContext = async (options: InventoryScopeOptions) =
)
: primaryScope.rootAssets
const treeCounts = buildInventoryTreeCounts(primaryScope.items)
return {
participants: primaryScope.participants.map(sanitizeInventoryParticipant),
participants_count: primaryScope.participants.length,
items_count: primaryScope.items.length,
...treeCounts,
unassigned_count: primaryScope.items.filter((item) => item.is_unassigned).length,
asset_types: buildScopeFilterEntities(rootAssetsForAssetTypes, 'assetType'),
storage_locations: buildScopeFilterEntities(rootAssetsForStorageLocations, 'storageLocation')
@ -662,6 +683,7 @@ const buildTaskScopeSnapshot = (scope: InventoryScopeBuildResult) => ({
scope_people: scope.scopePeople,
participants_count: scope.participants.length,
items_count: scope.items.length,
...buildInventoryTreeCounts(scope.items),
unassigned_count: scope.items.filter((item) => item.is_unassigned).length,
generated_at: new Date().toISOString()
})
@ -911,6 +933,7 @@ export const createInventoryTask = async (
? participantIdByPersonId.get(item.participant_person_id) || null
: null,
equipment_id: item.equipment_id,
snapshot_parent_equipment_id: item.snapshot_parent_equipment_id,
equipment_code: item.equipment_code,
equipment_name: item.equipment_name,
equipment_model: item.equipment_model,

View File

@ -0,0 +1,22 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import test from 'node:test'
const migrationPath = path.resolve(
__dirname,
'../../../prisma/migrations/20260717_inventory_item_relation_snapshot/migration.sql'
)
test('盘点父子关系迁移只新增快照字段,并且仅回填同任务内存在的父资产', () => {
const sql = readFileSync(migrationPath, 'utf8')
assert.match(sql, /ALTER TABLE `inventory_task_item`/i)
assert.match(sql, /ADD COLUMN `snapshot_parent_equipment_id` INT NULL/i)
assert.match(sql, /CREATE INDEX `inventory_task_item_task_id_snapshot_parent_equipment_id_idx`/i)
assert.match(sql, /INNER JOIN `inventory_task_item` AS `parent_item`/i)
assert.match(sql, /`parent_item`\.`task_id` = `child_item`\.`task_id`/i)
assert.match(sql, /`parent_item`\.`equipment_id` = `current_equipment`\.`parent_id`/i)
assert.doesNotMatch(sql, /DROP\s+(TABLE|COLUMN)/i)
assert.doesNotMatch(sql, /DELETE\s+FROM/i)
})

View File

@ -4,7 +4,7 @@ import type { SiteConfigResponse } from '@/api/settings'
const DEFAULT_SITE_NAME = '固定资产管理系统'
const DEFAULT_THEME_COLOR = '#10A37F'
const DEFAULT_LOGO_URL = '/branding/logo.svg'
const DEFAULT_LOGO_URL = '/branding/custom-logo.jpg'
const DEFAULT_FAVICON_URL = '/branding/favicon.svg'
const getIconMimeType = (iconUrl: string) => {

View File

@ -273,6 +273,8 @@ export interface InventoryTaskScopeSnapshot {
scope_people: InventoryScopePerson[]
participants_count: number
items_count: number
root_items_count?: number
accessory_items_count?: number
unassigned_count: number
generated_at: string
}
@ -302,6 +304,8 @@ export interface InventoryScopeContext {
participants: InventoryPreviewParticipantSample[]
participants_count: number
items_count: number
root_items_count?: number
accessory_items_count?: number
unassigned_count: number
asset_types: InventoryScopeEntity[]
storage_locations: InventoryScopeEntity[]
@ -310,6 +314,7 @@ export interface InventoryScopeContext {
export interface InventoryPreviewItemSample {
participant_person_id: number | null
equipment_id: number
snapshot_parent_equipment_id?: number | null
equipment_code: string
equipment_name: string
equipment_model: string | null
@ -335,6 +340,8 @@ export interface InventoryTaskPreview {
scope_people: InventoryScopePerson[]
participants_count: number
items_count: number
root_items_count?: number
accessory_items_count?: number
unassigned_count: number
department_breakdown: InventoryPreviewDepartmentBreakdown[]
asset_type_breakdown: InventoryPreviewAssetTypeBreakdown[]
@ -393,6 +400,7 @@ export interface InventoryTaskItem {
task_id: number
participant_id: number | null
equipment_id: number
snapshot_parent_equipment_id?: number | null
equipment_code: string
equipment_name: string
equipment_model: string | null

View File

@ -381,8 +381,11 @@
<p class="mt-1 text-lg font-semibold text-text-primary">{{ formatNumber(preview.participants_count) }}</p>
</div>
<div class="panel !p-3">
<p class="mobile-card__label">资产</p>
<p class="mt-1 text-lg font-semibold text-text-primary">{{ formatNumber(preview.items_count) }}</p>
<p class="mobile-card__label">主资产 / 配件</p>
<p class="mt-1 text-lg font-semibold text-text-primary">
{{ formatNumber(preview.root_items_count ?? preview.items_count) }} / {{ formatNumber(preview.accessory_items_count ?? 0) }}
</p>
<p class="mt-1 text-xs text-text-tertiary">共 {{ formatNumber(preview.items_count) }} 件</p>
</div>
<div class="panel !p-3">
<p class="mobile-card__label">待分配</p>
@ -484,8 +487,11 @@
<p class="mt-1 text-xl font-semibold text-text-primary">{{ formatNumber(preview.participants_count) }}</p>
</div>
<div class="panel !p-3">
<p class="mobile-card__label">资产</p>
<p class="mt-1 text-xl font-semibold text-text-primary">{{ formatNumber(preview.items_count) }}</p>
<p class="mobile-card__label">主资产 / 配件</p>
<p class="mt-1 text-xl font-semibold text-text-primary">
{{ formatNumber(preview.root_items_count ?? preview.items_count) }} / {{ formatNumber(preview.accessory_items_count ?? 0) }}
</p>
<p class="mt-1 text-xs text-text-tertiary">共 {{ formatNumber(preview.items_count) }} 件</p>
</div>
<div class="panel !p-3">
<p class="mobile-card__label">待分配</p>

View File

@ -5,152 +5,101 @@
</section>
<template v-else-if="task">
<section class="page-header">
<div>
<p class="page-kicker">Inventory Detail</p>
<h2 class="page-title">{{ task.title }}</h2>
<p class="mt-2 text-sm text-text-secondary">{{ task.description || '未填写任务说明' }}</p>
<div class="mt-3 flex flex-wrap gap-2">
<section class="card !p-4 sm:!p-5">
<div class="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<h2 class="text-xl font-semibold text-text-primary">{{ task.title }}</h2>
<span class="badge" :class="task.status === 'closed' ? 'badge-neutral' : 'badge-success'">
{{ getTaskStatusText(task.status) }}
</span>
</div>
<p v-if="task.description" class="mt-1 text-sm text-text-secondary">{{ task.description }}</p>
<p class="mt-2 text-xs leading-5 text-text-secondary">
{{ scopeLabel }} · {{ participantNameSummary }} · 已处理 {{ formatNumber(handledItemCount) }} / {{ formatNumber(task.items_count) }}
<span v-if="task.deadline_at"> · 截止 {{ formatDateTime(task.deadline_at) }}</span>
</p>
</div>
<div class="flex shrink-0 flex-wrap gap-2">
<router-link to="/inventory" class="btn-secondary text-sm">返回</router-link>
<button type="button" class="btn-secondary text-sm" :disabled="loading" @click="loadTask">刷新</button>
<button type="button" class="btn-secondary text-sm" @click="copyPublicLink">复制链接</button>
<button type="button" class="btn-secondary text-sm" @click="openPublicLink">手机页</button>
<button
v-if="task.status === 'active'"
type="button"
class="btn-secondary text-sm !text-status-error"
:disabled="closing"
@click="handleCloseTask"
>
{{ closing ? '结束中...' : '结束任务' }}
</button>
<button
type="button"
class="btn-secondary text-sm text-status-error"
:disabled="deleting"
@click="handleDeleteTask"
>
{{ deleting ? '删除中...' : '删除任务' }}
</button>
<details class="relative">
<summary class="btn-secondary list-none text-sm [&::-webkit-details-marker]:hidden">更多</summary>
<div class="absolute right-0 z-20 mt-2 w-40 overflow-hidden rounded-lg border border-border bg-bg-elevated py-1 shadow-lg">
<button type="button" class="w-full px-3 py-2 text-left text-sm text-text-primary hover:bg-bg-hover" @click="copyPublicLink">复制链接</button>
<button type="button" class="w-full px-3 py-2 text-left text-sm text-text-primary hover:bg-bg-hover" @click="openPublicLink">手机页</button>
<button
v-if="task.status === 'active'"
type="button"
class="w-full px-3 py-2 text-left text-sm text-status-error hover:bg-bg-hover"
:disabled="closing"
@click="handleCloseTask"
>
{{ closing ? '结束中...' : '结束任务' }}
</button>
<button
type="button"
class="w-full px-3 py-2 text-left text-sm text-status-error hover:bg-bg-hover"
:disabled="deleting"
@click="handleDeleteTask"
>
{{ deleting ? '删除中...' : '删除任务' }}
</button>
</div>
</details>
</div>
</div>
<div class="page-meta-grid">
<div class="page-meta">
<p class="page-meta__label">任务状态</p>
<p class="page-meta__value">{{ getTaskStatusText(task.status) }}</p>
<p class="page-meta__subvalue">
{{ task.closed_at ? `关闭于 ${formatDateTime(task.closed_at)}` : task.deadline_at ? `截止 ${formatDateTime(task.deadline_at)}` : `创建于 ${formatDateTime(task.created_at)}` }}
</p>
<div class="mt-4 grid gap-2 border-t border-border pt-4 text-sm sm:grid-cols-3">
<div class="rounded-lg bg-bg-elevated px-3 py-2">
<span class="text-text-tertiary">进度</span>
<span class="ml-2 font-medium text-text-primary">{{ taskProgressText }}</span>
</div>
<div class="page-meta">
<p class="page-meta__label">总体进度</p>
<p class="page-meta__value">{{ taskProgressText }}</p>
<p class="page-meta__subvalue">异常 {{ formatNumber(task.exception_count) }} 件</p>
<div class="rounded-lg bg-bg-elevated px-3 py-2">
<span class="text-text-tertiary">异常</span>
<span class="ml-2 font-medium text-text-primary">{{ formatNumber(task.exception_count) }} 件</span>
</div>
<div class="page-meta">
<p class="page-meta__label">参与人</p>
<p class="page-meta__value">{{ formatNumber(task.participants_count) }}</p>
<p class="page-meta__subvalue">待确认 {{ formatNumber(pendingParticipantCount) }} 名</p>
</div>
<div class="page-meta">
<p class="page-meta__label">无人归属资产</p>
<p class="page-meta__value">{{ formatNumber(unassignedItems.length) }}</p>
<p class="page-meta__subvalue">这部分会单列处理,管理人员也可直接代扫</p>
</div>
</div>
</section>
<section class="card">
<div class="list-header">
<h3 class="section-title">任务概览</h3>
<span class="badge" :class="task.status === 'closed' ? 'badge-neutral' : 'badge-success'">
{{ getTaskStatusText(task.status) }}
</span>
</div>
<div class="field-grid">
<div class="field-stack">
<p class="field-label">盘点范围</p>
<p class="text-sm text-text-primary">{{ scopeLabel }}</p>
</div>
<div class="field-stack">
<p class="field-label">创建人</p>
<p class="text-sm text-text-primary">{{ task.created_by_name || `用户 ${task.created_by}` }}</p>
</div>
<div class="field-stack">
<p class="field-label">截止时间</p>
<p class="text-sm text-text-primary">{{ task.deadline_at ? formatDateTime(task.deadline_at) : '未设置' }}</p>
</div>
<div class="field-stack">
<p class="field-label">手机盘点地址</p>
<div class="flex flex-col gap-2 sm:flex-row">
<input :value="publicLink" type="text" class="input font-mono text-xs" readonly />
<button type="button" class="btn-secondary text-sm sm:w-auto" @click="copyPublicLink">复制</button>
</div>
</div>
<div class="field-stack">
<p class="field-label">范围快照时间</p>
<p class="text-sm text-text-primary">
{{ task.scope_snapshot?.generated_at ? formatDateTime(task.scope_snapshot.generated_at) : '未记录' }}
</p>
<div v-if="unassignedItems.length > 0" class="rounded-lg bg-status-warning/10 px-3 py-2 text-status-warning">
无人归属 {{ formatNumber(unassignedItems.length) }} 件
</div>
</div>
<div class="mt-6 grid gap-6 lg:grid-cols-2">
<section class="space-y-3">
<h4 class="section-title">部门进度</h4>
<div v-if="task.department_breakdown.length === 0" class="empty-state">暂无部门进度数据。</div>
<div v-else class="space-y-3">
<div
v-for="department in task.department_breakdown"
:key="department.department_name"
class="panel"
>
<div class="flex items-start justify-between gap-3">
<div>
<p class="text-sm font-medium text-text-primary">{{ department.department_name }}</p>
<p class="mt-1 text-xs text-text-secondary">
已确认 {{ formatNumber(department.checked) }} / 异常 {{ formatNumber(department.exception) }} / 待盘点 {{ formatNumber(department.pending) }}
</p>
</div>
<span class="badge badge-neutral">{{ formatNumber(department.total) }} 件</span>
</div>
<details class="mt-3 rounded-lg border border-border bg-bg-elevated px-3 py-2">
<summary class="cursor-pointer text-sm text-text-secondary">范围与链接</summary>
<div class="mt-3 grid gap-3 border-t border-border pt-3 text-xs sm:grid-cols-2">
<p class="text-text-secondary">创建人:<span class="text-text-primary">{{ task.created_by_name || `用户 ${task.created_by}` }}</span></p>
<p class="text-text-secondary">范围快照:<span class="text-text-primary">{{ task.scope_snapshot?.generated_at ? formatDateTime(task.scope_snapshot.generated_at) : '未记录' }}</span></p>
<div class="sm:col-span-2">
<p class="text-text-tertiary">手机盘点链接</p>
<div class="mt-1 flex gap-2">
<input :value="publicLink" type="text" class="input min-w-0 flex-1 font-mono text-xs" readonly />
<button type="button" class="btn-secondary text-xs" @click="copyPublicLink">复制</button>
</div>
</div>
</section>
<section class="space-y-3">
<h4 class="section-title">资产类型进度</h4>
<div v-if="task.asset_type_breakdown.length === 0" class="empty-state">暂无资产类型进度数据。</div>
<div v-else class="space-y-3">
<div
v-for="assetType in task.asset_type_breakdown"
:key="assetType.asset_type_name"
class="panel"
>
<div class="flex items-start justify-between gap-3">
<div>
<p class="text-sm font-medium text-text-primary">{{ assetType.asset_type_name }}</p>
<p class="mt-1 text-xs text-text-secondary">
已确认 {{ formatNumber(assetType.checked) }} / 异常 {{ formatNumber(assetType.exception) }} / 待盘点 {{ formatNumber(assetType.pending) }}
</p>
</div>
<span class="badge badge-neutral">{{ formatNumber(assetType.total) }} 件</span>
</div>
<div v-if="task.department_breakdown.length" class="sm:col-span-2">
<p class="text-text-tertiary">部门</p>
<div class="mt-1 flex flex-wrap gap-2">
<span v-for="department in task.department_breakdown" :key="department.department_name" class="badge badge-neutral">
{{ department.department_name }} {{ formatNumber(department.checked + department.exception) }}/{{ formatNumber(department.total) }}
</span>
</div>
</div>
</section>
</div>
<div v-if="task.asset_type_breakdown.length" class="sm:col-span-2">
<p class="text-text-tertiary">资产类型</p>
<div class="mt-1 flex flex-wrap gap-2">
<span v-for="assetType in task.asset_type_breakdown" :key="assetType.asset_type_name" class="badge badge-neutral">
{{ assetType.asset_type_name }} {{ formatNumber(assetType.checked + assetType.exception) }}/{{ formatNumber(assetType.total) }}
</span>
</div>
</div>
</div>
</details>
</section>
<section v-if="task.status === 'active'" class="card">
<section v-if="task.status === 'active'" class="card !p-4 sm:!p-5">
<div class="list-header">
<div>
<h3 class="section-title">管理人员代扫</h3>
<p class="mt-1 text-sm text-text-secondary">管理员、行政或 IT 可替责任人完成扫码盘点,也能处理无人归属资产。</p>
</div>
<span class="badge badge-warning">全任务通用</span>
<h3 class="section-title">代扫</h3>
</div>
<div class="grid gap-4 lg:grid-cols-[minmax(0,1fr)_auto_auto] lg:items-end">
@ -307,7 +256,9 @@
<section class="card">
<div class="list-header">
<h3 class="section-title">盘点资产明细</h3>
<span class="badge badge-neutral">共 {{ formatNumber(filteredItems.length) }} 件</span>
<span class="badge badge-neutral">
主资产 {{ formatNumber(inventoryItemTree.root_count) }} · 配件 {{ formatNumber(inventoryItemTree.accessory_count) }}
</span>
</div>
<div class="filter-bar">
@ -335,17 +286,37 @@
<div class="mt-4 mobile-card-list md:hidden">
<article
v-for="item in filteredItems"
:key="item.id"
v-for="row in inventoryItemRows"
:key="row.item.id"
class="mobile-card"
>
<div class="mobile-card__row">
<div>
<p class="text-sm font-semibold text-text-primary">{{ item.equipment_name }}</p>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ item.equipment_code }}</p>
<div class="min-w-0" :style="{ paddingLeft: `${row.depth * 14}px` }">
<div class="flex items-start gap-2">
<button
v-if="row.has_children"
type="button"
class="mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border border-border text-text-secondary"
:aria-label="row.is_expanded ? '收起配件' : '展开配件'"
@click="toggleInventoryItemChildren(row.item.equipment_id)"
>
<ChevronRightIcon class="h-3.5 w-3.5 transition-transform" :class="{ 'rotate-90': row.is_expanded }" />
</button>
<span v-else-if="row.depth > 0" class="mt-0.5 shrink-0 text-xs text-text-tertiary">↳</span>
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<p class="text-sm font-semibold text-text-primary">{{ row.item.equipment_name }}</p>
<span v-if="row.depth > 0" class="badge badge-neutral">配件</span>
<span v-else-if="row.has_children" class="badge badge-success">主资产</span>
<span v-else class="badge badge-neutral">独立</span>
<span v-if="row.has_children" class="badge badge-neutral">含 {{ row.descendant_count }} 个配件</span>
</div>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ row.item.equipment_code }}</p>
</div>
</div>
</div>
<span class="badge" :class="getItemStatusClass(item.check_status)">
{{ getItemStatusText(item.check_status) }}
<span class="badge" :class="getItemStatusClass(row.item.check_status)">
{{ getItemStatusText(row.item.check_status) }}
</span>
</div>
@ -353,36 +324,36 @@
<div>
<p class="mobile-card__label">责任人</p>
<p class="mobile-card__value">
{{ item.snapshot_person_nickname || '待分配' }}
<span v-if="isInventoryPublicAssetPerson(item)" class="ml-2 text-status-warning">
{{ row.item.snapshot_person_nickname || '待分配' }}
<span v-if="isInventoryPublicAssetPerson(row.item)" class="ml-2 text-status-warning">
· {{ getInventoryPublicAssetLabel() }}
</span>
<span v-if="item.snapshot_person_name" class="block text-xs text-text-secondary">
{{ item.snapshot_person_name }}
<span v-if="row.item.snapshot_person_name" class="block text-xs text-text-secondary">
{{ row.item.snapshot_person_name }}
</span>
</p>
</div>
<div>
<p class="mobile-card__label">部门</p>
<p class="mobile-card__value">{{ item.snapshot_department_name || '未归属' }}</p>
<p class="mobile-card__value">{{ row.item.snapshot_department_name || '未归属' }}</p>
</div>
<div>
<p class="mobile-card__label">资产类型</p>
<p class="mobile-card__value">{{ item.asset_type_name || item.equipment_type }}</p>
<p class="mobile-card__value">{{ row.item.asset_type_name || row.item.equipment_type }}</p>
</div>
<div>
<p class="mobile-card__label">盘点时间</p>
<p class="mobile-card__value">{{ item.checked_at ? formatDateTime(item.checked_at) : '-' }}</p>
<p class="mobile-card__value">{{ row.item.checked_at ? formatDateTime(row.item.checked_at) : '-' }}</p>
</div>
</div>
<div v-if="item.exception_type || item.exception_note" class="mt-4 rounded-xl border border-status-warning/30 bg-status-warning/10 px-3 py-3 text-sm text-text-secondary">
<p class="font-medium text-status-warning">{{ getExceptionText(item.exception_type) }}</p>
<p v-if="item.exception_note" class="mt-1 text-text-secondary">{{ item.exception_note }}</p>
<div v-if="row.item.exception_type || row.item.exception_note" class="mt-4 rounded-xl border border-status-warning/30 bg-status-warning/10 px-3 py-3 text-sm text-text-secondary">
<p class="font-medium text-status-warning">{{ getExceptionText(row.item.exception_type) }}</p>
<p v-if="row.item.exception_note" class="mt-1 text-text-secondary">{{ row.item.exception_note }}</p>
</div>
</article>
<p v-if="filteredItems.length === 0" class="py-8 text-center text-sm text-text-tertiary">
<p v-if="inventoryItemRows.length === 0" class="py-8 text-center text-sm text-text-tertiary">
当前筛选条件下没有资产。
</p>
</div>
@ -401,36 +372,56 @@
</tr>
</thead>
<tbody>
<tr v-for="item in filteredItems" :key="item.id">
<tr v-for="row in inventoryItemRows" :key="row.item.id">
<td>
<p class="text-text-primary">{{ item.equipment_name }}</p>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ item.equipment_code }}</p>
<p class="mt-1 text-xs text-text-secondary">{{ item.asset_type_name || item.equipment_type }}</p>
<div class="flex items-start gap-2" :style="{ paddingLeft: `${row.depth * 18}px` }">
<button
v-if="row.has_children"
type="button"
class="mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center rounded border border-border text-text-secondary transition hover:bg-bg-hover"
:aria-label="row.is_expanded ? '收起配件' : '展开配件'"
@click="toggleInventoryItemChildren(row.item.equipment_id)"
>
<ChevronRightIcon class="h-4 w-4 transition-transform" :class="{ 'rotate-90': row.is_expanded }" />
</button>
<span v-else-if="row.depth > 0" class="mt-1 shrink-0 text-xs text-text-tertiary">↳</span>
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<p class="text-text-primary">{{ row.item.equipment_name }}</p>
<span v-if="row.depth > 0" class="badge badge-neutral">配件</span>
<span v-else-if="row.has_children" class="badge badge-success">主资产</span>
<span v-else class="badge badge-neutral">独立</span>
<span v-if="row.has_children" class="badge badge-neutral">含 {{ row.descendant_count }} 个配件</span>
</div>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ row.item.equipment_code }}</p>
<p class="mt-1 text-xs text-text-secondary">{{ row.item.asset_type_name || row.item.equipment_type }}</p>
</div>
</div>
</td>
<td>
<p class="text-text-primary">
{{ item.snapshot_person_nickname || '待分配' }}
<span v-if="isInventoryPublicAssetPerson(item)" class="ml-2 text-xs text-status-warning">
{{ row.item.snapshot_person_nickname || '待分配' }}
<span v-if="isInventoryPublicAssetPerson(row.item)" class="ml-2 text-xs text-status-warning">
{{ getInventoryPublicAssetLabel() }}
</span>
</p>
<p class="mt-1 text-xs text-text-secondary">{{ item.snapshot_person_name || '-' }}</p>
<p class="mt-1 text-xs text-text-secondary">{{ row.item.snapshot_person_name || '-' }}</p>
</td>
<td class="text-sm text-text-primary">{{ item.snapshot_department_name || '-' }}</td>
<td class="text-sm text-text-primary">{{ row.item.snapshot_department_name || '-' }}</td>
<td>
<span class="badge" :class="getItemStatusClass(item.check_status)">
{{ getItemStatusText(item.check_status) }}
<span class="badge" :class="getItemStatusClass(row.item.check_status)">
{{ getItemStatusText(row.item.check_status) }}
</span>
</td>
<td class="text-sm text-text-secondary">
{{ getExceptionText(item.exception_type) }}
<span v-if="item.exception_note" class="mt-1 block text-xs text-text-tertiary">{{ item.exception_note }}</span>
{{ getExceptionText(row.item.exception_type) }}
<span v-if="row.item.exception_note" class="mt-1 block text-xs text-text-tertiary">{{ row.item.exception_note }}</span>
</td>
<td class="text-sm text-text-secondary">
{{ item.checked_at ? formatDateTime(item.checked_at) : '-' }}
{{ row.item.checked_at ? formatDateTime(row.item.checked_at) : '-' }}
</td>
</tr>
<tr v-if="filteredItems.length === 0">
<tr v-if="inventoryItemRows.length === 0">
<td colspan="6" class="py-8 text-center text-text-secondary">当前筛选条件下没有资产。</td>
</tr>
</tbody>
@ -540,6 +531,7 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
import { ChevronRightIcon } from '@heroicons/vue/24/outline'
import { useRoute, useRouter } from 'vue-router'
import { getPersonOptions, type PersonOption } from '@/api/person'
import {
@ -558,6 +550,7 @@ import type {
InventoryTaskParticipant
} from '@/types'
import { formatDateTime, formatNumber, formatPercent } from '@/utils/format'
import { buildInventoryItemTree } from './inventoryItemTree'
import { getInventoryPublicAssetLabel, isInventoryPublicAssetPerson } from './inventoryPublicAsset'
import { buildInventoryScopeLabel } from './inventoryScopeSummary'
@ -588,6 +581,7 @@ const adminScanning = ref(false)
const itemFilter = ref<ItemFilter>('all')
const participantFilter = ref('all')
const itemKeyword = ref('')
const expandedInventoryItemIds = ref<Set<number>>(new Set())
const adminScanCode = ref('')
let personOptionSearchTimer: number | null = null
let latestPersonOptionRequestId = 0
@ -600,13 +594,22 @@ const publicLink = computed(() =>
task.value ? `${window.location.origin}/inventory/task/${task.value.public_token}` : ''
)
const scopeLabel = computed(() => (task.value ? buildInventoryScopeLabel(task.value) : '-'))
const handledItemCount = computed(() =>
task.value ? task.value.completed_count + task.value.exception_count : 0
)
const taskProgressText = computed(() => {
if (!task.value || task.value.items_count === 0) return '暂无资产'
return `${formatPercent((task.value.completed_count / task.value.items_count) * 100)}`
return `${formatPercent((handledItemCount.value / task.value.items_count) * 100)}`
})
const participantNameSummary = computed(() => {
const names = (task.value?.participants || [])
.map((participant) => participant.person_nickname || participant.person_name)
.filter(Boolean)
if (names.length === 0) return '暂无参与人'
if (names.length <= 3) return names.join('、')
return `${names.slice(0, 3).join('、')} 等 ${names.length} 人`
})
const pendingParticipantCount = computed(
() => participantRows.value.filter((participant) => participant.status === 'pending').length
)
const participantRows = computed<ParticipantProgressRow[]>(() => {
const statsMap = new Map<number, { total: number; pending: number; checked: number; exception: number }>()
@ -641,23 +644,44 @@ const participantRows = computed<ParticipantProgressRow[]>(() => {
}))
})
const filteredItems = computed(() => {
const hasActiveInventoryItemFilters = computed(() =>
itemFilter.value !== 'all' || participantFilter.value !== 'all' || Boolean(itemKeyword.value.trim())
)
const matchesInventoryItemFilters = (item: InventoryTaskItem) => {
const keyword = itemKeyword.value.trim().toLowerCase()
return (task.value?.items || []).filter((item) => {
const matchesFilter = itemFilter.value === 'all' || item.check_status === itemFilter.value
const matchesParticipant =
participantFilter.value === 'all' || String(item.participant_id || '') === participantFilter.value
const matchesKeyword =
!keyword ||
item.equipment_code.toLowerCase().includes(keyword) ||
item.equipment_name.toLowerCase().includes(keyword) ||
(item.snapshot_person_name || '').toLowerCase().includes(keyword) ||
(item.snapshot_person_nickname || '').toLowerCase().includes(keyword)
const matchesFilter = itemFilter.value === 'all' || item.check_status === itemFilter.value
const matchesParticipant =
participantFilter.value === 'all' || String(item.participant_id || '') === participantFilter.value
const matchesKeyword =
!keyword ||
item.equipment_code.toLowerCase().includes(keyword) ||
item.equipment_name.toLowerCase().includes(keyword) ||
(item.snapshot_person_name || '').toLowerCase().includes(keyword) ||
(item.snapshot_person_nickname || '').toLowerCase().includes(keyword)
return matchesFilter && matchesParticipant && matchesKeyword
return matchesFilter && matchesParticipant && matchesKeyword
}
const inventoryItemTree = computed(() =>
buildInventoryItemTree(task.value?.items || [], {
matches: matchesInventoryItemFilters,
expandedEquipmentIds: expandedInventoryItemIds.value,
autoExpandMatchingBranches: hasActiveInventoryItemFilters.value
})
})
)
const inventoryItemRows = computed(() => inventoryItemTree.value.rows)
const toggleInventoryItemChildren = (equipmentId: number) => {
const next = new Set(expandedInventoryItemIds.value)
if (next.has(equipmentId)) {
next.delete(equipmentId)
} else {
next.add(equipmentId)
}
expandedInventoryItemIds.value = next
}
const getTaskStatusText = (status: string) => (status === 'closed' ? '已关闭' : '进行中')

View File

@ -227,7 +227,9 @@
<section class="card">
<div class="list-header">
<h3 class="section-title">我的盘点清单</h3>
<span class="badge badge-neutral">共 {{ formatNumber(filteredItems.length) }} 件</span>
<span class="badge badge-neutral">
主资产 {{ formatNumber(inventoryItemTree.root_count) }} · 配件 {{ formatNumber(inventoryItemTree.accessory_count) }}
</span>
</div>
<div class="filter-bar">
@ -249,66 +251,86 @@
<div class="mt-4 mobile-card-list md:hidden">
<article
v-for="item in filteredItems"
:key="item.id"
v-for="row in inventoryItemRows"
:key="row.item.id"
class="mobile-card"
>
<div class="mobile-card__row">
<div>
<p class="text-sm font-semibold text-text-primary">{{ item.equipment_name }}</p>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ item.equipment_code }}</p>
<div class="min-w-0" :style="{ paddingLeft: `${row.depth * 14}px` }">
<div class="flex items-start gap-2">
<button
v-if="row.has_children"
type="button"
class="mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border border-border text-text-secondary"
:aria-label="row.is_expanded ? '收起配件' : '展开配件'"
@click="toggleInventoryItemChildren(row.item.equipment_id)"
>
<ChevronRightIcon class="h-3.5 w-3.5 transition-transform" :class="{ 'rotate-90': row.is_expanded }" />
</button>
<span v-else-if="row.depth > 0" class="mt-0.5 shrink-0 text-xs text-text-tertiary">↳</span>
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<p class="text-sm font-semibold text-text-primary">{{ row.item.equipment_name }}</p>
<span v-if="row.depth > 0" class="badge badge-neutral">配件</span>
<span v-else-if="row.has_children" class="badge badge-success">主资产</span>
<span v-else class="badge badge-neutral">独立</span>
<span v-if="row.has_children" class="badge badge-neutral">含 {{ row.descendant_count }} 个配件</span>
</div>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ row.item.equipment_code }}</p>
</div>
</div>
</div>
<span class="badge" :class="getItemStatusClass(item.check_status)">
{{ getItemStatusText(item.check_status) }}
<span class="badge" :class="getItemStatusClass(row.item.check_status)">
{{ getItemStatusText(row.item.check_status) }}
</span>
</div>
<div class="mobile-card__meta mt-4">
<div>
<p class="mobile-card__label">资产类型</p>
<p class="mobile-card__value">{{ item.asset_type_name || item.equipment_type }}</p>
<p class="mobile-card__value">{{ row.item.asset_type_name || row.item.equipment_type }}</p>
</div>
<div>
<p class="mobile-card__label">归属部门</p>
<p class="mobile-card__value">{{ item.snapshot_department_name || '未归属' }}</p>
<p class="mobile-card__value">{{ row.item.snapshot_department_name || '未归属' }}</p>
</div>
<div>
<p class="mobile-card__label">盘点时间</p>
<p class="mobile-card__value">{{ item.checked_at ? formatDateTime(item.checked_at) : '-' }}</p>
<p class="mobile-card__value">{{ row.item.checked_at ? formatDateTime(row.item.checked_at) : '-' }}</p>
</div>
<div>
<p class="mobile-card__label">异常说明</p>
<p class="mobile-card__value">{{ getExceptionText(item.exception_type) }}</p>
<p class="mobile-card__value">{{ getExceptionText(row.item.exception_type) }}</p>
</div>
</div>
<p v-if="item.exception_note" class="mt-4 text-sm text-text-secondary">
备注:{{ item.exception_note }}
<p v-if="row.item.exception_note" class="mt-4 text-sm text-text-secondary">
备注:{{ row.item.exception_note }}
</p>
<div
v-if="!isTaskClosed && item.check_status === 'pending'"
v-if="!isTaskClosed && row.item.check_status === 'pending'"
class="mt-4 grid gap-2 sm:grid-cols-2"
>
<button
type="button"
class="btn-primary w-full text-sm"
:disabled="processingCode === item.equipment_code"
@click="markItemChecked(item)"
:disabled="processingCode === row.item.equipment_code"
@click="markItemChecked(row.item)"
>
{{ processingCode === item.equipment_code ? '提交中...' : '确认在我这里' }}
{{ processingCode === row.item.equipment_code ? '提交中...' : '确认在我这里' }}
</button>
<button
type="button"
class="btn-secondary w-full text-sm"
@click="openExceptionModal(item)"
@click="openExceptionModal(row.item)"
>
上报异常
</button>
</div>
</article>
<p v-if="filteredItems.length === 0" class="py-8 text-center text-sm text-text-tertiary">
<p v-if="inventoryItemRows.length === 0" class="py-8 text-center text-sm text-text-tertiary">
当前筛选条件下没有资产。
</p>
</div>
@ -327,40 +349,60 @@
</tr>
</thead>
<tbody>
<tr v-for="item in filteredItems" :key="item.id">
<tr v-for="row in inventoryItemRows" :key="row.item.id">
<td>
<p class="text-text-primary">{{ item.equipment_name }}</p>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ item.equipment_code }}</p>
<div class="flex items-start gap-2" :style="{ paddingLeft: `${row.depth * 18}px` }">
<button
v-if="row.has_children"
type="button"
class="mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center rounded border border-border text-text-secondary transition hover:bg-bg-hover"
:aria-label="row.is_expanded ? '收起配件' : '展开配件'"
@click="toggleInventoryItemChildren(row.item.equipment_id)"
>
<ChevronRightIcon class="h-4 w-4 transition-transform" :class="{ 'rotate-90': row.is_expanded }" />
</button>
<span v-else-if="row.depth > 0" class="mt-1 shrink-0 text-xs text-text-tertiary">↳</span>
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<p class="text-text-primary">{{ row.item.equipment_name }}</p>
<span v-if="row.depth > 0" class="badge badge-neutral">配件</span>
<span v-else-if="row.has_children" class="badge badge-success">主资产</span>
<span v-else class="badge badge-neutral">独立</span>
<span v-if="row.has_children" class="badge badge-neutral">含 {{ row.descendant_count }} 个配件</span>
</div>
<p class="mt-1 text-xs font-mono text-text-tertiary">{{ row.item.equipment_code }}</p>
</div>
</div>
</td>
<td class="text-sm text-text-primary">{{ item.asset_type_name || item.equipment_type }}</td>
<td class="text-sm text-text-primary">{{ row.item.asset_type_name || row.item.equipment_type }}</td>
<td>
<span class="badge" :class="getItemStatusClass(item.check_status)">
{{ getItemStatusText(item.check_status) }}
<span class="badge" :class="getItemStatusClass(row.item.check_status)">
{{ getItemStatusText(row.item.check_status) }}
</span>
</td>
<td class="text-sm text-text-secondary">
{{ getExceptionText(item.exception_type) }}
<span v-if="item.exception_note" class="mt-1 block text-xs text-text-tertiary">{{ item.exception_note }}</span>
{{ getExceptionText(row.item.exception_type) }}
<span v-if="row.item.exception_note" class="mt-1 block text-xs text-text-tertiary">{{ row.item.exception_note }}</span>
</td>
<td class="text-sm text-text-secondary">
{{ item.checked_at ? formatDateTime(item.checked_at) : '-' }}
{{ row.item.checked_at ? formatDateTime(row.item.checked_at) : '-' }}
</td>
<td>
<div v-if="!isTaskClosed && item.check_status === 'pending'" class="table-action-group">
<div v-if="!isTaskClosed && row.item.check_status === 'pending'" class="table-action-group">
<button
type="button"
class="table-action table-action--primary"
:disabled="processingCode === item.equipment_code"
@click="markItemChecked(item)"
:disabled="processingCode === row.item.equipment_code"
@click="markItemChecked(row.item)"
>
{{ processingCode === item.equipment_code ? '提交中...' : '确认在我这里' }}
{{ processingCode === row.item.equipment_code ? '提交中...' : '确认在我这里' }}
</button>
<button type="button" class="table-action" @click="openExceptionModal(item)">上报异常</button>
<button type="button" class="table-action" @click="openExceptionModal(row.item)">上报异常</button>
</div>
<span v-else class="text-xs text-text-tertiary">-</span>
</td>
</tr>
<tr v-if="filteredItems.length === 0">
<tr v-if="inventoryItemRows.length === 0">
<td colspan="6" class="py-8 text-center text-text-secondary">当前筛选条件下没有资产。</td>
</tr>
</tbody>
@ -435,6 +477,7 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
import { ChevronRightIcon } from '@heroicons/vue/24/outline'
import { useRoute } from 'vue-router'
import CameraScannerModal from '@/components/CameraScannerModal.vue'
import {
@ -457,6 +500,7 @@ import type {
PublicInventoryTaskInfo
} from '@/types'
import { formatDateTime, formatNumber, formatPercent, playBeepSound, playErrorSound } from '@/utils/format'
import { buildInventoryItemTree } from './inventoryItemTree'
import { buildInventoryScopeLabel } from './inventoryScopeSummary'
type ItemFilter = 'all' | 'pending' | 'checked' | 'exception'
@ -490,6 +534,7 @@ const showManualEntry = ref(window.matchMedia('(pointer: fine)').matches)
const processingCode = ref('')
const itemFilter = ref<ItemFilter>('all')
const itemKeyword = ref('')
const expandedInventoryItemIds = ref<Set<number>>(new Set())
const scanInputRef = ref<HTMLInputElement>()
const exceptionTarget = ref<InventoryTaskItem | null>(null)
const exceptionForm = reactive<{
@ -527,21 +572,43 @@ const myProgressPercent = computed(() => {
if (summary.value.total === 0) return 0
return ((summary.value.checked + summary.value.exception) / summary.value.total) * 100
})
const filteredItems = computed(() => {
const hasActiveInventoryItemFilters = computed(() =>
itemFilter.value !== 'all' || Boolean(itemKeyword.value.trim())
)
const matchesInventoryItemFilters = (item: InventoryTaskItem) => {
const keyword = itemKeyword.value.trim().toLowerCase()
return items.value.filter((item) => {
const matchesFilter = itemFilter.value === 'all' || item.check_status === itemFilter.value
const matchesKeyword =
!keyword ||
item.equipment_code.toLowerCase().includes(keyword) ||
item.equipment_name.toLowerCase().includes(keyword)
return matchesFilter && matchesKeyword
const matchesFilter = itemFilter.value === 'all' || item.check_status === itemFilter.value
const matchesKeyword =
!keyword ||
item.equipment_code.toLowerCase().includes(keyword) ||
item.equipment_name.toLowerCase().includes(keyword)
return matchesFilter && matchesKeyword
}
const inventoryItemTree = computed(() =>
buildInventoryItemTree(items.value, {
matches: matchesInventoryItemFilters,
expandedEquipmentIds: expandedInventoryItemIds.value,
autoExpandMatchingBranches: hasActiveInventoryItemFilters.value
})
})
)
const inventoryItemRows = computed(() => inventoryItemTree.value.rows)
const toggleInventoryItemChildren = (equipmentId: number) => {
const next = new Set(expandedInventoryItemIds.value)
if (next.has(equipmentId)) {
next.delete(equipmentId)
} else {
next.add(equipmentId)
}
expandedInventoryItemIds.value = next
}
const resetSessionState = () => {
session.value = null
items.value = []
expandedInventoryItemIds.value = new Set()
summary.value = {
total: 0,
pending: 0,

View File

@ -0,0 +1,126 @@
import type { InventoryTaskItem } from '@/types'
export interface InventoryItemTreeRow {
item: InventoryTaskItem
depth: number
descendant_count: number
has_children: boolean
is_expanded: boolean
}
interface BuildInventoryItemTreeOptions {
matches: (item: InventoryTaskItem) => boolean
expandedEquipmentIds: ReadonlySet<number>
autoExpandMatchingBranches?: boolean
}
const compareInventoryItems = (left: InventoryTaskItem, right: InventoryTaskItem) =>
left.equipment_code.localeCompare(right.equipment_code, 'zh-CN')
/**
* 盘点任务必须按创建时冻结的关系显示,不能因为后来挂载/卸载配件而改变历史任务的层级。
*/
export const buildInventoryItemTree = (
items: InventoryTaskItem[],
{
matches,
expandedEquipmentIds,
autoExpandMatchingBranches = false
}: BuildInventoryItemTreeOptions
) => {
const itemByEquipmentId = new Map(items.map((item) => [item.equipment_id, item]))
const childrenByParentId = new Map<number, InventoryTaskItem[]>()
const roots: InventoryTaskItem[] = []
for (const item of items) {
const parentEquipmentId = item.snapshot_parent_equipment_id
const hasTaskParent = Boolean(
parentEquipmentId &&
parentEquipmentId !== item.equipment_id &&
itemByEquipmentId.has(parentEquipmentId)
)
if (!hasTaskParent) {
roots.push(item)
continue
}
const siblings = childrenByParentId.get(parentEquipmentId as number) || []
siblings.push(item)
childrenByParentId.set(parentEquipmentId as number, siblings)
}
roots.sort(compareInventoryItems)
childrenByParentId.forEach((children) => children.sort(compareInventoryItems))
const descendantCountByEquipmentId = new Map<number, number>()
const matchingBranchByEquipmentId = new Map<number, boolean>()
const countDescendants = (item: InventoryTaskItem, ancestry = new Set<number>()): number => {
if (ancestry.has(item.equipment_id)) return 0
if (descendantCountByEquipmentId.has(item.equipment_id)) {
return descendantCountByEquipmentId.get(item.equipment_id) || 0
}
const nextAncestry = new Set(ancestry)
nextAncestry.add(item.equipment_id)
const total = (childrenByParentId.get(item.equipment_id) || []).reduce(
(count, child) => count + 1 + countDescendants(child, nextAncestry),
0
)
descendantCountByEquipmentId.set(item.equipment_id, total)
return total
}
const branchMatches = (item: InventoryTaskItem, ancestry = new Set<number>()): boolean => {
if (ancestry.has(item.equipment_id)) return matches(item)
if (matchingBranchByEquipmentId.has(item.equipment_id)) {
return matchingBranchByEquipmentId.get(item.equipment_id) || false
}
const nextAncestry = new Set(ancestry)
nextAncestry.add(item.equipment_id)
const result = matches(item) || (childrenByParentId.get(item.equipment_id) || []).some((child) =>
branchMatches(child, nextAncestry)
)
matchingBranchByEquipmentId.set(item.equipment_id, result)
return result
}
const rows: InventoryItemTreeRow[] = []
const appendRows = (item: InventoryTaskItem, depth: number, ancestry = new Set<number>()) => {
if (ancestry.has(item.equipment_id) || !branchMatches(item)) return
const children = childrenByParentId.get(item.equipment_id) || []
const hasChildren = children.length > 0
const isExpanded = hasChildren && (
expandedEquipmentIds.has(item.equipment_id) ||
(autoExpandMatchingBranches && children.some((child) => branchMatches(child)))
)
rows.push({
item,
depth,
descendant_count: countDescendants(item),
has_children: hasChildren,
is_expanded: isExpanded
})
if (!isExpanded) return
const nextAncestry = new Set(ancestry)
nextAncestry.add(item.equipment_id)
for (const child of children) {
appendRows(child, depth + 1, nextAncestry)
}
}
for (const root of roots) {
appendRows(root, 0)
}
return {
rows,
root_count: roots.length,
accessory_count: Math.max(0, items.length - roots.length)
}
}

View File

@ -37,7 +37,7 @@
</div>
<div>
<p class="text-xs text-text-tertiary">默认文件</p>
<p class="mt-1 font-mono text-xs text-text-secondary">logo.svg / favicon.svg</p>
<p class="mt-1 font-mono text-xs text-text-secondary">custom-logo.jpg / favicon.svg</p>
</div>
<div>
<p class="text-xs text-text-tertiary">自定义覆盖</p>