服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2025-03-04 7aa936eec82e59b9c8d1d976e53cd836fb60a43c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using Microsoft.AspNetCore.Http;
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Entitys.MES.Position;
 
namespace Tiger.Business
{
    /// <summary>
    /// 工单 上下文
    /// </summary>
    public class WoContext : IWoContext
    {
        #region 工单管理
        /// <summary>
        /// 工单批次缓存字典
        /// </summary>
        public static Dictionary<string, WorkBatch> WoBatchDic { get; set; } = new Dictionary<string, WorkBatch>();
        public Dictionary<string, IWorkBatch> GetWoBatchDic() => WoBatchDic.ToDictionary(k => k.Key, v => v.Value as IWorkBatch);
 
        /// <summary>
        /// 从工单批次字典中获取一个工单批次对象
        /// </summary>
        /// <param name="workorder"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static WorkBatch GetBatch(string workorder, string lineCode, string batchNo = "", bool canDoWork = true)
        {
            if (ExistsBatch(workorder, lineCode, batchNo))
            {
                return WoBatchDic.WhereIF(canDoWork, q => q.Value.Batch?.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch?.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue())
                    .FirstOrDefault(q => !q.Value.Batch.IsNullOrEmpty() && q.Value.Batch.ORDER_NO == workorder && q.Value.Batch.ACT_LINE == lineCode && (batchNo.IsNullOrEmpty() || q.Value.Batch.BATCH_NO == batchNo)).Value;
            }
            return null;
        }
 
        /// <summary>
        /// 判断工单批次是否存在于工单批次字典中
        /// </summary>
        /// <param name="workorder"></param>
        /// <param name="lineCode"></param>
        /// <param name="batchNo"></param>
        /// <param name="canDoWork"></param>
        /// <returns></returns>
        public static bool ExistsBatch(string workorder, string lineCode, string batchNo = "", bool canDoWork = false)
        {
            return WoBatchDic.WhereIF(canDoWork, q => !q.Value.Batch.IsNullOrEmpty() && (q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue()))
                .Any(q => q.Value.Batch?.ORDER_NO == workorder && q.Value.Batch?.ACT_LINE == lineCode && (batchNo.IsNullOrEmpty() || q.Value.Batch?.BATCH_NO == batchNo));
        }
 
        /// 根据工单号,增加一个工单对象到工单批次字典中
        /// </summary>
        /// <param name="workorder"></param>
        /// <returns></returns>
        public static WorkBatch Add(string workorder)
        {
            var wb = new WorkBatch(workorder).Init("");
            WoBatchDic.Add(workorder, wb);
            return wb;
        }
 
        /// 根据工单号和产线,增加一个工单批次对象到工单批次字典中
        /// </summary>
        /// <param name="workorder"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static WorkBatch Add(string workorder, string linecode)
        {
            var wb = new WorkBatch(workorder).Init(linecode);
            WoBatchDic.Add(wb.Batch.BATCH_NO, wb);
            return wb;
        }
 
        /// 根据工单号,增加一个工单对象到工单批次字典中
        /// </summary>
        /// <param name="workorder"></param>
        /// <returns></returns>
        public IWorkBatch AddWo(string workorder)
        {
            return Add(workorder);
        }
 
        /// 根据工单号和产线,增加一个工单批次对象到工单批次字典中
        /// </summary>
        /// <param name="workorder"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public IWorkBatch AddBatch(string workorder, string linecode)
        {
            return Add(workorder, linecode);
        }
 
