云
4 天以前 be42a59e52e49ba484312517f924286083b9dfd2
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
<script lang="ts" setup>
  import type { VbenFormSchema } from "#/adapter/form";
  import type { MesProPalletApi } from "#/api/mes/pro/pallet";
 
  import { useVbenModal } from "@vben/common-ui";
  import { MesProPalletStatusEnum } from "@vben/constants";
 
  import { message } from "ant-design-vue";
 
  import { useVbenForm } from "#/adapter/form";
  import { bindBagCodePallet } from "#/api/mes/pro/bagCode";
  import {
    getPalletBindableList,
    getPalletSimpleList,
  } from "#/api/mes/pro/pallet";
  import { $t } from "#/locales";
 
  defineOptions({ name: "MesProBagCodeBindPallet" });
 
  /** 弹窗传入数据(batchId 为空表示所选袋码尚未录入生产批次) */
  interface ModalData {
    batchId?: number;
    batchCode?: string;
    itemId?: number;
    ids: number[];
  }
 
  const emit = defineEmits(["success"]);
 
  /** 未录入批次时的托盘展示名:正式托盘带批次号、临时托盘回退到临时码,便于在混合列表中分辨 */
  function palletLabel(pallet: MesProPalletApi.Pallet) {
    const code = pallet.code || pallet.tempCode || "-";
    const batchPart = pallet.batchCode ? `批次 ${pallet.batchCode}` : "临时托盘";
    return `${code}(${batchPart} / 已装 ${pallet.bagCount ?? 0} 袋)`;
  }
 
  /** 构建表单(托盘下拉选项由外部加载注入) */
  function buildSchema(palletOptions: { label: string; value: number }[]): VbenFormSchema[] {
    return [
      {
        fieldName: "batchCode",
        label: "生产批次",
        component: "Input",
        componentProps: {
          disabled: true,
        },
      },
      {
        fieldName: "bagCount",
        label: "绑定袋数",
        component: "InputNumber",
        componentProps: {
          class: "!w-full",
          disabled: true,
        },
      },
      {
        fieldName: "palletId",
        label: "目标托盘",
        component: "Select",
        componentProps: {
          optionFilterProp: "label",
          options: palletOptions,
          placeholder: "请选择目标托盘",
          showSearch: true,
        },
        rules: "selectRequired",
      },
    ];
  }
 
  const [Form, formApi] = useVbenForm({
    commonConfig: {
      componentProps: {
        class: "w-full",
      },
      labelWidth: 100,
    },
    layout: "horizontal",
    schema: [],
    showDefaultActions: false,
  });
 
  const [Modal, modalApi] = useVbenModal({
    async onConfirm() {
      const { valid } = await formApi.validate();
      if (!valid) return;
      const data = modalApi.getData<ModalData>();
      const values = await formApi.getValues();
      modalApi.lock();
      try {
        await bindBagCodePallet({ palletId: values.palletId, ids: data.ids });
        message.success($t("ui.actionMessage.operationSuccess"));
        emit("success");
        await modalApi.close();
      } finally {
        modalApi.unlock();
      }
    },
    async onOpenChange(isOpen: boolean) {
      if (!isOpen) return;
      const data = modalApi.getData<ModalData>();
      modalApi.lock();
      try {
        // 已录入批次:只列本批次的托盘;未录入批次:列该品种可用的临时托盘与正式托盘,
        // 选到正式托盘时由后端先把这批袋码补录进该批次,再完成绑定
        const pallets = data.batchId
          ? await getPalletSimpleList(data.batchId)
          : await getPalletBindableList(data.itemId);
        const options = pallets
          .filter((pallet) => pallet.status !== MesProPalletStatusEnum.CANCELED)
          .map((pallet) => ({
            label: data.batchId
              ? `${pallet.code}(已装 ${pallet.bagCount ?? 0} 袋)`
              : palletLabel(pallet),
            value: pallet.id!,
          }));
        await formApi.setState({ schema: buildSchema(options) });
        await formApi.setValues({
          bagCount: data.ids.length,
          batchCode: data.batchCode || "未录入批次",
        });
      } finally {
        modalApi.unlock();
      }
    },
  });
</script>
 
<template>
  <Modal title="袋码绑定托盘" class="w-2/5">
    <Form class="mx-4" />
  </Modal>
</template>