WEB/src/views/storage/stockcheck/pages/CheckStockPickerDialog.vue
2026-09-29 18:15:06 +08:00

384 lines
11 KiB
Vue
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.

<!--
CheckStockPickerDialog — 添加盘点商品(stock.check.addDetail)
数据来源:storage.inventory.list(库存明细,汇总模式 group_by_batch=false)
行上带 stock_id(= wms_stock_warehouse.id),即 addDetail 需要的 stock_ids。
同一商品在不同库位会有多行,提交前按 stock_id 去重(后端对已存在明细幂等跳过)。
仅 created(新建)/ rejected(驳回)状态可添加,后端会校验。
-->
<template>
<el-dialog
:model-value="visible"
:title="dialogTitle"
width="min(1080px, 95vw)"
align-center
:close-on-click-modal="false"
@update:model-value="handleVisibleChange"
@closed="handleClosed"
>
<div class="picker-body">
<!-- 筛选行 -->
<div class="picker-filters">
<el-input
v-model="filters.keyword"
size="small"
clearable
placeholder="商品编码 / 名称 / 条码"
class="filter-item filter-item--keyword"
@keyup.enter="handleSearch"
/>
<el-input
v-model="filters.batch_no"
size="small"
clearable
placeholder="批次号"
class="filter-item"
@keyup.enter="handleSearch"
/>
<el-button size="small" type="primary" :loading="loading" @click="handleSearch">
查询
</el-button>
<el-button size="small" @click="handleResetFilters">重置</el-button>
</div>
<!-- 库存列表 -->
<div ref="tableHostRef" class="table-host">
<el-table
ref="tableRef"
v-loading="loading"
:data="rows"
:row-key="(row: any) => row._key"
border
stripe
size="small"
:height="tableHeight"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="45" :reserve-selection="true" />
<el-table-column prop="item_code" label="商品编码" min-width="130" show-overflow-tooltip />
<el-table-column prop="item_name" label="商品名称" min-width="160" show-overflow-tooltip />
<el-table-column prop="spec" label="规格" min-width="110" show-overflow-tooltip />
<el-table-column prop="store_name" label="货主" min-width="110" show-overflow-tooltip />
<el-table-column prop="space_code" label="库位" min-width="120" show-overflow-tooltip />
<el-table-column prop="batch_no" label="批次号" min-width="110" show-overflow-tooltip />
<el-table-column prop="quantity" label="库存数量" width="100" align="center" />
<el-table-column prop="available_quantity" label="可用数量" width="100" align="center" />
<template #empty>
<div class="empty-tip">暂无库存数据</div>
</template>
</el-table>
</div>
<!-- 分页 -->
<div class="pagination-container">
<el-pagination
v-model:current-page="pagination.page"
v-model:page-size="pagination.pageSize"
:page-sizes="[10, 20, 50, 100]"
:total="pagination.total"
layout="total, sizes, prev, pager, next"
background
size="small"
@current-change="loadRows"
@size-change="handleSizeChange"
/>
</div>
</div>
<template #footer>
<div class="dialog-footer">
<span class="selected-tip">
已选 {{ selectedRows.length }} 行 · 去重后 {{ selectedStockIds.length }} 个商品
</span>
<div class="footer-buttons">
<el-button size="small" @click="handleVisibleChange(false)">取消</el-button>
<el-button
size="small"
type="primary"
:loading="submitting"
:disabled="!selectedStockIds.length"
@click="handleSubmit"
>
确认添加
</el-button>
</div>
</div>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import { computed, nextTick, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { useResizeObserver } from '@vueuse/core'
import { getInventoryList } from '@/api/storage.inventory/inventory'
import { stockCheckAddDetail } from '@/api/stock.check/stock.check'
import { isCheckApiSuccess } from '../config/template-config'
defineOptions({ name: 'CheckStockPickerDialog' })
const props = defineProps<{
visible: boolean
/** 目标盘点单ID */
taskId?: number
/** 目标盘点单号(展示用) */
taskTitle?: string
}>()
const emit = defineEmits<{
(e: 'update:visible', value: boolean): void
/** 添加成功 */
(e: 'success', result: { added: number; skipped: number }): void
}>()
const dialogTitle = computed(() =>
props.taskTitle ? `添加盘点商品 — ${props.taskTitle}` : '添加盘点商品'
)
const filters = reactive({ keyword: '', batch_no: '' })
const rows = ref<Record<string, any>[]>([])
const loading = ref(false)
const submitting = ref(false)
const selectedRows = ref<Record<string, any>[]>([])
const pagination = reactive({ page: 1, pageSize: 20, total: 0 })
const tableRef = ref<any>(null)
const tableHostRef = ref<HTMLElement | null>(null)
const tableHeight = ref(360)
useResizeObserver(tableHostRef, (entries) => {
const h = entries[0]?.contentRect.height ?? 0
if (h >= 120) tableHeight.value = Math.floor(h)
})
/** 提交前按 stock_id 去重 */
const selectedStockIds = computed(() => {
const ids = selectedRows.value
.map((row) => Number(row.stock_id))
.filter((id) => Number.isFinite(id) && id > 0)
return [...new Set(ids)]
})
function handleVisibleChange(value: boolean) {
emit('update:visible', value)
}
function handleClosed() {
tableRef.value?.clearSelection?.()
selectedRows.value = []
rows.value = []
submitting.value = false
}
/** 对象/数组属性统一转文本 */
function toText(value: any): string {
if (value === undefined || value === null || value === '') return ''
if (typeof value === 'string') return value
if (Array.isArray(value)) return value.map((v) => toText(v)).filter(Boolean).join('/')
if (typeof value === 'object') {
const picked = value.name ?? value.value ?? value.label ?? value.sku_property
return picked ? toText(picked) : ''
}
return String(value)
}
function mapRow(item: any, index: number): Record<string, any> {
const stockId = Number(item?.stock_id)
const spaceId = item?.space_id ?? item?.space?.id ?? ''
const batchNo = toText(item?.batch_no)
return {
_key: `${stockId}-${spaceId}-${batchNo}-${index}`,
stock_id: stockId,
item_code: toText(item?.stock?.item_code),
item_name: toText(item?.stock?.item_name),
spec: toText(item?.stock?.sku_property),
store_name: toText(item?.store_name ?? item?.stock?.store_name),
space_code: toText(item?.space?.space_code),
batch_no: batchNo,
quantity: item?.quantity ?? '—',
available_quantity: item?.available_quantity ?? '—'
}
}
async function loadRows() {
loading.value = true
try {
const keyword = filters.keyword.trim()
const batchNo = filters.batch_no.trim()
const res: any = await getInventoryList({
where: {
// stock_query 为虚拟筛选字段:按商品编码/名称/条码/SKU ID 模糊匹配
...(keyword ? { stock_query: { operator: 'like', value: keyword } } : {}),
...(batchNo ? { batch_no: batchNo } : {})
},
pagination: { page: pagination.page, pageSize: pagination.pageSize },
group_by_batch: false
})
if (!isCheckApiSuccess(res)) {
throw new Error(res?.message || '查询库存失败')
}
const data = res.data ?? res
const rawList: any[] = Array.isArray(data.list) ? data.list : []
rows.value = rawList
.map((item, index) => mapRow(item, index))
.filter((row) => Number.isFinite(row.stock_id) && row.stock_id > 0)
pagination.total = Number(data.total ?? data.pagination?.total ?? rows.value.length) || 0
nextTick(() => tableRef.value?.setScrollTop?.(0))
} catch (e: any) {
console.error('[stockcheck] 查询库存失败', e)
ElMessage.error(e?.message || '查询库存失败')
rows.value = []
pagination.total = 0
} finally {
loading.value = false
}
}
function handleSearch() {
pagination.page = 1
loadRows()
}
function handleResetFilters() {
filters.keyword = ''
filters.batch_no = ''
pagination.page = 1
loadRows()
}
function handleSizeChange() {
pagination.page = 1
loadRows()
}
function handleSelectionChange(selection: Record<string, any>[]) {
selectedRows.value = selection
}
async function handleSubmit() {
const taskId = Number(props.taskId)
const stockIds = selectedStockIds.value
if (!Number.isFinite(taskId) || taskId <= 0) {
ElMessage.error('缺少盘点单ID,无法添加明细')
return
}
if (!stockIds.length) {
ElMessage.warning('请先勾选要盘点的商品')
return
}
submitting.value = true
try {
const res: any = await stockCheckAddDetail({ id: taskId, stock_ids: stockIds })
if (!isCheckApiSuccess(res)) {
throw new Error(res?.message || '添加盘点明细失败')
}
const data = res.data ?? res
const added = Number(data.added ?? 0)
const skipped = Number(data.skipped ?? 0)
const invalid: number[] = Array.isArray(data.invalid_stock_ids) ? data.invalid_stock_ids : []
const parts = [`新增 ${added} 行`]
if (skipped) parts.push(`跳过已存在 ${skipped} 行`)
if (invalid.length) parts.push(`无效商品 ${invalid.length} 个`)
if (invalid.length) ElMessage.warning(parts.join(','))
else ElMessage.success(parts.join(','))
emit('update:visible', false)
emit('success', { added, skipped })
} catch (e: any) {
console.error('[stockcheck] 添加盘点明细失败', e)
ElMessage.error(e?.message || '添加盘点明细失败')
} finally {
submitting.value = false
}
}
watch(
() => props.visible,
(visible) => {
if (!visible) return
filters.keyword = ''
filters.batch_no = ''
pagination.page = 1
pagination.total = 0
selectedRows.value = []
rows.value = []
nextTick(() => tableRef.value?.clearSelection?.())
void loadRows()
}
)
</script>
<style lang="scss" scoped>
.picker-body {
display: flex;
flex-direction: column;
gap: 8px;
}
/* 筛选行:不要套卡片,仅做对齐 + 单行 */
.picker-filters {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
flex-shrink: 0;
.filter-item {
width: 200px;
}
.filter-item--keyword {
width: 260px;
}
}
.table-host {
flex: 1;
min-height: 200px;
min-width: 0;
overflow: hidden;
}
.empty-tip {
font-size: 13px;
color: #909399;
}
/* 分页器:右对齐 + 「每页条数」下拉与 UpperTable 同宽 80px */
.pagination-container {
flex-shrink: 0;
display: flex;
justify-content: flex-end;
align-items: center;
:deep(.el-select--small) {
width: 80px;
}
:deep(.el-input__wrapper) {
padding: 0 4px;
}
}
.dialog-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
.selected-tip {
font-size: 12px;
color: #909399;
}
.footer-buttons {
display: flex;
align-items: center;
gap: 8px;
}
}
</style>