✨ feat(batch): 添加生产工单编号字段并重构选择器组件
parent
ed6fdd5894
commit
0ea1b51d3b
|
|
@ -53,9 +53,4 @@ export const BatchApi = {
|
|||
getBackwardList: async (code: string) => {
|
||||
return await request.get({ url: `/mes/wm/batch/backward-list`, params: { code } })
|
||||
},
|
||||
|
||||
// 获取批次精简列表(主要用于前端下拉)
|
||||
getBatchSimpleList: async () => {
|
||||
return await request.get({ url: `/mes/wm/batch/simple-list` })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,67 @@
|
|||
<!-- MES 批次选择器:一次加载全量,前端过滤(支持 code) -->
|
||||
<!--
|
||||
MES 批次选择器:只读输入框 + 点击弹窗选择
|
||||
|
||||
交互:显示为只读 el-input,点击打开弹窗(单选模式)进行选择
|
||||
Props:
|
||||
modelValue — 绑定的批次 ID(v-model)
|
||||
itemId — 默认过滤的物料 ID(打开弹窗时预设物料过滤条件)
|
||||
disabled — 是否禁用
|
||||
clearable — 是否允许清空(鼠标悬停时显示清除图标)
|
||||
placeholder — 占位文字
|
||||
Events:
|
||||
update:modelValue — v-model 更新
|
||||
change(item) — 选中批次变化时触发,传递完整 BatchVO(清空时为 undefined)
|
||||
-->
|
||||
<template>
|
||||
<el-select
|
||||
v-model="selectValue"
|
||||
<div
|
||||
v-bind="attrs"
|
||||
class="w-full"
|
||||
:class="disabled ? 'cursor-not-allowed' : 'cursor-pointer'"
|
||||
@click="handleClick"
|
||||
@mouseenter="hovering = true"
|
||||
@mouseleave="hovering = false"
|
||||
>
|
||||
<el-tooltip :disabled="!selectedItem" placement="top" :show-after="500">
|
||||
<template #content>
|
||||
<div v-if="selectedItem" class="leading-6">
|
||||
<div>批次编号:{{ selectedItem.code }}</div>
|
||||
<div>物料编码:{{ selectedItem.itemCode || '-' }}</div>
|
||||
<div>物料名称:{{ selectedItem.itemName || '-' }}</div>
|
||||
<div>生产批号:{{ selectedItem.lotNumber || '-' }}</div>
|
||||
</div>
|
||||
</template>
|
||||
<el-input
|
||||
:model-value="displayLabel"
|
||||
:placeholder="placeholder"
|
||||
:disabled="disabled"
|
||||
:clearable="clearable"
|
||||
filterable
|
||||
:filter-method="handleFilter"
|
||||
class="!w-1/1"
|
||||
@change="handleChange"
|
||||
>
|
||||
<el-option v-for="item in filteredList" :key="item.id" :label="item.code" :value="item.id">
|
||||
<div class="flex items-center gap-8px">
|
||||
<span>{{ item.code }}</span>
|
||||
<el-tag v-if="item.itemCode" size="small" type="info" class="ml-4px">
|
||||
{{ item.itemCode }}
|
||||
</el-tag>
|
||||
readonly
|
||||
:suffix-icon="suffixIcon"
|
||||
:class="disabled ? 'is-select-disabled' : 'is-select-clickable'"
|
||||
/>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
</el-option>
|
||||
</el-select>
|
||||
<!-- 弹窗必须放在 div 外部,否则弹窗内的点击事件会冒泡到 div 触发 handleClick -->
|
||||
<WmBatchSelectDialog ref="dialogRef" :multiple="false" @selected="handleSelected" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { BatchApi, BatchVO } from '@/api/mes/wm/batch'
|
||||
import { Search, CircleClose } from '@element-plus/icons-vue'
|
||||
import WmBatchSelectDialog from './WmBatchSelectDialog.vue'
|
||||
|
||||
defineOptions({ name: 'WmBatchSelect' })
|
||||
// 组件有两个根节点(div + Dialog),Vue 不会自动继承 attrs;
|
||||
// 手动透传到外层 div,确保父组件传入的 class / style 等生效
|
||||
const attrs = useAttrs()
|
||||
|
||||
defineOptions({ name: 'WmBatchSelect', inheritAttrs: false })
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
modelValue?: number
|
||||
itemId?: number
|
||||
disabled?: boolean
|
||||
clearable?: boolean
|
||||
placeholder?: string
|
||||
modelValue?: number // 绑定的批次 ID
|
||||
itemId?: number // 默认过滤的物料 ID
|
||||
disabled?: boolean // 是否禁用
|
||||
clearable?: boolean // 是否允许清空
|
||||
placeholder?: string // 占位文字
|
||||
}>(),
|
||||
{
|
||||
disabled: false,
|
||||
|
|
@ -46,52 +75,98 @@ const emit = defineEmits<{
|
|||
change: [item: BatchVO | undefined]
|
||||
}>()
|
||||
|
||||
const allList = ref<BatchVO[]>([])
|
||||
const filteredList = ref<BatchVO[]>([])
|
||||
const dialogRef = ref() // 弹窗 Ref
|
||||
const hovering = ref(false) // 鼠标是否悬停
|
||||
|
||||
const selectValue = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (val) => emit('update:modelValue', val)
|
||||
// ==================== 名称回显 ====================
|
||||
const selectedItem = ref<BatchVO | undefined>() // 当前选中的批次对象
|
||||
|
||||
/** 输入框显示文本:展示批次编号 */
|
||||
const displayLabel = computed(() => {
|
||||
return selectedItem.value?.code ?? ''
|
||||
})
|
||||
|
||||
/** 前端过滤(code) */
|
||||
const handleFilter = (query: string) => {
|
||||
if (!query) {
|
||||
filteredList.value = allList.value
|
||||
/** 是否显示清除图标 */
|
||||
const showClear = computed(() => {
|
||||
return props.clearable && !props.disabled && hovering.value && props.modelValue != null
|
||||
})
|
||||
|
||||
/** 后缀图标:悬停且有值时显示清除,否则显示搜索 */
|
||||
const suffixIcon = computed(() => {
|
||||
return showClear.value ? CircleClose : Search
|
||||
})
|
||||
|
||||
/** 根据 ID 单条查询批次信息(用于编辑回显) */
|
||||
const resolveItemById = async (id: number | undefined) => {
|
||||
if (id == null) {
|
||||
selectedItem.value = undefined
|
||||
return
|
||||
}
|
||||
const keyword = query.toLowerCase()
|
||||
filteredList.value = allList.value.filter(
|
||||
(item) =>
|
||||
item.code?.toLowerCase().includes(keyword) || item.itemCode?.toLowerCase().includes(keyword)
|
||||
)
|
||||
if (selectedItem.value?.id === id) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
selectedItem.value = await BatchApi.getBatch(id)
|
||||
} catch (e) {
|
||||
console.error('[WmBatchSelect] resolveItemById failed:', e)
|
||||
}
|
||||
}
|
||||
|
||||
/** 监听 itemId 变化,前端过滤(如果是基于 itemId 的过滤) */
|
||||
/** 监听 modelValue 变化,触发回显 */
|
||||
watch(
|
||||
() => props.itemId,
|
||||
() => props.modelValue,
|
||||
(val) => {
|
||||
if (val) {
|
||||
filteredList.value = allList.value.filter((item) => item.itemId === val)
|
||||
} else {
|
||||
filteredList.value = allList.value
|
||||
}
|
||||
}
|
||||
resolveItemById(val)
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
/** 选中变化 */
|
||||
const handleChange = (val: number | undefined) => {
|
||||
const item = allList.value.find((o) => o.id === val)
|
||||
// ==================== 点击交互 ====================
|
||||
|
||||
/** 点击组件:清除或打开弹窗 */
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
if (props.disabled) {
|
||||
return
|
||||
}
|
||||
// 点击清除图标:清空选中
|
||||
const target = e.target as HTMLElement
|
||||
if (showClear.value && target.closest('.el-input__suffix')) {
|
||||
e.stopPropagation()
|
||||
selectedItem.value = undefined
|
||||
emit('update:modelValue', undefined)
|
||||
emit('change', undefined)
|
||||
return
|
||||
}
|
||||
// 打开弹窗,传入当前选中 ID 用于预选高亮,传入 itemId 用于默认过滤
|
||||
const selectedIds = props.modelValue != null ? [props.modelValue] : []
|
||||
dialogRef.value.open(selectedIds, props.itemId)
|
||||
}
|
||||
|
||||
/** 弹窗选中回调 */
|
||||
const handleSelected = (rows: BatchVO[]) => {
|
||||
if (!rows || rows.length === 0) {
|
||||
return
|
||||
}
|
||||
const item = rows[0]
|
||||
selectedItem.value = item
|
||||
emit('update:modelValue', item.id)
|
||||
emit('change', item)
|
||||
}
|
||||
|
||||
/** 加载批次列表 */
|
||||
onMounted(async () => {
|
||||
allList.value = await BatchApi.getBatchSimpleList()
|
||||
if (props.itemId) {
|
||||
filteredList.value = allList.value.filter((item) => item.itemId === props.itemId)
|
||||
} else {
|
||||
filteredList.value = allList.value
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
/* :deep 用于穿透 el-input 内部元素的 cursor 样式,UnoCSS 无法直接处理组件内部 DOM */
|
||||
.is-select-clickable {
|
||||
:deep(.el-input__wrapper),
|
||||
:deep(.el-input__inner) {
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
.is-select-disabled {
|
||||
:deep(.el-input__wrapper),
|
||||
:deep(.el-input__inner) {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,331 @@
|
|||
<!--
|
||||
MES 批次弹窗选择器(支持单选/多选)
|
||||
|
||||
对齐 KTG batchSelect/single.vue 的搜索字段和展示字段
|
||||
架构对齐 MdVendorSelectDialog.vue(ContentWrap 两栏布局 + 分页)
|
||||
|
||||
Props:
|
||||
multiple — true 多选(checkbox),false 单选(radio);默认 true
|
||||
Events:
|
||||
selected(rows: BatchVO[]) — 确认选择后触发,单选时数组长度为 1
|
||||
Expose:
|
||||
open(selectedIds?, itemId?) — 打开弹窗,可传入已选 ID 用于预选高亮,itemId 用于默认过滤物料
|
||||
-->
|
||||
<template>
|
||||
<Dialog title="批次选择" v-model="dialogVisible" width="75%">
|
||||
<ContentWrap>
|
||||
<el-form :inline="true" :model="queryParams" label-width="100px">
|
||||
<el-form-item label="批次编号">
|
||||
<el-input
|
||||
v-model="queryParams.code"
|
||||
placeholder="请输入批次编号"
|
||||
clearable
|
||||
@keyup.enter="handleQuery"
|
||||
class="!w-200px"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="产品物料">
|
||||
<MdItemSelect
|
||||
v-model="queryParams.itemId"
|
||||
placeholder="请选择产品物料"
|
||||
class="!w-200px"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="生产工单">
|
||||
<ProWorkOrderSelect
|
||||
v-model="queryParams.workOrderId"
|
||||
placeholder="请选择生产工单"
|
||||
class="!w-200px"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="销售订单编号">
|
||||
<el-input
|
||||
v-model="queryParams.salesOrderCode"
|
||||
placeholder="请输入销售订单编号"
|
||||
clearable
|
||||
@keyup.enter="handleQuery"
|
||||
class="!w-200px"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="采购订单编号">
|
||||
<el-input
|
||||
v-model="queryParams.purchaseOrderCode"
|
||||
placeholder="请输入采购订单编号"
|
||||
clearable
|
||||
@keyup.enter="handleQuery"
|
||||
class="!w-200px"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button @click="handleQuery">
|
||||
<Icon icon="ep:search" class="mr-5px" /> 搜索
|
||||
</el-button>
|
||||
<el-button @click="resetQuery">
|
||||
<Icon icon="ep:refresh" class="mr-5px" /> 重置
|
||||
</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</ContentWrap>
|
||||
<!-- 数据表格:对齐 KTG 展示字段 -->
|
||||
<ContentWrap>
|
||||
<el-table
|
||||
ref="tableRef"
|
||||
v-loading="loading"
|
||||
:data="list"
|
||||
:stripe="true"
|
||||
:show-overflow-tooltip="true"
|
||||
border
|
||||
row-key="id"
|
||||
:highlight-current-row="!multiple"
|
||||
@selection-change="handleSelectionChange"
|
||||
@row-click="handleRowClick"
|
||||
@row-dblclick="handleRowDblClick"
|
||||
>
|
||||
<!-- 多选:checkbox -->
|
||||
<el-table-column
|
||||
v-if="multiple"
|
||||
type="selection"
|
||||
:reserve-selection="true"
|
||||
width="50"
|
||||
align="center"
|
||||
/>
|
||||
<!-- 单选:radio -->
|
||||
<el-table-column v-else width="50" align="center">
|
||||
<template #default="{ row }">
|
||||
<el-radio
|
||||
v-model="selectedRadioId"
|
||||
:value="row.id"
|
||||
class="radio-no-label"
|
||||
@change="handleRadioChange(row)"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="批次编号" align="center" prop="code" width="150" />
|
||||
<el-table-column label="物料编码" align="center" prop="itemCode" width="150" />
|
||||
<el-table-column label="物料名称" align="left" prop="itemName" min-width="140" />
|
||||
<el-table-column label="规格型号" align="center" prop="itemSpecification" width="120" />
|
||||
<el-table-column label="单位" align="center" prop="unitName" width="80" />
|
||||
<el-table-column label="供应商编码" align="center" prop="vendorCode" width="120" />
|
||||
<el-table-column label="供应商名称" align="center" prop="vendorName" width="120" />
|
||||
<el-table-column label="客户编码" align="center" prop="clientCode" width="110" />
|
||||
<el-table-column label="客户名称" align="center" prop="clientName" width="110" />
|
||||
<el-table-column label="销售订单编号" align="center" prop="salesOrderCode" width="140" />
|
||||
<el-table-column label="采购订单编号" align="center" prop="purchaseOrderCode" width="140" />
|
||||
<el-table-column label="工单编码" align="center" prop="workOrderCode" width="140" />
|
||||
<el-table-column label="生产批号" align="center" prop="lotNumber" width="120" />
|
||||
<el-table-column label="生产日期" align="center" prop="produceDate" width="120">
|
||||
<template #default="scope">
|
||||
<span>{{ formatDate(scope.row.produceDate, 'YYYY-MM-DD') }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="有效期" align="center" prop="expireDate" width="120">
|
||||
<template #default="scope">
|
||||
<span>{{ formatDate(scope.row.expireDate, 'YYYY-MM-DD') }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="入库日期" align="center" prop="receiptDate" width="120">
|
||||
<template #default="scope">
|
||||
<span>{{ formatDate(scope.row.receiptDate, 'YYYY-MM-DD') }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
<Pagination
|
||||
:total="total"
|
||||
v-model:page="queryParams.pageNo"
|
||||
v-model:limit="queryParams.pageSize"
|
||||
@pagination="getList"
|
||||
/>
|
||||
</ContentWrap>
|
||||
<template #footer>
|
||||
<el-button type="primary" @click="confirmSelect">确 定</el-button>
|
||||
<el-button @click="dialogVisible = false">取 消</el-button>
|
||||
</template>
|
||||
</Dialog>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { formatDate } from '@/utils/formatTime'
|
||||
import { BatchApi, BatchVO } from '@/api/mes/wm/batch'
|
||||
import MdItemSelect from '@/views/mes/md/item/components/MdItemSelect.vue'
|
||||
import ProWorkOrderSelect from '@/views/mes/pro/workorder/components/ProWorkOrderSelect.vue'
|
||||
|
||||
defineOptions({ name: 'WmBatchSelectDialog' })
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
multiple?: boolean // true 多选(checkbox),false 单选(radio)
|
||||
}>(),
|
||||
{
|
||||
multiple: true
|
||||
}
|
||||
)
|
||||
|
||||
const message = useMessage()
|
||||
const emit = defineEmits<{
|
||||
selected: [rows: BatchVO[]]
|
||||
}>()
|
||||
|
||||
const dialogVisible = ref(false) // 弹窗是否展示
|
||||
const loading = ref(false) // 列表加载中
|
||||
const list = ref<BatchVO[]>([]) // 批次列表
|
||||
const total = ref(0) // 总条数
|
||||
|
||||
// ==================== 选中状态 ====================
|
||||
const tableRef = ref() // 表格 Ref
|
||||
const selectedRows = ref<BatchVO[]>([]) // 多选模式:选中行
|
||||
const selectedRadioId = ref<number>() // 单选模式:选中 ID
|
||||
const currentRadioRow = ref<BatchVO>() // 单选模式:选中行对象
|
||||
const preSelectedIds = ref<number[]>([]) // 打开弹窗时传入的已选 ID
|
||||
|
||||
/** 多选:checkbox 变化 */
|
||||
const handleSelectionChange = (rows: BatchVO[]) => {
|
||||
if (props.multiple) {
|
||||
selectedRows.value = rows
|
||||
}
|
||||
}
|
||||
|
||||
/** 单选:radio 变化 */
|
||||
const handleRadioChange = (row: BatchVO) => {
|
||||
currentRadioRow.value = row
|
||||
}
|
||||
|
||||
/** 单击行:单选模式下点击整行即选中 */
|
||||
const handleRowClick = (row: BatchVO) => {
|
||||
if (props.multiple) {
|
||||
return
|
||||
}
|
||||
selectedRadioId.value = row.id
|
||||
currentRadioRow.value = row
|
||||
}
|
||||
|
||||
/** 双击行:多选模式切换勾选,单选模式直接确认 */
|
||||
const handleRowDblClick = (row: BatchVO) => {
|
||||
if (props.multiple) {
|
||||
tableRef.value?.toggleRowSelection(row)
|
||||
return
|
||||
}
|
||||
selectedRadioId.value = row.id
|
||||
currentRadioRow.value = row
|
||||
confirmSelect()
|
||||
}
|
||||
|
||||
// ==================== 批次查询 ====================
|
||||
const queryParams = reactive({
|
||||
pageNo: 1, // 页码
|
||||
pageSize: 10, // 每页条数
|
||||
code: undefined as string | undefined, // 批次编码
|
||||
itemId: undefined as number | undefined, // 物料 ID(MdItemSelect)
|
||||
workOrderId: undefined as number | undefined, // 工单 ID(ProWorkOrderSelect)
|
||||
salesOrderCode: undefined as string | undefined, // 销售订单编号
|
||||
purchaseOrderCode: undefined as string | undefined // 采购订单编号
|
||||
})
|
||||
|
||||
/** 查询批次列表 */
|
||||
const getList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const data = await BatchApi.getBatchPage(queryParams)
|
||||
list.value = data.list
|
||||
total.value = data.total
|
||||
await nextTick()
|
||||
applyPreSelection()
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/** 恢复预选状态(当前页可见范围内) */
|
||||
const applyPreSelection = () => {
|
||||
if (preSelectedIds.value.length === 0) {
|
||||
return
|
||||
}
|
||||
if (props.multiple) {
|
||||
const table = tableRef.value
|
||||
if (!table) {
|
||||
return
|
||||
}
|
||||
list.value.forEach((row) => {
|
||||
if (preSelectedIds.value.includes(row.id)) {
|
||||
table.toggleRowSelection(row, true)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
const match = list.value.find((row) => preSelectedIds.value.includes(row.id))
|
||||
if (match) {
|
||||
selectedRadioId.value = match.id
|
||||
currentRadioRow.value = match
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 搜索按钮操作 */
|
||||
const handleQuery = () => {
|
||||
queryParams.pageNo = 1
|
||||
getList()
|
||||
}
|
||||
|
||||
/** 重置查询条件 */
|
||||
const resetQuery = () => {
|
||||
queryParams.code = undefined
|
||||
queryParams.itemId = undefined
|
||||
queryParams.workOrderId = undefined
|
||||
queryParams.salesOrderCode = undefined
|
||||
queryParams.purchaseOrderCode = undefined
|
||||
handleQuery()
|
||||
}
|
||||
|
||||
/** 确认选择 */
|
||||
const confirmSelect = () => {
|
||||
if (props.multiple) {
|
||||
if (selectedRows.value.length === 0) {
|
||||
message.warning('请至少选择一条数据')
|
||||
return
|
||||
}
|
||||
emit('selected', selectedRows.value)
|
||||
} else {
|
||||
if (!currentRadioRow.value) {
|
||||
message.warning('请选择一条数据')
|
||||
return
|
||||
}
|
||||
emit('selected', [currentRadioRow.value])
|
||||
}
|
||||
dialogVisible.value = false
|
||||
}
|
||||
|
||||
// ==================== 打开弹窗 ====================
|
||||
|
||||
/**
|
||||
* 打开弹窗
|
||||
* @param selectedIds 已选 ID,用于预选高亮
|
||||
* @param itemId 默认过滤的物料 ID(由外层 WmBatchSelect 的 itemId prop 传入)
|
||||
*/
|
||||
const open = async (selectedIds?: number[], itemId?: number) => {
|
||||
dialogVisible.value = true
|
||||
// 重置查询条件 + 页码,避免二次打开继承上次过滤上下文
|
||||
queryParams.code = undefined
|
||||
queryParams.itemId = itemId ?? undefined // 传入 itemId 则默认按物料过滤
|
||||
queryParams.workOrderId = undefined
|
||||
queryParams.salesOrderCode = undefined
|
||||
queryParams.purchaseOrderCode = undefined
|
||||
queryParams.pageNo = 1
|
||||
// 清空上一次的选中状态
|
||||
selectedRows.value = []
|
||||
selectedRadioId.value = undefined
|
||||
currentRadioRow.value = undefined
|
||||
preSelectedIds.value = selectedIds ?? []
|
||||
// 多选模式清空跨页缓存的勾选
|
||||
await nextTick()
|
||||
tableRef.value?.clearSelection()
|
||||
await getList()
|
||||
}
|
||||
defineExpose({ open })
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
/* 隐藏 radio 的 label 文字,只保留圆圈 */
|
||||
.radio-no-label {
|
||||
:deep(.el-radio__label) {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Loading…
Reference in New Issue