From 23006c64e0d7478ff2e8c723f2bfda3781b65b09 Mon Sep 17 00:00:00 2001
From: zhang_12370 <z2864490065@outlook.com>
Date: 星期五, 18 七月 2025 11:59:20 +0800
Subject: [PATCH] 1、优化配煤计算器 2、优化正是库存数据渲染问题

---
 src/views/calculator/index.vue |  508 ++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 309 insertions(+), 199 deletions(-)

diff --git a/src/views/calculator/index.vue b/src/views/calculator/index.vue
index c20d714..814e57f 100644
--- a/src/views/calculator/index.vue
+++ b/src/views/calculator/index.vue
@@ -85,20 +85,21 @@
                       <el-form-item :label="'鐓ょ' + (index + 1)">
                         <div class="input-wrapper">
                           <el-input
-                            v-model="item.name"
+                            v-model="item.coalId"
                             v-show="item.type !== '宸叉湁鐓�'"
                             placeholder="璇疯緭鍏�"
                             style="width: 100%"
                           />
 
                           <el-select
-                            v-model="item.name"
+                            v-model="item.coalId"
                             v-show="item.type === '宸叉湁鐓�'"
                             placeholder="璇烽�夋嫨"
                             style="width: 100%"
+                            @change="handleCoalSelectChange(index, item.coalId)"
                           >
                             <el-option
-                              v-for="ele in coalInfoList"
+                              v-for="ele in infoCoals"
                               :key="ele.key"
                               :label="ele.value"
                               :value="ele.key"
@@ -111,12 +112,28 @@
                     <el-col :span="6">
                       <el-form-item label="鍙戠儹閲�">
                         <el-input
+                          v-if="item.type !== '鏈煡鐓�'"
+                          :value="
+                            infoCoals.find((coal) => coal.key === item.coalId)
+                              ?.item.coalValues.find((value) => value.fieldName === '鍙戠儹閲�')?.coalValue || '0'
+                          "
+                          type="number"
+                          style="width: 100%"
+                          :disabled="true"
+                        >
+                          <template v-slot:suffix>
+                            <i style="font-style: normal">kcal</i>
+                          </template>
+                        </el-input>
+                        <el-input
+                          v-else
                           v-model="item.cv"
                           type="number"
+                          placeholder="璇疯緭鍏ュ彂鐑噺"
                           style="width: 100%"
                         >
                           <template v-slot:suffix>
-                            <i style="font-style: normal">kcal/kg</i>
+                            <i style="font-style: normal">kcal</i>
                           </template>
                         </el-input>
                       </el-form-item>
@@ -124,7 +141,24 @@
                     <el-col :span="6">
                       <el-form-item label="浠锋牸">
                         <el-input
+                        v-if="item.type !== '鏈煡鐓�'"
+                        
+                          :value="
+                            infoCoals.find((coal) => coal.key === item.coalId)
+                              ?.item.priceExcludingTax || 0
+                          "
+                          type="number"
+                          style="width: 100%"
+                          :disabled="item.type === '宸叉湁鐓�'"
+                        >
+                          <template v-slot:suffix>
+                            <i style="font-style: normal">鍏�/鍚�</i>
+                          </template>
+                        </el-input>
+                        <el-input
+                        v-else
                           v-model="item.price"
+                          placeholder="璇疯緭鍏ヤ环鏍�"
                           type="number"
                           style="width: 100%"
                         >
@@ -139,6 +173,23 @@
                     <el-col :span="6">
                       <el-form-item label="纭垎">
                         <el-input
+                          v-if="item.type !== '鏈煡鐓�'"
+                          :disabled="item.type === '宸叉湁鐓�'"
+                          v-model="item.sulfur"
+                          :value="
+                            infoCoals.find((coal) => coal.key === item.coalId)
+                              ?.item.coalValues.find((value) => value.fieldName === '纭垎')?.coalValue || '0'
+                          "
+                          type="number"
+                          placeholder="鍙��"
+                          style="width: 100%"
+                        >
+                          <template v-slot:suffix>
+                            <i style="font-style: normal">%</i>
+                          </template>
+                        </el-input>
+                        <el-input
+                          v-else
                           v-model="item.sulfur"
                           type="number"
                           placeholder="鍙��"
