1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<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>