feat: restructure basic info menu, add kitting monitor table, and implement user preferences api

This commit is contained in:
DXC
2026-03-24 09:29:20 +08:00
parent 706d7e551c
commit b5610de1f1
9 changed files with 775 additions and 457 deletions

View File

@ -1,7 +1,9 @@
from flask import Blueprint
from .inbound import inbound_bp
from .bom import bom_bp
from .user import user_bp
v1_bp = Blueprint('v1', __name__)
v1_bp.register_blueprint(inbound_bp, url_prefix='/inbound')
v1_bp.register_blueprint(bom_bp, url_prefix='/bom')
v1_bp.register_blueprint(user_bp, url_prefix='/user')

View File

@ -375,11 +375,11 @@ def calculate_kitting():
if not entries or not isinstance(entries, list):
return jsonify({'code': 400, 'msg': '参数格式错误,需要数组'}), 400
results = BomService.calculate_kitting(entries)
result = BomService.calculate_kitting(entries)
return jsonify({
'code': 200,
'msg': '计算成功',
'data': results
'data': result
})
except Exception as e:
current_app.logger.error(f'MRP齐套计算失败: {str(e)}')

View File

@ -0,0 +1,65 @@
from flask import Blueprint, request, jsonify, current_app
from app.models.system import SysUser
from app.extensions import db
from flask_jwt_extended import jwt_required, get_jwt_identity
from app.utils.decorators import audit_log
user_bp = Blueprint('user', __name__)
# ==============================================================================
# 用户偏好配置 API
# ==============================================================================
@user_bp.route('/preferences', methods=['GET'])
@jwt_required()
def get_preferences():
"""
读取当前用户的 preferences 字段
GET /api/v1/user/preferences
"""
try:
user_id = get_jwt_identity()
user = SysUser.query.get(user_id)
if not user:
return jsonify({'code': 404, 'msg': '用户不存在'}), 404
return jsonify({
'code': 200,
'msg': 'success',
'data': user.preferences or {}
})
except Exception as e:
current_app.logger.error(f'读取用户偏好失败: {str(e)}')
return jsonify({'code': 500, 'msg': f'读取失败: {str(e)}'}), 500
@user_bp.route('/preferences', methods=['PUT'])
@jwt_required()
@audit_log(module_name='系统', action_type='修改偏好配置')
def save_preferences():
"""
保存/更新当前用户的 preferences 字段
PUT /api/v1/user/preferences
入参: 任意 JSON 字典
"""
try:
user_id = get_jwt_identity()
user = SysUser.query.get(user_id)
if not user:
return jsonify({'code': 404, 'msg': '用户不存在'}), 404
new_prefs = request.get_json()
if new_prefs is None or not isinstance(new_prefs, dict):
return jsonify({'code': 400, 'msg': '参数必须是 JSON 对象'}), 400
user.preferences = new_prefs
db.session.commit()
return jsonify({
'code': 200,
'msg': '保存成功',
'data': user.preferences
})
except Exception as e:
db.session.rollback()
current_app.logger.error(f'保存用户偏好失败: {str(e)}')
return jsonify({'code': 500, 'msg': f'保存失败: {str(e)}'}), 500

View File

@ -22,6 +22,7 @@ class SysUser(db.Model):
role = db.Column(db.String(50))
status = db.Column(db.String(20), default='active')
password_hash = db.Column(db.Text)
preferences = db.Column(db.JSON, default=dict) # 用户偏好/个性化配置(如齐套监控列表)
created_at = db.Column(db.DateTime, default=beijing_time)
def set_password(self, password):
@ -61,6 +62,7 @@ class SysUser(db.Model):
'department': self.department,
'role': self.role,
'status': self.status,
'preferences': self.preferences or {},
'created_at': self.created_at.isoformat() if self.created_at else None
}

View File

