zhang_12370
3 天以前 e986cee1c804ecdf6d03c080ce9a8bb187f724a4
1、煤质字段添加校验 使其唯一
2、优化首页 数据刷新
3、开发配煤计算器
已修改4个文件
已添加2个文件
1476 ■■■■■ 文件已修改
src/api/calculator/index.js 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/api/publicApi/index.js 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/basicInformation/index.vue 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/basicInformation/mould/coalMeiZhiZiDuanWeiHu.vue 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/calculator/index copy.vue 1380 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/index.vue 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/api/calculator/index.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,11 @@
import request from '@/utils/request'
// /officialInventory/coalBlendingList
// èŽ·å–ç…¤ç‚­é…æ¯”åˆ—è¡¨
export function getCoalBlendingList(query) {
  return request({
    url: '/officialInventory/coalBlendingList',
    method: 'get',
    params: query
  })
}
src/api/publicApi/index.js
@@ -29,4 +29,14 @@
        method: 'get',
        params: query
    })
}
// /coalField/coalFieldList
// æŸ¥è¯¢ç…¤è´¨å­—段列表
export function getCoalFieldList(query) {
    return request({
        url: '/coalField/coalFieldList',
        method: 'get',
        params: query
    })
}
src/views/basicInformation/index.vue
@@ -491,10 +491,10 @@
 * ç…¤è´¨æ–¹æ¡ˆè¡¨æ ¼åˆ—配置
 */
const coalQualityMaintenanceColumns = ref([
  { prop: "plan", label: "方案名称", minWidth: 100 },
  { prop: "plan", label: "煤质方案", minWidth: 100 },
  {
    prop: "fieldIds",
    label: "字段名称",
    label: "煤质字段",
    minWidth: 200,
    showOverflowTooltip: true,
    slot: true,
@@ -505,15 +505,15 @@
      return cellValue || "--";
    },
  },
  { prop: "schemeDesc", label: "字段描述", minWidth: 100 },
  { prop: "schemeDesc", label: "煤质描述", minWidth: 100 },
]);
/**
 * ç…¤è´¨å­—段表格列配置
 */
const coalMeiZhiZiDuanWeiHuColumns = ref([
  { prop: "fieldName", label: "字段名称", minWidth: 200 },
  { prop: "fieldDescription", label: "字段描述", minWidth: 200 },
  { prop: "fieldName", label: "煤质字段", minWidth: 200 },
  { prop: "fieldDescription", label: "煤质描述", minWidth: 200 },
]);
// ===== äº‹ä»¶å¤„理函数 =====
src/views/basicInformation/mould/coalMeiZhiZiDuanWeiHu.vue
@@ -19,6 +19,7 @@
              v-model="formData.fieldName"
              placeholder="请输入字段名称"
              :disabled="isViewMode"
              @blur="checkFieldNameExists"
          />
        </el-form-item>
        <el-form-item label="字段描述" prop="fieldDescription">
