606 lines
18 KiB
Vue
606 lines
18 KiB
Vue
<template>
|
||
<div class="package-material-container" :style="{ height: containerHeight }">
|
||
<scan-box
|
||
ref="scanBox"
|
||
width="250px"
|
||
:tree-props="{ children: 'children', label: 'label' }"
|
||
:auto-submit="false"
|
||
:preset-tree="presetTree"
|
||
:available-fields="availableFieldsRef"
|
||
@condition-change="handleConditionChange"
|
||
@reset="handleConditionReset"
|
||
@condition-clear="handleConditionClear"
|
||
@submit="handleSubmit"
|
||
/>
|
||
|
||
<div class="main-content">
|
||
<div class="toolbar">
|
||
<div class="toolbar-left">
|
||
<el-button size="small" type="primary" :loading="loading" @click="loadList">刷新</el-button>
|
||
<el-button
|
||
type="danger"
|
||
size="small"
|
||
plain
|
||
:disabled="!selectedIds.length"
|
||
:loading="batchDeleting"
|
||
@click="handleBatchDelete"
|
||
>
|
||
批量删除
|
||
</el-button>
|
||
<!-- <el-tag size="small" type="info">共 {{ pagination.total }} 条</el-tag> -->
|
||
</div>
|
||
<div class="toolbar-right">
|
||
<el-button size="small" type="primary" plain @click="searchFormVisible = true">快速搜索</el-button>
|
||
<el-button type="success" size="small" @click="openCreate">新增包材</el-button>
|
||
<el-button type="warning" size="small" plain @click="openBatchImport">批量导入</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="upper-wrapper">
|
||
<upper-table
|
||
ref="upperTable"
|
||
title="包材列表"
|
||
default-align="center"
|
||
table-id="material-package-material"
|
||
:data="tableData"
|
||
:columns="columns"
|
||
:table-size="tableSize"
|
||
:pagination="pagination"
|
||
:show-index="true"
|
||
:show-selection="true"
|
||
:show-actions="true"
|
||
:loading="loading"
|
||
:query-time="queryTime"
|
||
row-key="id"
|
||
@page-change="handlePageChange"
|
||
@page-size-change="handlePageSizeChange"
|
||
@refresh="loadList"
|
||
@selection-change="onSelectionChange"
|
||
>
|
||
<template #column-dimensions="{ row }">
|
||
{{ formatDimensions(row) }}
|
||
</template>
|
||
|
||
<template #column-weight="{ row }">
|
||
{{ formatNumber(row.weight) }}
|
||
</template>
|
||
|
||
<template #column-cost_price="{ row }">
|
||
{{ formatNumber(row.cost_price) }}
|
||
</template>
|
||
|
||
<template #column-status="{ row }">
|
||
<el-tag :type="formatPackageMaterialStatus(row.status).type" size="small">
|
||
{{ formatPackageMaterialStatus(row.status).text }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #column-actions="{ row }">
|
||
<el-button link type="primary" size="small" @click.stop="openEdit(row)">编辑</el-button>
|
||
<el-button link type="danger" size="small" @click.stop="handleDelete(row)">删除</el-button>
|
||
</template>
|
||
|
||
<template #empty>
|
||
<div class="empty-tip">暂无包材数据,可调整左侧筛选条件</div>
|
||
</template>
|
||
</upper-table>
|
||
</div>
|
||
</div>
|
||
|
||
<el-dialog
|
||
v-model="dialogVisible"
|
||
:title="dialogMode === 'create' ? '新增包材' : '编辑包材'"
|
||
width="720px"
|
||
align-center
|
||
@close="resetForm"
|
||
>
|
||
<el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
|
||
<el-row :gutter="16">
|
||
<el-col :span="12">
|
||
<el-form-item label="包材编码" prop="material_code">
|
||
<el-input
|
||
v-model="formData.material_code"
|
||
placeholder="大写字母/数字,仓库内唯一"
|
||
:disabled="dialogMode === 'edit'"
|
||
@blur="normalizeMaterialCode"
|
||
/>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="包材名称" prop="material_name">
|
||
<el-input v-model="formData.material_name" placeholder="请输入包材名称" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="包材类型" prop="material_type">
|
||
<el-input v-model="formData.material_type" placeholder="如 纸箱、气泡袋" clearable />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="规格" prop="spec">
|
||
<el-input v-model="formData.spec" placeholder="如 60×40×30cm" clearable />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="长度" prop="length">
|
||
<el-input-number
|
||
v-model="formData.length"
|
||
:min="0"
|
||
:precision="2"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="宽度" prop="width">
|
||
<el-input-number
|
||
v-model="formData.width"
|
||
:min="0"
|
||
:precision="2"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="高度" prop="height">
|
||
<el-input-number
|
||
v-model="formData.height"
|
||
:min="0"
|
||
:precision="2"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="重量" prop="weight">
|
||
<el-input-number
|
||
v-model="formData.weight"
|
||
:min="0"
|
||
:precision="3"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="成本价" prop="cost_price">
|
||
<el-input-number
|
||
v-model="formData.cost_price"
|
||
:min="0"
|
||
:precision="2"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="状态" prop="status">
|
||
<el-radio-group v-model="formData.status">
|
||
<el-radio :label="1">启用</el-radio>
|
||
<el-radio :label="0">停用</el-radio>
|
||
</el-radio-group>
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button @click="dialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="saving" @click="submitForm">保存</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<BatchImportDialog v-model="batchImportVisible" @success="onBatchImportSuccess" />
|
||
<PackageMaterialSearchForm v-model="searchFormVisible" />
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, reactive, ref } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import type { FormInstance, FormRules } from 'element-plus'
|
||
import ScanBox from '@/components/ScanBox/index.vue'
|
||
import UpperTable from '@/components/UpperTable/index.vue'
|
||
import BatchImportDialog from './component/BatchImportDialog.vue'
|
||
import PackageMaterialSearchForm from './form/PackageMaterialSearchForm.vue'
|
||
import {
|
||
packageMaterialCreate,
|
||
packageMaterialDelete,
|
||
packageMaterialGet,
|
||
packageMaterialUpdate
|
||
} from '@/api/setting.packagematerial/packagematerial'
|
||
import { useSettingsStore } from '@/stores/modules/settings'
|
||
import {
|
||
buildPackageMaterialListPayload,
|
||
parsePackageMaterialListResponse,
|
||
formatDimensions,
|
||
formatNumber,
|
||
formatPackageMaterialStatus,
|
||
normalizePackageMaterialScanConditionToWhere,
|
||
packageMaterialColumns,
|
||
packageMaterialScanFields,
|
||
type PackageMaterialRecord
|
||
} from './package-material-config'
|
||
|
||
const settingsStore = useSettingsStore()
|
||
const scanBox = ref<any>(null)
|
||
const upperTable = ref<any>(null)
|
||
|
||
const presetTree = ref<any[]>([])
|
||
const availableFieldsRef = reactive(JSON.parse(JSON.stringify(packageMaterialScanFields)))
|
||
|
||
const shortcutExpanded = computed(() => settingsStore.shortcutExpanded)
|
||
const containerHeight = computed(() =>
|
||
shortcutExpanded.value ? 'calc(100vh - 150px)' : 'calc(100vh - 100px)'
|
||
)
|
||
|
||
const columns = packageMaterialColumns
|
||
const loading = ref(false)
|
||
const saving = ref(false)
|
||
const batchDeleting = ref(false)
|
||
const selectedIds = ref<number[]>([])
|
||
const tableData = ref<PackageMaterialRecord[]>([])
|
||
const queryTime = ref(0)
|
||
const tableSize = ref<'small' | 'default' | 'large'>('small')
|
||
const currentConditionRaw = ref<any>(null)
|
||
|
||
const pagination = reactive({ currentPage: 1, pageSize: 20, total: 0 })
|
||
|
||
const dialogVisible = ref(false)
|
||
const dialogMode = ref<'create' | 'edit'>('create')
|
||
const batchImportVisible = ref(false)
|
||
const searchFormVisible = ref(false)
|
||
const formRef = ref<FormInstance>()
|
||
const formData = reactive({
|
||
id: undefined as number | undefined,
|
||
material_code: '',
|
||
material_name: '',
|
||
material_type: '',
|
||
spec: '',
|
||
length: undefined as number | undefined,
|
||
width: undefined as number | undefined,
|
||
height: undefined as number | undefined,
|
||
weight: undefined as number | undefined,
|
||
cost_price: undefined as number | undefined,
|
||
status: 1
|
||
})
|
||
|
||
const rules: FormRules = {
|
||
material_code: [{ required: true, message: '请输入包材编码', trigger: 'blur' }],
|
||
material_name: [{ required: true, message: '请输入包材名称', trigger: 'blur' }]
|
||
}
|
||
|
||
const isSuccess = (res: any) => res && [0, 1, 200].includes(res.code)
|
||
|
||
function normalizeMaterialCode() {
|
||
formData.material_code = String(formData.material_code || '').trim().toUpperCase()
|
||
}
|
||
|
||
function onSelectionChange(rows: PackageMaterialRecord[]) {
|
||
selectedIds.value = (rows || [])
|
||
.map((r) => r.id)
|
||
.filter((id): id is number => id != null && Number.isFinite(Number(id)))
|
||
.map((id) => Number(id))
|
||
}
|
||
|
||
function pickOptionalNumber(value: number | undefined) {
|
||
return value === undefined || value === null ? undefined : value
|
||
}
|
||
|
||
function buildCreatePayload() {
|
||
const payload: Record<string, unknown> = {
|
||
material_code: formData.material_code.trim(),
|
||
material_name: formData.material_name.trim(),
|
||
status: formData.status
|
||
}
|
||
const materialType = formData.material_type.trim()
|
||
const spec = formData.spec.trim()
|
||
if (materialType) payload.material_type = materialType
|
||
if (spec) payload.spec = spec
|
||
const length = pickOptionalNumber(formData.length)
|
||
const width = pickOptionalNumber(formData.width)
|
||
const height = pickOptionalNumber(formData.height)
|
||
const weight = pickOptionalNumber(formData.weight)
|
||
const costPrice = pickOptionalNumber(formData.cost_price)
|
||
if (length !== undefined) payload.length = length
|
||
if (width !== undefined) payload.width = width
|
||
if (height !== undefined) payload.height = height
|
||
if (weight !== undefined) payload.weight = weight
|
||
if (costPrice !== undefined) payload.cost_price = costPrice
|
||
return payload
|
||
}
|
||
|
||
function buildUpdatePayload() {
|
||
if (!formData.id) return null
|
||
const payload: Record<string, unknown> = { id: formData.id }
|
||
const materialName = formData.material_name.trim()
|
||
if (materialName) payload.material_name = materialName
|
||
const materialType = formData.material_type.trim()
|
||
const spec = formData.spec.trim()
|
||
payload.material_type = materialType || ''
|
||
payload.spec = spec || ''
|
||
const length = pickOptionalNumber(formData.length)
|
||
const width = pickOptionalNumber(formData.width)
|
||
const height = pickOptionalNumber(formData.height)
|
||
const weight = pickOptionalNumber(formData.weight)
|
||
const costPrice = pickOptionalNumber(formData.cost_price)
|
||
if (length !== undefined) payload.length = length
|
||
if (width !== undefined) payload.width = width
|
||
if (height !== undefined) payload.height = height
|
||
if (weight !== undefined) payload.weight = weight
|
||
if (costPrice !== undefined) payload.cost_price = costPrice
|
||
payload.status = formData.status
|
||
return payload
|
||
}
|
||
|
||
async function loadList() {
|
||
if (loading.value) return
|
||
loading.value = true
|
||
const start = Date.now()
|
||
try {
|
||
const where = normalizePackageMaterialScanConditionToWhere(currentConditionRaw.value)
|
||
const res: any = await packageMaterialGet(
|
||
buildPackageMaterialListPayload({
|
||
page: pagination.currentPage,
|
||
pageSize: pagination.pageSize,
|
||
where
|
||
})
|
||
)
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '获取包材列表失败')
|
||
tableData.value = []
|
||
pagination.total = 0
|
||
return
|
||
}
|
||
const data = parsePackageMaterialListResponse(res)
|
||
tableData.value = data.list
|
||
pagination.total = data.total
|
||
if (data.page) pagination.currentPage = data.page
|
||
if (data.pageSize) pagination.pageSize = data.pageSize
|
||
} catch {
|
||
tableData.value = []
|
||
pagination.total = 0
|
||
ElMessage.error('获取包材列表失败')
|
||
} finally {
|
||
queryTime.value = Date.now() - start
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleConditionChange(condition: any) {
|
||
currentConditionRaw.value = condition
|
||
pagination.currentPage = 1
|
||
loadList()
|
||
}
|
||
|
||
function handleSubmit() {
|
||
pagination.currentPage = 1
|
||
loadList()
|
||
}
|
||
|
||
function handleConditionClear() {
|
||
currentConditionRaw.value = null
|
||
pagination.currentPage = 1
|
||
loadList()
|
||
}
|
||
|
||
function handleConditionReset() {
|
||
currentConditionRaw.value = null
|
||
pagination.currentPage = 1
|
||
if (scanBox.value?.reset) {
|
||
scanBox.value.reset()
|
||
} else if (scanBox.value?.clear) {
|
||
scanBox.value.clear()
|
||
}
|
||
loadList()
|
||
}
|
||
|
||
function handlePageChange(page: number) {
|
||
pagination.currentPage = page
|
||
loadList()
|
||
}
|
||
|
||
function handlePageSizeChange(size: number) {
|
||
pagination.pageSize = size
|
||
pagination.currentPage = 1
|
||
loadList()
|
||
}
|
||
|
||
function resetForm() {
|
||
formData.id = undefined
|
||
formData.material_code = ''
|
||
formData.material_name = ''
|
||
formData.material_type = ''
|
||
formData.spec = ''
|
||
formData.length = undefined
|
||
formData.width = undefined
|
||
formData.height = undefined
|
||
formData.weight = undefined
|
||
formData.cost_price = undefined
|
||
formData.status = 1
|
||
formRef.value?.clearValidate()
|
||
}
|
||
|
||
function openCreate() {
|
||
dialogMode.value = 'create'
|
||
dialogVisible.value = true
|
||
resetForm()
|
||
}
|
||
|
||
function openEdit(row: PackageMaterialRecord) {
|
||
if (!row.id) return
|
||
dialogMode.value = 'edit'
|
||
dialogVisible.value = true
|
||
formData.id = row.id
|
||
formData.material_code = row.material_code || ''
|
||
formData.material_name = row.material_name || ''
|
||
formData.material_type = row.material_type || ''
|
||
formData.spec = row.spec || ''
|
||
formData.length = row.length
|
||
formData.width = row.width
|
||
formData.height = row.height
|
||
formData.weight = row.weight
|
||
formData.cost_price = row.cost_price
|
||
formData.status = row.status === 0 ? 0 : 1
|
||
formRef.value?.clearValidate()
|
||
}
|
||
|
||
async function submitForm() {
|
||
if (!formRef.value || saving.value) return
|
||
const valid = await formRef.value.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
normalizeMaterialCode()
|
||
saving.value = true
|
||
try {
|
||
if (dialogMode.value === 'create') {
|
||
const res: any = await packageMaterialCreate(buildCreatePayload() as any)
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '新增失败')
|
||
return
|
||
}
|
||
ElMessage.success('新增成功')
|
||
} else {
|
||
const payload = buildUpdatePayload()
|
||
if (!payload) return
|
||
const res: any = await packageMaterialUpdate(payload as any)
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '更新失败')
|
||
return
|
||
}
|
||
ElMessage.success('更新成功')
|
||
}
|
||
dialogVisible.value = false
|
||
loadList()
|
||
} catch {
|
||
ElMessage.error('保存失败')
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
async function deleteByIds(ids: number[]) {
|
||
if (!ids.length) return
|
||
const res: any = await packageMaterialDelete({ ids })
|
||
if (!isSuccess(res)) {
|
||
ElMessage.error(res?.message || '删除失败')
|
||
return false
|
||
}
|
||
ElMessage.success('删除成功')
|
||
return true
|
||
}
|
||
|
||
function handleDelete(row: PackageMaterialRecord) {
|
||
if (!row.id) return
|
||
const name = row.material_name || row.material_code || String(row.id)
|
||
ElMessageBox.confirm(`确认删除包材「${name}」吗?`, '提示', { type: 'warning' })
|
||
.then(async () => {
|
||
const ok = await deleteByIds([row.id as number])
|
||
if (!ok) return
|
||
if (tableData.value.length === 1 && pagination.currentPage > 1) {
|
||
pagination.currentPage -= 1
|
||
}
|
||
selectedIds.value = selectedIds.value.filter((id) => id !== row.id)
|
||
loadList()
|
||
})
|
||
.catch(() => undefined)
|
||
}
|
||
|
||
function handleBatchDelete() {
|
||
if (!selectedIds.value.length || batchDeleting.value) return
|
||
ElMessageBox.confirm(`确认删除选中的 ${selectedIds.value.length} 条包材吗?`, '提示', {
|
||
type: 'warning'
|
||
})
|
||
.then(async () => {
|
||
batchDeleting.value = true
|
||
try {
|
||
const ok = await deleteByIds([...selectedIds.value])
|
||
if (!ok) return
|
||
selectedIds.value = []
|
||
if (upperTable.value?.clearSelection) {
|
||
upperTable.value.clearSelection()
|
||
}
|
||
loadList()
|
||
} finally {
|
||
batchDeleting.value = false
|
||
}
|
||
})
|
||
.catch(() => undefined)
|
||
}
|
||
|
||
function openBatchImport() {
|
||
batchImportVisible.value = true
|
||
}
|
||
|
||
function onBatchImportSuccess() {
|
||
loadList()
|
||
}
|
||
|
||
onMounted(() => {
|
||
loadList()
|
||
})
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.package-material-container {
|
||
display: flex;
|
||
width: 100%;
|
||
background-color: #f0f2f5;
|
||
|
||
.main-content {
|
||
flex: 1;
|
||
display: flex;
|
||
flex-direction: column;
|
||
padding: 10px;
|
||
overflow: hidden;
|
||
min-width: 0;
|
||
}
|
||
|
||
.toolbar {
|
||
padding: 5px 8px;
|
||
border-bottom: 1px solid #ebeef5;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
gap: 12px;
|
||
flex-wrap: wrap;
|
||
background-color: #fff;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.toolbar-left {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
flex-wrap: wrap;
|
||
min-width: 0;
|
||
}
|
||
|
||
.toolbar-right {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.upper-wrapper {
|
||
flex: 1 1 auto;
|
||
min-height: 0;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.upper-wrapper :deep(.upper-table-container) {
|
||
height: 100%;
|
||
}
|
||
|
||
.empty-tip {
|
||
text-align: center;
|
||
font-size: 13px;
|
||
color: #909399;
|
||
padding: 16px;
|
||
}
|
||
}
|
||
</style>
|