<template>
|
<div class="device-site-page art-full-height">
|
<ArtSearchBar
|
v-model="searchForm"
|
:items="searchItems"
|
:showExpand="true"
|
@search="handleSearch"
|
@reset="handleReset"
|
/>
|
|
<ElCard class="art-table-card">
|
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
|
<template #left>
|
<ElSpace wrap>
|
<ElButton v-auth="'add'" @click="showDialog('add')" v-ripple>新增路径</ElButton>
|
<ElButton v-auth="'add'" @click="openInitDialog()" v-ripple>路径初始化</ElButton>
|
<ElButton
|
v-auth="'delete'"
|
type="danger"
|
:disabled="selectedRows.length === 0"
|
@click="handleBatchDelete"
|
v-ripple
|
>
|
批量删除
|
</ElButton>
|
<ListExportPrint
|
class="inline-flex"
|
:preview-visible="previewVisible"
|
@update:previewVisible="handlePreviewVisibleChange"
|
:report-title="reportTitle"
|
:selected-rows="selectedRows"
|
:query-params="reportQueryParams"
|
:columns="columns"
|
:preview-rows="previewRows"
|
:preview-meta="resolvedPreviewMeta"
|
:total="pagination.total"
|
:disabled="loading"
|
@export="handleExport"
|
@print="handlePrint"
|
/>
|
</ElSpace>
|
</template>
|
</ArtTableHeader>
|
|
<ArtTable
|
:loading="loading"
|
:data="data"
|
:columns="columns"
|
:pagination="pagination"
|
@selection-change="handleSelectionChange"
|
@pagination:size-change="handleSizeChange"
|
@pagination:current-change="handleCurrentChange"
|
/>
|
|
<DeviceSiteDialog
|
v-model:visible="dialogVisible"
|
:dialog-type="dialogType"
|
:device-site-data="currentDeviceSiteData"
|
:type-options="typeOptions"
|
:device-options="deviceOptions"
|
:area-options="areaOptions"
|
@submit="handleDialogSubmit"
|
/>
|
|
<DeviceSiteDetailDrawer
|
v-model:visible="detailDrawerVisible"
|
:loading="detailLoading"
|
:detail="detailData"
|
/>
|
|
<DeviceSiteInitDialog
|
v-model:visible="initDialogVisible"
|
:initial-data="currentInitData"
|
:type-options="typeOptions"
|
:device-options="deviceOptions"
|
:area-options="areaOptions"
|
:station-options="stationOptions"
|
@submit="handleInitSubmit"
|
/>
|
</ElCard>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed, onMounted, ref } from 'vue'
|
import { ElMessage } from 'element-plus'
|
import { useUserStore } from '@/store/modules/user'
|
import { useAuth } from '@/hooks/core/useAuth'
|
import { useTable } from '@/hooks/core/useTable'
|
import { useCrudPage } from '@/views/system/common/useCrudPage'
|
import { usePrintExportPage } from '@/views/system/common/usePrintExportPage'
|
import ListExportPrint from '@/components/biz/list-export-print/index.vue'
|
import { defaultResponseAdapter } from '@/utils/table/tableUtils'
|
import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
|
import { fetchBasStationOptionPage } from '@/api/bas-station'
|
import { fetchWarehouseAreasList } from '@/api/warehouse-areas'
|
import { fetchDictDataPage } from '@/api/system-manage'
|
import {
|
fetchDeleteDeviceSite,
|
fetchDeviceSitePage,
|
fetchExportDeviceSiteReport,
|
fetchGetDeviceSiteDetail,
|
fetchGetDeviceSiteMany,
|
fetchInitDeviceSite,
|
fetchSaveDeviceSite,
|
fetchUpdateDeviceSite
|
} from '@/api/device-site'
|
import DeviceSiteDialog from './modules/device-site-dialog.vue'
|
import DeviceSiteDetailDrawer from './modules/device-site-detail-drawer.vue'
|
import DeviceSiteInitDialog from './modules/device-site-init-dialog.vue'
|
import { createDeviceSiteTableColumns } from './deviceSiteTable.columns'
|
import {
|
DEVICE_SITE_REPORT_STYLE,
|
DEVICE_SITE_REPORT_TITLE,
|
buildDeviceSiteDialogModel,
|
buildDeviceSiteInitModel,
|
buildDeviceSiteInitPayload,
|
buildDeviceSitePageQueryParams,
|
buildDeviceSitePrintRows,
|
buildDeviceSiteReportMeta,
|
buildDeviceSiteSavePayload,
|
buildDeviceSiteSearchParams,
|
createDeviceSiteSearchState,
|
getDeviceSitePaginationKey,
|
normalizeDeviceSiteListRow,
|
resolveDeviceSiteAreaOptions,
|
resolveDeviceSiteDeviceOptions,
|
resolveDeviceSiteTypeOptions
|
} from './deviceSitePage.helpers'
|
|
defineOptions({ name: 'DeviceSite' })
|
|
const { hasAuth } = useAuth()
|
const userStore = useUserStore()
|
|
const searchForm = ref(createDeviceSiteSearchState())
|
const typeOptions = ref([])
|
const deviceOptions = ref([])
|
const areaOptions = ref([])
|
const stationOptions = ref([])
|
const stationOptionsLoaded = ref(false)
|
const stationOptionsLoading = ref(null)
|
const detailDrawerVisible = ref(false)
|
const detailLoading = ref(false)
|
const detailData = ref({})
|
const initDialogVisible = ref(false)
|
const currentInitData = ref(buildDeviceSiteInitModel())
|
let handleDeleteAction = null
|
|
const reportTitle = DEVICE_SITE_REPORT_TITLE
|
const reportQueryParams = computed(() => buildDeviceSiteSearchParams(searchForm.value))
|
const typeLabelMap = computed(
|
() =>
|
new Map(typeOptions.value.map((item) => [String(item.value), item.label]).filter(([value, label]) => value && label))
|
)
|
const deviceLabelMap = computed(
|
() =>
|
new Map(deviceOptions.value.map((item) => [String(item.value), item.label]).filter(([value, label]) => value && label))
|
)
|
const areaLabelMap = computed(
|
() =>
|
new Map(areaOptions.value.map((item) => [String(item.value), item.label]).filter(([value, label]) => value && label))
|
)
|
|
function resolveTypeLabel(value) {
|
return typeLabelMap.value.get(String(value)) || ''
|
}
|
|
function resolveDeviceLabel(value) {
|
return deviceLabelMap.value.get(String(value)) || ''
|
}
|
|
function resolveAreaLabel(value) {
|
return areaLabelMap.value.get(String(value)) || ''
|
}
|
|
const searchItems = computed(() => [
|
{ label: '关键字', key: 'condition', type: 'input', props: { clearable: true, placeholder: '请输入站点/名称/目标/标签' } },
|
{
|
label: '站点类型',
|
key: 'type',
|
type: 'select',
|
props: { clearable: true, filterable: true, multiple: true, collapseTags: true, options: typeOptions.value }
|
},
|
{ label: '作业站点', key: 'site', type: 'input', props: { clearable: true, placeholder: '请输入作业站点' } },
|
{ label: '名称', key: 'name', type: 'input', props: { clearable: true, placeholder: '请输入名称' } },
|
{ label: '目标站点', key: 'target', type: 'input', props: { clearable: true, placeholder: '请输入目标站点' } },
|
{ label: '站点标签', key: 'label', type: 'input', props: { clearable: true, placeholder: '请输入站点标签' } },
|
{ label: '设备类型', key: 'device', type: 'select', props: { clearable: true, filterable: true, options: deviceOptions.value } },
|
{ label: '设备编号', key: 'deviceCode', type: 'input', props: { clearable: true, placeholder: '请输入设备编号' } },
|
{ label: '设备站点', key: 'deviceSite', type: 'input', props: { clearable: true, placeholder: '请输入设备站点' } },
|
{ label: '巷道', key: 'channel', type: 'number', props: { min: 0, controlsPosition: 'right', placeholder: '请输入巷道' } },
|
{ label: '源库区', key: 'areaIdStart', type: 'number', props: { min: 0, controlsPosition: 'right', placeholder: '请输入源库区' } },
|
{ label: '目标库区', key: 'areaIdEnd', type: 'number', props: { min: 0, controlsPosition: 'right', placeholder: '请输入目标库区' } },
|
{ label: '状态', key: 'status', type: 'select', props: { clearable: true, options: [{ label: '正常', value: 1 }, { label: '冻结', value: 0 }] } },
|
{ label: '备注', key: 'memo', type: 'input', props: { clearable: true, placeholder: '请输入备注' } }
|
])
|
|
function createInitRecordFromRow(row) {
|
return buildDeviceSiteInitModel({
|
deviceType: row.device || '',
|
typeIds: row.type || row.typeText || '',
|
channel: row.channel,
|
areaIdStart: row.areaIdStart,
|
areaIdEnd: row.areaIdEnd,
|
name: row.name,
|
label: row.label,
|
rows: [{ deviceSiteName: row.deviceSite || '', siteName: row.site || '', target: row.target || '' }]
|
})
|
}
|
|
async function openDetail(row) {
|
detailDrawerVisible.value = true
|
detailLoading.value = true
|
try {
|
const detail = await guardRequestWithMessage(fetchGetDeviceSiteDetail(row.id), {}, {
|
timeoutMessage: '路径详情加载超时,已停止等待'
|
})
|
detailData.value = normalizeDeviceSiteListRow(detail, resolveTypeLabel, resolveDeviceLabel, resolveAreaLabel)
|
} catch (error) {
|
detailDrawerVisible.value = false
|
detailData.value = {}
|
ElMessage.error(error?.message || '获取路径详情失败')
|
} finally {
|
detailLoading.value = false
|
}
|
}
|
|
async function openEditDialog(row) {
|
try {
|
const detail = await guardRequestWithMessage(fetchGetDeviceSiteDetail(row.id), {}, {
|
timeoutMessage: '路径详情加载超时,已停止等待'
|
})
|
showDialog('edit', detail)
|
} catch (error) {
|
ElMessage.error(error?.message || '获取路径详情失败')
|
}
|
}
|
|
async function ensureStationOptions() {
|
if (stationOptionsLoaded.value) {
|
return
|
}
|
|
if (stationOptionsLoading.value) {
|
await stationOptionsLoading.value
|
return
|
}
|
|
stationOptionsLoading.value = (async () => {
|
const response = await guardRequestWithMessage(
|
fetchBasStationOptionPage(
|
{
|
current: 1,
|
pageSize: 100
|
},
|
{
|
showErrorMessage: false
|
}
|
),
|
{ records: [] },
|
{
|
timeoutMessage: '站点列表加载超时,已停止等待'
|
}
|
)
|
stationOptions.value = buildSelectOptions(defaultResponseAdapter(response).records, ['stationName', 'name'])
|
stationOptionsLoaded.value = true
|
})()
|
|
try {
|
await stationOptionsLoading.value
|
} finally {
|
stationOptionsLoading.value = null
|
}
|
}
|
|
async function openInitDialog(record = null) {
|
await ensureStationOptions()
|
currentInitData.value = record ? createInitRecordFromRow(record) : buildDeviceSiteInitModel()
|
initDialogVisible.value = true
|
}
|
|
const {
|
columns,
|
columnChecks,
|
data,
|
loading,
|
pagination,
|
getData,
|
replaceSearchParams,
|
resetSearchParams,
|
handleSizeChange,
|
handleCurrentChange,
|
refreshData,
|
refreshCreate,
|
refreshUpdate,
|
refreshRemove
|
} = useTable({
|
core: {
|
apiFn: fetchDeviceSitePage,
|
apiParams: buildDeviceSitePageQueryParams(searchForm.value),
|
paginationKey: getDeviceSitePaginationKey(),
|
columnsFactory: () =>
|
createDeviceSiteTableColumns({
|
handleView: openDetail,
|
handleInit: hasAuth('add') ? openInitDialog : null,
|
handleEdit: hasAuth('update') ? openEditDialog : null,
|
handleDelete: hasAuth('delete') ? (row) => handleDeleteAction?.(row) : null,
|
canEdit: hasAuth('update'),
|
canDelete: hasAuth('delete')
|
})
|
},
|
transform: {
|
dataTransformer: (records) => {
|
if (!Array.isArray(records)) {
|
return []
|
}
|
return records.map((item) => normalizeDeviceSiteListRow(item, resolveTypeLabel, resolveDeviceLabel, resolveAreaLabel))
|
}
|
}
|
})
|
|
const {
|
dialogVisible,
|
dialogType,
|
currentRecord: currentDeviceSiteData,
|
selectedRows,
|
handleSelectionChange,
|
showDialog,
|
handleDialogSubmit,
|
handleDelete,
|
handleBatchDelete
|
} = useCrudPage({
|
createEmptyModel: () => buildDeviceSiteDialogModel(),
|
buildEditModel: (record) => buildDeviceSiteDialogModel(record),
|
buildSavePayload: (formData) => buildDeviceSiteSavePayload(formData),
|
saveRequest: fetchSaveDeviceSite,
|
updateRequest: fetchUpdateDeviceSite,
|
deleteRequest: fetchDeleteDeviceSite,
|
entityName: '路径',
|
resolveRecordLabel: (record) => record?.name || record?.site || record?.deviceSite || record?.id,
|
refreshCreate,
|
refreshUpdate,
|
refreshRemove
|
})
|
handleDeleteAction = handleDelete
|
|
const buildPreviewMeta = (rows) => {
|
const now = new Date()
|
return {
|
reportDate: now.toLocaleDateString('zh-CN'),
|
printedAt: now.toLocaleString('zh-CN', { hour12: false }),
|
operator: userStore.getUserInfo?.name || userStore.getUserInfo?.username || '',
|
count: rows.length,
|
reportStyle: { ...DEVICE_SITE_REPORT_STYLE }
|
}
|
}
|
|
const resolvePrintRecords = async (payload) => {
|
if (Array.isArray(payload?.ids) && payload.ids.length > 0) {
|
return defaultResponseAdapter(await fetchGetDeviceSiteMany(payload.ids)).records
|
}
|
return defaultResponseAdapter(
|
await fetchDeviceSitePage({
|
...reportQueryParams.value,
|
current: 1,
|
pageSize: Number(pagination.total) > 0 ? Number(pagination.total) : Number(payload?.pageSize) || 20
|
})
|
).records
|
}
|
|
const { previewVisible, previewRows, previewMeta, handlePreviewVisibleChange, handleExport, handlePrint } =
|
usePrintExportPage({
|
downloadFileName: 'device-site.xlsx',
|
requestExport: (payload) =>
|
fetchExportDeviceSiteReport(payload, {
|
headers: {
|
Authorization: userStore.accessToken || ''
|
}
|
}),
|
resolvePrintRecords,
|
buildPreviewRows: (records) => buildDeviceSitePrintRows(records, resolveTypeLabel, resolveDeviceLabel, resolveAreaLabel),
|
buildPreviewMeta
|
})
|
|
const resolvedPreviewMeta = computed(() =>
|
buildDeviceSiteReportMeta({
|
previewMeta: previewMeta.value,
|
count: previewRows.value.length,
|
orientation: previewMeta.value?.reportStyle?.orientation || DEVICE_SITE_REPORT_STYLE.orientation
|
})
|
)
|
|
function handleSearch(params) {
|
replaceSearchParams(buildDeviceSiteSearchParams(params))
|
getData()
|
}
|
|
function handleReset() {
|
Object.assign(searchForm.value, createDeviceSiteSearchState())
|
resetSearchParams()
|
}
|
|
async function handleInitSubmit(formData) {
|
try {
|
await fetchInitDeviceSite(buildDeviceSiteInitPayload(formData))
|
ElMessage.success('初始化成功')
|
initDialogVisible.value = false
|
currentInitData.value = buildDeviceSiteInitModel()
|
await refreshData()
|
} catch (error) {
|
ElMessage.error(error?.message || '初始化失败')
|
}
|
}
|
|
function buildSelectOptions(records = [], labelKeys = ['label', 'name', 'stationName']) {
|
if (!Array.isArray(records)) {
|
return []
|
}
|
return records
|
.map((item) => {
|
if (!item || typeof item !== 'object') {
|
return null
|
}
|
const value = item.value ?? item.id ?? item.dictValue
|
if (value === void 0 || value === null || value === '') {
|
return null
|
}
|
const label = labelKeys.map((key) => item[key]).find((value2) => value2 !== void 0 && value2 !== null && String(value2).trim() !== '')
|
return { value: String(value), label: String(label || value).trim() }
|
})
|
.filter(Boolean)
|
}
|
|
async function loadTypeOptions() {
|
const response = await guardRequestWithMessage(
|
fetchDictDataPage({ current: 1, pageSize: 200, dictTypeCode: 'sys_task_type', status: 1 }),
|
{ records: [] },
|
{ timeoutMessage: '站点类型加载超时,已停止等待' }
|
)
|
typeOptions.value = resolveDeviceSiteTypeOptions(defaultResponseAdapter(response).records)
|
}
|
|
async function loadDeviceOptions() {
|
const response = await guardRequestWithMessage(
|
fetchDictDataPage({ current: 1, pageSize: 200, dictTypeCode: 'sys_device_type', status: 1 }),
|
{ records: [] },
|
{ timeoutMessage: '设备类型加载超时,已停止等待' }
|
)
|
deviceOptions.value = resolveDeviceSiteDeviceOptions(defaultResponseAdapter(response).records)
|
}
|
|
async function loadAreaOptions() {
|
const response = await guardRequestWithMessage(fetchWarehouseAreasList(), [], {
|
timeoutMessage: '库区加载超时,已停止等待'
|
})
|
areaOptions.value = resolveDeviceSiteAreaOptions(defaultResponseAdapter(response).records)
|
}
|
|
onMounted(async () => {
|
await Promise.all([loadTypeOptions(), loadDeviceOptions(), loadAreaOptions()])
|
})
|
</script>
|