zhang_12370
3 天以前 aaf943ada13005f2b22d01e3d7080b8740d9aa96
src/views/calculator/index.vue
@@ -85,25 +85,26 @@
                      <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"
                            >{{ ele.value }}
                          </el-option>
                              >{{ ele.value }}
                            </el-option>
                          </el-select>
                        </div>
                      </el-form-item>
@@ -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>
@@ -255,8 +340,8 @@
        </div>
        <div class="footer">
          <el-button @click="cancel">重置</el-button>
          <el-button type="primary" @click="submitForm" plain>
            查看计算结果
          <el-button type="primary" @click="addWarehoused" plain>
            添加至待入库
          </el-button>
          <el-button type="primary" @click="submitForm">计算最优配比</el-button>
        </div>
@@ -289,13 +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">
                    {{
                      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">
@@ -313,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">
@@ -340,6 +434,20 @@
                    >{{ 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>
@@ -364,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">
@@ -384,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>
@@ -405,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"; // 假设有一个API获取煤种信息
import { getCoalBlendingList,addPendingInventory } from "@/api/calculator/index.js";
const data = reactive({
  formInline: {
@@ -425,7 +543,7 @@
  coalForms: [
    {
      type: "未知煤",
      name: "煤A",
      coalId: "煤A",
      cv: 6200, // 发热量
      price: 450, // 价格
      sulfur: 0.6, // 硫分
@@ -434,7 +552,7 @@
    },
    {
      type: "未知煤",
      name: "煤B",
      coalId: "煤B",
      cv: 5800,
      price: 380,
      sulfur: 1.0,
@@ -443,7 +561,7 @@
    },
    {
      type: "未知煤",
      name: "煤C",
      coalId: "煤C",
      cv: 5400,
      price: 320,
      sulfur: 1.4,
@@ -457,11 +575,43 @@
    optimal: null,
    alternatives: [],
    error: null,
    createCoal: null,
  },
});
const handleCoalSelectChange = (index, coalId) => {
  const selectedCoal = infoCoals.value.find(item => item.key === coalId);
  if (selectedCoal && selectedCoal.item && selectedCoal.item.coalValues) {
    console.log('选中的煤种:', selectedCoal);
    // 安全地获取各项煤质参数
    const getCoalValue = (fieldName) => {
      const coalValue = selectedCoal.item.coalValues.find(
        (value) => value.fieldName === fieldName
      );
      return coalValue ? coalValue.coalValue : '';
    };
    // 更新表单数据
    data.coalForms[index].price = selectedCoal.item.priceExcludingTax || 0;
    data.coalForms[index].cv = getCoalValue("发热量") || 0;
    data.coalForms[index].sulfur = getCoalValue("硫分") || 0;
    data.coalForms[index].ash = getCoalValue("灰分") || 0;
    data.coalForms[index].moisture = getCoalValue("水分") || 0;
    // 设置价格(如果有的话)
    if (selectedCoal.item.priceExcludingTax) {
      data.coalForms[index].price = selectedCoal.item.priceExcludingTax;
    }
    console.log('更新后的表单数据:', data.coalForms[index]);
  } else {
    console.warn('未找到选中的煤种数据或数据格式不正确');
  }
};
const coalInfoList = ref([]);
// onMounted
onMounted(async () => {
const getCoalInfo = async () => {
  let result = await getCoalInfoList();
  if (result.code === 200) {
    result.data.forEach((item) => {
@@ -474,7 +624,70 @@
  } 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) => {
  // 数据验证
@@ -554,12 +767,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),
      };
    })
@@ -582,7 +795,7 @@
  data.coalForms = [
    {
      type: "未知煤",
      name: "煤A",
      coalId: "煤A",
      cv: 6200,
      price: 450,
      sulfur: 0.6,
@@ -591,7 +804,7 @@
    },
    {
      type: "未知煤",
      name: "煤B",
      coalId: "煤B",
      cv: 5800,
      price: 380,
      sulfur: 1.0,
@@ -600,7 +813,7 @@
    },
    {
      type: "未知煤",
      name: "煤C",
      coalId: "煤C",
      cv: 5400,
      price: 320,
      sulfur: 1.4,
@@ -616,12 +829,78 @@
  };
  ElMessage.success("表单已重置");
};
const addWarehoused = () => {
  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;
  }
  // result.value.optimal.props fieldsResultList
  // result.value.optimal.instructions 表格数据coalResultList
  console.log("添加至待入库数据:", result.value.optimal);
  // cost保留两位小数
  result.value.optimal.props.totalTonnage = formInline.value.totalTonnage;
  result.value.optimal.instructions.forEach((item) => {
    item.officialId = item.coalId; // 添加官方ID字段
  });
  result.value.optimal.props.cost = parseFloat(
    result.value.optimal.props.cost.toFixed(2)
  );
  result.value.optimal.props.totalTonnage = formInline.value.totalTonnage;
  let arr = [result.value.optimal.props, [...result.value.optimal.instructions]];
  let params = {
    fieldsResultList: arr[0],
    coalResultList: arr[1],
  };
 addPendingInventory(params).then((res)=>{
   if(res.code === 200 && res.data== true){
     ElMessage.success("添加至待入库成功");
   }
 })
};
const submitForm = () => {
  console.log(coalForms.value)
  console.log("煤种信息:", coalForms.value);
  // 数据验证
  let validCoals = coalForms.value.filter(
    (coal) => coal.name && coal.cv && coal.price
    (coal) => coal.coalId && coal.cv && coal.price
  );
  if (validCoals.length < 2) {
@@ -743,7 +1022,7 @@
  while (coalForms.value.length < num) {
    coalForms.value.push({
      type: "未知煤",
      name: `煤${String.fromCharCode(65 + coalForms.value.length)}`,
      coalId: `煤${String.fromCharCode(65 + coalForms.value.length)}`,
      cv: 0,
      price: 0,
      sulfur: "",
@@ -756,11 +1035,11 @@
// 处理煤种类型变化
const handleCoalTypeChange = (index) => {
  // 当煤种类型改变时,清空煤种名称,避免数据混乱
  coalForms.value[index].name = "";
  coalForms.value[index].coalId = "";
};
</script>
<style scoped>
<style scoped lang="scss">
.view {
  display: flex;
  gap: 10px;
@@ -992,6 +1271,7 @@
  background: #f5f7fa;
  border-radius: 4px;
  font-size: 13px;
  align-items: center;
}
.prop-label {
@@ -1150,4 +1430,10 @@
    padding: 4px 8px;
  }
}
:deep(.el-input__wrapper) {
  min-height: 24px !important;
}
:deep(.el-input__inner) {
  min-height: 24px !important;
}
</style>