服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-10-16 8bc453e47b30f0acf4333bec955ec86dadf428d5
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.IBusiness.MES.WorkAction;
using Tiger.Model;
using Tiger.Model.Entitys.MES.Position;
using Tiger.Model.MES.Yada;
 
namespace Tiger.Business.MES.WorkAction
{
    public class ThreeInOne : IThreeInOne
    {
        #region Propertys & Variables
        #region 固定写法,工序中的必要信息
        public bool IsFinished { get; set; } = false;
        public IWorkStep CurStep { get; set; }
        public IPosition CurPosition { get; set; }
        public MES_WO_NODE_ACT NodeAct { get; set; }
        public MES_WO_ACTION Setting { get; set; }
        #endregion
        public MES_CUST_SN FlowCSN  { get; set; }
        public MES_CUST_SN CustCSN { get; set; }
        public BAS_LABEL_TEMP Label { get; set; }
        public List<BAS_LABEL_PV> LabelPV { get; set; }
        public List<mes_MOPickList> MoPickList { get; set; }
        public BAS_ITEM_CUST CustInfo { get; set; }
        #endregion Propertys & Variables
 
        #region Functions
        /// <summary>
        /// 初始化工序行为
        /// </summary>
        /// <returns></returns>
        public void Init(IWorkStep curStep, IPosition position, MES_WO_NODE_ACT nodeAct, MES_WO_ACTION setting)
        {
            #region 固定写法,给默认变量赋值
            CurStep = curStep;
            CurPosition = position;
            NodeAct = nodeAct;
            Setting = setting;
            #endregion
 
            //获取打印模板
            Label = Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE == setting.LABEL_CODE).IncludesAllFirstLayer().First();
            LabelPV = Biz.Db.Queryable<BAS_LABEL_PV>().ToList();
 
            //获取当前产品在当前客户的关系
            CustInfo = Biz.Db.Queryable<BAS_ITEM_CUST>().Where(q => q.ITEM_ID == CurPosition.WorkBatch.WO.ItemInfo.ID && q.CUST_CODE == CurPosition.WorkBatch.WO.CUST_CODE).First();
 
            CurStep.Message = Biz.L("扫描厂内条码或客户条码");
            CurStep.Status = StepStatus.Normal;
        }
 
        /// <summary>
        /// 获取行为开始的提示信息
        /// </summary>
        /// <returns></returns>
        public Locale GetBeginMsg()
        {
            var msg = new Locale("MES.WorkAction.ThreeInOne.BeginMsg", CurPosition.CurSN);
            //var msg = new Locale($"开始检查条码[{CurPosition.CurSN}]的是否已绑定客户条码");
            return msg;
        }
 
