// 空托预警服务
|
import http from '../api/http';
|
|
// 空托预警阈值
|
const WARNING_THRESHOLD = 50;
|
|
// 检查空托数量的服务
|
const checkEmptyPalletWarning = async (globalState, store) => {
|
try {
|
// 检查是否已经存在未清除的空托预警消息
|
const globalMessageList = globalState.messageList || [];
|
|
const hasExistingWarning = globalMessageList.some(msg =>
|
msg.businessType === 'pallet_warning' && msg.type === 'warning'
|
);
|
|
if (hasExistingWarning) {
|
return;
|
}
|
|
// 调用API获取成品库空托数量,添加proStockAttribute等于5的过滤条件
|
const requestData = {
|
Page: 1,
|
Rows: 9999,
|
Filter: [
|
{
|
Name: 'proStockAttribute',
|
Value: 5,
|
Operator: 'eq'
|
}
|
],
|
Wheres: '',
|
Sort: '',
|
Order: 'desc'
|
};
|
|
let apiData = null;
|
let apiSuccess = false;
|
|
try {
|
// 调用API获取成品库空托数量
|
apiData = await http.post('/api/ProStockView/GetPageData', requestData);
|
apiSuccess = true;
|
} catch (error) {
|
console.error('API请求失败:', error);
|
}
|
|
// 如果API失败,不使用模拟数据,直接返回
|
if (!apiSuccess || !apiData) {
|
return;
|
}
|
|
let totalEmptyPalletCount = 0;
|
|
if (apiData && apiData.rows) {
|
const stockData = apiData.rows;
|
|
// 遍历所有返回记录,直接累加库存数量(API已过滤空托)
|
for (const stock of stockData) {
|
// 获取库存数量,尝试所有可能的字段
|
let stockQuantity = 0;
|
|
// 尝试从所有可能的字段获取库存数量
|
const quantityFields = ['sumStocks', '库存数量', 'quantity', 'stockCount', 'sumStock', '库存数', 'count'];
|
for (const field of quantityFields) {
|
if (stock[field] !== undefined && stock[field] !== null) {
|
// 尝试解析为数字
|
const parsedQuantity = typeof stock[field] === 'number' ? stock[field] : parseInt(stock[field]);
|
if (!isNaN(parsedQuantity)) {
|
stockQuantity = parsedQuantity;
|
break;
|
}
|
}
|
}
|
|
// 累加空托数量
|
totalEmptyPalletCount += stockQuantity;
|
}
|
|
// 只有当空托数量少于50时才发送预警
|
if (totalEmptyPalletCount < WARNING_THRESHOLD) {
|
sendPalletWarningMessage(globalState, store, totalEmptyPalletCount, WARNING_THRESHOLD);
|
}
|
}
|
} catch (error) {
|
console.error('检查空托数量失败:', error);
|
console.error('错误堆栈:', error.stack);
|
}
|
};
|
|
// 发送空托警告消息
|
const sendPalletWarningMessage = (globalState, store, emptyPalletCount, warningThreshold) => {
|
// 创建警告消息
|
const warningMessage = {
|
id: Date.now(),
|
title: '空托数量预警',
|
message: `成品库空托数量不足,当前总数量:${emptyPalletCount},低于预警阈值:${warningThreshold},建议及时补充!`,
|
type: 'warning',
|
businessType: 'pallet_warning',
|
createTime: new Date().toLocaleString(),
|
relatedData: {
|
EmptyPalletCount: emptyPalletCount,
|
Threshold: warningThreshold
|
}
|
};
|
|
// 直接添加消息到全局消息列表
|
if (globalState && globalState.messageList) {
|
// 检查消息是否已存在
|
const isNewMessage = !globalState.messageList.some(m => m.id === warningMessage.id);
|
globalState.messageList.push(warningMessage);
|
|
|
}
|
};
|
|
// 初始化空托预警服务
|
const initEmptyPalletWarning = (app) => {
|
const globalState = app.config.globalProperties.$global;
|
const store = app.config.globalProperties.$store;
|
|
// 设置定时器,每60秒检查一次
|
const checkEmptyPalletTimer = setInterval(() => {
|
checkEmptyPalletWarning(globalState, store);
|
}, 30000); // 每30秒检查一次
|
|
// 20秒后执行初始检查
|
setTimeout(() => {
|
checkEmptyPalletWarning(globalState, store);
|
}, 10000); // 10秒后执行初始检查
|
|
// 保存定时器引用,方便后续清理
|
app.config.globalProperties.$global.checkEmptyPalletTimer = checkEmptyPalletTimer;
|
|
// 方便手动测试,将check方法挂载到window对象
|
window.testEmptyPalletWarning = () => {
|
checkEmptyPalletWarning(globalState, store);
|
};
|
};
|
|
export default {
|
init: initEmptyPalletWarning,
|
check: checkEmptyPalletWarning
|
};
|