2026-06-26 16:11:03 +08:00

458 lines
13 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.

<template>
<div class="package-supplier-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>
<el-button type="success" size="small" @click="openCreate">新增供应商</el-button>
</div>
<div class="upper-wrapper">
<upper-table
ref="upperTable"
title="包装供应商列表"
default-align="center"
table-id="material-package-supplier"
: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-status="{ row }">
<el-tag :type="formatPackageSupplierStatus(row.status).type" size="small">
{{ formatPackageSupplierStatus(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="560px"
align-center
@close="resetForm"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="110px">
<el-form-item label="供应商编码" prop="supplier_code">
<el-input
v-model="formData.supplier_code"
placeholder="大写字母/数字,仓库内唯一"
:disabled="dialogMode === 'edit'"
@blur="normalizeSupplierCode"
/>
</el-form-item>
<el-form-item label="供应商名称" prop="supplier_name">
<el-input v-model="formData.supplier_name" placeholder="请输入供应商名称" />
</el-form-item>
<el-form-item label="联系人" prop="contact_name">
<el-input v-model="formData.contact_name" placeholder="选填" clearable />
</el-form-item>
<el-form-item label="联系电话" prop="contact_phone">
<el-input v-model="formData.contact_phone" placeholder="选填" clearable />
</el-form-item>
<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-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="submitForm">保存</el-button>
</template>
</el-dialog>
</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 {
packageSupplierCreate,
packageSupplierDelete,
packageSupplierGet,
packageSupplierUpdate
} from '@/api/setting.packagesupplier/packagesupplier'
import { useSettingsStore } from '@/stores/modules/settings'
import {
buildPackageSupplierListPayload,
parsePackageSupplierListResponse,
formatPackageSupplierStatus,
normalizePackageSupplierScanConditionToWhere,
packageSupplierColumns,
packageSupplierScanFields,
type PackageSupplierRecord
} from './package-supplier-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(packageSupplierScanFields)))
const shortcutExpanded = computed(() => settingsStore.shortcutExpanded)
const containerHeight = computed(() =>
shortcutExpanded.value ? 'calc(100vh - 150px)' : 'calc(100vh - 100px)'
)
const columns = packageSupplierColumns
const loading = ref(false)
const saving = ref(false)
const batchDeleting = ref(false)
const selectedIds = ref<number[]>([])
const tableData = ref<PackageSupplierRecord[]>([])
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 formRef = ref<FormInstance>()
const formData = reactive({
id: undefined as number | undefined,
supplier_code: '',
supplier_name: '',
contact_name: '',
contact_phone: '',
status: 1
})
const rules: FormRules = {
supplier_code: [{ required: true, message: '请输入供应商编码', trigger: 'blur' }],
supplier_name: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }]
}
const isSuccess = (res: any) => res && [0, 1, 200].includes(res.code)
function normalizeSupplierCode() {
formData.supplier_code = String(formData.supplier_code || '').trim().toUpperCase()
}
function onSelectionChange(rows: PackageSupplierRecord[]) {
selectedIds.value = (rows || [])
.map((r) => r.id)
.filter((id): id is number => id != null && Number.isFinite(Number(id)))
.map((id) => Number(id))
}
function buildCreatePayload() {
const payload: Record<string, unknown> = {
supplier_code: formData.supplier_code.trim(),
supplier_name: formData.supplier_name.trim(),
status: formData.status
}
const contactName = formData.contact_name.trim()
const contactPhone = formData.contact_phone.trim()
if (contactName) payload.contact_name = contactName
if (contactPhone) payload.contact_phone = contactPhone
return payload
}
function buildUpdatePayload() {
if (!formData.id) return null
const payload: Record<string, unknown> = { id: formData.id }
const supplierName = formData.supplier_name.trim()
if (supplierName) payload.supplier_name = supplierName
payload.contact_name = formData.contact_name.trim()
payload.contact_phone = formData.contact_phone.trim()
payload.status = formData.status
return payload
}
async function loadList() {
if (loading.value) return
loading.value = true
const start = Date.now()
try {
const where = normalizePackageSupplierScanConditionToWhere(currentConditionRaw.value)
const res: any = await packageSupplierGet(
buildPackageSupplierListPayload({
page: pagination.currentPage,
pageSize: pagination.pageSize,
where
})
)
if (!isSuccess(res)) {
ElMessage.error(res?.message || '获取供应商列表失败')
tableData.value = []
pagination.total = 0
return
}
const data = parsePackageSupplierListResponse(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.supplier_code = ''
formData.supplier_name = ''
formData.contact_name = ''
formData.contact_phone = ''
formData.status = 1
formRef.value?.clearValidate()
}
function openCreate() {
dialogMode.value = 'create'
dialogVisible.value = true
resetForm()
}
function openEdit(row: PackageSupplierRecord) {
if (!row.id) return
dialogMode.value = 'edit'
dialogVisible.value = true
formData.id = row.id
formData.supplier_code = row.supplier_code || ''
formData.supplier_name = row.supplier_name || ''
formData.contact_name = row.contact_name || ''
formData.contact_phone = row.contact_phone || ''
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
normalizeSupplierCode()
saving.value = true
try {
if (dialogMode.value === 'create') {
const res: any = await packageSupplierCreate(buildCreatePayload() as any)
if (!isSuccess(res)) {
ElMessage.error(res?.message || '新增失败')
return
}
ElMessage.success('新增成功')
} else {
const payload = buildUpdatePayload()
if (!payload) return
const res: any = await packageSupplierUpdate(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 packageSupplierDelete({ ids })
if (!isSuccess(res)) {
ElMessage.error(res?.message || '删除失败')
return false
}
ElMessage.success('删除成功')
return true
}
function handleDelete(row: PackageSupplierRecord) {
if (!row.id) return
const name = row.supplier_name || row.supplier_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)
}
onMounted(() => {
loadList()
})
</script>
<style lang="scss" scoped>
.package-supplier-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;
}
.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>