        /// <summary>
        /// 尝试开始执行工序行为
        /// </summary>
        /// <returns></returns>
        public ApiAction<SubmitOutput> TryBegin(SubmitInput input)
        {
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
 
            //获取工单备料信息
            MoPickList = Biz.DataSource["YadaU9C"].Client.Queryable<mes_MOPickList>().Where(q => q.MoDocNo == CurPosition.WorkBatch.WO.ORDER_NO).ToList();
 
            //只允许单个产品进行客户条码绑定
            if (CurPosition.CurWipSNs.Count > 1)
            {
                action.Data.Data = new
                {
                    NeedPrint = false,
                    NeedScan = false,
                    Label = "",
                };
                CurStep.Message = Biz.L("扫描厂内条码或客户条码");
                CurStep.Status = StepStatus.Error;
                action.Data.ShortMsg = new("扫描厂内条码或客户条码", ShortMessage.Types.Error);
                action.IsSuccessed = false;
                //action.LocaleMsg = new($"条码[{0}]是包装/载具条码,请重新扫描单个厂内条码进行客户条码绑定");
                action.LocaleMsg = new("MES.WorkAction.ThreeInOne.NotProductSN", CurPosition.CurSN);
                //重置工序操作
                CurPosition.ResetNode();
 
                return action;
            }
 
            var wipSn = CurPosition.CurWipSNs.First();
            FlowCSN = Biz.Db.Queryable<MES_CUST_SN>().Where(q => q.FLOW_SN == wipSn.SN || q.CUST_SN == wipSn.SN).First();
            //找不到生产记录,认为扫描错误
            if (FlowCSN.IsNullOrEmpty())
            {
                action.Data.Data = new
                {
                    NeedPrint = false,
                    NeedScan = false,
                    Label = "",
                };
                CurStep.Message = Biz.L("投料错误");
                CurStep.Status = StepStatus.Error;
                action.Data.ShortMsg = new("投料错误", ShortMessage.Types.Failed);
                action.IsSuccessed = false;
                //action.LocaleMsg = new($"投料错误,找不到条码[{0}]的生产记录");
                action.LocaleMsg = new("MES.WorkAction.ThreeInOne.InputSnError", wipSn.SN);
                //重置工序操作
                CurPosition.ResetNode();
                return action;
            }
            else
            {
                //厂内条码已绑定
                if (FlowCSN.IsBound)
                {
                    //物料料号是当前工单料号或者在工单备料表中,直接结束行为
                    if (FlowCSN.ITEM_CODE == CurPosition.WorkBatch.WO.ITEM_CODE || MoPickList.Any(q => q.Code == FlowCSN.ITEM_CODE))
                    {
                        action = End(input);
                        action.Data.Data = new
                        {
                            NeedPrint = false,
                            NeedScan = false,
                            Label = "",
                        };
                        return action;
                    }
                    else
                    {
                        action.Data.Data = new
                        {
                            NeedPrint = false,
                            NeedScan = false,
                            Label = "",
                        };
                        CurStep.Message = Biz.L("投料错误");
                        CurStep.Status = StepStatus.Error;
                        action.Data.ShortMsg = new("投料错误", ShortMessage.Types.Failed);
                        action.IsSuccessed = false;
                        //action.LocaleMsg = new($"产品(厂内条码[{0}])不适用于当前工单[{1}]的生产");
                        action.LocaleMsg = new("MES.WorkAction.ThreeInOne.InputSnOrderError", FlowCSN.FLOW_SN, CurPosition.WorkBatch.WO.ORDER_NO);
                        //重置工序操作
                        CurPosition.ResetNode();
                        return action;
                    }
                }
                //厂内条码未绑定客户条码
                else
                {
                    if (FlowCSN.IsFlowSn)
                    {
                        //如果客户条码标签模板为空则不需要打印,等待扫描客户条码进行绑定
                        if (Label.IsNullOrEmpty())
                        {
                            action.Data.Data = new
                            {
                                NeedPrint = false,
                                NeedScan = true,
                                Label = "",
                            };
                            CurStep.Message = Biz.L("扫描客户标签");
                            CurStep.Status = StepStatus.Normal;
                            action.Data.ShortMsg = new("扫描客户标签", ShortMessage.Types.Normal);
                            //action.LocaleMsg = new Locale($"请扫描厂内条码[{0}]需要绑定客户条码");
                            action.LocaleMsg = new Locale("MES.WorkAction.ThreeInOne.BeginScan", wipSn.SN);
                        }
                        else
                        {
                            //设置打印变量值
                            Label = CurPosition.SetLabelVariables(LabelPV, Label, this);
                            action.Data.Data = new
                            {
                                NeedPrint = true,
                                NeedScan = true,
                                Label,
                            };
                            CurStep.Message = Biz.L("扫描已打印的客户标签");
                            CurStep.Status = StepStatus.Normal;
                            action.Data.ShortMsg = new("扫描已打印的客户标签", ShortMessage.Types.Normal);
                            //action.LocaleMsg = new Locale($"开始打印厂内条码[{0}]对应的客户条码");
                            action.LocaleMsg = new Locale("MES.WorkAction.ThreeInOne.BeginPrint", wipSn.SN);
                        }
                    }
                    else
                    {
                        action.Data.Data = new
                        {
                            NeedPrint = false,
                            NeedScan = false,
                            Label = "",
                        };
                        CurStep.Message = Biz.L("扫描厂内条码");
                        CurStep.Status = StepStatus.Error;
                        action.Data.ShortMsg = new("扫描厂内条码", ShortMessage.Types.Failed);
                        action.IsSuccessed = false;
                        //action.LocaleMsg = new($"客户条码[{0}]未被绑定,请先扫描厂内条码");
                        action.LocaleMsg = new("MES.WorkAction.ThreeInOne.PleaseScanFlowSn", wipSn.SN);
                        //重置工序操作
                        CurPosition.ResetNode();
                        return action;
                    }
                }
 
                //客户条码已绑定当前工单或者其批次属于当前工单上料物料的推荐批次,直接结束行为
                //if (FlowCSN.WORK_ORDER == CurPosition.WorkBatch.WO.ORDER_NO || MoPickList.Any(q => q.Code == FlowCSN.ITEM_CODE && q.SuggestedBatch == FlowCSN.WORK_ORDER))
                //{
                //    FlowCSN.WORK_ORDER = CurPosition.WorkBatch.WO.ORDER_NO;
                //    FlowCSN.ITEM_CODE = CurPosition.WorkBatch.WO.ITEM_CODE;
                //    action.Data.Data = new
                //    {
                //        NeedPrint = false,
                //        NeedScan = false,
                //        Label = "",
                //    };
                //    action = End(input);
                //}
                //else
                //{
                //    action.Data.Data = new
                //    {
                //        NeedPrint = false,
                //        NeedScan = false,
                //        Label = "",
                //    };
                //    CurStep.Message = Biz.L("投料错误");
                //    CurStep.Status = StepStatus.Error;
                //    action.Data.ShortMsg = new("投料错误", ShortMessage.Types.Failed);
                //    action.IsSuccessed = false;
                //    //action.LocaleMsg = new($"产品的客户条码[{0}]属于工单[{1}]不适用于当前工单[{2}]的生产");
                //    action.LocaleMsg = new("MES.WorkAction.ThreeInOne.InputSnOrderError", FlowCSN.CUST_SN, FlowCSN.WORK_ORDER, CurPosition.WorkBatch.WO.ORDER_NO);
                //    //如果行为设置为出错需要重置工序操作
                //    if (NodeAct.NEED_RESET == "Y")
                //    {
                //        CurPosition.ResetNode();
                //    }
                //    return action;
                //}
            }
 
            //如果返回成功则认为当前行为可以开始执行,否则返回失败
            action.IsSuccessed = true;
            
            return action;
        }
 
