From 21599300cc73ff7384b74cc5e4ac2cbd82bdc293 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 28 十月 2026 18:34:29 +0800
Subject: [PATCH] feat(wls): 更新销售退货功能支持关联销售出库单

---
 src/views/mes/pro/bagCode/modules/generate-form.vue |  258 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 240 insertions(+), 18 deletions(-)

diff --git a/src/views/mes/pro/bagCode/modules/generate-form.vue b/src/views/mes/pro/bagCode/modules/generate-form.vue
index 316e9fd..5613a5d 100644
--- a/src/views/mes/pro/bagCode/modules/generate-form.vue
+++ b/src/views/mes/pro/bagCode/modules/generate-form.vue
@@ -1,42 +1,153 @@
 <script lang="ts" setup>
   import type { VbenFormSchema } from "#/adapter/form";
+  import type { MdmItemApi } from "#/api/mdm/item";
   import type { MesProBagCodeApi } from "#/api/mes/pro/bagCode";
+
+  import { computed, markRaw, ref } from "vue";
 
   import { useVbenModal } from "@vben/common-ui";
 
-  import { message } from "ant-design-vue";
+  import { Button, message, Progress } from "ant-design-vue";
 
   import { useVbenForm } from "#/adapter/form";
   import { generateBagCodes } from "#/api/mes/pro/bagCode";
+  import { getUnit } from "#/api/mdm/unit";
+  import { MdmItemSelect } from "#/views/basicData/mdm/components";
+
+  import { isFinalTaskStatus, useGenerateTask } from "./use-generate-task";
 
   defineOptions({ name: "MesProBagCodeGenerateForm" });
 
   const emit = defineEmits(["success"]);
 