@@ -153,6 +204,23 @@
                     <el-col :span="6">
                       <el-form-item label="鐏板垎">
                         <el-input
+                          v-if="item.type !== '鏈煡鐓�'"
+                          :disabled="item.type === '宸叉湁鐓�'"
+                          v-model="item.ash"
+                          :value="
+                            infoCoals.find((coal) => coal.key === item.coalId)
+                              ?.item.coalValues.find((value) => value.fieldName === '鐏板垎')?.coalValue || '0'
+                          "
+                          type="number"
+                          placeholder="鍙��"
+                          style="width: 100%"
+                        >
+                          <template v-slot:suffix>
+                            <i style="font-style: normal">%</i>
+                          </template>
+                        </el-input>
+                        <el-input
+                          v-else
                           v-model="item.ash"
                           type="number"
                           placeholder="鍙��"
@@ -167,6 +235,23 @@
                     <el-col :span="6">
                       <el-form-item label="姘村垎">
                         <el-input
+                        v-if="item.type !== '鏈煡鐓�'"
+                        :disabled="item.type === '宸叉湁鐓�'"
+                        v-model="item.moisture"
+                        :value="
+                            infoCoals.find((coal) => coal.key === item.coalId)
+                              ?.item.coalValues.find((value) => value.fieldName === '姘村垎')?.coalValue || '0'
+                          "
+                          type="number"
+                          placeholder="鍙��"
+                          style="width: 100%"
+                        >
+                          <template v-slot:suffix>
+                            <i style="font-style: normal">%</i>
+                          </template>
+                        </el-input>
+                        <el-input
+                        v-else
                           v-model="item.moisture"
                           type="number"
                           placeholder="鍙��"
@@ -202,7 +287,7 @@
                       style="width: 100%"
                     >
                       <template v-slot:suffix>
-                        <i style="font-style: normal">kcal/kg</i>
+                        <i style="font-style: normal">kcal</i>
                       </template>
                     </el-input>
                   </el-form-item>
@@ -289,17 +374,22 @@
                 class="result-table"
                 style="width: 100%"
               >
-                <el-table-column prop="name" label="鐓ょ" min-width="80">
+                <el-table-column prop="coalId" label="鐓ょ" min-width="80">
                   <template #default="scope">
-                    {{ matchCoalName(scope.row.name) }}
+                    {{
+                      infoCoals.find((coal) => coal.key === scope.row.coalId)
+                        ?.value ||
+                      "" ||
+                      matchCoalName(scope.row.coalId)
+                    }}
                   </template>
                 </el-table-column>
                 <el-table-column prop="ratio" label="閰嶆瘮" min-width="80">
                   <template #default="scope"> {{ scope.row.ratio }}% </template>
                 </el-table-column>
-                <el-table-column prop="tonnage" label="鍚ㄦ暟" min-width="80">
+                <el-table-column prop="quantity" label="鍚ㄦ暟" min-width="80">
                   <template #default="scope">
-                    {{ scope.row.tonnage }}鍚�
+                    {{ scope.row.quantity }}鍚�
                   </template>
                 </el-table-column>
                 <el-table-column prop="scoops" label="閾叉暟" min-width="80">
@@ -317,7 +407,7 @@
                 <div class="prop-item">
                   <span class="prop-label">鍙戠儹閲�:</span>
                   <span class="prop-value"
-                    >{{ result.optimal.props.cv.toFixed(2) }} kcal/kg</span
+                    >{{ result.optimal.props.cv.toFixed(2) }} kcal</span
                   >
                 </div>
                 <div class="prop-item">
@@ -382,15 +472,24 @@
                   class="alt-table"
                   style="width: 100%"
                 >