        /// 从工单批次字典中删除一个工单的所有批次对象
        /// </summary>
        /// <param name="workorder"></param>
        /// <returns></returns>
        public static bool RemoveAll(string workorder)
        {
            try
            {
                var list = WoBatchDic.Keys.ToList().Where(q => q.Contains(workorder));
                foreach (var batch in list)
                {
                    WoBatchDic.Remove(batch);
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Default.Fatal(ex, $"从工单批次字典中删除一个工单[{workorder}]对象异常");
                return false;
            }
        }
 
        /// 从工单批次字典中删除一个工单的所有批次对象
        /// </summary>
        /// <param name="workorder"></param>
        /// <returns></returns>
        public bool RemoveWo(string workorder)
        {
            return RemoveAll(workorder);
        }
 
        /// 从工单批次字典中删除一个工单批次对象
        /// </summary>
        /// <param name="batchNo"></param>
        /// <returns></returns>
        public static bool Remove(string batchNo)
        {
            try
            {
                if (WoBatchDic.ContainsKey(batchNo))
                {
                    WoBatchDic.Remove(batchNo);
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Default.Fatal(ex, $"从工单批次字典中删除一个工单批次[{batchNo}]对象异常");
                return false;
            }
        }
 
        /// 从工单批次字典中删除一个工单批次对象
        /// </summary>
        /// <param name="batchNo"></param>
        /// <returns></returns>
        public bool RemoveBatch(string batchNo)
        {
            return Remove(batchNo);
        }
 
        /// <summary>
        /// 获取SN的下一个工序节点列表
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public static ApiAction<List<MES_WO_NODE>> GetSnNextNodes(string sn)
        {
            var action = new ApiAction<List<MES_WO_NODE>>(new List<MES_WO_NODE>());
            try
            {
                var wipSNs = Biz.Db.Queryable<MES_WIP_DATA>().IncludesAllFirstLayer().Where(q => (q.SN == sn || q.FLOW_SN == sn || q.TRAY_SN == sn || q.INNER_SN == sn || q.CARTON_SN == sn || q.PALLET_SN == sn)).ToList();
                if (wipSNs.IsNullOrEmpty())
                {
                    var woSNs = Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => (q.SN == sn || q.TRAY_SN == sn || q.OUTER_SN == sn)).ToList();
                    //查找到条码已绑定的工单
                    if (!woSNs.IsNullOrEmpty())
                    {
                        foreach (var woSN in woSNs)
                        {
                            var wipSN = new MES_WIP_DATA()
                            {
                                SN = sn,
                                FLOW_SN = sn,
                                STATUS = MES_WIP_DATA.STATUSs.Init.GetValue(),
                                ITEM_CODE = woSN.ITEM_CODE,
                                WORK_ORDER = woSN.WORK_ORDER,
                                BATCH_NO = woSN.BATCH_NO,
                                HOLD_FLAG = "N",
                                FINISHED_FLAG = "N",
                                UNBIND_FLAG = "N",
                                INV_FLAG = "N",
                                DFT_FLAG = "N",
                                DFT_COUNT = 0,
                            };
                            wipSNs.Add(wipSN);
                        }
                    }
                }
                if (wipSNs.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = new($"找不到条码[{sn}]的生产信息", sn);
                    action.LocaleMsg = new("MES.WoContext.SnNotFound", sn);
                }
                else
                {
                    if (!WoBatchDic.Any(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER))
                    {
                        Add(wipSNs.First().WORK_ORDER);
                    }
                    var wo = WoBatchDic.FirstOrDefault(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER).Value;
                    action.Data = wo.GetNextNodes(wipSNs.First());
                }
            }
            catch (Exception ex)
            {
                //action.CatchExceptionWithLog(ex, $"获取条码[{sn}]的下一个工序节点列表异常");
                action.CatchExceptionWithLog(ex, Biz.L("MES.WoContext.GetSnNextNodesException", sn));
            }
            return action;
        }
 
        /// <summary>
        /// 设置当前条码的工序信息
        /// </summary>
        public static ApiAction<OperInfo> GetSnOperInfo(string sn)
        {
            var action = new ApiAction<OperInfo>(new OperInfo());
            try
            {
                var wipSNs = Biz.Db.Queryable<MES_WIP_DATA>().IncludesAllFirstLayer().Where(q => (q.SN == sn || q.FLOW_SN == sn || q.TRAY_SN == sn || q.INNER_SN == sn || q.CARTON_SN == sn || q.PALLET_SN == sn)).ToList();
                if (wipSNs.IsNullOrEmpty())
                {
                    var woSNs = Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => (q.SN == sn || q.TRAY_SN == sn || q.OUTER_SN == sn)).ToList();
                    //查找到条码已绑定的工单
                    if (!woSNs.IsNullOrEmpty())
                    {
                        foreach (var woSN in woSNs)
                        {
                            var wipSN = new MES_WIP_DATA()
                            {
                                SN = sn,
                                FLOW_SN = sn,
                                STATUS = MES_WIP_DATA.STATUSs.Init.GetValue(),
                                ITEM_CODE = woSN.ITEM_CODE,
                                WORK_ORDER = woSN.WORK_ORDER,
                                BATCH_NO = woSN.BATCH_NO,
                                HOLD_FLAG = "N",
                                FINISHED_FLAG = "N",
                                UNBIND_FLAG = "N",
                                INV_FLAG = "N",
                                DFT_FLAG = "N",
                                DFT_COUNT = 0,
                            };
                            wipSNs.Add(wipSN);
                        }
                    }
                }
                if (wipSNs.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = new($"找不到条码[{sn}]的生产信息", sn);
                    action.LocaleMsg = new("MES.WoContext.SnNotFound", sn);
                }
                else
                {
                    if (!WoBatchDic.Any(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER))
                    {
                        Add(wipSNs.First().WORK_ORDER);
                    }
                    var wo = WoBatchDic.FirstOrDefault(q => q.Value.WO.ORDER_NO == wipSNs.First().WORK_ORDER).Value;
                    action.Data = new()
                    {
                        CurNode = wipSNs.First().NODE_NAME,
                        NextNode = string.Join(",", wo.GetNextNodes(wipSNs.First()).Select(q => q.NODE_NAME)),
                    };
                }
            }
            catch (Exception ex)
            {
                //action.CatchExceptionWithLog(ex, $"获取条码[{sn}]的工序信息异常");
                action.CatchExceptionWithLog(ex, Biz.L("MES.WoContext.GetSnOperInfoException", sn));
            }
            return action;
        }
        #endregion
    }
}