gaoluyang
2026-11-02 2fe330a44f9e8bdd8a1afd6124c57851f2a5b7ea
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<script lang="ts" setup>
import type { MesWmSortTaskApi } from '#/api/mes/wm/sorting';
 
import { computed, h, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import type { ColumnsType } from 'ant-design-vue/es/table/interface';
 
import {
  Button,
  Input,
  InputNumber,
  message,
  Modal as AntModal,
  Table,
  Tag,
} from 'ant-design-vue';
 
import {
  finishSortTask,
  getSortTask,
  scanSortLine,
  submitSortProductionRequest,
} from '#/api/mes/wm/sorting';
 
import { SORT_TASK_STATUS } from '../data';
 
defineOptions({ name: 'MesWmSortTaskSortModal' });
 
const emit = defineEmits<{ success: [] }>();
 
const detail = ref<MesWmSortTaskApi.SortTask>({});
/** 每行的扫码输入 */
const scanBarcode = ref<Record<string, string>>({});
/** 每行的本次分拣数量(留空则按剩余数量) */
const scanQuantity = ref<Record<string, number | undefined>>({});
const carrier = ref('');
const shippingNumber = ref('');
 
const lines = computed<MesWmSortTaskApi.SortTaskLine[]>(
  () => detail.value.lines ?? [],
);
 
/** 是否可执行分拣(待分拣 / 分拣中) */
const canSort = computed(
  () =>
    detail.value.status === SORT_TASK_STATUS.PENDING ||
    detail.value.status === SORT_TASK_STATUS.SORTING,
);
 
/** 未完成的行 */
const unfinishedLines = computed(() =>
  lines.value.filter(
    (line) =>
      !line.scanned ||
      Number(line.sortedQuantity ?? 0) < Number(line.planQuantity ?? 0),
  ),
);
 
/** 是否全部扫码完成 */
const allSorted = computed(
  () => lines.value.length > 0 && unfinishedLines.value.length === 0,
);
 
/** 剩余待分拣数量 */
function remainQuantity(line: MesWmSortTaskApi.SortTaskLine) {
  return Math.max(
    Number(line.planQuantity ?? 0) - Number(line.sortedQuantity ?? 0),
    0,
  );
}
 
/** 货位指引文案 */
function locationText(line: MesWmSortTaskApi.SortTaskLine) {
  const parts = [line.warehouseName, line.areaName, line.locationName].filter(
    Boolean,
  );
  return parts.length > 0 ? parts.join(' / ') : '-';
}
 
/** 重新加载任务详情 */
async function reloadDetail() {
  if (!detail.value.id) {
    return;
  }
  detail.value = await getSortTask(detail.value.id);
}
 
/** 扫码:校验条码并累加分拣数量 */
async function handleScan(line: MesWmSortTaskApi.SortTaskLine) {
  const barcode = (scanBarcode.value[line.id!] ?? '').trim();
  if (!barcode) {
    message.warning(`请先扫描【${line.itemName}】的物料条码或批次号`);
    return;
  }
  const quantity =
    scanQuantity.value[line.id!] ?? (remainQuantity(line) || undefined);
  try {
    await scanSortLine({
      taskId: detail.value.id!,
      lineId: line.id!,
      barcode,
      quantity,
    });
    scanBarcode.value[line.id!] = '';
    scanQuantity.value[line.id!] = undefined;
    await reloadDetail();
    message.success(`【${line.itemName}】扫码成功,已分拣 ${quantity}`);
    emit('success');
  } catch (error: any) {
    // 后端已给出「条码 → 期望条码/批次」的对比说明,直接展示
    message.error(error?.message || '扫码校验失败');
  }
}
 
/** 完成分拣并触发配送 */
async function handleFinish() {
  if (!allSorted.value) {
    AntModal.warning({
      title: '还不能完成分拣',
      content: `以下明细未扫码或未分拣到计划数量:${unfinishedLines.value
        .map(
          (line) =>
            `${line.itemName}(计划 ${line.planQuantity ?? 0},已分拣 ${
              line.sortedQuantity ?? 0
            })`,
        )
        .join(';')}。请逐条扫码完成后再次提交。`,
    });
    return;
  }
  AntModal.confirm({
    title: '确认完成分拣并触发配送?',
    content:
      '完成后将自动生成对应的销售出库单(配送出库),并记录承运信息,任务不可再修改。',
    onOk: async () => {
      await finishSortTask({
        id: detail.value.id!,
        carrier: carrier.value,
        shippingNumber: shippingNumber.value,
      });
      message.success('分拣完成,已生成销售出库单');
      emit('success');
      await modalApi.close();
    },
  });
}
 
/** 提交生产申请 */
async function handleSubmitProductionRequest() {
  const result = await submitSortProductionRequest(detail.value.id!);
  const failures = result.failures ?? [];
  if (failures.length > 0) {
    AntModal.warning({
      title: `生产申请部分未提交成功(成功 ${result.createdCount ?? 0} 条)`,
      content: h(
        'div',
        failures.map((item) => h('div', item)),
      ),
    });
  } else if ((result.createdCount ?? 0) === 0) {
    message.info(
      `本次没有新增生产申请:${result.skippedCount ?? 0} 条明细已有生产申请,可在「主生产计划」中查看并下发。`,
    );
  } else {
    message.success(
      `已提交 ${result.createdCount} 条生产申请(MPS 草稿),可在「主生产计划」中审核下发`,
    );
  }
  emit('success');
}
 
const columns: ColumnsType<MesWmSortTaskApi.SortTaskLine> = [
  {
    title: '物料',
    dataIndex: 'itemName',
    width: 220,
    customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
      h('div', [
        h('div', record.itemName ?? '-'),
        h(
          'div',
          { class: 'text-xs text-gray-500' },
          `${record.itemCode ?? '-'}|条码 ${record.itemBarCode ?? '未维护'}`,
        ),
      ]),
  },
  {
    title: '批次号',
    dataIndex: 'batchCode',
    width: 140,
    customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
      record.batchCode ?? '-',
  },
  {
    title: '货位指引',
    dataIndex: 'locationName',
    width: 200,
    customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
      locationText(record),
  },
  {
    title: '计划数量',
    dataIndex: 'planQuantity',
    width: 110,
    align: 'right',
  },
  {
    title: '已分拣',
    dataIndex: 'sortedQuantity',
    width: 100,
    align: 'right',
  },
  {
    title: '状态',
    dataIndex: 'scanned',
    width: 90,
    customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
      record.scanned && remainQuantity(record) === 0
        ? h(Tag, { color: 'success' }, '已完成')
        : h(Tag, { color: 'warning' }, '待分拣'),
  },
  {
    title: '扫码核验',
    dataIndex: 'scan',
    width: 320,
    customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
      h('div', { class: 'flex items-center gap-1' }, [
        h(Input, {
          value: scanBarcode.value[record.id!] ?? '',
          size: 'small',
          placeholder: '扫物料条码 / 批次号',
          disabled: !canSort.value,
          allowClear: true,
          'onUpdate:value': (value: string) => {
            scanBarcode.value[record.id!] = value;
          },
          onPressEnter: () => handleScan(record),
        }),
        h(InputNumber, {
          value: scanQuantity.value[record.id!],
          class: 'w-[90px]',
          min: 0,
          placeholder: String(remainQuantity(record)),
          disabled: !canSort.value,
          'onUpdate:value': (value: unknown) => {
            scanQuantity.value[record.id!] =
              value === null || value === undefined ? undefined : Number(value);
          },
        }),
        h(
          Button,
          {
            type: 'primary',
            size: 'small',
            disabled: !canSort.value,
            onClick: () => handleScan(record),
          },
          () => '扫码',
        ),
      ]),
  },
];
 
const [Modal, modalApi] = useVbenModal({
  confirmText: '关闭',
  async onConfirm() {
    await modalApi.close();
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      detail.value = {};
      scanBarcode.value = {};
      scanQuantity.value = {};
      carrier.value = '';
      shippingNumber.value = '';
      return;
    }
    const data = modalApi.getData<{ id: number }>();
    if (!data?.id) {
      return;
    }
    modalApi.lock();
    try {
      detail.value = await getSortTask(data.id);
      carrier.value = detail.value.carrier ?? '';
      shippingNumber.value = detail.value.shippingNumber ?? '';
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal
    :title="`订单分拣 ${detail.code ? `- ${detail.code}` : ''}`"
    class="w-[1100px]"
  >
    <div class="mb-3 grid grid-cols-3 gap-x-6 gap-y-1 text-sm">
      <div>发货通知单:{{ detail.noticeCode || '-' }}</div>
      <div>销售订单:{{ detail.saleOrderCode || '-' }}</div>
      <div>收货人:{{ detail.recipientName || '-' }}</div>
      <div>联系电话:{{ detail.recipientTelephone || '-' }}</div>
      <div class="col-span-2">收货地址:{{ detail.recipientAddress || '-' }}</div>
      <div v-if="detail.status === SORT_TASK_STATUS.FINISHED">
        承运商:{{ detail.carrier || '-' }}
      </div>
      <div v-if="detail.status === SORT_TASK_STATUS.FINISHED">
        物流单号:{{ detail.shippingNumber || '-' }}
      </div>
    </div>
    <Table
      :columns="columns"
      :data-source="lines"
      :pagination="false"
      row-key="id"
      size="small"
      :scroll="{ y: 380 }"
      bordered
    />
    <div
      v-if="canSort"
      class="mt-3 flex items-center gap-2 rounded border border-solid border-[#dcdfe6] p-3"
    >
      <span class="text-sm">配送信息:</span>
      <Input
        v-model:value="carrier"
        class="w-[180px]"
        placeholder="承运商 / 配送方式"
        allow-clear
      />
      <Input
        v-model:value="shippingNumber"
        class="w-[200px]"
        placeholder="物流单号"
        allow-clear
      />
      <div class="ml-auto text-sm">
        已完成
        {{ lines.length - unfinishedLines.length }} / {{ lines.length }} 行
      </div>
    </div>
    <template #prepend-footer>
      <div class="flex flex-auto items-center gap-2">
        <Button
          :disabled="!canSort || allSorted"
          @click="handleSubmitProductionRequest"
        >
          提交生产申请
        </Button>
        <Button
          v-if="canSort"
          type="primary"
          :disabled="!allSorted"
          @click="handleFinish"
        >
          完成分拣并触发配送
        </Button>
      </div>
    </template>
  </Modal>
</template>