@@ -41,8 +42,10 @@
</template>
<script setup>
import {ref, reactive, watch, defineProps} from "vue";
import {ref, reactive, watch, defineProps, computed, onMounted} from "vue";
import {ElMessage} from "element-plus";
import {addOrEditCoalField} from "@/api/basicInformation/coalFieldMaintenance.js";
import {getCoalFieldList} from "@/api/publicApi/index.js";
const props = defineProps({
  form: {
@@ -64,6 +67,21 @@
const copyForm = defineModel("copyForm", {
  required: true,
  type: Object,
});
// å­˜å‚¨å·²æœ‰çš„字段列表
const existingFields = ref([]);
// ç»„件挂载时获取已有字段列表
onMounted(async () => {
  try {
    const {data, code} = await getCoalFieldList();
    if (code === 200) {
      existingFields.value = data || [];
    }
  } catch (error) {
    console.error("获取字段列表失败", error);
  }
});
// è¡¨å•引用
const formRef = ref();
@@ -128,9 +146,49 @@
  emit("handleBeforeClose");
  emit("update:coalMaintenanceFieldDialogVisible", false);
};
// æ£€æŸ¥å­—段名称是否已存在
const checkFieldNameExists = () => {
  if (!formData.value.fieldName) return;
  const isNameExists = existingFields.value.some(field =>
    field.fieldName === formData.value.fieldName
  );
  // ç¼–辑模式下,如果是当前字段的原名称则不提示
  if (isNameExists && !(props.addOrEdit === 'edit' && props.form.fieldName === formData.value.fieldName)) {
    ElMessage.warning("该字段名称已存在,请换其他名字");
    // å¯é€‰ï¼šè‡ªåŠ¨æ¸…ç©ºè¾“å…¥æ¡†
    // formData.value.fieldName = '';
  }
};
const rules = reactive({
  fieldName: [
    {required: true, message: "请输入煤种名称", trigger: "blur"},
    {required: true, message: "请输入字段名称", trigger: "blur"},
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback();
          return;
        }
        // æ£€æŸ¥å­—段名称是否已存在
        const isNameExists = existingFields.value.some(field =>
          field.fieldName === value
        );
        // ç¼–辑模式下,如果是当前字段的原名称则允许
        if (isNameExists) {
          if (props.addOrEdit === 'edit' && props.form.fieldName === value) {
            callback();
          } else {
            callback(new Error("该字段名称已存在,请换其他名字"));
          }
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
});
</script>
src/views/calculator/index copy.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,1380 @@
<template>
  <div class="app-container">
    <div class="view">
      <div class="left-card">
        <div class="count-region">数值输入区</div>
        <div class="scroll">
          <div>
            <div class="title">通用设置</div>
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
              label-width="110"
              label-position="top"
            >
              <el-row :gutter="16">
                <el-col :span="8">
                  <el-form-item label="待配煤种数量">
                    <el-input
                      v-model="formInline.num"
                      type="number"
                      style="width: 100%"
                      @change="updateCoalFields"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="参与配煤总吨数">
                    <el-input
                      v-model="formInline.totalTonnage"
                      type="number"
                      style="width: 100%"
                    >
                      <template v-slot:suffix>
                        <i style="font-style: normal">吨</i>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="每铲重量">
                    <el-input
                      v-model="formInline.scoopWeight"
                      type="number"
                      style="width: 100%"
                    >
                      <template v-slot:suffix>
                        <i style="font-style: normal">吨</i>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
          <div>
            <div class="title">煤种属性</div>
            <div class="coal-forms-container">
              <el-form
                :model="coalForms"
                :inline="true"
                label-width="110"
                label-position="top"
              >
                <div
                  v-for="(item, index) in coalForms"
                  :key="index"
                  style="margin-bottom: 15px"
                >
                  <el-row :gutter="16">
                    <el-col :span="6">
                      <el-form-item label="煤种类型">
                        <el-select
                          v-model="item.type"
                          placeholder="请选择"
                          style="width: 100%"
                          @change="handleCoalTypeChange(index)"
                        >
                          <el-option label="已有煤" value="已有煤" />
                          <el-option label="未知煤" value="未知煤" />
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item :label="'煤种' + (index + 1)">
                        <div class="input-wrapper">
                          <el-input
                            v-model="item.coalId"
                            v-show="item.type !== '已有煤'"
                            placeholder="请输入"
                            style="width: 100%"
                          />
                          <el-select
                            v-model="item.coalId"
                            :value="
                            infoCoals.find((coal) => coal.key === item.coalId)
                              ?.value || ''
                          "
                            v-show="item.type === '已有煤'"
                            placeholder="请选择"
                            style="width: 100%"
                          >
                            <el-option
                              v-for="ele in infoCoals"
                              :key="ele.key"
                              :label="ele.value"
                              :value="ele.key"
                              >{{ ele.value }}
                            </el-option>
                          </el-select>
                        </div>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="发热量">
                        <el-input
                          v-model="item.cv"
                          type="number"
                          style="width: 100%"
                        >
                          <template v-slot:suffix>
                            <i style="font-style: normal">kcal/kg</i>
                          </template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="ä»·æ ¼">
                        <el-input
                        v-if="item.type !== '未知煤'"
                          :value="
                            infoCoals.find((coal) => coal.key === item.coalId)
                              ?.item.priceExcludingTax || ''
                          "
                          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"
                          type="number"
                          style="width: 100%"
                        >
                          <template v-slot:suffix>
                            <i style="font-style: normal">元/吨</i>
                          </template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="16">
                    <el-col :span="6">
                      <el-form-item label="硫分">
                        <el-input
                          v-if="item.type !== '未知煤'"
                          :disabled="item.type === '已有煤'"
                          :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="可选"
                          style="width: 100%"
                        >
                          <template v-slot:suffix>
                            <i style="font-style: normal">%</i>
                          </template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="灰分">
                        <el-input
                          v-if="item.type !== '未知煤'"
                          :disabled="item.type === '已有煤'"
                          :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="可选"
                          style="width: 100%"
                        >
                          <template v-slot:suffix>
                            <i style="font-style: normal">%</i>
                          </template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label="水分">
                        <el-input
                        v-if="item.type !== '未知煤'"
                        :disabled="item.type === '已有煤'"
                        :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="可选"
                          style="width: 100%"
                        >
                          <template v-slot:suffix>
                            <i style="font-style: normal">%</i>
                          </template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-divider />
                </div>
              </el-form>
            </div>
          </div>
          <div>
            <div class="title">配煤约束条件</div>
            <el-form
              :inline="true"
              :model="constraints"
              class="demo-form-inline"
              label-width="110"
              label-position="top"
            >
              <el-row :gutter="16">
                <el-col :span="6">
                  <el-form-item label="混合煤最低发热量(CV)">
                    <el-input
                      v-model="constraints.minCalorific"
                      type="number"
                      style="width: 100%"
                    >
                      <template v-slot:suffix>
                        <i style="font-style: normal">kcal/kg</i>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="混合煤最高硫分">
                    <el-input
                      v-model="constraints.maxSulfur"
                      type="number"
                      placeholder="可选"
                      style="width: 100%"
                    >
                      <template v-slot:suffix>
                        <i style="font-style: normal">%</i>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="混合煤最高灰分">
                    <el-input
                      v-model="constraints.maxAsh"
                      type="number"
                      placeholder="可选"
                      style="width: 100%"
                    >
                      <template v-slot:suffix>
                        <i style="font-style: normal">%</i>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="混合煤最高水分">
                    <el-input
                      v-model="constraints.maxMoisture"
                      type="number"
                      placeholder="可选"
                      style="width: 100%"
                    >
                      <template v-slot:suffix>
                        <i style="font-style: normal">%</i>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </div>
        <div class="footer">
          <el-button @click="cancel">重置</el-button>
          <el-button type="primary" @click="addWarehoused" plain>
            æ·»åŠ è‡³å¾…å…¥åº“
          </el-button>
          <el-button type="primary" @click="submitForm">计算最优配比</el-button>
        </div>
      </div>
      <div class="right-card">
        <div class="count-region">配煤优化结果</div>
        <div class="result-scroll">
          <!-- é”™è¯¯ä¿¡æ¯ -->
          <div v-if="result.show && result.error" class="error-box">
            <el-alert
              :title="result.error"
              type="error"
              :closable="false"
              show-icon
            />
          </div>
          <!-- æœ€ä¼˜é…æ¯”结果 -->
          <div
            v-if="result.show && result.optimal && !result.error"
            class="result-section"
          >
            <div class="result-title">🎯 æœ€ä¼˜é…æ¯”结果</div>
            <!-- é…æ¯”表 -->
            <div class="table-container">
              <el-table
                :data="result.optimal.instructions"
                border
                size="small"
                class="result-table"
                style="width: 100%"
              >
                <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="quantity" label="吨数" min-width="80">
                  <template #default="scope">
                    {{ scope.row.quantity }}吨
                  </template>
                </el-table-column>
                <el-table-column prop="scoops" label="铲数" min-width="80">
                  <template #default="scope">
                    {{ scope.row.scoops }}铲
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <!-- æ··åˆç…¤å±žæ€§ -->
            <div class="props-section">
              <div class="props-title">📊 æ··åˆç…¤å±žæ€§</div>
              <div class="props-grid">
                <div class="prop-item">
                  <span class="prop-label">发热量:</span>
                  <span class="prop-value"
                    >{{ result.optimal.props.cv.toFixed(2) }} kcal/kg</span
                  >
                </div>
                <div class="prop-item">
                  <span class="prop-label">硫分:</span>
                  <span class="prop-value"
                    >{{ result.optimal.props.sulfur.toFixed(2) }}%</span
                  >
                </div>
                <div class="prop-item">
                  <span class="prop-label">灰分:</span>
                  <span class="prop-value"
                    >{{ result.optimal.props.ash.toFixed(2) }}%</span
                  >
                </div>
                <div class="prop-item">
                  <span class="prop-label">水分:</span>
                  <span class="prop-value"
                    >{{ result.optimal.props.moisture.toFixed(2) }}%</span
                  >
                </div>
                <div class="prop-item">
                  <span class="prop-label">成本:</span>
                  <span class="prop-value cost"
                    >{{ result.optimal.props.cost.toFixed(2) }} å…ƒ/吨</span
                  >
                </div>
                <div class="prop-item">
                  <span class="prop-label">生成:</span>
                  <el-autocomplete
                    v-model="result.optimal.props.createCoal"
                    :fetch-suggestions="querySearch"
                    clearable
                    size="small"
                    class="inline-input red-border"
                    style="width: 180px; min-height: 24px !important"
                    placeholder="请输入生成煤种"
                    @blur="handleSelect($event)"
                    @select="handleSelect($event)"
                  />
                </div>
              </div>
            </div>
          </div>
          <!-- å¤‡é€‰æ–¹æ¡ˆ -->
          <div
            v-if="result.show && result.alternatives.length > 0"
            class="alternatives-section"
          >
            <div class="result-title">🔄 å¤‡é€‰æ–¹æ¡ˆ</div>
            <div
              v-for="(alt, index) in result.alternatives"
              :key="index"
              class="alt-item"
            >
              <div class="alt-title">{{ alt.desc }}</div>
              <div class="table-container">
                <el-table
                  :data="alt.instructions"
                  border
                  size="small"
                  class="alt-table"
                  style="width: 100%"
                >
                  <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="quantity" label="吨数" min-width="80">
                    <template #default="scope">
                      {{ scope.row.quantity }}吨
                    </template>
                  </el-table-column>
                  <el-table-column prop="scoops" label="铲数" min-width="80">
                    <template #default="scope">
                      {{ scope.row.scoops }}铲
                    </template>
                  </el-table-column>
                </el-table>
              </div>
              <div class="alt-props">
                <span>发热量: {{ alt.props.cv.toFixed(2) }} kcal/kg,</span>
                <span>硫分: {{ alt.props.sulfur.toFixed(2) }}%,</span>
                <span>灰分: {{ alt.props.ash.toFixed(2) }}%,</span>
                <span>水分: {{ alt.props.moisture.toFixed(2) }}%,</span>
                <span class="cost"
                  >成本: {{ alt.props.cost.toFixed(2) }} å…ƒ/吨</span
                >
              </div>
            </div>
          </div>
          <!-- ç©ºçŠ¶æ€ -->
          <div v-if="!result.show" class="empty-state">
            <el-empty description="点击左侧计算最优配比按钮查看结果" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, toRefs, nextTick, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getCoalInfoList } from "@/api/procureMent"; // å‡è®¾æœ‰ä¸€ä¸ªAPI获取煤种信息
import { getCoalBlendingList } from "@/api/calculator/index.js";
const data = reactive({
  formInline: {
    num: 3, // é»˜è®¤3个煤种
    totalTonnage: 1000, // å‚与配煤总吨数
    scoopWeight: 50, // æ¯é“²é‡é‡
  },
  // çº¦æŸæ¡ä»¶
  constraints: {
    minCalorific: 5600, // æ··åˆç…¤æœ€ä½Žå‘热量
    maxSulfur: 1.2, // æ··åˆç…¤æœ€é«˜ç¡«åˆ†
    maxAsh: 15.0, // æ··åˆç…¤æœ€é«˜ç°åˆ†
    maxMoisture: "", // æ··åˆç…¤æœ€é«˜æ°´åˆ†
  },
  coalForms: [
    {
      type: "未知煤",
      coalId: "煤A",
      cv: 6200, // å‘热量
      price: 450, // ä»·æ ¼
      sulfur: 0.6, // ç¡«åˆ†
      ash: 12.0, // ç°åˆ†
      moisture: 8.0, // æ°´åˆ†
    },
    {
      type: "未知煤",
      coalId: "煤B",
      cv: 5800,
      price: 380,
      sulfur: 1.0,
      ash: 14.0,
      moisture: 10.0,
    },
    {
      type: "未知煤",
      coalId: "煤C",
      cv: 5400,
      price: 320,
      sulfur: 1.4,
      ash: 16.0,
      moisture: 12.0,
    },
  ],
  // è®¡ç®—结果
  result: {
    show: false,
    optimal: null,
    alternatives: [],
    error: null,
    createCoal: null,
  },
});
const coalInfoList = ref([]);
// onMounted
const getCoalInfo = async () => {
  let result = await getCoalInfoList();
  if (result.code === 200) {
    result.data.forEach((item) => {
      let obj = {
        value: item.coal,
        key: item.id,
      };
      coalInfoList.value.push(obj);
    });
  } else {
    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;
};
// è‡ªåŠ¨è¡¥å…¨æœç´¢
const querySearch = (q, cb) => {
  const res = q
    ? coalInfoList.value.filter((c) => c.value.includes(q))
    : coalInfoList.value;
  cb(res);
};
// é€‰æ‹©/失焦时,优先存 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
  );
  result.value.optimal.props.createCoal = found ? found.key : val;
  // æ–°å¢žï¼šå¦‚果匹配成功,留一个id字段
  if (found) {
    result.value.optimal.props.coalId = found.key;
  } else {
    result.value.optimal.props.coalId = null;
  }
  let match = matchCoalName(result.value.optimal.props.createCoal);
  if (match && match !== result.value.optimal.props.createCoal) {
    result.value.optimal.props.createCoal = match;
  }
};
onMounted(async () => {
  getCoalInfo();
  geInfoCoals();
});
const infoCoals = ref([]);
// åˆå§‹åŒ–煤种字段
const geInfoCoals = async () => {
  let res = await getCoalBlendingList();
  if (res.code === 200) {
    infoCoals.value = res.data.map((item) => ({
      value: item.supplierCoal,
      key: item.coalId,
      item,
    }));
    console.log(infoCoals.value);
  } else {
    ElMessage.error("获取煤种信息失败,请稍后重试");
  }
};
// çº¿æ€§è§„划求解函数
const solveBlend = (coals, constraints) => {
  // æ•°æ®éªŒè¯
  if (constraints.maxSulfur) {
    let missingSulfur = coals.some((coal) => !coal.sulfur && coal.sulfur !== 0);
    if (missingSulfur) {
      throw new Error(
        "如果设置了最大硫分约束,则所有参与配比的煤种都必须提供硫分数据。"
      );
    }
  }
  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("如果设置了最大水分约束,则所有煤种都必须提供水分数据。");
    }
  }
  // ç®€å•的线性规划求解(最小化成本)
  // è¿™é‡Œä½¿ç”¨ç®€åŒ–的算法,实际项目中可以集成更专业的求解器
  try {
    // æ¨¡æ‹Ÿæ±‚解过程
    let totalCoals = coals.length;
    let ratios = new Array(totalCoals).fill(0);
    // ç®€å•的等权重分配作为初始解
    let avgRatio = 1 / totalCoals;
    ratios = ratios.map(() => avgRatio);
    // éªŒè¯çº¦æŸæ¡ä»¶
    let blendProps = calcBlendProps(coals, ratios);
    if (constraints.minCalorific && blendProps.cv < constraints.minCalorific) {
      // è°ƒæ•´é…æ¯”以满足最低发热量
      let highCvCoals = 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;
    }
    return ratios;
  } catch (error) {
    throw error;
  }
};
// è®¡ç®—混合属性
const calcBlendProps = (coals, ratios) => {
  let cv = 0,
    sulfur = 0,
    ash = 0,
    moisture = 0,
    cost = 0;
  for (let i = 0; i < coals.length; i++) {
    cv += ratios[i] * Number(coals[i].cv || 0);
    sulfur += ratios[i] * Number(coals[i].sulfur || 0);
    ash += ratios[i] * Number(coals[i].ash || 0);
    moisture += ratios[i] * Number(coals[i].moisture || 0);
    cost += ratios[i] * Number(coals[i].price || 0);
  }
  return { cv, sulfur, ash, moisture, cost };
};
// ç”Ÿæˆæ“ä½œæŒ‡ä»¤
const genInstructions = (coals, ratios, total, scoop) => {
  return coals
    .map((coal, i) => {
      if (ratios[i] < 1e-6) return null;
      let quantity = ratios[i] * total;
      let scoops = quantity / scoop;
      return {
        coalId: coal.coalId,
        ratio: (ratios[i] * 100).toFixed(2),
        quantity: quantity.toFixed(1),
        scoops: scoops.toFixed(1),
      };
    })
    .filter(Boolean);
};
const cancel = () => {
  // é‡ç½®è¡¨å•逻辑
  data.formInline = {
    num: 3,
    totalTonnage: 1000,
    scoopWeight: 50,
  };
  data.constraints = {
    minCalorific: 5600,
    maxSulfur: 1.2,
    maxAsh: 15.0,
    maxMoisture: "",
  };
  data.coalForms = [
    {
      type: "未知煤",
      coalId: "煤A",
      cv: 6200,
      price: 450,
      sulfur: 0.6,
      ash: 12.0,
      moisture: 8.0,
    },
    {
      type: "未知煤",
      coalId: "煤B",
      cv: 5800,
      price: 380,
      sulfur: 1.0,
      ash: 14.0,
      moisture: 10.0,
    },
    {
      type: "未知煤",
      coalId: "煤C",
      cv: 5400,
      price: 320,
      sulfur: 1.4,
      ash: 16.0,
      moisture: 12.0,
    },
  ];
  data.result = {
    show: false,
    optimal: null,
    alternatives: [],
    error: null,
  };
  ElMessage.success("表单已重置");
};
const addWarehoused = () => {
  console.log("添加至待入库数据:", result.value.optimal);
  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.coalId);
  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;
    }
  }
  if (!allFound) {
    ElMessage.error("配比中包含未知煤种,请先添加至煤种列表");
    return;
  }
  let createCoalFound = false;
  for (const item of coalInfoList.value) {
    if (item.key === result.value.optimal.props.coalId) {
      createCoalFound = true;
      break;
    }
  }
  if (!createCoalFound) {
    ElMessage.warning("生成煤种是未知煤种,无法添加至待入库");
    return;
  }
  // cost保留两位小数
  result.value.optimal.props.cost = parseFloat(
    result.value.optimal.props.cost.toFixed(2)
  );
  result.value.optimal.props.totalTonnage = formInline.value.totalTonnage;
  const optimalArray = Object.entries(result.value.optimal.props).map(
    ([key, value]) => ({
      [key]: value,
    })
  );
  let arr = [[...optimalArray], [...result.value.optimal.instructions]];
  console.log("添加至待入库数据:", arr);
};
const submitForm = () => {
  // æ•°æ®éªŒè¯
  let validCoals = coalForms.value.filter(
    (coal) => coal.coalId && coal.cv && coal.price
  );
  if (validCoals.length < 2) {
    ElMessage.error("至少需要2个有效的煤种数据(名称、发热量、价格为必填)");
    return;
  }
  try {
    // æ±‚解最优配比
    let ratios = solveBlend(validCoals, constraints.value);
    if (!ratios) {
      data.result.error = "无可行解,请检查约束条件或煤种数据";
      data.result.show = true;
      return;
    }
    // è®¡ç®—结果
    let props = calcBlendProps(validCoals, ratios);
    let instructions = genInstructions(
      validCoals,
      ratios,
      formInline.value.totalTonnage,
      formInline.value.scoopWeight
    );
    data.result = {
      show: true,
      optimal: {
        ratios,
        props,
        instructions,
      },
      alternatives: [],
      error: null,
    };
    // ç”Ÿæˆå¤‡é€‰æ–¹æ¡ˆ
    generateAlternatives(validCoals);
    ElMessage.success("配煤优化计算完成");
  } catch (error) {
    data.result.error = error.message || "计算过程中发生错误";
    data.result.show = true;
    ElMessage.error(data.result.error);
  }
};
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%",
      mod: {
        minCalorific: constraints.value.minCalorific * 0.995,
        maxSulfur: constraints.value.maxSulfur * 1.005,
      },
    },
  ];
  data.result.alternatives = [];
  for (let alt of altList) {
    try {
      let altConstraints = Object.assign({}, constraints.value, alt.mod);
      let altRatios = solveBlend(coals, altConstraints);
      if (!altRatios) continue;
      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,
      });
    } catch (error) {
      console.warn(`备选方案 ${alt.desc} è®¡ç®—失败:`, error);
    }
  }
};
const { formInline, constraints, coalForms, result } = toRefs(data);
const updateCoalFields = () => {
  const num = parseInt(formInline.value.num);
  if (isNaN(num) || num <= 0) {
    coalForms.value = [];
    return;
  }
  // å¦‚果当前数组长度大于所需数量,截断
  if (coalForms.value.length > num) {
    coalForms.value = coalForms.value.slice(0, num);
    return;
  }
  // å¦åˆ™ï¼Œå¡«å……新的空对象
  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].coalId = "";
};
</script>
<style scoped lang="scss">
.view {
  display: flex;
  gap: 10px;
}
.left-card {
  background: #fff;
  flex: 1;
  min-width: 0;
  padding: 16px;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.coal-forms-container {
  overflow-x: auto;
  padding-bottom: 8px;
}
.coal-forms-container::-webkit-scrollbar {
  height: 6px;
}
.coal-forms-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}
.coal-forms-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}
.coal-forms-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
.count-region {
  font-size: 18px;
  color: #000000;
  line-height: 25px;
}
.scroll {
  height: calc(100vh - 14em);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 8px;
}
.scroll::-webkit-scrollbar {
  width: 6px;
}
.scroll::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}
.scroll::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}
.scroll::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
.title {
  font-size: 14px;
  color: #165dff;
  line-height: 20px;
  font-weight: 600;
  padding-left: 10px;
  position: relative;
  margin: 6px 0;
}
.title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 3px; /* è°ƒæ•´åž‚直位置 */
  width: 4px; /* å°æ•°æ¡å®½åº¦ */
  height: 14px; /* å°æ•°æ¡é«˜åº¦ */
  background-color: #165dff; /* è“è‰² */
}
.el-divider--horizontal {
  margin: 12px 0;
}
.footer {
  text-align: right;
}
.right-card {
  background: #fff;
  width: 600px;
  min-width: 400px;
  height: auto;
  padding: 16px;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.result-scroll {
  height: calc(100vh - 14em);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 8px;
}
.result-scroll::-webkit-scrollbar {
  width: 6px;
}
.result-scroll::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}
.result-scroll::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}
.result-scroll::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
.error-box {
  margin-bottom: 20px;
}
.result-section,
.alternatives-section {
  margin-bottom: 20px;
}
.result-title {
  font-size: 16px;
  color: #165dff;
  font-weight: 600;
  margin-bottom: 15px;
  padding-left: 10px;
  position: relative;
}
.result-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 3px;
  width: 4px;
  height: 16px;
  background-color: #165dff;
}
.result-table,
.alt-table {
  margin-bottom: 15px;
}
.table-container {
  overflow-x: auto;
  margin-bottom: 15px;
  border-radius: 4px;
}
.table-container::-webkit-scrollbar {
  height: 6px;
}
.table-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}
.table-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}
.table-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
.input-wrapper {
  position: relative;
  min-height: 32px;
  width: 100%;
}
.input-wrapper .el-input,
.input-wrapper .el-select {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  transition: opacity 0.2s ease-in-out;
}
/* ç¡®ä¿input-wrapper内的组件宽度 */
.input-wrapper :deep(.el-input),
.input-wrapper :deep(.el-select) {
  width: 100% !important;
}
.input-wrapper :deep(.el-input__wrapper),
.input-wrapper :deep(.el-select .el-input__wrapper) {
  width: 100% !important;
}
.props-section {
  margin-top: 15px;
}
.props-title {
  font-size: 14px;
  color: #333;
  font-weight: 600;
  margin-bottom: 10px;
}
.props-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.prop-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 12px;
  background: #f5f7fa;
  border-radius: 4px;
  font-size: 13px;
  align-items: center;
}
.prop-label {
  color: #606266;
}
.prop-value {
  font-weight: 600;
  color: #303133;
}
.prop-value.cost {
  color: #e6a23c;
  font-weight: bold;
}
.alt-item {
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  background: #fafafa;
}
.alt-title {
  font-size: 14px;
  color: #409eff;
  font-weight: 600;
  margin-bottom: 10px;
}
.alt-props {
  font-size: 12px;
  color: #606266;
  margin-top: 10px;
  line-height: 1.6;
}
.alt-props .cost {
  color: #e6a23c;
  font-weight: 600;
}
.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
}
/* é˜²æ­¢é¡µé¢æŠ–动的样式 */
:deep(.el-input) {
  width: 100% !important;
  min-width: 100% !important;
}
:deep(.el-select) {
  width: 100% !important;
  min-width: 100% !important;
}
:deep(.el-form-item) {
  margin-bottom: 18px;
  width: 100%;
}
:deep(.el-form-item__label) {
  padding-bottom: 6px;
  font-size: 14px;
  line-height: 1.5;
  height: auto;
}
:deep(.el-form-item__content) {
  min-height: 32px;
  line-height: 32px;
  width: 100%;
}
:deep(.el-col) {
  padding-right: 8px;
  box-sizing: border-box;
}
:deep(.el-col:last-child) {
  padding-right: 0;
}
/* ç¡®ä¿è¾“入框容器有固定高度和宽度 */
:deep(.el-input__wrapper) {
  min-height: 32px;
  box-sizing: border-box;
  width: 100% !important;
  min-width: 100% !important;
}
:deep(.el-select .el-input__wrapper) {
  min-height: 32px;
  box-sizing: border-box;
  width: 100% !important;
  min-width: 100% !important;
}
/* é˜²æ­¢tooltip引起的抖动 */
:deep(.el-tooltip) {
  display: block;
  width: 100%;
}
/* ç»Ÿä¸€è¡Œé«˜å’Œé—´è· */
:deep(.el-row) {
  margin-bottom: 0;
}
/* é˜²æ­¢å†…容变化引起的布局跳动 */
:deep(.el-input__inner),
:deep(.el-select__input) {
  min-height: 30px;
  line-height: 30px;
}
/* å“åº”式设计 */
@media (max-width: 1200px) {
  .view {
    flex-direction: column;
  }
  .left-card {
    width: 100%;
  }
  .right-card {
    width: 100%;
    min-width: auto;
  }
  .scroll {
    height: calc(100vh - 20em);
  }
  .result-scroll {
    height: calc(100vh - 20em);
  }
}
@media (max-width: 768px) {
  .props-grid {
    grid-template-columns: 1fr;
  }
  .table-container {
    font-size: 12px;
  }
  :deep(.el-table .cell) {
    padding: 4px 8px;
  }
}
:deep(.el-input__wrapper) {
  min-height: 24px !important;
}
:deep(.el-input__inner) {
  min-height: 24px !important;
}
</style>
src/views/index.vue
@@ -495,6 +495,9 @@
    Xkeys,
    Yvalues,
  };
  nextTick(() => {
      initAreaChart();
    });
};
onMounted(() => {
  getList();