From 46624ce480a3c14da21f39cc4ad5f7eecea2bb1e Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 17 九月 2026 16:24:28 +0800
Subject: [PATCH] feat(mes): 优化生产批次和袋码管理功能

---
 src/views/mes/pro/bagCode/modules/generate-form.vue |  165 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 151 insertions(+), 14 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..46ba37e 100644
--- a/src/views/mes/pro/bagCode/modules/generate-form.vue
+++ b/src/views/mes/pro/bagCode/modules/generate-form.vue
@@ -1,6 +1,9 @@
 <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";
 
@@ -8,35 +11,132 @@
 
   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";
 
   defineOptions({ name: "MesProBagCodeGenerateForm" });
 
   const emit = defineEmits(["success"]);
 
+  // 鎵�閫夊搧绉嶄富璁¢噺鍗曚綅绫诲瀷锛�48鍗冨厠/100鍗冨厠 -> 姣忔墭 N 琚嬶紱鍚� -> 鏃犳墭鐩樼洿鎺ヨ緭琚嬬爜鏁�
+  const unitType = ref("");
+  const kgPerPallet = 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 tipText = computed(() => {
+    if (unitTypeError.value) {
+      return "璇ュ搧绉嶄富璁¢噺鍗曚綅鏈厤缃被鍨嬶紙浠呮敮鎸� 48鍗冨厠/100鍗冨厠/鍚級锛岃鍏堝墠寰�銆岃閲忓崟浣嶃�嶇淮鎶ゅ悗閲嶈瘯";
+    }
+    if (!unitType.value) {
+      return "";
+    }
+    if (isTon.value) {
+      return `璁¢噺鍗曚綅绫诲瀷涓恒�屽惃銆嶏細涓嶇敓鎴愭墭鐩橈紝琚嬬爜浠呯粦瀹氱敓浜ф壒娆�${
+        bagCountVal.value ? `锛屽皢鐢熸垚 ${bagCountVal.value} 涓鐮乣 : ""
+      }`;
+    }
+    const per = kgPerPallet.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 (kgPerPallet.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 = "";
+    kgPerPallet.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) {
+            kgPerPallet.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,12 +155,30 @@
     async onConfirm() {
       const { valid } = await formApi.validate();
       if (!valid) return;
+      if (unitTypeError.value) {
+        message.warning(tipText.value);
+        return;
+      }
       modalApi.lock();
       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,
+        };
+        if (isTon.value) {
+          data.bagCount = values.bagCount;
+        } else {
+          data.palletCount = values.palletCount;
+        }
+        const result = await generateBagCodes(data);
+        message.success(
+          `宸叉垚鍔熺敓鎴� ${result.totalCount} 鏉¤鐮乣 +
+            (result.palletIds?.length
+              ? `锛屽苟鑷姩鐢熸垚 ${result.palletIds.length} 涓墭鐩榒
+              : ""),
+        );
         emit("success");
         await modalApi.close();
       } finally {
@@ -71,6 +189,18 @@
       if (!isOpen) return;
       modalApi.lock();
       try {
+        unitType.value = "";
+        kgPerPallet.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();
@@ -82,5 +212,12 @@
 <template>
   <Modal title="鐢熸垚琚嬬爜鏁版嵁鍖�" class="w-2/5">
     <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>
   </Modal>
 </template>

--
Gitblit v1.9.3