        /// <summary>
        /// 工序行为提交数据
        /// </summary>
        /// <returns></returns>
        public ApiAction<SubmitOutput> Submit(SubmitInput input)
        {
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
 
            //查询是否存在已导入的客户条码
            CustCSN = Biz.Db.Queryable<MES_CUST_SN>().Where(q => q.CUST_SN == input.Data).First();
 
            var isOK = true;
            //客户条码扫到厂内条码
            if (isOK && FlowCSN.FLOW_SN == input.Data)
            {
                CurStep.SetStatusMessage(StepStatus.Error, Biz.L("请扫描正确的客户标签"));
                action.Data.ShortMsg = new("请扫描正确的客户标签", ShortMessage.Types.Failed);
                action.IsSuccessed = isOK = false;
                //action.LocaleMsg = new($"厂内条码[{0}]绑定客户条码[{1}]失败,厂内条码与客户条码不能一样,请扫描正确的客户标签");
                action.LocaleMsg = new("MES.WorkAction.ThreeInOne.TheSameSN", FlowCSN.FLOW_SN, input.Data);
            }
            //客户条码已绑定其他厂内条码
            if (isOK && !CustCSN.IsNullOrEmpty() && !CustCSN.FLOW_SN.IsNullOrEmpty() && CustCSN.FLOW_SN != FlowCSN.FLOW_SN)
            {
                CurStep.SetStatusMessage(StepStatus.Error, Biz.L("请扫描正确的客户标签"));
                action.Data.ShortMsg = new("请扫描正确的客户标签", ShortMessage.Types.Failed);
                action.IsSuccessed = isOK = false;
                //action.LocaleMsg = new($"厂内条码[{0}]绑定客户条码[{1}]失败,客户条码已绑定厂内条码[{2}],请扫描正确的客户标签");
                action.LocaleMsg = new("MES.WorkAction.ThreeInOne.BindingOtherSN", CurPosition.CurWipSNs.First().SN, CustCSN.CUST_SN, CustCSN.FLOW_SN);
            }
            //客户条码不属于当前工单的客户
            if (isOK && !CustCSN.IsNullOrEmpty() && CustCSN.CUST_CODE.IsNullOrEmpty(CurPosition.WorkBatch.WO.CUST_CODE) != CurPosition.WorkBatch.WO.CUST_CODE)
            {
                CurStep.SetStatusMessage(StepStatus.Error, Biz.L("请扫描正确的客户标签"));
                action.Data.ShortMsg = new("请扫描正确的客户标签", ShortMessage.Types.Failed);
                action.IsSuccessed = isOK = false;
                //action.LocaleMsg = new($"客户条码[{0}]属于客户[{1}],与当前工单的客户[{2}]不一致,请扫描正确的客户标签");
                action.LocaleMsg = new("MES.WorkAction.ThreeInOne.NotSameCustomer", CustCSN.CUST_SN, CustCSN.CUST_CODE, CurPosition.WorkBatch.WO.CUST_CODE);
            }
            //验证产品客户标签条码规则
            if (isOK && !CustInfo.IsNullOrEmpty() && !CustInfo.RULE_CODE.IsNullOrEmpty() && !Biz.CodeRule[CustInfo.RULE_CODE].IsNullOrEmpty())
            {
                var result = Biz.CodeRule[CustInfo.RULE_CODE].Verify(CustCSN.CUST_SN);
                if (!result.IsSuccessed)
                {
                    CurStep.SetStatusMessage(StepStatus.Error, Biz.L("请扫描正确的客户标签"));
                    action.Data.ShortMsg = new("请扫描正确的客户标签", ShortMessage.Types.Failed);
                    action.IsSuccessed = isOK = false;
                    //action.LocaleMsg = new Locale($"标签条码[{0}]未通过客户标签规则[{1}]验证,请扫描正确的客户标签");
                    action.LocaleMsg = new Locale("MES.WorkAction.ThreeInOne.CustSnVerifyError", CustCSN.CUST_SN, CustInfo.RULE_CODE);
                }
            }
 
            //验证通过则保存
            if (isOK)
            {
                FlowCSN.CUST_SN = input.Data;
                action = End(input);
            }
            else
            {
                //如果行为设置为出错需要重置工序操作
                if (NodeAct.NEED_RESET == "Y")
                {
                    CurPosition.ResetNode();
                }
            }
 
            return action;
        }
        /// <summary>
        /// 结束执行工序行为
        /// </summary>
        /// <returns></returns>
        public ApiAction<SubmitOutput> End(SubmitInput input)
        {
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
 
            //更新厂内流程码
            FlowCSN.CUST_CODE = CurPosition.WorkBatch.WO.CUST_CODE;
            FlowCSN.PKG_ORDER = CurPosition.WorkBatch.WO.ORDER_NO;
            FlowCSN.PROD_CODE = CurPosition.WorkBatch.WO.ITEM_CODE;
 
            //记录行为操作记录
            var wipActs = new List<MES_WIP_ACT>();
            var woSns = new List<BIZ_MES_WO_SN>();
            var snHiss = new List<MES_WIP_HIS>();
            foreach (var wipSn in CurPosition.CurWipSNs)
            {
                //更新在制品信息SN为客户条码
                wipSn.SN = FlowCSN.CUST_SN;
                //更新在制品信息历史得SN为客户条码
                var his = CurPosition.CurWipSNHiss.First(q => q.WIP_ID == wipSn.ID);
                his.SN = FlowCSN.CUST_SN;
                snHiss.Add(his);
                //更新工单条码表的
                var woSn = CurPosition.WorkBatch.WoSNs.First(q => q.WIP_ID == wipSn.ID);
                woSn.SN = FlowCSN.CUST_SN;
                woSns.Add(woSn);
                var wipAct = new MES_WIP_ACT()
                {
                    AUTH_ORG = CurPosition.WorkBatch.WO.AUTH_ORG,
                    AUTH_PROD = CurPosition.CurLine.LINE_CODE,
                    HIS_ID = CurPosition.CurWipSNHiss.First(q => q.SN == wipSn.SN).ID,
                    WIP_ID = wipSn.ID,
                    SN = wipSn.SN,
                    STATUS = wipSn.STATUS,
                    ITEM_CODE = wipSn.ITEM_CODE,
                    WORK_ORDER = wipSn.WORK_ORDER,
                    BATCH_NO = wipSn.BATCH_NO,
                    ROT_CODE = wipSn.ROT_CODE,
                    NODE_ID = wipSn.NODE_ID,
                    NODE_NAME = wipSn.NODE_NAME,
                    ACT_ID = NodeAct.ID,
                    ACT_NAME = NodeAct.ACT_NAME,
                    FTY_CODE = wipSn.FTY_CODE,
                    WS_CODE = wipSn.WS_CODE,
                    LINE_CODE = wipSn.LINE_CODE,
                    POST_CODE = wipSn.POST_CODE,
                    OPER_CODE = wipSn.OPER_CODE,
                    SEGMENT = wipSn.SEGMENT,
                    FLOW_SN = wipSn.FLOW_SN,
                    TRAY_SN = wipSn.TRAY_SN,
                    INNER_SN = wipSn.INNER_SN,
                    CARTON_SN = wipSn.CARTON_SN,
                    PALLET_SN = wipSn.PALLET_SN,
                    OPERATION_TIME = DateTime.Now,
                    SFTS_CODE = wipSn.SFTS_CODE,
                    SFT_CODE = wipSn.SFT_CODE,
                    PRD_CODE = wipSn.PRD_CODE,
                    ACT_TYPE = NodeAct.ACT_TYPE,
                    ACT_SN = wipSn.SN,
                    ACT_VALUE_1 = Label?.LABEL_CODE,
                    ACT_VALUE_2 = Label?.Variables.ToJson(),
                    ACT_RESULT = "Y",
                    ELAPSED_TIME = CurStep.GetElapsedTime().TotalMilliseconds.ToInt64(),
                    TRACE_INFO = $"厂内条码[{FlowCSN.FLOW_SN}]绑定客户条码[{FlowCSN.CUST_SN}]成功",
                };
                wipActs.Add(wipAct);
            }
 
            //创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
            var _wipSns = CurPosition.CurWipSNs.Clone();
            var _snHiss = snHiss.Clone();
            var _woSns = woSns.Clone();
            var _wipActs = wipActs.Clone();
            var _FlowCSN = FlowCSN.Clone();
            var _CustCSN = CustCSN.Clone();
            //保存数据
            CurStep.DBSubmitAction = () =>
            {
                var db = CurPosition.GetCommitDB();
                db.Storageable(_wipSns, CurPosition.UserCode).ExecuteCommand();
                db.Storageable(_snHiss, CurPosition.UserCode).ExecuteCommand();
                db.Storageable(_woSns, CurPosition.UserCode).ExecuteCommand();
                db.Storageable(_wipActs, CurPosition.UserCode).ExecuteCommand();
                db.Storageable(_FlowCSN, CurPosition.UserCode).ExecuteCommand();
                if (!_CustCSN.IsNullOrEmpty())
                {
                    db.Deleteable(_CustCSN).ExecuteCommand();
                }
            };
 
            IsFinished = true;
            CurStep.Message = Biz.L("客户条码绑定成功");
            CurStep.Status = StepStatus.Finished;
            action.Data.ShortMsg = new("客户条码绑定成功", ShortMessage.Types.Success);
            //action.LocaleMsg = new($"厂内条码[{0}]已绑定客户条码[{1}]完成", Label.LABEL_NAME);
            action.LocaleMsg = new("MES.WorkAction.ThreeInOne.BindingComplete", FlowCSN.FLOW_SN, FlowCSN.CUST_SN);
            return action;
        }
 
        #endregion Functions
    }
}