+  // 鎵�閫夊搧绉嶄富璁¢噺鍗曚綅绫诲瀷锛�48琚�/100琚� -> 姣忔墭 N 琚嬶紱鍚� -> 鏃犳墭鐩樼洿鎺ヨ緭琚嬬爜鏁�
+  const unitType = ref("");
+  const bagsPerPallet = ref<number | null>(null);
+  const isTon = ref(false);
+  const unitTypeError = ref(false);
+  const palletCountVal = ref<number | null>(null);
+  const bagCountVal = ref<number | null>(null);
+
+  /** 骞傜瓑閿細鎵撳紑寮圭獥鏃剁敓鎴愶紝缃戠粶閲嶈瘯澶嶇敤锛涘け璐ュ悗閲嶆柊鐢熸垚 */
+  const requestId = ref("");
+
+  function newRequestId() {
+    return `${Date.now()}-${Math.random().toString(36).slice(2)}-${Math.random()
+      .toString(36)
+      .slice(2)}`;
+  }
+
+  /** 鎹㈢畻/閰嶇疆鎻愮ず鏂囨 */
+  const tipText = computed(() => {
+    if (unitTypeError.value) {
+      return "璇ュ搧绉嶄富璁¢噺鍗曚綅鏈厤缃被鍨嬶紙浠呮敮鎸� 48琚�/100琚�/鍚級锛岃鍏堝墠寰�銆岃閲忓崟浣嶃�嶇淮鎶ゅ悗閲嶈瘯";
+    }
+    if (!unitType.value) {
+      return "";
+    }
+    if (isTon.value) {
+      return `璁¢噺鍗曚綅绫诲瀷涓恒�屽惃銆嶏細涓嶇敓鎴愭墭鐩橈紝琚嬬爜浠呯粦瀹氱敓浜ф壒娆�${
+        bagCountVal.value ? `锛屽皢鐢熸垚 ${bagCountVal.value} 涓鐮乣 : ""
+      }`;
+    }
+    const per = bagsPerPallet.value ?? 0;
+    const count = palletCountVal.value ?? 0;
+    return `璁¢噺鍗曚綅绫诲瀷涓恒��${unitType.value}銆嶏細姣忔墭 ${per} 琚�${
+      count > 0
+        ? ` 脳 ${count} 鎵� = ${per * count} 涓鐮侊紝灏嗚嚜鍔ㄧ敓鎴� ${count} 涓墭鐩樺苟缁戝畾`
+        : "锛岃杈撳叆鎵樼洏鏁�"
+    }`;
+  });
+
   function buildSchema(): VbenFormSchema[] {
-    return [
+    const schema: VbenFormSchema[] = [
       {
-        fieldName: "count",
-        label: "鐢熸垚琚嬫暟",
+        fieldName: "itemId",
+        label: "鍝佺",
+        component: markRaw(MdmItemSelect),
+        componentProps: {
+          placeholder: "璇烽�夋嫨鍝佺锛堜骇鍝佸搧绉嶏級",
+          onChange: handleItemChange,
+        },
+        rules: "selectRequired",
+      },
+    ];
+    if (bagsPerPallet.value) {
+      schema.push({
+        fieldName: "palletCount",
+        label: "鎵樼洏鏁�",
         component: "InputNumber",
         componentProps: {
           class: "!w-full",
           min: 1,
-          placeholder: "璇疯緭鍏ラ渶瑕佺敓鎴愮殑琚嬬爜鏁伴噺",
+          placeholder: "璇疯緭鍏ユ墭鐩樻暟锛堢郴缁熻嚜鍔ㄧ敓鎴愭墭鐩樺苟鍧囧垎缁戝畾琚嬬爜锛�",
           precision: 0,
+          onChange: (value: number | string) => {
+            palletCountVal.value = Number(value) || null;
+          },
         },
         rules: "required",
-      },
-      {
-        fieldName: "remark",
-        label: "澶囨敞",
-        component: "Textarea",
+      });
+    }
+    if (isTon.value) {
+      schema.push({
+        fieldName: "bagCount",
+        label: "琚嬬爜鏁�",
+        component: "InputNumber",
         componentProps: {
-          placeholder: "璇疯緭鍏ュ娉�",
-          rows: 2,
+          class: "!w-full",
+          min: 1,
+          placeholder: "璇疯緭鍏ヨ鐮佹暟锛堝惃鍝佺鏃犳墭鐩橈紝琚嬬爜浠呯粦瀹氭壒娆★級",
+          precision: 0,
+          onChange: (value: number | string) => {
+            bagCountVal.value = Number(value) || null;
+          },
         },
+        rules: "required",
+      });
+    }
+    schema.push({
+      fieldName: "remark",
+      label: "澶囨敞",
+      component: "Textarea",
+      componentProps: {
+        placeholder: "璇疯緭鍏ュ娉�",
+        rows: 2,
       },
-    ];
+    });
+    return schema;
+  }
+
+  /** 閫夋嫨鍝佺鍚庤鍙栦富璁¢噺鍗曚綅绫诲瀷锛屽姩鎬佸垏鎹㈡墭鐩樻暟/琚嬬爜鏁拌緭鍏� */
+  async function handleItemChange(item?: MdmItemApi.Item) {
+    unitType.value = "";
+    bagsPerPallet.value = null;
+    isTon.value = false;
+    unitTypeError.value = false;
+    palletCountVal.value = null;
+    bagCountVal.value = null;
+    if (item?.unitMeasureId) {
+      try {
+        const unit = await getUnit(item.unitMeasureId);
+        const type = (unit?.type ?? "").trim();
+        unitType.value = type;
+        if (type === "鍚�") {
+          isTon.value = true;
+        } else {
+          const matcher = /^(\d{1,5})琚�$/.exec(type);
+          if (matcher) {
+            bagsPerPallet.value = Number(matcher[1]);
+          } else {
+            unitTypeError.value = true;
+          }
+        }
+      } catch {
+        unitTypeError.value = true;
+      }
+    } else if (item) {
+      unitTypeError.value = true;
+    }
+    await formApi.setState({ schema: buildSchema() });
   }
 
   const [Form, formApi] = useVbenForm({
@@ -55,32 +166,143 @@
     async onConfirm() {
       const { valid } = await formApi.validate();
       if (!valid) return;
+      if (task.value && !isFinalTaskStatus(task.value.status)) return;
+      if (unitTypeError.value) {
+        message.warning(tipText.value);
+        return;
+      }
       modalApi.lock();
+      let asyncStarted = false;
       try {
         const values =
           (await formApi.getValues()) as MesProBagCodeApi.GenerateParams;
-        await generateBagCodes(values);
-        message.success(`宸叉垚鍔熺敓鎴� ${values.count} 鏉¤鐮乣);
+        const data: MesProBagCodeApi.GenerateParams = {
+          itemId: values.itemId,
+          remark: values.remark,
+          requestId: requestId.value,
+        };
+        if (isTon.value) {
+          data.bagCount = values.bagCount;
+        } else {
+          data.palletCount = values.palletCount;
+        }
+        const result = await generateBagCodes(data);
+        if (result.async && result.taskId) {
+          asyncStarted = true;
+          modalApi.unlock();
+          // 澶ф壒閲忓凡杞悗鍙帮細淇濇寔寮圭獥鎵撳紑锛屽睍绀鸿繘搴﹀苟杞
+          startGenerateTask({
+            id: result.taskId,
+            status: 10,
+            statusName: "鎺掗槦涓�",
+            progress: 0,
+            totalCount: result.totalCount,
+            finishedCount: 0,
+            palletCount: result.palletCount,
+            uuid: result.uuid,
+          });
+          return;
+        }
+        message.success(
+          `宸叉垚鍔熺敓鎴� ${result.totalCount} 鏉¤鐮乣 +
+            (result.palletCount
+              ? `锛屽苟鑷姩鐢熸垚 ${result.palletCount} 涓墭鐩榒
+              : ""),
+        );
         emit("success");
         await modalApi.close();
       } finally {
-        modalApi.unlock();
+        if (!asyncStarted) modalApi.unlock();
       }
     },
     async onOpenChange(isOpen: boolean) {
-      if (!isOpen) return;
+      if (!isOpen) {
+        stopGenerateTaskPolling();
+        return;
+      }
       modalApi.lock();
       try {
+        resetGenerateTask();
+        requestId.value = newRequestId();
+        unitType.value = "";
+        bagsPerPallet.value = null;
+        isTon.value = false;
+        unitTypeError.value = false;
+        palletCountVal.value = null;
+        bagCountVal.value = null;
+        await formApi.setValues({
+          itemId: undefined,
+          palletCount: undefined,
+          bagCount: undefined,
+          remark: undefined,
+        });
         await formApi.setState({ schema: buildSchema() });
       } finally {
         modalApi.unlock();
       }
     },
   });
