feat(warning): 库存预警邮件支持顺延发送,前端物料列表新增预警状态展示列

This commit is contained in:
DXC
2026-05-12 14:44:23 +08:00
parent 9a96fad3cf
commit 8356774a8a
2 changed files with 58 additions and 8 deletions

View File

@ -101,6 +101,8 @@ class InventoryWarningService:
total_red = 0
total_yellow = 0
total_red_cascaded = 0 # 红色顺延到黄色
total_yellow_cascaded = 0 # 黄色顺延到红色
sent_red = False
sent_yellow = False
processed_settings = []
@ -121,7 +123,18 @@ class InventoryWarningService:
if red_th is not None and inv <= red_th:
total_red += 1
red_emails = InventoryWarningService._parse_emails(setting.red_emails)
if red_emails:
emails_to_use = red_emails
use_yellow_channel = False # 是否走黄色通道(顺延时为 True
if not emails_to_use:
# ★ 红色预警但无 red_emails顺延使用 yellow_emails ★
emails_to_use = InventoryWarningService._parse_emails(setting.yellow_emails)
if emails_to_use:
total_yellow += 1
total_red_cascaded += 1
use_yellow_channel = True
print(f"[InventoryWarning] 物料「{name}」红色预警触发,但 red_emails 为空,顺延使用 yellow_emails 发黄色预警")
if emails_to_use:
processed_settings.append(setting)
row = {
'name': name,
@ -129,10 +142,14 @@ class InventoryWarningService:
'qty': round(inv, 2),
'threshold': round(red_th, 2),
}
for email in red_emails:
red_rows_by_email[email].append(row)
if use_yellow_channel:
for email in emails_to_use:
yellow_rows_by_email[email].append(row)
else:
for email in emails_to_use:
red_rows_by_email[email].append(row)
else:
print(f"[InventoryWarning] 物料「{name}」红单跳过:无 red_emails 配置")
print(f"[InventoryWarning] 物料「{name}」红单跳过:无 red_emails 且 yellow_emails 也为空")
# ★ 黄色预警red_threshold < 库存 <= yellow_threshold走 setting.yellow_emails ★
elif (
@ -141,7 +158,18 @@ class InventoryWarningService:
):
total_yellow += 1
yellow_emails = InventoryWarningService._parse_emails(setting.yellow_emails)
if yellow_emails:
emails_to_use = yellow_emails
use_red_channel = False # 是否走红色通道(顺延时为 True
if not emails_to_use:
# ★ 黄色预警但无 yellow_emails顺延使用 red_emails ★
emails_to_use = InventoryWarningService._parse_emails(setting.red_emails)
if emails_to_use:
total_red += 1
total_yellow_cascaded += 1
use_red_channel = True
print(f"[InventoryWarning] 物料「{name}」黄色预警触发,但 yellow_emails 为空,顺延使用 red_emails 发红色预警")
if emails_to_use:
processed_settings.append(setting)
row = {
'name': name,
@ -149,10 +177,14 @@ class InventoryWarningService:
'qty': round(inv, 2),
'threshold': round(yellow_th, 2),
}
for email in yellow_emails:
yellow_rows_by_email[email].append(row)
if use_red_channel:
for email in emails_to_use:
red_rows_by_email[email].append(row)
else:
for email in emails_to_use:
yellow_rows_by_email[email].append(row)
else:
print(f"[InventoryWarning] 物料「{name}」黄单跳过:无 yellow_emails 配置")
print(f"[InventoryWarning] 物料「{name}」黄单跳过:无 yellow_emails 且 red_emails 也为空")
else:
continue
@ -193,6 +225,8 @@ class InventoryWarningService:
return {
'red_count': total_red,
'yellow_count': total_yellow,
'red_cascaded_count': total_red_cascaded,
'yellow_cascaded_count': total_yellow_cascaded,
'red_sent': sent_red,
'yellow_sent': sent_yellow,
'timestamp': now.strftime('%Y-%m-%d %H:%M:%S')

View File

@ -302,6 +302,22 @@
</el-tag>
</template>
</el-table-column>
<el-table-column v-if="userStore.hasPermission('material_list:view_warning')" label="预警状态" width="120" align="center">
<template #default="{ row }">
<template v-if="row.warningStatus === 2">
<el-tag type="danger" size="small">红色预警</el-tag>
<div style="font-size: 11px; color: #999;">阈值: {{ row.warningRed }}</div>
</template>
<template v-else-if="row.warningStatus === 1">
<el-tag type="warning" size="small">黄色预警</el-tag>
<div style="font-size: 11px; color: #999;">阈值: {{ row.warningYellow }}</div>
</template>
<template v-else-if="row.warningEnabled">
<el-tag type="success" size="small">已配置</el-tag>
</template>
<span v-else style="color: #c0c4cc;">-</span>
</template>
</el-table-column>
<el-table-column v-if="userStore.hasPermission('material_list:operation')" label="操作" width="280" fixed="right" align="center">
<template #default="scope">
<el-button v-if="userStore.hasPermission('material_list:operation')" link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>