-                  <el-table-column prop="name" label="鐓ょ" min-width="80" />
+                  <el-table-column prop="coalId" label="鐓ょ" min-width="80">
+                    <template #default="scope">
+                      {{
+                        infoCoals.find((coal) => coal.key === scope.row.coalId)
+                          ?.value ||
+                        "" ||
+                        matchCoalName(scope.row.coalId)
+                      }}
+                    </template>
+                  </el-table-column>
                   <el-table-column prop="ratio" label="閰嶆瘮" min-width="80">
                     <template #default="scope">
                       {{ scope.row.ratio }}%
                     </template>
                   </el-table-column>
-                  <el-table-column prop="tonnage" label="鍚ㄦ暟" min-width="80">
+                  <el-table-column prop="quantity" label="鍚ㄦ暟" min-width="80">
                     <template #default="scope">
-                      {{ scope.row.tonnage }}鍚�
+                      {{ scope.row.quantity }}鍚�
                     </template>
                   </el-table-column>
                   <el-table-column prop="scoops" label="閾叉暟" min-width="80">
@@ -402,7 +501,7 @@
               </div>
 
               <div class="alt-props">
-                <span>鍙戠儹閲�: {{ alt.props.cv.toFixed(2) }} kcal/kg锛�</span>
+                <span>鍙戠儹閲�: {{ alt.props.cv.toFixed(2) }} kcal锛�</span>
                 <span>纭垎: {{ alt.props.sulfur.toFixed(2) }}%锛�</span>
                 <span>鐏板垎: {{ alt.props.ash.toFixed(2) }}%锛�</span>
                 <span>姘村垎: {{ alt.props.moisture.toFixed(2) }}%锛�</span>
@@ -423,9 +522,10 @@
 </template>
 
 <script setup>
-import { reactive, toRefs, nextTick, onMounted } from "vue";
+import { reactive, toRefs, nextTick, onMounted, ref } from "vue";
 import { ElMessage, ElMessageBox } from "element-plus";
 import { getCoalInfoList } from "@/api/procureMent"; // 鍋囪鏈変竴涓狝PI鑾峰彇鐓ょ淇℃伅
