服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-08-16 eec9f268552af1d0ce8c95312930770669f1cc18
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
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;
using static Tiger.Business.Biz;
 
namespace Tiger.Business.MES
{
    /// <summary>
    /// 工单 上下文
    /// </summary>
    public class WoContext
    {
        #region 工单管理
        /// <summary>
        /// 
        /// </summary>
        public static Dictionary<string, WorkBatch> WoBatchDic { get; set; } = new Dictionary<string, WorkBatch>();
 
        /// <summary>
        /// 从工单批次字典中获取一个工单批次对象
        /// </summary>
        /// <param name="workorder"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static WorkBatch GetBatch(string workorder, string lineCode)
        {
            return WoBatchDic.FirstOrDefault(q => q.Value.Batch.ORDER_NO == workorder && q.Value.Batch.ACT_LINE == lineCode).Value;
        }
 
        /// <summary>
        /// 判断工单批次是否存在于工单批次字典中
        /// </summary>
        /// <param name="workorder"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static bool ExistsBatch(string workorder, string lineCode)
        {
            return WoBatchDic.Any(q => q.Value.Batch.ORDER_NO == workorder && q.Value.Batch.ACT_LINE == lineCode);
        }
 
        /// 从工单批次字典中删除一个工单批次对象
        /// </summary>
        /// <param name="batchNo"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static bool RemoveWo(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="batchNo"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static bool RemoveBatch(string batchNo)
        {
            try
            {
                if (WoBatchDic.ContainsKey(batchNo))
                {
                    WoBatchDic.Remove(batchNo);
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Default.Fatal(ex, $"从工单批次字典中删除一个工单批次[{batchNo}]对象异常");
                return false;
            }
        }
 
        /// <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",
                                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))
                    {
                        var wb = new WorkBatch(wipSNs.First().WORK_ORDER).Init("");
                        WoBatchDic.Add(wb.Batch.BATCH_NO, wb);
                    }
                    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",
                                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))
                    {
                        var wb = new WorkBatch(wipSNs.First().WORK_ORDER).Init("");
                        WoBatchDic.Add(wb.Batch.BATCH_NO, wb);
                    }
                    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
    }
}