@ -252,7 +252,7 @@ class BomService:
# ====================== MRP 齐套模拟计算 ======================
@staticmethod
def calculate_kitting(entries: list) -> list:
def calculate_kitting(entries: list) -> dict:
"""
MRP 齐套模拟计算
@ -261,13 +261,20 @@ class BomService:
2. 按子件 base_id 合并需求量:需求量 = dosage * (1 + loss_rate/100) * target_qty
3. 跨 StockBuy / StockSemi / StockProduct 聚合可用库存
4. 计算缺口shortage = available_quantity - required_quantity
5. 计算每个 BOM 的"当前库存可生产最大套数"(木桶效应)
:param entries: [{"bom_no": "BOM-001", "target_qty": 10}, ...]
:return: [{base_id, name, spec, unit, required_qty, available_qty, shortage, children: [...]}, ...]
:return: {
"bom_summary": [{bom_no, parent_name, max_producible}],
"materials": [{base_id, material_name, spec, unit, required_qty, available_qty, shortage, bom_sources}]
}
"""
# Step 1: 展开所有 BOM 的子件,聚合需求量
demand_map = {} # child_id -> {base_id, material_name, spec, unit, required_qty, bom_sources: []}
# 记录每个 entry 的元信息(用于后续 per-BOM 产量计算)
entry_meta = {} # bom_no -> {parent_name, version, children: {child_id: {dosage, loss_rate}}}
for entry in entries:
bom_no = entry.get('bom_no')
target_qty = float(entry.get('target_qty', 0) or 0)
@ -286,6 +293,18 @@ class BomService:
if not latest_version:
continue
# 获取父件名称
parent_row = db.session.query(
BomTable.parent_id, MaterialBase.name
).join(
MaterialBase, BomTable.parent_id == MaterialBase.id
).filter(
BomTable.bom_no == bom_no,
BomTable.version == latest_version
).first()
parent_name = parent_row.name if parent_row else ''
# 查询该 BOM 所有子件
rows = db.session.query(
BomTable, MaterialBase.name, MaterialBase.spec_model, MaterialBase.unit
@ -297,12 +316,26 @@ class BomService:
BomTable.is_enabled == True
).all()
entry_meta[bom_no] = {
'parent_name': parent_name,
'version': latest_version,
'children': {}
}
for bom, child_name, child_spec, child_unit in rows:
dosage = float(bom.dosage or 0)
loss_rate = float(bom.loss_rate or 0)
adj_dosage = dosage * (1 + loss_rate / 100.0)
qty_needed = adj_dosage * target_qty
# 记录 per-unit 用量(用于 max_producible 计算)
entry_meta[bom_no]['children'][bom.child_id] = {
'dosage': dosage,
'loss_rate': loss_rate,
'adj_dosage': adj_dosage,
'per_unit': adj_dosage # 每生产1套该BOM所需的此子件数量
}
if bom.child_id not in demand_map:
demand_map[bom.child_id] = {
'base_id': bom.child_id,
@ -323,14 +356,11 @@ class BomService:
# Step 2: 批量查询三张库存表的可用库存
child_ids = list(demand_map.keys())
if not child_ids:
return []
return {'bom_summary': [], 'materials': []}
# StockBuy.available_quantity, StockSemi.available_quantity, StockProduct.available_quantity
available_map = {cid: 0.0 for cid in child_ids}
for model_cls in (StockBuy, StockSemi, StockProduct):
if model_cls is None:
continue
rows = db.session.query(
model_cls.base_id,
func.coalesce(model_cls.available_quantity, 0)
@ -341,12 +371,12 @@ class BomService:
if base_id in available_map:
available_map[base_id] += float(qty)
# Step 3: 构造结果,计算缺口
results = []
# Step 3: 构造物料结果,计算缺口
materials = []
for base_id, info in demand_map.items():
avail = available_map.get(base_id, 0.0)
shortage = avail - info['required_qty']
results.append({
materials.append({
'base_id': base_id,
'material_name': info['material_name'],
'spec': info['spec'],
@ -357,9 +387,34 @@ class BomService:
'bom_sources': info['bom_sources']
})
# 按缺件数量降序排列(最缺的排前面)
results.sort(key=lambda x: x['shortage'])
return results
# 按缺件数量升序(最缺的排前面)
materials.sort(key=lambda x: x['shortage'])
# Step 4: 计算每个 BOM 的"当前库存可生产最大套数"(木桶效应)
# 算法:对每个 BOM 的所有子件,计算 Floor(available_qty / per_unit_demand)
# 取最小值 = 该 BOM 的最大可生产套数
bom_summary = []
for bom_no, meta in entry_meta.items():
min_producible = float('inf')
for child_id, child_info in meta['children'].items():
avail = available_map.get(child_id, 0.0)
per_unit = child_info['adj_dosage']
if per_unit > 0:
producible = int(avail // per_unit)
if producible < min_producible:
min_producible = producible
max_prod = int(min_producible) if min_producible != float('inf') else 0
bom_summary.append({
'bom_no': bom_no,
'parent_name': meta['parent_name'],
'version': meta['version'],
'max_producible': max_prod
})
return {
'bom_summary': bom_summary,
'materials': materials
}
# ====================== 兼容旧接口 ======================
@staticmethod

View File

@ -44,8 +44,41 @@ export function deleteBom(bomNo: string, version: string) {
}
// MRP 齐套模拟计算
export function calculateKitting(entries: { bom_no: string; target_qty: number }[]) {
return request({
export interface BomKittingEntry {
bom_no: string
target_qty: number
}
export interface BomSummary {
bom_no: string
parent_name: string
version: string
max_producible: number
}
export interface KittingMaterial {
base_id: number
material_name: string
spec: string
unit: string
required_qty: number
available_qty: number
shortage: number
bom_sources: Array<{
bom_no: string
dosage: number
loss_rate: number
target_qty: number
}>
}
export interface KittingResult {
bom_summary: BomSummary[]
materials: KittingMaterial[]
}
export function calculateKitting(entries: BomKittingEntry[]) {
return request<KittingResult>({
url: '/v1/bom/calculate-kitting',
method: 'post',
data: entries

View File

@ -0,0 +1,18 @@
import request from '@/utils/request'
// 读取当前用户的 preferences
export function getUserPreferences() {
return request({
url: '/v1/user/preferences',
method: 'get'
})
}
// 保存/更新当前用户的 preferences
export function saveUserPreferences(data: Record<string, any>) {
return request({
url: '/v1/user/preferences',
method: 'put',
data
})
}

View File

@ -39,17 +39,24 @@ const routes: Array<RouteRecordRaw> = [
]
},
// 3. 基础信息
// 3. 基础信息(父级菜单)
{
path: '/material',
path: '/basic',
component: Layout,
redirect: '/material/index',
meta: { title: '基础信息', icon: 'Box' },
redirect: '/basic/material',
children: [
{
path: 'index',
path: 'material',
name: 'MaterialBase',
component: () => import('@/views/material/list.vue'),
meta: { title: '基础信息', icon: 'Box' }
meta: { title: '物料基础信息', icon: 'Box' }
},
{
path: 'kitting',
name: 'BasicKitting',
component: () => import('@/views/basic/kitting/index.vue'),
meta: { title: '产能与齐套分析', icon: 'Cpu' }
}
]
},
@ -159,12 +166,6 @@ const routes: Array<RouteRecordRaw> = [
name: 'BomManage',
component: BomManage,
meta: { title: 'BOM配方管理', icon: 'list' }
},
{
path: 'kitting',
name: 'BomKitting',
component: () => import('@/views/basic/kitting/index.vue'),
meta: { title: '齐套计算器', icon: 'Cpu' }
}
]
},

View File

@ -1,29 +1,118 @@
<template>
<div class="app-container">
<!-- ==================== 排产目标区 ==================== -->
<el-card shadow="always" style="margin-bottom: 16px;">
<template #header>
<div class="card-header">
<span class="title">排产目标</span>
<span class="sub-title">选择 BOM 并设置计划生产数量</span>
<el-card shadow="never">
<!-- ==================== 顶部工具栏 ==================== -->
<div class="toolbar">
<div class="toolbar-left">
<el-button type="primary" @click="openAddDialog">
<el-icon><Plus /></el-icon>
添加监控设备
</el-button>
<el-button type="default" @click="openCalculatorDialog">
<el-icon><Cpu /></el-icon>
齐套推演计算器
</el-button>
</div>
<div class="toolbar-right">
<el-input
v-model="searchKeyword"
placeholder="搜索 BOM 编号或产品名称..."
style="width: 240px;"
clearable
@input="handleFilter"
prefix-icon="Search"
/>
<el-button :icon="Refresh" circle @click="loadData" />
</div>
</div>
</template>
<el-row :gutter="20" align="middle">
<!-- BOM 搜索选择器 -->
<el-col :span="10">
<!-- ==================== 监控表格 ==================== -->
<el-table
v-loading="loading"
:data="filteredTableData"
border
stripe
:row-class-name="tableRowClassName"
height="calc(100vh - 240px)"
>
<el-table-column label="BOM编号" prop="bom_no" width="180" show-overflow-tooltip />
<el-table-column label="产品名称" prop="parent_name" min-width="160" show-overflow-tooltip />
<el-table-column label="规格" prop="spec" min-width="140" show-overflow-tooltip />
<el-table-column label="版本" prop="version" width="100" align="center" />
<el-table-column label="预警底线套数" prop="alert_threshold" width="130" align="center">
<template #default="{ row }">
<span v-if="!row._editing">{{ row.alert_threshold }}</span>
<el-input-number
v-else
v-model="row.alert_threshold"
:min="0"
:precision="0"
size="small"
style="width: 90px;"
/>
</template>
</el-table-column>
<el-table-column label="库存可生产个数" prop="max_producible" width="150" align="center" sortable>
<template #default="{ row }">
<span :class="getProducibleClass(row)" style="font-weight: 700; font-size: 15px;">
{{ row.max_producible }}
</span>
</template>
</el-table-column>
<el-table-column label="缺件物料数" prop="shortage_count" width="120" align="center" sortable>
<template #default="{ row }">
<span v-if="row.shortage_count > 0" style="color: #f56c6c; font-weight: 600;">
{{ row.shortage_count }}
</span>
<span v-else style="color: #67c23a;">无缺件</span>
</template>
</el-table-column>
<el-table-column label="状态" width="100" align="center">
<template #default="{ row }">
<el-tag v-if="row.max_producible < row.alert_threshold" type="danger" effect="dark">预警</el-tag>
<el-tag v-else type="success" effect="dark">正常</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" width="140" align="center" fixed="right">
<template #default="{ row }">
<template v-if="!row._editing">
<el-button type="primary" link size="small" @click="editThreshold(row)">改阈值</el-button>
<el-button type="danger" link size="small" @click="removeWatch(row)">删除</el-button>
</template>
<template v-else>
<el-button type="success" link size="small" @click="saveThreshold(row)">保存</el-button>
<el-button type="info" link size="small" @click="cancelEdit(row)">取消</el-button>
</template>
</template>
</el-table-column>
</el-table>
<el-pagination
style="margin-top: 12px;"
v-model:current-page="page"
v-model:page-size="pageSize"
:total="filteredTableData.length"
:page-sizes="[10, 20, 50, 100]"
layout="total, prev, pager, next"
background
/>
<!-- ==================== 添加监控设备对话框 ==================== -->
<el-dialog v-model="addDialogVisible" title="添加监控设备" width="680px">
<div class="add-dialog-body">
<el-form :model="addForm" label-width="100px">
<el-form-item label="选择 BOM">
<el-select
v-model="pendingBomNo"
v-model="addForm.bom_no"
filterable
remote
reserve-keyword
placeholder="搜索 BOM 编号或成品名称..."
:remote-method="searchBomOptions"
:loading="bomSearchLoading"
style="width: 100%"
@change="onBomSelected"
style="width: 100%;"
@change="onAddBomSelected"
clearable
default-first-option
>
<el-option
v-for="item in bomOptions"
@ -37,240 +126,272 @@
</div>
</el-option>
</el-select>
</el-col>
</el-form-item>
<el-form-item label="产品名称">
<el-input v-model="addForm.parent_name" disabled />
</el-form-item>
<el-form-item label="版本">
<el-input v-model="addForm.version" disabled />
</el-form-item>
<el-form-item label="预警底线套数">
<el-input-number v-model="addForm.alert_threshold" :min="0" :precision="0" style="width: 100%;" />
</el-form-item>
</el-form>
</div>
<template #footer>
<el-button @click="addDialogVisible = false">取消</el-button>
<el-button type="primary" :disabled="!addForm.bom_no" @click="confirmAdd">确认添加</el-button>
</template>
</el-dialog>
<!-- 计划数量 -->
<el-col :span="4">
<el-input-number
v-model="pendingQty"
:min="1"
:max="999999"
placeholder="计划数量"
style="width: 100%"
<!-- ==================== 齐套推演计算器对话框 ==================== -->
<el-dialog v-model="calcDialogVisible" title="齐套推演计算器" width="900px">
<div class="calc-dialog-body">
<el-alert type="info" :closable="false" style="margin-bottom: 16px;">
选择 BOM 并设置计划生产数量系统将计算整体缺件情况及各产品的库存可生产最大套数木桶效应
</el-alert>
<!-- BOM 搜索 + 添加 -->
<el-row :gutter="12" style="margin-bottom: 16px;">
<el-col :span="12">
<el-select
v-model="calcPendingBomNo"
filterable
remote
reserve-keyword
placeholder="搜索 BOM 编号..."
:remote-method="searchBomOptions"
:loading="bomSearchLoading"
style="width: 100%;"
clearable
@change="onCalcBomSelected"
>
<el-option
v-for="item in bomOptions"
:key="item.bom_no"
:label="`${item.bom_no} (${item.parent_name})`"
:value="item.bom_no"
/>
</el-select>
</el-col>
<!-- 添加按钮 -->
<el-col :span="4">
<el-button
type="primary"
:disabled="!pendingBomNo || !pendingQty"
@click="addBomTarget"
>
<el-icon><Plus /></el-icon>
添加到排产
</el-button>
<el-col :span="6">
<el-input-number v-model="calcPendingQty" :min="1" :max="999999" style="width: 100%;" />
</el-col>
<!-- 模拟计算按钮 -->
<el-col :span="6" style="text-align: right;">
<el-button
type="success"
size="large"
:disabled="!bomTargets.length"
:loading="calculating"
@click="runSimulation"
>
<el-icon><Cpu /></el-icon>
开始模拟计算
<el-col :span="6">
<el-button type="primary" :disabled="!calcPendingBomNo" @click="addCalcBom" style="width: 100%;">
<el-icon><Plus /></el-icon> 添加
</el-button>
</el-col>
</el-row>
<!-- 已选择 BOM 列表 -->
<el-table
v-if="bomTargets.length"
:data="bomTargets"
border
style="margin-top: 16px;"
size="small"
>
<!-- 计算中 BOM 列表 -->
<el-table v-if="calcTargets.length" :data="calcTargets" border size="small" style="margin-bottom: 16px;">
<el-table-column label="BOM编号" prop="bom_no" width="200" />
<el-table-column label="品名称" prop="parent_name" show-overflow-tooltip />
<el-table-column label="品名称" prop="parent_name" show-overflow-tooltip />
<el-table-column label="版本" prop="version" width="100" align="center" />
<el-table-column label="计划数量" width="150" align="center">
<el-table-column label="计划数量" width="130" align="center">
<template #default="{ row }">
<el-input-number
v-model="row.target_qty"
:min="1"
:max="999999"
size="small"
@change="() => {}"
/>
<el-input-number v-model="row.target_qty" :min="1" :max="999999" size="small" style="width: 100px;" />
</template>
</el-table-column>
<el-table-column label="操作" width="80" align="center">
<el-table-column label="操作" width="60" align="center">
<template #default="{ $index }">
<el-button type="danger" size="small" text @click="removeBomTarget($index)">
<el-button type="danger" link size="small" @click="removeCalcBom($index)">
<el-icon><Delete /></el-icon>
</el-button>
</template>
</el-table-column>
</el-table>
<el-empty v-else description="请在上方搜索并添加 BOM 目标" style="padding: 20px 0;" />
</el-card>
<!-- ==================== 齐套分析结果区 ==================== -->
<el-card shadow="always" v-loading="calculating">
<template #header>
<div class="card-header">
<span class="title">齐套分析结果</span>
<div class="header-right">
<!-- 搜索过滤 -->
<el-input
v-model="resultKeyword"
placeholder="搜索物料名称/规格..."
style="width: 220px; margin-right: 10px;"
clearable
@input="filterResults"
prefix-icon="Search"
/>
<!-- 批量设置预警 -->
<el-button
type="warning"
:disabled="!shortageItems.length"
:loading="batchWarningLoading"
@click="handleBatchWarning"
>
<el-icon><Bell /></el-icon>
批量设置预警
</el-button>
</div>
</div>
</template>
<!-- 结果统计 -->
<div v-if="resultData.length" class="summary-bar">
<el-tag type="success" effect="dark">总物料数{{ resultData.length }}</el-tag>
<el-tag type="danger" effect="dark">缺件数量{{ shortageCount }}</el-tag>
<el-tag type="warning" effect="dark">库存不足{{ shortagePercent }}%</el-tag>
<el-empty v-else description="请在上方添加 BOM" style="padding: 12px 0;" />
</div>
<!-- 结果表格 -->
<el-table
v-if="filteredData.length"
:data="paginatedData"
border
:row-class-name="tableRowClassName"
height="calc(100vh - 560px)"
:expand-row-keys="expandedRows"
@expand-change="toggleExpand"
>
<el-table-column type="expand" width="50">
<!-- 计算结果 -->
<div v-if="calcResult" class="calc-result">
<div class="result-summary">
<el-tag type="success" effect="dark">参与计算{{ calcTargets.length }} BOM</el-tag>
<el-tag type="danger" effect="dark">缺件物料{{ calcResult.materials.filter((m: any) => m.shortage < 0).length }} </el-tag>
</div>
<!-- BOM 可生产套数汇总 -->
<div class="bom-summary-title"> BOM 库存可生产最大套数木桶效应</div>
<el-table :data="calcResult.bom_summary" border size="small" style="margin-bottom: 16px;">
<el-table-column label="BOM编号" prop="bom_no" width="160" />
<el-table-column label="产品名称" prop="parent_name" show-overflow-tooltip />
<el-table-column label="版本" prop="version" width="100" align="center" />
<el-table-column label="库存可生产最大套数" prop="max_producible" width="180" align="center" sortable>
<template #default="{ row }">
<div style="padding: 8px 16px; background: #f5f7fa; border-radius: 4px;">
<p style="margin: 0 0 8px; font-size: 12px; color: #909399; font-weight: bold;">BOM 来源明细</p>
<el-table :data="row.bom_sources" size="small" border>
<el-table-column prop="bom_no" label="BOM编号" />
<el-table-column prop="dosage" label="单位用量" align="right" />
<el-table-column prop="loss_rate" label="损耗率%" align="right" />
<el-table-column prop="target_qty" label="目标数量" align="right" />
<el-table-column label="小计" align="right">
<template #default="{ row: src }">
{{ ((src.dosage || 0) * (1 + (src.loss_rate || 0) / 100) * (src.target_qty || 0)).toFixed(4) }}
</template>
</el-table-column>
</el-table>
</div>
</template>
</el-table-column>
<el-table-column label="物料名称" prop="material_name" show-overflow-tooltip min-width="160" />
<el-table-column label="规格" prop="spec" show-overflow-tooltip min-width="140" />
<el-table-column label="单位" prop="unit" width="70" align="center" />
<el-table-column label="所需总数" prop="required_qty" width="120" align="right" sortable>
<template #default="{ row }">{{ row.required_qty.toFixed(4) }}</template>
</el-table-column>
<el-table-column label="当前可用库存" prop="available_qty" width="130" align="right" sortable>
<template #default="{ row }">{{ row.available_qty.toFixed(4) }}</template>
</el-table-column>
<el-table-column label="缺口" prop="shortage" width="120" align="right" sortable>
<template #default="{ row }">
<span :class="row.shortage < 0 ? 'shortage-red' : 'shortage-green'">
{{ row.shortage.toFixed(4) }}
<span :class="row.max_producible > 0 ? 'shortage-green' : 'shortage-red'" style="font-weight: 700;">
{{ row.max_producible }}
</span>
</template>
</el-table-column>
<el-table-column label="状态" width="90" align="center">
</el-table>
<!-- 缺件物料明细 -->
<div class="shortage-title">缺件物料明细</div>
<el-table
v-if="shortageMaterials.length"
:data="shortageMaterials"
border
size="small"
max-height="280"
>
<el-table-column label="物料名称" prop="material_name" min-width="160" show-overflow-tooltip />
<el-table-column label="规格" prop="spec" min-width="120" show-overflow-tooltip />
<el-table-column label="单位" prop="unit" width="70" align="center" />
<el-table-column label="所需总数" prop="required_qty" width="110" align="right">
<template #default="{ row }">{{ row.required_qty.toFixed(4) }}</template>
</el-table-column>
<el-table-column label="当前库存" prop="available_qty" width="110" align="right">
<template #default="{ row }">{{ row.available_qty.toFixed(4) }}</template>
</el-table-column>
<el-table-column label="缺口" prop="shortage" width="110" align="right">
<template #default="{ row }">
<el-tag v-if="row.shortage < 0" type="danger" size="small">缺件</el-tag>
<el-tag v-else type="success" size="small">充足</el-tag>
<span class="shortage-red" style="font-weight: 600;">{{ row.shortage.toFixed(4) }}</span>
</template>
</el-table-column>
</el-table>
<el-empty v-else-if="!calculating && hasCalculated" description="暂无分析结果" style="padding: 20px 0;" />
<el-empty v-else description="请先在上方设置排产目标并点击「开始模拟计算」" style="padding: 40px 0;" />
<!-- 分页 -->
<el-pagination
v-if="filteredData.length"
style="margin-top: 12px;"
v-model:current-page="resultPage"
v-model:page-size="resultPageSize"
:total="filteredData.length"
:page-sizes="[20, 50, 100, 200]"
layout="total, prev, pager, next"
background
@size-change="resultPage = 1"
/>
</el-card>
<!-- 批量预警设置对话框 -->
<el-dialog v-model="warningDialogVisible" title="批量设置预警阈值" width="500px">
<el-form :model="warningForm" label-width="100px">
<el-form-item label="黄线预警阈值">
<el-input-number
v-model="warningForm.yellowThreshold"
:min="0"
:precision="2"
style="width: 100%;"
/>
</el-form-item>
<el-form-item label="红线预警阈值">
<el-input-number
v-model="warningForm.redThreshold"
:min="0"
:precision="2"
style="width: 100%;"
/>
</el-form-item>
<el-form-item>
<el-alert type="info" :closable="false">
将为以下 {{ shortageItems.length }} 种缺件物料统一设置预警阈值
</el-alert>
</el-form-item>
<el-form-item>
<div class="shortage-preview">
<el-tag v-for="item in shortageItems" :key="item.base_id" type="danger" size="small" style="margin: 2px;">
{{ item.material_name }}
</el-tag>
<el-empty v-else description="各 BOM 所需物料均库存充足" />
</div>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="warningDialogVisible = false">取消</el-button>
<el-button type="warning" :loading="submitting" @click="submitBatchWarning">确认设置</el-button>
<el-button @click="calcDialogVisible = false">关闭</el-button>
<el-button
type="success"
:disabled="!calcTargets.length"
:loading="calculating"
@click="runCalculation"
>
<el-icon><Cpu /></el-icon>
开始模拟计算
</el-button>
</template>
</el-dialog>
</el-card>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import { ref, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Cpu, Delete, Bell } from '@element-plus/icons-vue'
import { Plus, Cpu, Delete, Refresh } from '@element-plus/icons-vue'
import { getUserPreferences, saveUserPreferences } from '@/api/user'
import { getBomList, calculateKitting } from '@/api/bom'
import { batchSetWarning } from '@/api/material_base'
import type { KittingResult } from '@/api/bom'
// ---- BOM 搜索相关 ----
const pendingBomNo = ref('')
const pendingQty = ref(1)
// ---- 表格数据 ----
interface WatchItem {
bom_no: string
parent_name: string
spec: string
version: string
alert_threshold: number
max_producible: number
shortage_count: number
shortage_materials: any[]
_editing?: boolean
_original_threshold?: number
}
const loading = ref(false)
const tableData = ref<WatchItem[]>([])
const searchKeyword = ref('')
const page = ref(1)
const pageSize = ref(20)
const filteredTableData = computed(() => {
if (!searchKeyword.value.trim()) return paginatedData.value
const kw = searchKeyword.value.trim().toLowerCase()
return tableData.value.filter(item =>
(item.bom_no || '').toLowerCase().includes(kw) ||
(item.parent_name || '').toLowerCase().includes(kw)
)
})
const paginatedData = computed(() => {
const start = (page.value - 1) * pageSize.value
return filteredTableData.value.slice(start, start + pageSize.value)
})
const handleFilter = () => {
page.value = 1
}
// ---- 表格行样式 ----
const getProducibleClass = (row: WatchItem) =>
row.max_producible < row.alert_threshold ? 'shortage-red' : 'shortage-green'
const tableRowClassName = ({ row }: { row: WatchItem }) =>
row.max_producible < row.alert_threshold ? 'danger-row' : ''
// ---- 数据加载 ----
const loadData = async () => {
loading.value = true
try {
// 1. 读取用户监控列表
const prefRes: any = await getUserPreferences()
const watchlist: any[] = prefRes.data?.bom_kitting_watchlist || []
if (!watchlist.length) {
tableData.value = []
loading.value = false
return
}
// 2. 调用齐套算法(以 target_qty=1 计算当前库存可生产套数)
const entries = watchlist.map(item => ({
bom_no: item.bom_no,
target_qty: item.alert_threshold > 0 ? item.alert_threshold : 1
}))
const kittingRes: any = await calculateKitting(entries)
if (kittingRes.code !== 200) {
ElMessage.error(kittingRes.msg || '计算失败')
tableData.value = []
loading.value = false
return
}
const result: KittingResult = kittingRes.data || { bom_summary: [], materials: [] }
// 3. 合并 watchlist 和计算结果
tableData.value = watchlist.map(item => {
const summary = result.bom_summary.find(s => s.bom_no === item.bom_no) || {
max_producible: 0
}
const shortageMaterials = result.materials.filter(m =>
m.shortage < 0 && m.bom_sources.some((s: any) => s.bom_no === item.bom_no)
)
return {
bom_no: item.bom_no,
parent_name: item.parent_name || '',
spec: item.spec || '',
version: item.version || '',
alert_threshold: item.alert_threshold || 0,
max_producible: summary.max_producible || 0,
shortage_count: shortageMaterials.length,
shortage_materials: shortageMaterials
}
})
} catch (e: any) {
ElMessage.error(e?.message || '加载失败')
} finally {
loading.value = false
}
}
// ---- 添加监控设备 ----
const addDialogVisible = ref(false)
const bomOptions = ref<any[]>([])
const bomSearchLoading = ref(false)
let bomSearchTimer: ReturnType<typeof setTimeout> | null = null
const addForm = ref({
bom_no: '',
parent_name: '',
version: '',
spec: '',
alert_threshold: 10
})
const searchBomOptions = async (query: string) => {
if (bomSearchTimer) clearTimeout(bomSearchTimer)
bomSearchTimer = setTimeout(async () => {
@ -286,216 +407,237 @@ const searchBomOptions = async (query: string) => {
}, 300)
}
// ---- 排产目标列表 ----
interface BomTarget {
bom_no: string
parent_name: string
version: string
target_qty: number
const onAddBomSelected = async (bomNo: string) => {
if (!bomNo) { addForm.value = { bom_no: '', parent_name: '', version: '', spec: '', alert_threshold: 10 }; return }
const found = bomOptions.value.find(b => b.bom_no === bomNo)
if (found) {
addForm.value.parent_name = found.parent_name || ''
addForm.value.version = found.version || ''
addForm.value.spec = found.parent_spec || ''
}
}
const bomTargets = ref<BomTarget[]>([])
const openAddDialog = () => {
addForm.value = { bom_no: '', parent_name: '', version: '', spec: '', alert_threshold: 10 }
bomOptions.value = []
addDialogVisible.value = true
}
const onBomSelected = async (bomNo: string) => {
const confirmAdd = async () => {
if (!addForm.value.bom_no) {
ElMessage.warning('请选择 BOM')
return
}
if (tableData.value.find(t => t.bom_no === addForm.value.bom_no)) {
ElMessage.warning('该 BOM 已在监控列表中')
return
}
// 追加到 preferences
const prefRes: any = await getUserPreferences().catch(() => ({ data: {} }))
const prefs = prefRes.data || {}
const watchlist: any[] = prefs.bom_kitting_watchlist || []
watchlist.push({
bom_no: addForm.value.bom_no,
parent_name: addForm.value.parent_name,
spec: addForm.value.spec,
version: addForm.value.version,
alert_threshold: addForm.value.alert_threshold
})
await saveUserPreferences({ ...prefs, bom_kitting_watchlist: watchlist })
ElMessage.success('添加成功')
addDialogVisible.value = false
await loadData()
}
// ---- 编辑预警阈值 ----
const editThreshold = (row: WatchItem) => {
row._editing = true
row._original_threshold = row.alert_threshold
}
const cancelEdit = (row: WatchItem) => {
row.alert_threshold = row._original_threshold ?? row.alert_threshold
row._editing = false
}
const saveThreshold = async (row: WatchItem) => {
try {
const prefRes: any = await getUserPreferences().catch(() => ({ data: {} }))
const prefs = prefRes.data || {}
const watchlist: any[] = (prefs.bom_kitting_watchlist || []).map((item: any) =>
item.bom_no === row.bom_no ? { ...item, alert_threshold: row.alert_threshold } : item
)
await saveUserPreferences({ ...prefs, bom_kitting_watchlist: watchlist })
row._editing = false
ElMessage.success('阈值已更新')
await loadData()
} catch {
ElMessage.error('保存失败')
}
}
// ---- 删除监控 ----
const removeWatch = async (row: WatchItem) => {
await ElMessageBox.confirm(`确定移除「${row.bom_no}」的监控?`, '确认删除', { type: 'warning' })
try {
const prefRes: any = await getUserPreferences().catch(() => ({ data: {} }))
const prefs = prefRes.data || {}
const watchlist = (prefs.bom_kitting_watchlist || []).filter(
(item: any) => item.bom_no !== row.bom_no
)
await saveUserPreferences({ ...prefs, bom_kitting_watchlist: watchlist })
ElMessage.success('已移除')
await loadData()
} catch {
ElMessage.error('移除失败')
}
}
// ---- 齐套推演计算器 ----
const calcDialogVisible = ref(false)
const calcPendingBomNo = ref('')
const calcPendingQty = ref(1)
const calcTargets = ref<any[]>([])
const calculating = ref(false)
const calcResult = ref<KittingResult | null>(null)
const openCalculatorDialog = () => {
calcPendingBomNo.value = ''
calcPendingQty.value = 1
calcTargets.value = []
calcResult.value = null
bomOptions.value = []
calcDialogVisible.value = true
}
const onCalcBomSelected = async (bomNo: string) => {
if (!bomNo) return
const found = bomOptions.value.find(b => b.bom_no === bomNo)
if (found && !bomTargets.value.find(t => t.bom_no === bomNo)) {
bomTargets.value.push({
if (found && !calcTargets.value.find(t => t.bom_no === bomNo)) {
calcTargets.value.push({
bom_no: found.bom_no,
parent_name: found.parent_name,
version: found.version,
target_qty: pendingQty.value || 1
parent_name: found.parent_name || '',
version: found.version || '',
target_qty: calcPendingQty.value || 1
})
}
pendingBomNo.value = ''
pendingQty.value = 1
calcPendingBomNo.value = ''
calcPendingQty.value = 1
}
const addBomTarget = () => {
onBomSelected(pendingBomNo.value)
const addCalcBom = () => {
onCalcBomSelected(calcPendingBomNo.value)
}
const removeBomTarget = (index: number) => {
bomTargets.value.splice(index, 1)
const removeCalcBom = (index: number) => {
calcTargets.value.splice(index, 1)
}
// ---- 模拟计算 ----
const calculating = ref(false)
const resultData = ref<any[]>([])
const hasCalculated = ref(false)
const runSimulation = async () => {
if (!bomTargets.value.length) {
ElMessage.warning('请先添加 BOM 排产目标')
const runCalculation = async () => {
if (!calcTargets.value.length) {
ElMessage.warning('请先添加 BOM')
return
}
calculating.value = true
hasCalculated.value = false
calcResult.value = null
try {
const entries = bomTargets.value.map(t => ({
const entries = calcTargets.value.map(t => ({
bom_no: t.bom_no,
target_qty: t.target_qty
}))
const res: any = await calculateKitting(entries)
if (res.code === 200) {
resultData.value = res.data || []
hasCalculated.value = true
resultPage.value = 1
ElMessage.success(`齐套分析完成,共 ${resultData.value.length} 种物料`)
calcResult.value = res.data as KittingResult
ElMessage.success('计算完成')
} else {
ElMessage.error(res.msg || '计算失败')
}
} catch (e: any) {
ElMessage.error(e?.message || '网络错误,计算失败')
ElMessage.error(e?.message || '网络错误')
} finally {
calculating.value = false
}
}
// ---- 结果过滤与分页 ----
const resultKeyword = ref('')
const resultPage = ref(1)
const resultPageSize = ref(20)
const expandedRows = ref<number[]>([])
const shortageMaterials = computed(() =>
(calcResult.value?.materials || []).filter((m: any) => m.shortage < 0)
)
const filteredData = computed(() => {
if (!resultKeyword.value.trim()) return resultData.value
const kw = resultKeyword.value.trim().toLowerCase()
return resultData.value.filter(item =>
(item.material_name || '').toLowerCase().includes(kw) ||
(item.spec || '').toLowerCase().includes(kw)
)
// ---- 挂载加载 ----
onMounted(() => {
loadData()
})
const paginatedData = computed(() => {
const start = (resultPage.value - 1) * resultPageSize.value
return filteredData.value.slice(start, start + resultPageSize.value)
})
const filterResults = () => {
resultPage.value = 1
}
const shortageCount = computed(() =>
resultData.value.filter(r => r.shortage < 0).length
)
const shortagePercent = computed(() =>
resultData.value.length
? Math.round((shortageCount.value / resultData.value.length) * 100)
: 0
)
const shortageItems = computed(() =>
resultData.value.filter(r => r.shortage < 0)
)
// ---- 行展开 ----
const toggleExpand = (row: any) => {
const idx = expandedRows.value.indexOf(row.base_id)
if (idx >= 0) {
expandedRows.value.splice(idx, 1)
} else {
expandedRows.value.push(row.base_id)
}
}
// ---- 表格行样式 ----
const tableRowClassName = ({ row }: { row: any }) =>
row.shortage < 0 ? 'shortage-row' : ''
// ---- 批量设置预警 ----
const warningDialogVisible = ref(false)
const batchWarningLoading = ref(false)
const submitting = ref(false)
const warningForm = ref({ yellowThreshold: 10, redThreshold: 5 })
const handleBatchWarning = () => {
warningForm.value.yellowThreshold = 10
warningForm.value.redThreshold = 5
warningDialogVisible.value = true
}
const submitBatchWarning = async () => {
if (!shortageItems.value.length) {
ElMessage.warning('没有缺件物料,无需设置预警')
return
}
submitting.value = true
try {
const payload = shortageItems.value.map(item => ({
baseId: item.base_id,
isEnabled: true,
yellowThreshold: warningForm.value.yellowThreshold,
redThreshold: warningForm.value.redThreshold
}))
const res: any = await batchSetWarning(payload)
if (res.code === 200) {
ElMessage.success(`批量设置成功:新建 ${res.data?.created || 0} 条,更新 ${res.data?.updated || 0}`)
warningDialogVisible.value = false
} else {
ElMessage.error(res.msg || '设置失败')
}
} catch {
ElMessage.error('批量设置预警失败')
} finally {
submitting.value = false
}
}
</script>
<style scoped>
.card-header {
display: flex;
align-items: center;
gap: 12px;
}
.title {
font-size: 16px;
font-weight: 600;
color: #303133;
}
.sub-title {
font-size: 13px;
color: #909399;
}
.header-right {
margin-left: auto;
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
flex-wrap: wrap;
gap: 10px;
}
.summary-bar {
.toolbar-left {
display: flex;
gap: 10px;
}
.toolbar-right {
display: flex;
align-items: center;
gap: 8px;
}
.add-dialog-body {
padding: 8px 0;
}
.calc-dialog-body {
max-height: 420px;
overflow-y: auto;
}
.calc-result {
margin-top: 16px;
border-top: 1px solid #ebeef5;
padding-top: 16px;
}
.result-summary {
display: flex;
gap: 12px;
margin-bottom: 12px;
}
.bom-summary-title {
font-size: 13px;
font-weight: 600;
color: #606266;
margin-bottom: 8px;
}
.shortage-title {
font-size: 13px;
font-weight: 600;
color: #f56c6c;
margin-bottom: 8px;
}
.shortage-red {
color: #f56c6c;
font-weight: 600;
}
.shortage-green {
color: #67c23a;
}
.shortage-preview {
max-height: 160px;
overflow-y: auto;
display: flex;
flex-wrap: wrap;
gap: 4px;
}
/* 缺件行高亮 */
:deep(.shortage-row) {
:deep(.danger-row) {
background-color: #fef0f0 !important;
}
:deep(.shortage-row:hover > td) {
:deep(.danger-row:hover > td) {
background-color: #fee !important;
}
</style>