1286 lines
37 KiB
Vue
1286 lines
37 KiB
Vue
<!-- 库位管理(左树 + 右货架行布局) -->
|
||
<!-- eslint-disable vue/no-v-model-argument -->
|
||
<template>
|
||
<div class="page-container storage-page" ref="pageContainerRef" :style="pageContainerStyle" v-loading="clearSlotsLoading" element-loading-text="正在清空格子..." element-loading-background="rgba(255,255,255,0.7)">
|
||
<header class="page-head">
|
||
<h1>库位管理</h1>
|
||
<p class="page-desc">维护库位设施树结构(罐→层→盒),以及叶子节点的格子管理。</p>
|
||
</header>
|
||
|
||
<section class="panel">
|
||
<div class="flex gap-4">
|
||
<!-- 左侧:库位树 -->
|
||
<div class="w-[280px] shrink-0 border-r border-[var(--mint-border)] pr-4">
|
||
<div class="panel-top storage-aside__head">
|
||
<h2>库位树</h2>
|
||
<el-button type="primary" size="small" @click="handleAddRoot">
|
||
<el-icon><Plus /></el-icon>
|
||
新增
|
||
</el-button>
|
||
</div>
|
||
|
||
<el-input
|
||
v-model="treeKeyword"
|
||
placeholder="搜索节点"
|
||
clearable
|
||
class="storage-tree-search"
|
||
/>
|
||
|
||
<el-tree
|
||
ref="treeRef"
|
||
:data="treeData"
|
||
:props="{ label: 'nodeName', children: 'children' }"
|
||
node-key="id"
|
||
highlight-current
|
||
default-expand-all
|
||
:filter-node-method="filterNode"
|
||
@node-click="handleNodeClick"
|
||
>
|
||
<template #default="{ data }">
|
||
<div class="storage-tree-node">
|
||
<span v-if="data.isLeaf === 1 && slotStatsMap[data.id]" class="storage-tree-node__stats">
|
||
{{ slotStatsMap[data.id].occupied }}/{{ slotStatsMap[data.id].total }}
|
||
</span>
|
||
<span class="storage-tree-node__name">{{ data.nodeName }}</span>
|
||
<span v-if="data.isLeaf === 1" class="storage-tree-node__leaf">存</span>
|
||
<div class="storage-tree-node__actions">
|
||
<button title="新增子节点" @click.stop="handleAddChild(data)">+</button>
|
||
<button title="编辑" @click.stop="handleEditNode(data)">✎</button>
|
||
<button title="删除" @click.stop="handleDeleteNode(data)">×</button>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
</el-tree>
|
||
</div>
|
||
|
||
<!-- 右侧:格子内容 -->
|
||
<div class="flex-1 min-w-0">
|
||
<template v-if="!selectedNode">
|
||
<el-empty description="请在左侧选择一个叶子节点查看格子" />
|
||
</template>
|
||
|
||
<template v-else-if="selectedNode.isLeaf !== 1">
|
||
<div class="storage-content-header">
|
||
<div>
|
||
<h3>
|
||
{{
|
||
buildTreePathLabelFromFlat(selectedNode.id, flatList, { nameKey: 'nodeName' })
|
||
}}
|
||
</h3>
|
||
<p class="storage-content-subtitle">非叶子节点,请继续选择子节点</p>
|
||
</div>
|
||
</div>
|
||
<el-empty description="此节点下还有子节点,请展开选择叶子节点查看格子" />
|
||
</template>
|
||
|
||
<template v-else>
|
||
<div class="storage-content-header">
|
||
<div>
|
||
<h3>
|
||
{{
|
||
buildTreePathLabelFromFlat(selectedNode.id, flatList, { nameKey: 'nodeName' })
|
||
}}
|
||
</h3>
|
||
<div class="storage-content-stats">
|
||
<span>总数 <strong>{{ slotStats.total }}</strong></span>
|
||
<span class="stats-dot">·</span>
|
||
<span>空闲 <strong>{{ slotStats.empty }}</strong></span>
|
||
<span class="stats-dot">·</span>
|
||
<span>占用 <strong>{{ slotStats.occupied }}</strong></span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="storage-toolbar">
|
||
<el-button type="primary" @click="batchDialogVisible = true">
|
||
批量生成格子
|
||
</el-button>
|
||
<el-button
|
||
v-if="slotList.length > 0"
|
||
class="btn-ghost"
|
||
:loading="clearSlotsLoading"
|
||
@click="handleClearSlots"
|
||
>
|
||
清空格子
|
||
</el-button>
|
||
</div>
|
||
|
||
<div v-if="slotList.length === 0" class="storage-empty-cells">
|
||
<el-empty description="暂无格子" />
|
||
</div>
|
||
|
||
<div v-else class="shelf-scroll-wrapper" v-loading="slotLoading">
|
||
<div class="shelf-container">
|
||
<div v-for="(row, ri) in shelfRows" :key="ri" class="shelf-row-wrapper">
|
||
<div class="shelf-row">
|
||
<div class="shelf-label">{{ row.label }}</div>
|
||
<div class="shelf-cells">
|
||
<div
|
||
class="shelf-cell shelf-cell--add"
|
||
title="在此行前面增加格子"
|
||
@click="handleAddSlotInRow(ri)"
|
||
>
|
||
<span class="add-icon">+</span>
|
||
</div>
|
||
<div
|
||
v-for="cell in row.cells"
|
||
:key="cell.id"
|
||
class="shelf-cell"
|
||
:class="{ occupied: cell.slotStatus === '占用' }"
|
||
@click="handleEditSlot(cell)"
|
||
>
|
||
<button class="cell-delete-btn" @click.stop="handleDeleteSlot(cell)" title="删除">×</button>
|
||
<span class="cell-code">{{ cell.slotCode }}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="shelf-add-row" title="在下方新增一行" @click="handleOpenAddRowDialog(ri)">
|
||
<span class="add-row-icon">+ 新增行</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 节点新增/编辑弹窗 -->
|
||
<el-dialog
|
||
v-model="nodeDialogVisible"
|
||
:title="nodeDialogTitle"
|
||
width="560px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
@close="handleNodeDialogClose"
|
||
>
|
||
<el-form
|
||
ref="nodeFormRef"
|
||
:model="nodeFormData"
|
||
:rules="nodeFormRules"
|
||
label-position="top"
|
||
class="storage-dialog-form"
|
||
scroll-to-error
|
||
:scroll-into-view-options="{ behavior: 'smooth', block: 'center' }"
|
||
>
|
||
<div class="form-grid-2">
|
||
<el-form-item label="上级节点">
|
||
<el-select
|
||
v-model="nodeFormData.parentId"
|
||
placeholder="请选择上级节点(空为根节点)"
|
||
clearable
|
||
filterable
|
||
>
|
||
<el-option
|
||
v-for="item in flatList"
|
||
:key="item.id"
|
||
:label="buildTreePathLabelFromFlat(item.id, flatList, { nameKey: 'nodeName' })"
|
||
:value="item.id"
|
||
/>
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="节点名称" prop="nodeName">
|
||
<el-input v-model="nodeFormData.nodeName" placeholder="请输入节点名称" />
|
||
</el-form-item>
|
||
<el-form-item label="节点类型">
|
||
<el-select v-model="nodeFormData.nodeType" placeholder="请选择节点类型">
|
||
<el-option label="设施" value="facility" />
|
||
<el-option label="层" value="layer" />
|
||
<el-option label="盒" value="box" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="是否存储格">
|
||
<el-switch v-model="nodeFormData.isLeaf" :active-value="1" :inactive-value="0" />
|
||
</el-form-item>
|
||
<el-form-item label="备注" class="form-col-span-2">
|
||
<el-input v-model="nodeFormData.remark" placeholder="可选" />
|
||
</el-form-item>
|
||
</div>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="nodeDialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button
|
||
class="btn-main"
|
||
type="primary"
|
||
:loading="nodeSubmitLoading"
|
||
@click="handleNodeSubmit"
|
||
>
|
||
<el-icon><Check /></el-icon>
|
||
确定
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 格子编辑弹窗 -->
|
||
<el-dialog
|
||
v-model="slotDialogVisible"
|
||
:title="slotDialogTitle"
|
||
width="480px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
@close="handleSlotDialogClose"
|
||
>
|
||
<el-form
|
||
ref="slotFormRef"
|
||
:model="slotFormData"
|
||
:rules="slotFormRules"
|
||
label-position="top"
|
||
class="storage-dialog-form"
|
||
scroll-to-error
|
||
:scroll-into-view-options="{ behavior: 'smooth', block: 'center' }"
|
||
>
|
||
<div class="form-grid-2">
|
||
<el-form-item label="格子编码" prop="slotCode">
|
||
<el-input v-model="slotFormData.slotCode" placeholder="请输入格子编码" />
|
||
</el-form-item>
|
||
<el-form-item label="格子状态">
|
||
<el-select v-model="slotFormData.slotStatus" placeholder="请选择状态">
|
||
<el-option label="空" value="空" />
|
||
<el-option label="占用" value="占用" />
|
||
</el-select>
|
||
</el-form-item>
|
||
</div>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="slotDialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button
|
||
class="btn-main"
|
||
type="primary"
|
||
:loading="slotSubmitLoading"
|
||
@click="handleSlotSubmit"
|
||
>
|
||
<el-icon><Check /></el-icon>
|
||
确定
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 批量生成格子弹窗 -->
|
||
<el-dialog
|
||
v-model="batchDialogVisible"
|
||
title="批量生成格子"
|
||
width="480px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
@close="handleBatchDialogClose"
|
||
>
|
||
<el-form
|
||
ref="batchFormRef"
|
||
:model="batchForm"
|
||
label-position="top"
|
||
class="storage-dialog-form"
|
||
>
|
||
<div class="form-grid-2">
|
||
<el-form-item label="行数">
|
||
<el-input type="number" step="any" v-model="batchForm.rows" :min="1" :max="20" style="width: 100%" />
|
||
</el-form-item>
|
||
<el-form-item label="列数">
|
||
<el-input type="number" step="any" v-model="batchForm.cols" :min="1" :max="20" style="width: 100%" />
|
||
</el-form-item>
|
||
<el-form-item label="编码前缀" class="form-col-span-2">
|
||
<template #label>
|
||
编码前缀<span class="form-label-tip">可选,生成格式:前缀 + 行号-列号</span>
|
||
</template>
|
||
<el-input v-model="batchForm.codePrefix" placeholder="如 A、B-、01-" />
|
||
</el-form-item>
|
||
<div class="form-col-span-2 storage-batch-preview">
|
||
将生成 <strong>{{ batchForm.rows * batchForm.cols }}</strong> 个格子({{
|
||
batchForm.rows
|
||
}}
|
||
行 × {{ batchForm.cols }} 列)
|
||
</div>
|
||
</div>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="batchDialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button
|
||
class="btn-main"
|
||
type="primary"
|
||
:loading="batchSubmitLoading"
|
||
@click="handleBatchSubmit"
|
||
>
|
||
<el-icon><Check /></el-icon>
|
||
确认生成
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 新增行弹窗 -->
|
||
<el-dialog
|
||
v-model="addRowDialogVisible"
|
||
title="新增行"
|
||
width="400px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
@close="handleAddRowDialogClose"
|
||
>
|
||
<el-form :model="addRowForm" label-position="top" class="storage-dialog-form">
|
||
<el-form-item label="列数">
|
||
<el-input type="number" step="any" v-model="addRowForm.cols" :min="1" :max="20" style="width: 100%" />
|
||
</el-form-item>
|
||
<div class="storage-batch-preview">
|
||
将在当前行下方新增 <strong>{{ addRowForm.cols }}</strong> 个格子
|
||
</div>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="addRowDialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button
|
||
class="btn-main"
|
||
type="primary"
|
||
:loading="addSlotLoading"
|
||
@click="handleAddRowBelowConfirm"
|
||
>
|
||
<el-icon><Check /></el-icon>
|
||
确认
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</section>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import { Plus, Close, Check } from '@element-plus/icons-vue'
|
||
import {
|
||
fetchStorageFacilityList,
|
||
fetchCreateStorageFacility,
|
||
fetchUpdateStorageFacility,
|
||
fetchDeleteStorageFacility,
|
||
fetchStorageSlotList,
|
||
fetchBatchCreateSlots,
|
||
fetchCreateStorageSlot,
|
||
fetchUpdateStorageSlot,
|
||
fetchDeleteStorageSlot
|
||
} from '@/api/admin'
|
||
import { buildTreePathLabelFromFlat } from '@/utils/tree-path'
|
||
|
||
defineOptions({ name: 'StorageFacility' })
|
||
|
||
// Row labels use sequential numbers (1, 2, 3...)
|
||
|
||
// ==================== 树形数据 ====================
|
||
const loading = ref(false)
|
||
const flatList = ref<any[]>([])
|
||
const treeData = ref<any[]>([])
|
||
const treeRef = ref()
|
||
const treeKeyword = ref('')
|
||
|
||
const buildTree = (list: any[]) => {
|
||
const map = new Map<string, any>()
|
||
const roots: any[] = []
|
||
list.forEach((item) => {
|
||
map.set(item.id, { ...item, children: [] })
|
||
})
|
||
list.forEach((item) => {
|
||
const node = map.get(item.id)!
|
||
if (item.parentId && item.parentId !== '0' && map.has(item.parentId)) {
|
||
map.get(item.parentId)!.children.push(node)
|
||
} else {
|
||
roots.push(node)
|
||
}
|
||
})
|
||
return roots
|
||
}
|
||
|
||
const loadData = async () => {
|
||
loading.value = true
|
||
try {
|
||
const res: any = await fetchStorageFacilityList()
|
||
const list = res?.list || res || []
|
||
flatList.value = list
|
||
treeData.value = buildTree(list)
|
||
nextTick(loadAllSlotStats)
|
||
} catch (error) {
|
||
console.error('加载库位数据失败:', error)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const filterNode = (value: string, data: any) => {
|
||
if (!value) return true
|
||
return data.nodeName?.includes(value)
|
||
}
|
||
|
||
watch(treeKeyword, (val) => {
|
||
treeRef.value?.filter(val)
|
||
})
|
||
|
||
// ==================== 节点选中 → 格子面板 ====================
|
||
const selectedNode = ref<any>(null)
|
||
const slotLoading = ref(false)
|
||
const slotList = ref<any[]>([])
|
||
|
||
// 各叶子节点的格子统计 { id: { total, occupied, empty } }
|
||
const slotStatsMap = ref<Record<string, { total: number; occupied: number; empty: number }>>({})
|
||
|
||
/** 当前选中节点的格子统计 */
|
||
const slotStats = computed(() => {
|
||
const total = slotList.value.length
|
||
const occupied = slotList.value.filter((s) => s.slotStatus === '占用').length
|
||
return { total, occupied, empty: total - occupied }
|
||
})
|
||
|
||
/** slotList 变化时同步更新统计 Map */
|
||
watch(slotList, () => {
|
||
if (selectedNode.value?.id) {
|
||
const total = slotList.value.length
|
||
const occupied = slotList.value.filter((s) => s.slotStatus === '占用').length
|
||
slotStatsMap.value = {
|
||
...slotStatsMap.value,
|
||
[selectedNode.value.id]: { total, occupied, empty: total - occupied }
|
||
}
|
||
}
|
||
})
|
||
|
||
/** 加载所有叶子节点的格子统计 */
|
||
const loadAllSlotStats = async () => {
|
||
const leafNodes = flatList.value.filter((item) => item.isLeaf === 1)
|
||
if (leafNodes.length === 0) return
|
||
const results = await Promise.allSettled(
|
||
leafNodes.map(async (node) => {
|
||
const res: any = await fetchStorageSlotList({ facilityNodeId: node.id })
|
||
const list = res?.list || res || []
|
||
const total = list.length
|
||
const occupied = list.filter((s: any) => s.slotStatus === '占用').length
|
||
return { id: node.id, total, occupied, empty: total - occupied }
|
||
})
|
||
)
|
||
const map: Record<string, any> = { ...slotStatsMap.value }
|
||
results.forEach((r) => {
|
||
if (r.status === 'fulfilled') {
|
||
map[r.value.id] = r.value
|
||
}
|
||
})
|
||
slotStatsMap.value = map
|
||
}
|
||
|
||
const handleNodeClick = (data: any) => {
|
||
selectedNode.value = data
|
||
if (data.isLeaf === 1) {
|
||
loadSlots(data.id)
|
||
} else {
|
||
slotList.value = []
|
||
}
|
||
}
|
||
|
||
const loadSlots = async (facilityNodeId: string) => {
|
||
slotLoading.value = true
|
||
try {
|
||
const res: any = await fetchStorageSlotList({ facilityNodeId })
|
||
slotList.value = res?.list || res || []
|
||
} catch (error) {
|
||
console.error('加载格子数据失败:', error)
|
||
slotList.value = []
|
||
} finally {
|
||
slotLoading.value = false
|
||
}
|
||
}
|
||
|
||
// ==================== 货架行计算(按 slotCode 中的行号分组) ====================
|
||
const batchForm = reactive({
|
||
rows: 4,
|
||
cols: 6,
|
||
codePrefix: ''
|
||
})
|
||
|
||
/** 解析 slotCode "{prefix}{row}-{col}" 格式,提取行号和列号 */
|
||
const parseSlotCode = (
|
||
code: string
|
||
): { prefix: string; row: number; col: number } | null => {
|
||
const match = code.match(/^(.*?)(\d+)-(\d+)$/)
|
||
if (!match) return null
|
||
return { prefix: match[1], row: parseInt(match[2], 10), col: parseInt(match[3], 10) }
|
||
}
|
||
|
||
const shelfRows = computed(() => {
|
||
const rowMap = new Map<number, { prefix: string; cells: any[] }>()
|
||
const ungrouped: any[] = []
|
||
|
||
for (const slot of slotList.value) {
|
||
const parsed = parseSlotCode(slot.slotCode || '')
|
||
if (parsed) {
|
||
if (!rowMap.has(parsed.row)) {
|
||
rowMap.set(parsed.row, { prefix: parsed.prefix, cells: [] })
|
||
}
|
||
rowMap.get(parsed.row)!.cells.push(slot)
|
||
} else {
|
||
ungrouped.push(slot)
|
||
}
|
||
}
|
||
|
||
// 按行号排序,每个行内按列号排序
|
||
const result: { label: string; cells: any[]; rowNum: number; prefix: string }[] = []
|
||
const sortedEntries = [...rowMap.entries()].sort(([a], [b]) => a - b)
|
||
|
||
for (const [rowNum, data] of sortedEntries) {
|
||
data.cells.sort((a, b) => {
|
||
const pa = parseSlotCode(a.slotCode || '')
|
||
const pb = parseSlotCode(b.slotCode || '')
|
||
return (pa?.col ?? 0) - (pb?.col ?? 0)
|
||
})
|
||
result.push({
|
||
label: String(result.length + 1),
|
||
cells: data.cells,
|
||
rowNum,
|
||
prefix: data.prefix
|
||
})
|
||
}
|
||
|
||
// 无法解析编码的格子归入末尾
|
||
if (ungrouped.length > 0) {
|
||
result.push({
|
||
label: String(result.length + 1),
|
||
cells: ungrouped,
|
||
rowNum: result.length + 1,
|
||
prefix: ''
|
||
})
|
||
}
|
||
|
||
return result
|
||
})
|
||
|
||
// ==================== 节点弹窗 ====================
|
||
const nodeDialogVisible = ref(false)
|
||
const nodeDialogTitle = ref('新增根节点')
|
||
const isNodeEdit = ref(false)
|
||
const nodeSubmitLoading = ref(false)
|
||
const nodeFormRef = ref()
|
||
|
||
const nodeFormData = reactive({
|
||
id: '',
|
||
parentId: '',
|
||
nodeName: '',
|
||
nodeType: 'facility',
|
||
isLeaf: 0 as 0 | 1,
|
||
remark: ''
|
||
})
|
||
|
||
const nodeFormRules = {
|
||
nodeName: [{ required: true, message: '请输入节点名称', trigger: 'blur' }]
|
||
}
|
||
|
||
const handleAddRoot = () => {
|
||
isNodeEdit.value = false
|
||
nodeDialogTitle.value = '新增根节点'
|
||
Object.assign(nodeFormData, {
|
||
id: '',
|
||
parentId: '',
|
||
nodeName: '',
|
||
nodeType: 'facility',
|
||
isLeaf: 0,
|
||
remark: ''
|
||
})
|
||
nodeDialogVisible.value = true
|
||
nextTick(() => nodeFormRef.value?.clearValidate?.())
|
||
}
|
||
|
||
const handleAddChild = (data: any) => {
|
||
isNodeEdit.value = false
|
||
nodeDialogTitle.value = '新增子节点'
|
||
Object.assign(nodeFormData, {
|
||
id: '',
|
||
parentId: data.id,
|
||
nodeName: '',
|
||
nodeType: 'layer',
|
||
isLeaf: 0,
|
||
remark: ''
|
||
})
|
||
nodeDialogVisible.value = true
|
||
nextTick(() => nodeFormRef.value?.clearValidate?.())
|
||
}
|
||
|
||
const handleEditNode = (data: any) => {
|
||
isNodeEdit.value = true
|
||
nodeDialogTitle.value = '编辑节点'
|
||
Object.assign(nodeFormData, {
|
||
id: data.id,
|
||
parentId: data.parentId || '',
|
||
nodeName: data.nodeName || '',
|
||
nodeType: data.nodeType || 'facility',
|
||
isLeaf: data.isLeaf ?? 0,
|
||
remark: data.remark || ''
|
||
})
|
||
nodeDialogVisible.value = true
|
||
nextTick(() => nodeFormRef.value?.clearValidate?.())
|
||
}
|
||
|
||
const handleNodeSubmit = async () => {
|
||
await nodeFormRef.value?.validate?.()
|
||
nodeSubmitLoading.value = true
|
||
try {
|
||
const payload = {
|
||
parentId: nodeFormData.parentId || undefined,
|
||
nodeName: nodeFormData.nodeName.trim(),
|
||
nodeType: nodeFormData.nodeType,
|
||
isLeaf: nodeFormData.isLeaf,
|
||
remark: nodeFormData.remark?.trim() || undefined
|
||
}
|
||
if (isNodeEdit.value && nodeFormData.id) {
|
||
await fetchUpdateStorageFacility(nodeFormData.id, payload)
|
||
} else {
|
||
await fetchCreateStorageFacility(payload)
|
||
}
|
||
ElMessage.success(isNodeEdit.value ? '更新成功' : '创建成功')
|
||
nodeDialogVisible.value = false
|
||
await loadData()
|
||
} catch {
|
||
/* 校验或接口错误已由全局处理 */
|
||
} finally {
|
||
nodeSubmitLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleNodeDialogClose = () => {
|
||
Object.assign(nodeFormData, {
|
||
id: '',
|
||
parentId: '',
|
||
nodeName: '',
|
||
nodeType: 'facility',
|
||
isLeaf: 0,
|
||
remark: ''
|
||
})
|
||
nodeFormRef.value?.clearValidate?.()
|
||
}
|
||
|
||
const handleDeleteNode = async (data: any) => {
|
||
try {
|
||
const hasChildren = data.children?.length > 0
|
||
const msg = hasChildren
|
||
? '该节点下有子节点,删除将一并删除。确定继续吗?'
|
||
: '确定要删除该节点吗?'
|
||
await ElMessageBox.confirm(msg, '提示', { type: 'warning' })
|
||
await fetchDeleteStorageFacility(data.id)
|
||
ElMessage.success('删除成功')
|
||
if (selectedNode.value?.id === data.id) {
|
||
selectedNode.value = null
|
||
slotList.value = []
|
||
}
|
||
await loadData()
|
||
} catch (error: any) {
|
||
if (error !== 'cancel') console.error('删除失败:', error)
|
||
}
|
||
}
|
||
|
||
// ==================== 格子弹窗 ====================
|
||
const slotDialogVisible = ref(false)
|
||
const slotDialogTitle = ref('编辑格子')
|
||
const isSlotEdit = ref(false)
|
||
const slotSubmitLoading = ref(false)
|
||
const slotFormRef = ref()
|
||
const editingSlotId = ref<string | null>(null)
|
||
|
||
const slotFormData = reactive({
|
||
slotCode: '',
|
||
slotStatus: '空',
|
||
remark: ''
|
||
})
|
||
|
||
const slotFormRules = {
|
||
slotCode: [{ required: true, message: '请输入格子编码', trigger: 'blur' }]
|
||
}
|
||
|
||
const handleEditSlot = (row: any) => {
|
||
isSlotEdit.value = true
|
||
slotDialogTitle.value = '编辑格子'
|
||
editingSlotId.value = row.id
|
||
Object.assign(slotFormData, {
|
||
slotCode: row.slotCode || '',
|
||
slotStatus: row.slotStatus || '空',
|
||
remark: row.remark || ''
|
||
})
|
||
slotDialogVisible.value = true
|
||
nextTick(() => slotFormRef.value?.clearValidate?.())
|
||
}
|
||
|
||
const handleSlotSubmit = async () => {
|
||
await slotFormRef.value?.validate?.()
|
||
slotSubmitLoading.value = true
|
||
try {
|
||
const payload = {
|
||
facilityNodeId: selectedNode.value?.id,
|
||
slotCode: slotFormData.slotCode.trim(),
|
||
slotStatus: slotFormData.slotStatus,
|
||
remark: slotFormData.remark?.trim() || undefined
|
||
}
|
||
if (isSlotEdit.value && editingSlotId.value) {
|
||
await fetchUpdateStorageSlot(editingSlotId.value, payload)
|
||
}
|
||
ElMessage.success('更新成功')
|
||
slotDialogVisible.value = false
|
||
if (selectedNode.value?.id) await loadSlots(selectedNode.value.id)
|
||
} catch {
|
||
/* 校验或接口错误已由全局处理 */
|
||
} finally {
|
||
slotSubmitLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleSlotDialogClose = () => {
|
||
Object.assign(slotFormData, { slotCode: '', slotStatus: '空', remark: '' })
|
||
editingSlotId.value = null
|
||
slotFormRef.value?.clearValidate?.()
|
||
}
|
||
|
||
const handleDeleteSlot = async (row: any) => {
|
||
try {
|
||
await ElMessageBox.confirm('确定要删除该格子吗?', '提示', { type: 'warning' })
|
||
await fetchDeleteStorageSlot(row.id)
|
||
ElMessage.success('删除成功')
|
||
if (selectedNode.value?.id) await loadSlots(selectedNode.value.id)
|
||
} catch (error: any) {
|
||
if (error !== 'cancel') console.error('删除失败:', error)
|
||
}
|
||
}
|
||
|
||
// ==================== 批量生成 ====================
|
||
const batchDialogVisible = ref(false)
|
||
const batchSubmitLoading = ref(false)
|
||
const batchFormRef = ref()
|
||
|
||
const handleBatchSubmit = async () => {
|
||
if (!selectedNode.value?.id) return
|
||
batchSubmitLoading.value = true
|
||
try {
|
||
await fetchBatchCreateSlots({
|
||
facilityNodeId: selectedNode.value.id,
|
||
rows: batchForm.rows,
|
||
cols: batchForm.cols,
|
||
codePrefix: batchForm.codePrefix || undefined
|
||
})
|
||
ElMessage.success('批量生成成功')
|
||
batchDialogVisible.value = false
|
||
await loadSlots(selectedNode.value.id)
|
||
} catch {
|
||
/* 校验或接口错误已由全局处理 */
|
||
} finally {
|
||
batchSubmitLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleBatchDialogClose = () => {
|
||
batchForm.rows = 4
|
||
batchForm.cols = 6
|
||
batchForm.codePrefix = ''
|
||
}
|
||
|
||
// ==================== 清空格子 ====================
|
||
const clearSlotsLoading = ref(false)
|
||
|
||
const handleClearSlots = async () => {
|
||
if (slotList.value.length === 0) return
|
||
try {
|
||
await ElMessageBox.confirm(
|
||
`确定清空当前节点下所有 ${slotList.value.length} 个格子吗?此操作不可恢复。`,
|
||
'提示',
|
||
{ type: 'warning' }
|
||
)
|
||
clearSlotsLoading.value = true
|
||
for (const slot of slotList.value) {
|
||
await fetchDeleteStorageSlot(slot.id)
|
||
}
|
||
ElMessage.success('已清空所有格子')
|
||
slotList.value = []
|
||
} catch (error: any) {
|
||
if (error !== 'cancel') console.error('清空失败:', error)
|
||
} finally {
|
||
clearSlotsLoading.value = false
|
||
}
|
||
}
|
||
|
||
// ==================== 单独新增行/格子 ====================
|
||
const addSlotLoading = ref(false)
|
||
|
||
/** 在指定行前面追加一个格子 */
|
||
const handleAddSlotInRow = async (rowIndex: number) => {
|
||
if (!selectedNode.value?.id || addSlotLoading.value) return
|
||
const row = shelfRows.value[rowIndex]
|
||
if (!row) return
|
||
|
||
// 计算该行下一个列号
|
||
let nextCol = 1
|
||
for (const cell of row.cells) {
|
||
const parsed = parseSlotCode(cell.slotCode || '')
|
||
if (parsed && parsed.col >= nextCol) nextCol = parsed.col + 1
|
||
}
|
||
|
||
const slotCode = `${row.prefix}${row.rowNum}-${nextCol}`
|
||
addSlotLoading.value = true
|
||
try {
|
||
await fetchCreateStorageSlot({
|
||
facilityNodeId: selectedNode.value.id,
|
||
slotCode,
|
||
slotStatus: '空'
|
||
})
|
||
ElMessage.success('新增格子成功')
|
||
await loadSlots(selectedNode.value.id)
|
||
} catch {
|
||
/* 校验或接口错误已由全局处理 */
|
||
} finally {
|
||
addSlotLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 在指定行下方新增一行(弹窗确认,含行号重排插入) */
|
||
const addRowDialogVisible = ref(false)
|
||
const addRowForm = reactive({ cols: 4 })
|
||
const addRowTargetIndex = ref(-1)
|
||
|
||
const handleOpenAddRowDialog = (rowIndex: number) => {
|
||
addRowTargetIndex.value = rowIndex
|
||
addRowForm.cols = 4
|
||
addRowDialogVisible.value = true
|
||
}
|
||
|
||
const handleAddRowDialogClose = () => {
|
||
addRowForm.cols = 4
|
||
addRowTargetIndex.value = -1
|
||
}
|
||
|
||
const handleAddRowBelowConfirm = async () => {
|
||
if (!selectedNode.value?.id || addSlotLoading.value) return
|
||
const rowIndex = addRowTargetIndex.value
|
||
if (rowIndex < 0 || rowIndex >= shelfRows.value.length) return
|
||
|
||
const currentRow = shelfRows.value[rowIndex]
|
||
const insertRowNum = currentRow.rowNum + 1
|
||
const prefix = currentRow.prefix
|
||
const numCols = addRowForm.cols
|
||
|
||
addSlotLoading.value = true
|
||
try {
|
||
// 1. 将当前行之后的所有行号 +1(从大到小排,避免行号冲突)
|
||
const rowsToShift = shelfRows.value
|
||
.filter((r) => r.rowNum > currentRow.rowNum)
|
||
.sort((a, b) => b.rowNum - a.rowNum)
|
||
|
||
for (const row of rowsToShift) {
|
||
for (const cell of row.cells) {
|
||
const parsed = parseSlotCode(cell.slotCode || '')
|
||
if (parsed) {
|
||
const newCode = `${parsed.prefix}${parsed.row + 1}-${parsed.col}`
|
||
await fetchUpdateStorageSlot(cell.id, { slotCode: newCode })
|
||
}
|
||
}
|
||
}
|
||
|
||
// 2. 在插入位置创建新行的格子
|
||
for (let c = 1; c <= numCols; c++) {
|
||
await fetchCreateStorageSlot({
|
||
facilityNodeId: selectedNode.value.id,
|
||
slotCode: `${prefix}${insertRowNum}-${c}`,
|
||
slotStatus: '空'
|
||
})
|
||
}
|
||
|
||
ElMessage.success('新增行成功')
|
||
addRowDialogVisible.value = false
|
||
await loadSlots(selectedNode.value.id)
|
||
} catch {
|
||
/* 校验或接口错误已由全局处理 */
|
||
} finally {
|
||
addSlotLoading.value = false
|
||
}
|
||
}
|
||
|
||
// ==================== 容器高度自适应 ====================
|
||
const pageContainerRef = ref<HTMLElement>()
|
||
const pageContainerStyle = ref<Record<string, string>>({})
|
||
|
||
const updateContainerHeight = () => {
|
||
const el = pageContainerRef.value
|
||
if (!el) return
|
||
const top = Math.ceil(el.getBoundingClientRect().top)
|
||
// 减去父容器 .layout-content 的 bottom padding
|
||
const parentPaddingBottom = parseInt(getComputedStyle(el.parentElement!).paddingBottom) || 0
|
||
pageContainerStyle.value = {
|
||
height: `calc(100vh - ${top + parentPaddingBottom}px - 15px)`,
|
||
overflow: 'hidden'
|
||
}
|
||
}
|
||
|
||
// ==================== 初始化 ====================
|
||
onMounted(() => {
|
||
loadData()
|
||
nextTick(updateContainerHeight)
|
||
window.addEventListener('resize', updateContainerHeight)
|
||
})
|
||
|
||
onBeforeUnmount(() => {
|
||
window.removeEventListener('resize', updateContainerHeight)
|
||
})
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
/* Full-height layout: panel stops 15px from viewport bottom */
|
||
.storage-page {
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: unset !important;
|
||
}
|
||
|
||
.page-head {
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.panel {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow: hidden;
|
||
display: flex;
|
||
flex-direction: column;
|
||
padding: 0;
|
||
|
||
/* The flex row (left tree + right content) */
|
||
> .flex {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow: hidden;
|
||
}
|
||
}
|
||
|
||
/* Left tree area — scroll independently */
|
||
.panel > .flex > :first-child {
|
||
display: flex;
|
||
flex-direction: column;
|
||
height: 100%;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.panel > .flex > :first-child :deep(.el-tree) {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow-y: auto;
|
||
scrollbar-width: none;
|
||
|
||
&::-webkit-scrollbar {
|
||
display: none;
|
||
}
|
||
}
|
||
|
||
/* Right content area — scroll independently */
|
||
.panel > .flex > :last-child {
|
||
overflow-y: auto;
|
||
padding: 0 4px;
|
||
scrollbar-width: none;
|
||
|
||
&::-webkit-scrollbar {
|
||
display: none;
|
||
}
|
||
}
|
||
|
||
.storage-aside__head {
|
||
flex-direction: column;
|
||
align-items: stretch;
|
||
justify-content: flex-start;
|
||
flex-wrap: nowrap;
|
||
gap: 8px;
|
||
}
|
||
|
||
.storage-tree-search {
|
||
margin-bottom: 12px;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.storage-tree-node {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
flex: 1;
|
||
font-size: 13px;
|
||
overflow: hidden;
|
||
|
||
&__stats {
|
||
font-size: 10px;
|
||
padding: 1px 5px;
|
||
border-radius: 3px;
|
||
background: #d1fae5;
|
||
color: #059669;
|
||
flex-shrink: 0;
|
||
font-weight: 600;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__name {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__leaf {
|
||
font-size: 11px;
|
||
padding: 1px 6px;
|
||
border-radius: 3px;
|
||
background: #ecfdf5;
|
||
color: #059669;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
&__actions {
|
||
display: flex;
|
||
gap: 2px;
|
||
margin-left: auto;
|
||
opacity: 0;
|
||
transition: opacity 0.15s;
|
||
flex-shrink: 0;
|
||
|
||
button {
|
||
background: none;
|
||
border: none;
|
||
cursor: pointer;
|
||
padding: 2px 4px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 1;
|
||
|
||
&:hover {
|
||
color: #10b981;
|
||
}
|
||
}
|
||
}
|
||
|
||
&:hover .storage-tree-node__actions {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
:deep(.el-tree-node__content) {
|
||
&:hover .storage-tree-node__actions {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
.storage-content-header {
|
||
margin-top: 10px;
|
||
margin-bottom: 16px;
|
||
padding-bottom: 12px;
|
||
border-bottom: 1px solid #f0f0f0;
|
||
}
|
||
|
||
.storage-content-stats {
|
||
font-size: 12px;
|
||
color: #909399;
|
||
margin-top: 4px;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
|
||
strong {
|
||
color: #303133;
|
||
}
|
||
|
||
.stats-dot {
|
||
color: #dcdfe6;
|
||
}
|
||
}
|
||
|
||
.storage-toolbar {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-bottom: 16px;
|
||
}
|
||
|
||
.storage-empty-cells {
|
||
padding: 20px 0;
|
||
}
|
||
|
||
.storage-dialog-form {
|
||
:deep(.el-form-item) {
|
||
margin-bottom: 0;
|
||
}
|
||
}
|
||
|
||
.storage-batch-preview {
|
||
background: #ecfdf5;
|
||
border-radius: 6px;
|
||
padding: 10px 14px;
|
||
font-size: 13px;
|
||
color: #059669;
|
||
margin-top: 4px;
|
||
}
|
||
|
||
/* Shelf layout — scroll wrapper for overflow */
|
||
.shelf-scroll-wrapper {
|
||
overflow: auto;
|
||
max-height: 100%;
|
||
}
|
||
|
||
.shelf-container {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 0;
|
||
}
|
||
|
||
.shelf-row-wrapper {
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
|
||
.shelf-row {
|
||
display: flex;
|
||
align-items: stretch;
|
||
}
|
||
|
||
.shelf-add-row {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-start;
|
||
padding: 4px 0 4px 32px;
|
||
cursor: pointer;
|
||
opacity: 0;
|
||
transition: opacity 0.15s;
|
||
|
||
.shelf-row-wrapper:hover & {
|
||
opacity: 1;
|
||
}
|
||
|
||
.add-row-icon {
|
||
font-size: 12px;
|
||
color: #6ee7b7;
|
||
padding: 2px 12px;
|
||
border: 1px dashed #a7f3d0;
|
||
border-radius: 4px;
|
||
transition: all 0.15s;
|
||
}
|
||
|
||
&:hover .add-row-icon {
|
||
color: #10b981;
|
||
border-color: #10b981;
|
||
background: #ecfdf5;
|
||
}
|
||
}
|
||
|
||
.shelf-label {
|
||
width: 28px;
|
||
flex-shrink: 0;
|
||
background: #059669;
|
||
color: #fff;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
font-size: 12px;
|
||
font-weight: 600;
|
||
border-radius: 4px 0 0 4px;
|
||
}
|
||
|
||
.shelf-cells {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 4px;
|
||
}
|
||
|
||
.shelf-cell {
|
||
width: 40px;
|
||
height: 40px;
|
||
border: 1px solid #d1fae5;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
cursor: pointer;
|
||
transition: all 0.15s;
|
||
position: relative;
|
||
border-radius: 4px;
|
||
flex-shrink: 0;
|
||
|
||
&:hover {
|
||
background: #ecfdf5;
|
||
}
|
||
|
||
/* 占用状态 — 深绿背景 */
|
||
&.occupied {
|
||
background: #059669;
|
||
border-color: #047857;
|
||
|
||
&:hover {
|
||
background: #047857;
|
||
}
|
||
|
||
.cell-code {
|
||
color: #fff;
|
||
}
|
||
}
|
||
|
||
/* 新增格子按钮 */
|
||
&--add {
|
||
border: 1px dashed #a7f3d0;
|
||
background: transparent;
|
||
|
||
&:hover {
|
||
background: #ecfdf5;
|
||
border-color: #10b981;
|
||
}
|
||
|
||
.add-icon {
|
||
font-size: 20px;
|
||
color: #6ee7b7;
|
||
line-height: 1;
|
||
}
|
||
|
||
&:hover .add-icon {
|
||
color: #10b981;
|
||
}
|
||
}
|
||
}
|
||
|
||
.cell-delete-btn {
|
||
position: absolute;
|
||
top: 0;
|
||
right: 0;
|
||
width: 14px;
|
||
height: 14px;
|
||
border: none;
|
||
border-radius: 0 4px 0 4px;
|
||
background: rgba(245, 108, 108, 0.85);
|
||
color: #fff;
|
||
font-size: 10px;
|
||
line-height: 14px;
|
||
text-align: center;
|
||
cursor: pointer;
|
||
padding: 0;
|
||
opacity: 0;
|
||
transition: opacity 0.15s;
|
||
z-index: 1;
|
||
|
||
&:hover {
|
||
background: #f56c6c;
|
||
}
|
||
|
||
.shelf-cell:hover & {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
.cell-code {
|
||
font-size: 10px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
text-align: center;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
max-width: 34px;
|
||
}
|
||
</style>
|