<script lang="ts" setup>
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
import type { MdmItemApi } from '#/api/mdm/item';
|
import type { MesMdItemTypeApi } from '#/api/mes/md/item/type';
|
|
import { computed, ref } from 'vue';
|
|
import { confirm, Page, useVbenModal } from '#/packages/effects/common-ui/src';
|
import { downloadFileFromBlobPart, isEmpty } from '#/packages/utils/src';
|
|
import { message, Tag } from 'ant-design-vue';
|
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
import {
|
deleteItem,
|
deleteItemList,
|
// exportItem,
|
getItemPage,
|
updateItemStatus,
|
} from '#/api/mdm/item';
|
import { getItemTypeSimpleList } from '#/api/mes/md/item/type';
|
import { $t } from '#/locales';
|
|
import { useGridColumns, useGridFormSchema } from './data';
|
import Form from './modules/form.vue';
|
|
defineOptions({ name: 'MdmItem' });
|
|
const [FormModal, formModalApi] = useVbenModal({
|
connectedComponent: Form,
|
destroyOnClose: true,
|
});
|
|
// 分类列表缓存
|
const categoryList = ref<MesMdItemTypeApi.ItemType[]>([]);
|
|
const TAG_COLORS = [
|
'green',
|
'orange',
|
'blue',
|
'purple',
|
'cyan',
|
'magenta',
|
'geekblue',
|
'volcano',
|
'gold',
|
'lime',
|
] as const;
|
|
/** 分类ID → 颜色映射(基于分类在列表中的索引循环分配) */
|
const categoryColorMap = computed(() => {
|
const map = new Map<number, string>();
|
categoryList.value.forEach((item, index) => {
|
map.set(item.id!, TAG_COLORS[index % TAG_COLORS.length]!);
|
});
|
return map;
|
});
|
|
/** 获取分类Tag颜色 */
|
function getCategoryColor(categoryId?: number): string {
|
if (!categoryId) return '';
|
return categoryColorMap.value.get(categoryId) || '';
|
}
|
|
/** 获取分类名称 */
|
function getCategoryName(categoryId?: number) {
|
if (!categoryId || !categoryList.value.length) return '';
|
const category = categoryList.value.find((item) => item.id === categoryId);
|
return category?.name || '';
|
}
|
|
/** 刷新表格 */
|
function handleRefresh() {
|
gridApi.query();
|
}
|
|
/** 导出表格 */
|
// async function handleExport() {
|
// const data = await exportItem(await gridApi.formApi.getValues());
|
// downloadFileFromBlobPart({ fileName: '物料.xls', source: data });
|
// }
|
|
/** 创建物料 */
|
function handleCreate() {
|
formModalApi.setData(null).open();
|
}
|
|
/** 编辑物料 */
|
function handleEdit(row: MdmItemApi.Item) {
|
formModalApi.setData(row).open();
|
}
|
|
/** 删除物料 */
|
async function handleDelete(row: MdmItemApi.Item) {
|
const hideLoading = message.loading({
|
content: $t('ui.actionMessage.deleting', [row.name]),
|
duration: 0,
|
});
|
try {
|
await deleteItem(row.id!);
|
message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
|
handleRefresh();
|
} finally {
|
hideLoading();
|
}
|
}
|
|
/** 批量删除物料 */
|
async function handleDeleteBatch() {
|
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
|
const hideLoading = message.loading({
|
content: $t('ui.actionMessage.deletingBatch'),
|
duration: 0,
|
});
|
try {
|
await deleteItemList(checkedIds.value);
|
checkedIds.value = [];
|
message.success($t('ui.actionMessage.deleteSuccess'));
|
handleRefresh();
|
} finally {
|
hideLoading();
|
}
|
}
|
|
/** 更新状态 */
|
async function handleStatusChange(row: MdmItemApi.Item, status: boolean) {
|
const newStatus = status ? 0 : 1;
|
const hideLoading = message.loading({
|
content: '正在更新状态...',
|
duration: 0,
|
});
|
try {
|
await updateItemStatus(row.id!, newStatus);
|
message.success('状态更新成功');
|
handleRefresh();
|
} finally {
|
hideLoading();
|
}
|
}
|
|
const checkedIds = ref<number[]>([]);
|
function handleRowCheckboxChange({
|
records,
|
}: {
|
records: MdmItemApi.Item[];
|
}) {
|
checkedIds.value = records.map((item) => item.id!);
|
}
|
|
const [Grid, gridApi] = useVbenVxeGrid({
|
formOptions: {
|
schema: useGridFormSchema(),
|
},
|
gridOptions: {
|
columns: useGridColumns(),
|
height: 'auto',
|
keepSource: true,
|
proxyConfig: {
|
ajax: {
|
query: async ({ page }, formValues) => {
|
// 加载分类列表(用于显示分类名称)
|
if (!categoryList.value.length) {
|
try {
|
categoryList.value = await getItemTypeSimpleList();
|
} catch {
|
categoryList.value = [];
|
}
|
}
|
return await getItemPage({
|
pageNo: page.currentPage,
|
pageSize: page.pageSize,
|
...formValues,
|
});
|
},
|
},
|
},
|
rowConfig: {
|
keyField: 'id',
|
isHover: true,
|
},
|
toolbarConfig: {
|
refresh: true,
|
search: true,
|
},
|
} as VxeTableGridOptions<MdmItemApi.Item>,
|
gridEvents: {
|
checkboxAll: handleRowCheckboxChange,
|
checkboxChange: handleRowCheckboxChange,
|
},
|
});
|
</script>
|
|
<template>
|
<Page auto-content-height>
|
<FormModal @success="handleRefresh" />
|
<Grid table-title="物料列表">
|
<template #toolbar-tools>
|
<TableAction
|
:actions="[
|
{
|
label: $t('ui.actionTitle.create', ['物料']),
|
type: 'primary',
|
icon: ACTION_ICON.ADD,
|
auth: ['mdm:item:create'],
|
onClick: handleCreate,
|
},
|
// {
|
// label: $t('ui.actionTitle.export'),
|
// type: 'primary',
|
// icon: ACTION_ICON.DOWNLOAD,
|
// auth: ['mdm:item:export'],
|
// onClick: handleExport,
|
// },
|
{
|
label: $t('ui.actionTitle.deleteBatch'),
|
type: 'primary',
|
danger: true,
|
icon: ACTION_ICON.DELETE,
|
auth: ['mdm:item:delete'],
|
disabled: isEmpty(checkedIds),
|
onClick: handleDeleteBatch,
|
},
|
]"
|
/>
|
</template>
|
<template #categoryName="{ row }">
|
<Tag v-if="getCategoryColor(row.categoryId)" :color="getCategoryColor(row.categoryId)">{{ getCategoryName(row.categoryId) }}</Tag>
|
<span v-else>{{ getCategoryName(row.categoryId) }}</span>
|
</template>
|
<template #isBatchManaged="{ row }">
|
<Tag v-if="row.isBatchManaged" color="success">是</Tag>
|
<Tag v-else>否</Tag>
|
</template>
|
<template #actions="{ row }">
|
<TableAction
|
:actions="[
|
{
|
label: $t('common.edit'),
|
type: 'link',
|
icon: ACTION_ICON.EDIT,
|
auth: ['mdm:item:update'],
|
onClick: handleEdit.bind(null, row),
|
},
|
{
|
label: $t('common.delete'),
|
type: 'link',
|
danger: true,
|
icon: ACTION_ICON.DELETE,
|
auth: ['mdm:item:delete'],
|
popConfirm: {
|
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
|
confirm: handleDelete.bind(null, row),
|
},
|
},
|
]"
|
/>
|
</template>
|
</Grid>
|
</Page>
|
</template>
|