+import { getCoalBlendingList,addPendingInventory } from "@/api/calculator/index.js";
 
 const data = reactive({
   formInline: {
@@ -443,7 +543,7 @@
   coalForms: [
     {
       type: "鏈煡鐓�",
-      name: "鐓",
+      coalId: "鐓",
       cv: 6200, // 鍙戠儹閲�
       price: 450, // 浠锋牸
       sulfur: 0.6, // 纭垎
@@ -452,7 +552,7 @@
     },
     {
       type: "鏈煡鐓�",
-      name: "鐓",
+      coalId: "鐓",
       cv: 5800,
       price: 380,
       sulfur: 1.0,
@@ -461,7 +561,7 @@
     },
     {
       type: "鏈煡鐓�",
-      name: "鐓",
+      coalId: "鐓",
       cv: 5400,
       price: 320,
       sulfur: 1.4,
@@ -478,111 +578,132 @@
     createCoal: null,
   },
 });
+const handleCoalSelectChange = (index, coalId) => {
+  const selectedCoal = infoCoals.value.find(item => item.key === coalId);
+  
+  if (!selectedCoal?.item?.coalValues) {
+    console.warn('鏈壘鍒伴�変腑鐨勭叅绉嶆暟鎹垨鏁版嵁鏍煎紡涓嶆纭�');
+    return;
+  }
+  
+  // 鑾峰彇鐓よ川鍙傛暟鐨勯�氱敤鍑芥暟
+  const getCoalValue = (fieldName) => {
+    return selectedCoal.item.coalValues.find(
+      value => value.fieldName === fieldName
+    )?.coalValue || 0;
+  };
+
+  // 鎵归噺鏇存柊琛ㄥ崟鏁版嵁
+  Object.assign(data.coalForms[index], {
+    price: selectedCoal.item.priceExcludingTax || 0,
+    cv: getCoalValue("鍙戠儹閲�"),
+    sulfur: getCoalValue("纭垎"),
+    ash: getCoalValue("鐏板垎"),
+    moisture: getCoalValue("姘村垎")
+  });
+};
 const coalInfoList = ref([]);
-// onMounted
+// 鑾峰彇鐓ょ淇℃伅
 const getCoalInfo = async () => {
-  let result = await getCoalInfoList();
-  if (result.code === 200) {
-    result.data.forEach((item) => {
-      let obj = {
+  try {
+    const result = await getCoalInfoList();
+    if (result.code === 200) {
+      coalInfoList.value = result.data.map(item => ({
         value: item.coal,
         key: item.id,
-      };
-      coalInfoList.value.push(obj);
-    });
-  } else {
+      }));
+    } else {
+      ElMessage.error("鑾峰彇鐓ょ淇℃伅澶辫触锛岃绋嶅悗閲嶈瘯");
+    }
+  } catch (error) {
     ElMessage.error("鑾峰彇鐓ょ淇℃伅澶辫触锛岃绋嶅悗閲嶈瘯");
   }
 };
-// 琛ㄦ牸灞曠ず鐢細浼樺厛鐢� key 鍖归厤涓枃鍚嶏紝鍐嶇敤 value 鍖归厤锛屾渶鍚庡師鏍疯繑鍥�
+// 琛ㄦ牸灞曠ず鐢細鍖归厤鐓ょ鍚嶇О
 const matchCoalName = (name) => {
-  if (!name || !Array.isArray(coalInfoList.value) || coalInfoList.value.length === 0) return name;
-  // key 鍖归厤
-  const byKey = coalInfoList.value.find(item => String(item.key) === String(name));
-  if (byKey) return byKey.value;
-  // value 鍖归厤
-  const byValue = coalInfoList.value.find(item => item.value === name);
-  if (byValue) return byValue.value;
-  // 鍘熸牱杩斿洖
-  return name;
+  if (!name || !coalInfoList.value?.length) return name;
+  
+  // 浼樺厛鎸� key 鍖归厤锛屽啀鎸� value 鍖归厤
+  const foundCoal = coalInfoList.value.find(item => 
+    String(item.key) === String(name) || item.value === name
+  );
+  
+  return foundCoal?.value || name;
 };
 // 鑷姩琛ュ叏鎼滅储
 const querySearch = (q, cb) => {
-  const res = q
-    ? coalInfoList.value.filter((c) => c.value.includes(q))
+  const results = q 
+    ? coalInfoList.value.filter(c => c.value.includes(q))
     : coalInfoList.value;
-  cb(res);
+  cb(results);
 };
-// 閫夋嫨/澶辩劍鏃讹紝浼樺厛瀛� key锛屾壘涓嶅埌鍒欏瓨鍘熷��
+
+// 閫夋嫨/澶辩劍澶勭悊
 const handleSelect = (item) => {
-  const val = item.value || (item.target && item.target.value) || "";
-  const found = coalInfoList.value.find(
-    (c) => c.value === val || c.key === val
-  );
+  const val = item.value || item.target?.value || "";
+  const found = coalInfoList.value.find(c => c.value === val || c.key === val);
+  
   result.value.optimal.props.createCoal = found ? found.key : val;
-  // 鏂板锛氬鏋滃尮閰嶆垚鍔燂紝鐣欎竴涓猧d瀛楁
-  if (found) {
-    result.value.optimal.props.createCoalId = found.key;
-  } else {
-    result.value.optimal.props.createCoalId = null;
-  }
-  let match = matchCoalName(result.value.optimal.props.createCoal);
-  if (match && match !== result.value.optimal.props.createCoal) {
-    result.value.optimal.props.createCoal = match;
+  result.value.optimal.props.coalId = found?.key || null;
+  
+  // 鏇存柊鏄剧ず鍚嶇О
+  const matchedName = matchCoalName(result.value.optimal.props.createCoal);
+  if (matchedName !== result.value.optimal.props.createCoal) {
+    result.value.optimal.props.createCoal = matchedName;
   }
 };
 onMounted(async () => {
   getCoalInfo();
+  geInfoCoals();
 });
+const infoCoals = ref([]);
+// 鍒濆鍖栫叅绉嶅瓧娈�
+const geInfoCoals = async () => {
+  try {
+    const res = await getCoalBlendingList();
+    if (res.code === 200) {
+      console.log("鑾峰彇鐓ょ淇℃伅鎴愬姛", res.data);
+      infoCoals.value = res.data.map(item => ({
+        value: item.supplierCoal,
+        key: item.coalId,
+        item,
+      }));
+    } else {
+      ElMessage.error("鑾峰彇鐓ょ淇℃伅澶辫触锛岃绋嶅悗閲嶈瘯");
+    }
+  } catch (error) {
+    ElMessage.error("鑾峰彇鐓ょ淇℃伅澶辫触锛岃绋嶅悗閲嶈瘯");
+  }
+};
 // 绾挎�ц鍒掓眰瑙e嚱鏁�
 const solveBlend = (coals, constraints) => {
   // 鏁版嵁楠岃瘉
-  if (constraints.maxSulfur) {
-    let missingSulfur = coals.some((coal) => !coal.sulfur && coal.sulfur !== 0);
-    if (missingSulfur) {
-      throw new Error(
-        "濡傛灉璁剧疆浜嗘渶澶х~鍒嗙害鏉燂紝鍒欐墍鏈夊弬涓庨厤姣旂殑鐓ょ閮藉繀椤绘彁渚涚~鍒嗘暟鎹��"
-      );
+  const validateConstraint = (constraintValue, fieldName, coalField) => {
+    if (constraintValue && coals.some(coal => !coal[coalField] && coal[coalField] !== 0)) {
+      throw new Error(`濡傛灉璁剧疆浜�${fieldName}绾︽潫锛屽垯鎵�鏈夌叅绉嶉兘蹇呴』鎻愪緵${fieldName}鏁版嵁銆俙);
     }
-  }
-  if (constraints.maxAsh) {
-    let missingAsh = coals.some((coal) => !coal.ash && coal.ash !== 0);
-    if (missingAsh) {
-      throw new Error("濡傛灉璁剧疆浜嗘渶澶х伆鍒嗙害鏉燂紝鍒欐墍鏈夌叅绉嶉兘蹇呴』鎻愪緵鐏板垎鏁版嵁銆�");
-    }
-  }
-  if (constraints.maxMoisture) {
-    let missingMoisture = coals.some(
-      (coal) => !coal.moisture && coal.moisture !== 0
-    );
-    if (missingMoisture) {
-      throw new Error("濡傛灉璁剧疆浜嗘渶澶ф按鍒嗙害鏉燂紝鍒欐墍鏈夌叅绉嶉兘蹇呴』鎻愪緵姘村垎鏁版嵁銆�");
-    }
-  }
+  };
 
-  // 绠�鍗曠殑绾挎�ц鍒掓眰瑙o紙鏈�灏忓寲鎴愭湰锛�
-  // 杩欓噷浣跨敤绠�鍖栫殑绠楁硶锛屽疄闄呴」鐩腑鍙互闆嗘垚鏇翠笓涓氱殑姹傝В鍣�
+  validateConstraint(constraints.maxSulfur, '鏈�澶х~鍒�', 'sulfur');
+  validateConstraint(constraints.maxAsh, '鏈�澶х伆鍒�', 'ash');
+  validateConstraint(constraints.maxMoisture, '鏈�澶ф按鍒�', 'moisture');
+
   try {
-    // 妯℃嫙姹傝В杩囩▼
-    let totalCoals = coals.length;
-    let ratios = new Array(totalCoals).fill(0);
-
     // 绠�鍗曠殑绛夋潈閲嶅垎閰嶄綔涓哄垵濮嬭В
-    let avgRatio = 1 / totalCoals;
-    ratios = ratios.map(() => avgRatio);
+    let ratios = new Array(coals.length).fill(1 / coals.length);
 
-    // 楠岃瘉绾︽潫鏉′欢
+    // 楠岃瘉绾︽潫鏉′欢骞惰皟鏁撮厤姣�
     let blendProps = calcBlendProps(coals, ratios);
 
     if (constraints.minCalorific && blendProps.cv < constraints.minCalorific) {
-      // 璋冩暣閰嶆瘮浠ユ弧瓒虫渶浣庡彂鐑噺
-      let highCvCoals = coals
+      // 浼樺厛浣跨敤楂樺彂鐑噺鐓ょ
+      const sortedCoals = coals
         .map((coal, i) => ({ index: i, cv: coal.cv }))
         .sort((a, b) => b.cv - a.cv);
 
-      ratios = new Array(totalCoals).fill(0);
-      ratios[highCvCoals[0].index] = 0.6;
-      ratios[highCvCoals[1] ? highCvCoals[1].index : 0] = 0.4;
+      ratios = new Array(coals.length).fill(0);
+      ratios[sortedCoals[0].index] = 0.6;
+      if (sortedCoals[1]) ratios[sortedCoals[1].index] = 0.4;
     }
 
     return ratios;
@@ -613,12 +734,12 @@
   return coals
     .map((coal, i) => {
       if (ratios[i] < 1e-6) return null;
-      let tonnage = ratios[i] * total;
-      let scoops = tonnage / scoop;
+      let quantity = ratios[i] * total;
+      let scoops = quantity / scoop;
       return {
-        name: coal.name,
+        coalId: coal.coalId,
         ratio: (ratios[i] * 100).toFixed(2),
-        tonnage: tonnage.toFixed(1),
+        quantity: quantity.toFixed(1),
         scoops: scoops.toFixed(1),
       };
     })
@@ -641,7 +762,7 @@
   data.coalForms = [
     {
       type: "鏈煡鐓�",
-      name: "鐓",
+      coalId: "鐓",
       cv: 6200,
       price: 450,
       sulfur: 0.6,
@@ -650,7 +771,7 @@
     },
     {
       type: "鏈煡鐓�",
-      name: "鐓",
+      coalId: "鐓",
       cv: 5800,
       price: 380,
       sulfur: 1.0,
@@ -659,7 +780,7 @@
     },
     {
       type: "鏈煡鐓�",
-      name: "鐓",
+      coalId: "鐓",
       cv: 5400,
       price: 320,
       sulfur: 1.4,
@@ -676,55 +797,66 @@
   ElMessage.success("琛ㄥ崟宸查噸缃�");
 };
 const addWarehoused = () => {
-  console.log(coalInfoList.value);
-  if (!result.value) {
-    ElMessage.error("璇峰厛璁$畻鏈�浼橀厤姣斿悗鍐嶆坊鍔犺嚦寰呭叆搴�");
-    return;
-  }
-  if (result.value.optimal === null) {
-    ElMessage.error("璇峰厛璁$畻鏈�浼橀厤姣�");
-    return;
-  }
-  if (!result.value.optimal.props.createCoal) {
-    ElMessage.error("璇峰厛閫夋嫨鐢熸垚鐓ょ");
-    return;
-  }
-  const coals = result.value.optimal.instructions.map((item) => item.name);
-  let allFound = true;
-  for (const element of coals) {
-    let found = false;
-    for (const item of coalInfoList.value) {
-      if (item.key === element) {
-        found = true;
-        break;
-      }
-    }
-    if (!found) {
-      allFound = false;
-      break;
+  // 楠岃瘉鍓嶇疆鏉′欢
+  const validationChecks = [
+    { condition: !result.value, message: "璇峰厛璁$畻鏈�浼橀厤姣斿悗鍐嶆坊鍔犺嚦寰呭叆搴�" },
+    { condition: !result.value.optimal, message: "璇峰厛璁$畻鏈�浼橀厤姣�" },
+    { condition: !result.value.optimal.props.createCoal, message: "璇峰厛閫夋嫨鐢熸垚鐓ょ" }
+  ];
+
+  for (const check of validationChecks) {
+    if (check.condition) {
+      ElMessage.error(check.message);
+      return;
     }
   }
-  if (!allFound) {
+
+  // 楠岃瘉閰嶆瘮涓殑鐓ょ
+  const coals = result.value.optimal.instructions.map(item => item.coalId);
+  const allCoalsFound = coals.every(coalId => 
+    coalInfoList.value.some(item => item.key === coalId)
+  );
+
+  if (!allCoalsFound) {
     ElMessage.error("閰嶆瘮涓寘鍚湭鐭ョ叅绉嶏紝璇峰厛娣诲姞鑷崇叅绉嶅垪琛�");
     return;
   }
-  let createCoalFound = false;
-  for (const item of coalInfoList.value) {
-    if (item.key === result.value.optimal.props.createCoalId) {
-      console.log("鐢熸垚鐓ょ宸插瓨鍦�");
-      createCoalFound = true;
-      break;
-    }
-  }
-  if (!createCoalFound) {
+
+  // 楠岃瘉鐢熸垚鐓ょ
+  const createCoalExists = coalInfoList.value.some(
+    item => item.key === result.value.optimal.props.coalId
+  );
+
+  if (!createCoalExists) {
     ElMessage.warning("鐢熸垚鐓ょ鏄湭鐭ョ叅绉嶏紝鏃犳硶娣诲姞鑷冲緟鍏ュ簱");
     return;
   }
+
+  // 鍑嗗鏁版嵁
+  const optimalData = result.value.optimal;
+  optimalData.props.totalTonnage = formInline.value.totalTonnage;
+  optimalData.props.cost = parseFloat(optimalData.props.cost.toFixed(2));
+  
+  // 娣诲姞瀹樻柟ID
+  optimalData.instructions.forEach(item => {
+    item.officialId = item.coalId;
+  });
+
+  const params = {
+    fieldsResultList: optimalData.props,
+    coalResultList: optimalData.instructions,
+  };
+
+  addPendingInventory(params).then(res => {
+    if (res.code === 200 && res.data === true) {
+      ElMessage.success("娣诲姞鑷冲緟鍏ュ簱鎴愬姛");
+    }
+  });
 };
 const submitForm = () => {
   // 鏁版嵁楠岃瘉
-  let validCoals = coalForms.value.filter(
-    (coal) => coal.name && coal.cv && coal.price
+  const validCoals = coalForms.value.filter(
+    coal => coal.coalId && coal.cv && coal.price
   );
 
   if (validCoals.length < 2) {
@@ -734,7 +866,7 @@
 
   try {
     // 姹傝В鏈�浼橀厤姣�
-    let ratios = solveBlend(validCoals, constraints.value);
+    const ratios = solveBlend(validCoals, constraints.value);
     if (!ratios) {
       data.result.error = "鏃犲彲琛岃В锛岃妫�鏌ョ害鏉熸潯浠舵垨鐓ょ鏁版嵁";
       data.result.show = true;
@@ -742,8 +874,8 @@
     }
 
     // 璁$畻缁撴灉
-    let props = calcBlendProps(validCoals, ratios);
-    let instructions = genInstructions(
+    const props = calcBlendProps(validCoals, ratios);
+    const instructions = genInstructions(
       validCoals,
       ratios,
       formInline.value.totalTonnage,
@@ -752,11 +884,7 @@
 
     data.result = {
       show: true,
-      optimal: {
-        ratios,
-        props,
-        instructions,
-      },
+      optimal: { ratios, props, instructions },
       alternatives: [],
       error: null,
     };
@@ -774,57 +902,42 @@
 
 const generateAlternatives = (coals) => {
   const altList = [
-    {
-      desc: "鍙戠儹閲忛檷1%",
-      mod: { minCalorific: constraints.value.minCalorific * 0.99 },
-    },
-    {
-      desc: "鍙戠儹閲忛檷2%",
-      mod: { minCalorific: constraints.value.minCalorific * 0.98 },
-    },
-    {
-      desc: "纭垎鍗�1%",
-      mod: { maxSulfur: constraints.value.maxSulfur * 1.01 },
-    },
-    {
-      desc: "纭垎鍗�2%",
-      mod: { maxSulfur: constraints.value.maxSulfur * 1.02 },
-    },
-    {
-      desc: "鍙戠儹閲忛檷0.5%涓旂~鍒嗗崌0.5%",
+    { desc: "鍙戠儹閲忛檷1%", mod: { minCalorific: constraints.value.minCalorific * 0.99 } },
+    { desc: "鍙戠儹閲忛檷2%", mod: { minCalorific: constraints.value.minCalorific * 0.98 } },
+    { desc: "纭垎鍗�1%", mod: { maxSulfur: constraints.value.maxSulfur * 1.01 } },
+    { desc: "纭垎鍗�2%", mod: { maxSulfur: constraints.value.maxSulfur * 1.02 } },
+    { 
+      desc: "鍙戠儹閲忛檷0.5%涓旂~鍒嗗崌0.5%", 
       mod: {
         minCalorific: constraints.value.minCalorific * 0.995,
         maxSulfur: constraints.value.maxSulfur * 1.005,
-      },
+      }
     },
   ];
 
-  data.result.alternatives = [];
-
-  for (let alt of altList) {
+  data.result.alternatives = altList.reduce((alternatives, alt) => {
     try {
-      let altConstraints = Object.assign({}, constraints.value, alt.mod);
-      let altRatios = solveBlend(coals, altConstraints);
-      if (!altRatios) continue;
+      const altConstraints = { ...constraints.value, ...alt.mod };
+      const altRatios = solveBlend(coals, altConstraints);
+      
+      if (altRatios) {
+        const altProps = calcBlendProps(coals, altRatios);
+        const altInstructions = genInstructions(
+          coals, altRatios, formInline.value.totalTonnage, formInline.value.scoopWeight
+        );
 
-      let altProps = calcBlendProps(coals, altRatios);
-      let altInstructions = genInstructions(
-        coals,
-        altRatios,
-        formInline.value.totalTonnage,
-        formInline.value.scoopWeight
-      );
-
-      data.result.alternatives.push({
-        desc: alt.desc,
-        ratios: altRatios,
-        props: altProps,
-        instructions: altInstructions,
-      });
+        alternatives.push({
+          desc: alt.desc,
+          ratios: altRatios,
+          props: altProps,
+          instructions: altInstructions,
+        });
+      }
     } catch (error) {
       console.warn(`澶囬�夋柟妗� ${alt.desc} 璁$畻澶辫触:`, error);
     }
-  }
+    return alternatives;
+  }, []);
 };
 
 const { formInline, constraints, coalForms, result } = toRefs(data);
@@ -836,30 +949,27 @@
     return;
   }
 
-  // 濡傛灉褰撳墠鏁扮粍闀垮害澶т簬鎵�闇�鏁伴噺锛屾埅鏂�
+  // 鎴柇澶氫綑鐨勬垨濉厖涓嶈冻鐨勭叅绉�
   if (coalForms.value.length > num) {
     coalForms.value = coalForms.value.slice(0, num);
-    return;
-  }
-
-  // 鍚﹀垯锛屽~鍏呮柊鐨勭┖瀵硅薄
-  while (coalForms.value.length < num) {
-    coalForms.value.push({
-      type: "鏈煡鐓�",
-      name: `鐓�${String.fromCharCode(65 + coalForms.value.length)}`,
-      cv: 0,
-      price: 0,
-      sulfur: "",
-      ash: "",
-      moisture: "",
-    });
+  } else {
+    while (coalForms.value.length < num) {
+      coalForms.value.push({
+        type: "鏈煡鐓�",
+        coalId: `鐓�${String.fromCharCode(65 + coalForms.value.length)}`,
+        cv: 0,
+        price: 0,
+        sulfur: "",
+        ash: "",
+        moisture: "",
+      });
+    }
   }
 };
 
 // 澶勭悊鐓ょ绫诲瀷鍙樺寲
 const handleCoalTypeChange = (index) => {
-  // 褰撶叅绉嶇被鍨嬫敼鍙樻椂锛屾竻绌虹叅绉嶅悕绉帮紝閬垮厤鏁版嵁娣蜂贡
-  coalForms.value[index].name = "";
+  coalForms.value[index].coalId = "";
 };
 </script>
 

--
Gitblit v1.9.3