wangxinhui
2026-03-26 ad05ef2341fe19a4220f7ada1987636f9ec4a1a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// 空托预警服务
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
};