+
+  const {
+    task,
+    canceling: generateTaskCanceling,
+    cancel: cancelGenerateTask,
+    reset: resetGenerateTask,
+    stopPolling: stopGenerateTaskPolling,
+    start: startGenerateTask,
+  } = useGenerateTask(modalApi, {
+    onSuccess: () => emit("success"),
+    onFailed: () => {
+      // 澶辫触鍚庢崲鏂扮殑骞傜瓑閿紝鍏佽鐢ㄦ埛鐩存帴閲嶆柊鎻愪氦
+      requestId.value = newRequestId();
+    },
+  });
 </script>
 
 <template>
   <Modal title="鐢熸垚琚嬬爜鏁版嵁鍖�" class="w-2/5">
-    <Form class="mx-4" />
+    <template v-if="task">
+      <div class="mx-4 mb-2">
+        <p class="mb-1 text-sm">
+          浠诲姟 #{{ task.id }} 路 {{ task.statusName }}
+        </p>
+        <p class="mb-2 text-sm text-gray-500">
+          宸茬敓鎴� {{ task.finishedCount ?? 0 }} / {{ task.totalCount ?? 0 }} 鏉�
+        </p>
+        <Progress
+          :percent="task.progress ?? 0"
+          :status="
+            task.status === 50
+              ? 'exception'
+              : task.status === 30
+                ? 'success'
+                : 'active'
+          "
+        />
+        <p v-if="task.errorMsg" class="mt-2 text-sm text-red-500">
+          {{ task.errorMsg }}
+        </p>
+        <div class="mt-4 flex justify-end">
+          <Button
+            danger
+            size="small"
+            :loading="generateTaskCanceling"
+            @click="cancelGenerateTask"
+          >
+            鍙栨秷鐢熸垚
+          </Button>
+        </div>
+      </div>
+    </template>
+    <template v-else>
+      <Form class="mx-4" />
+      <div
+        v-if="tipText"
+        class="mx-4 mb-2 rounded bg-gray-50 px-3 py-2 text-sm"
+        :class="unitTypeError ? 'text-red-500' : 'text-gray-500'"
+      >
+        {{ tipText }}
+      </div>
+    </template>
   </Modal>
 </template>

--
Gitblit v1.9.3