gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
261
262
263
264
265
266
267
268
269
270
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesWmMaterialStockApi } from '#/api/mes/wm/materialstock';
 
import { computed, nextTick, ref } from 'vue';
 
import { Alert, message, Modal } from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getMaterialStockPage } from '#/api/mes/wm/materialstock';
import { MdItemTypeTree } from '#/views/mes/md/item/type/components';
 
import { useSelectGridColumns, useSelectGridFormSchema } from '../data';
 
/** 虚拟仓过滤模式:'exclude' 排除虚拟仓(默认),'only' 只看虚拟仓,'all' 不过滤 */
type VirtualFilter = 'all' | 'exclude' | 'only';
 
const props = withDefaults(
  defineProps<{
    batchId?: number;
    itemId?: number;
    mdmItemId?: number;
    virtualFilter?: VirtualFilter;
    warehouseId?: number;
  }>(),
  {
    batchId: undefined,
    itemId: undefined,
    mdmItemId: undefined,
    virtualFilter: 'exclude',
    warehouseId: undefined,
  },
);
 
const emit = defineEmits<{
  selected: [rows: MesWmMaterialStockApi.MaterialStock[]];
}>();
 
const open = ref(false);
const multiple = ref(false); // 是否多选;默认按单选选择器使用
const selectedRows = ref<MesWmMaterialStockApi.MaterialStock[]>([]);
const preSelectedIds = ref<number[]>([]);
const searchItemTypeId = ref<number>();
 
/** 当前 props 是否带预过滤 */
const showAlert = computed(
  () =>
    props.batchId !== null ||
    props.warehouseId !== null ||
    props.virtualFilter === 'only',
);
 
/** 预过滤提示文字 */
const alertTitle = computed(() => {
  const parts: string[] = [];
  if (props.batchId !== null) {
    parts.push('批次');
  }
  if (props.warehouseId !== null) {
    parts.push('仓库');
  }
  if (props.virtualFilter === 'only') {
    parts.push('只看虚拟仓');
  }
  return `已按${parts.join('/')}预过滤`;
});
 
/** 获取多选记录,包含 VXE reserve 跨页记录 */
function getMultipleSelectedRows() {
  const selectedMap = new Map<number, MesWmMaterialStockApi.MaterialStock>();
  const records = [
    ...(gridApi.grid.getCheckboxReserveRecords?.() ?? []),
    ...(gridApi.grid.getCheckboxRecords?.() ?? []),
  ] as MesWmMaterialStockApi.MaterialStock[];
  records.forEach((row) => {
    const rowId = row.id;
    if (rowId !== undefined) {
      selectedMap.set(rowId, row);
    }
  });
  return [...selectedMap.values()];
}
 
/** 处理多选勾选变化 */
function handleCheckboxSelectChange() {
  selectedRows.value = getMultipleSelectedRows();
}
 
/** 处理单选切换 */
function handleRadioChange(row: MesWmMaterialStockApi.MaterialStock) {
  selectedRows.value = [row];
}
 
/** 多选模式下切换行勾选 */
async function toggleMultipleRow(row: MesWmMaterialStockApi.MaterialStock) {
  const selected = gridApi.grid.isCheckedByCheckboxRow(row);
  await gridApi.grid.setCheckboxRow(row, !selected);
  selectedRows.value = getMultipleSelectedRows();
}
 
/** 双击行:单选直接确认;多选切换勾选 */
async function handleRowDblclick({
  row,
}: {
  row: MesWmMaterialStockApi.MaterialStock;
}) {
  if (multiple.value) {
    await toggleMultipleRow(row);
    return;
  }
  selectedRows.value = [row];
  await gridApi.grid.setRadioRow(row);
  handleConfirm();
}
 
/** 回显预选 */
async function applyPreSelection() {
  if (preSelectedIds.value.length === 0) {
    return;
  }
  const rows = gridApi.grid.getData() as MesWmMaterialStockApi.MaterialStock[];
  for (const row of rows) {
    if (
      row.id === undefined ||
      row.id === null ||
      !preSelectedIds.value.includes(row.id)
    ) {
      continue;
    }
    if (multiple.value) {
      await gridApi.grid.setCheckboxRow(row, true);
    } else {
      await gridApi.grid.setRadioRow(row);
      selectedRows.value = [row];
      return;
    }
  }
  if (multiple.value) {
    selectedRows.value = getMultipleSelectedRows();
  }
}
 
const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useSelectGridFormSchema(),
  },
  gridOptions: {
    columns: useSelectGridColumns(false),
    height: 480,
    keepSource: true,
    checkboxConfig: { highlight: true, range: true, reserve: true },
    radioConfig: { highlight: true, trigger: 'row' },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getMaterialStockPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
            batchId: props.batchId,
            // 默认查未冻结
            frozen: false,
            itemTypeId: searchItemTypeId.value,
            itemId: props.mdmItemId,
            warehouseId: formValues.warehouseId ?? props.warehouseId,
            virtualFilter:
              props.virtualFilter === 'all' ? undefined : props.virtualFilter,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<MesWmMaterialStockApi.MaterialStock>,
  gridEvents: {
    cellDblclick: handleRowDblclick,
    checkboxAll: handleCheckboxSelectChange,
    checkboxChange: handleCheckboxSelectChange,
    radioChange: ({ row }: { row: MesWmMaterialStockApi.MaterialStock }) => {
      handleRadioChange(row);
    },
  },
});
 
/** 物料分类树节点点击 */
function handleTypeNodeClick(row: undefined | { id?: number }) {
  searchItemTypeId.value = row?.id;
  gridApi.query();
}
 
/** 重置查询和选择状态 */
async function resetQueryState() {
  selectedRows.value = [];
  searchItemTypeId.value = undefined;
  await gridApi.grid.clearCheckboxRow();
  await gridApi.grid.clearCheckboxReserve();
  await gridApi.grid.clearRadioRow();
  await gridApi.formApi.resetForm();
}
 
/** 打开弹窗 */
async function openModal(
  selectedIds?: number[],
  options?: { multiple?: boolean },
) {
  open.value = true;
  multiple.value = options?.multiple ?? false;
  preSelectedIds.value = selectedIds || [];
  await nextTick();
  gridApi.setGridOptions({
    columns: useSelectGridColumns(multiple.value),
  });
  await resetQueryState();
  await gridApi.query();
  await nextTick();
  await applyPreSelection();
}
 
/** 关闭弹窗 */
async function closeModal() {
  open.value = false;
  await resetQueryState();
}
 
/** 确认选择 */
function handleConfirm() {
  const rows = multiple.value ? getMultipleSelectedRows() : selectedRows.value;
  if (rows.length === 0) {
    message.warning(multiple.value ? '请至少选择一条数据' : '请选择一条数据');
    return;
  }
  emit('selected', multiple.value ? rows : [rows[0]!]);
  open.value = false;
}
 
defineExpose({ open: openModal });
</script>
 
<template>
  <Modal
    v-model:open="open"
    :destroy-on-close="true"
    title="库存物资选择"
    width="80%"
    @cancel="closeModal"
    @ok="handleConfirm"
  >
    <Alert
      v-if="showAlert"
      class="mb-3"
      :message="alertTitle"
      show-icon
      type="info"
    />
    <div class="flex gap-3">
      <div class="bg-card w-1/6 rounded p-2">
        <MdItemTypeTree @node-click="handleTypeNodeClick" />
      </div>
      <div class="w-5/6">
        <Grid table-title="库存列表" />
      </div>
    </div>
  </Modal>
</template>