服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-09-04 0116b5d2ed16a0825da9e7474fc786ee06b2d60c
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
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;
 
namespace Tiger.Business.MES.WorkAction
{
    public class WipExtInfo : IWipExtInfo
    {
        #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 BAS_WIP_EXT ExtInfo { get; set; } 
        public List<MES_WIP_EXT> CurWipExt { 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
 
            //获取扩展字段定义
            var seq = Setting.ITEM_CODE.ToInt32();
            ExtInfo = Biz.Db.Queryable<BAS_WIP_EXT>().Where(q => q.SEQ == seq).First();
            //获取当前SN的扩展信息
            CurWipExt = Biz.Db.Queryable<MES_WIP_EXT>().Where(q => CurPosition.CurWipSNs.Any(w => w.SN == q.SN)).ToList();
            foreach (var wipSN in CurPosition.CurWipSNs)
            {
                if (!CurWipExt.Any(q => q.SN == wipSN.SN))
                {
                    CurWipExt.Add(new()
                    {
                        AUTH_ORG = wipSN.AUTH_ORG,
                        AUTH_PROD = wipSN.AUTH_PROD,
                        SN = wipSN.SN,
                        WIP_ID = wipSN.ID,
                    });
                }
            }
        }
 
        /// <summary>
        /// 获取行为开始的提示信息
        /// </summary>
        /// <returns></returns>
        public Locale GetBeginMsg()
        {
            var msg = new Locale("MES.WorkAction.WipExtInfo.BeginMsg", ExtInfo.FIELD_NAME);
            //var msg = new Locale($"请扫描产品[{CurPosition.CurWipSN.SN}]需要绑定的[{ExtInfo.FIELD_NAME}]标签条码");
            return msg;
        }
 
        /// <summary>
        /// 尝试开始执行工序行为
        /// </summary>
        /// <returns></returns>
        public ApiAction<SubmitOutput> TryBegin(SubmitInput input)
        {
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
 
            action.LocaleMsg = GetBeginMsg();
 
            //如果返回成功则认为当前行为可以开始执行,否则返回失败
            CurStep.Message = Biz.L("绑定开始");
            CurStep.Status = StepStatus.Normal;
            action.IsSuccessed = true;
            action.Data.ShortMsg = new("绑定开始", ShortMessage.Types.Success);
            return action;
        }
 
        /// <summary>
        /// 工序行为提交数据
        /// </summary>
        /// <returns></returns>
        public ApiAction<SubmitOutput> Submit(SubmitInput input)
        {
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
 
            action = SaveExtInfo(input, action);
            //上料数据保存失败
            if (!action.IsSuccessed)
            {
                //如果行为设置为出错需要重置工序操作
                if (NodeAct.NEED_RESET == "Y")
                {
                    CurPosition.ResetNode();
                }
            }
            return action;
        }
 
        public ApiAction<SubmitOutput> SaveExtInfo(SubmitInput input, ApiAction<SubmitOutput> action)
        {
            try
            {
                var isOK = true;
                //验证扫描的条码是否SN的扩展信息的标签
                if (input.SN.IsNullOrEmpty())
                {
                    CurStep.Message = Biz.L("绑定失败");
                    CurStep.Status = StepStatus.Error;
                    action.Data.ShortMsg = new("绑定失败", ShortMessage.Types.Failed);
                    action.IsSuccessed = isOK = false;
                    //action.LocaleMsg = new Locale($"错误:[{ExtInfo.FIELD_NAME}]标签条码不能为空,请重新扫描");
                    action.LocaleMsg = new Locale("MES.WorkAction.WipExtInfo.SnEmptyError", ExtInfo.FIELD_NAME);
                }
                if (CurPosition.CurWipSNs.Any(w => w.SN == input.SN))
                {
                    CurStep.Message = Biz.L("绑定失败");
                    CurStep.Status = StepStatus.Error;
                    action.Data.ShortMsg = new("绑定失败", ShortMessage.Types.Failed);
                    action.IsSuccessed = isOK = false;
                    //action.LocaleMsg = new Locale($"错误:扫描到产品条码[{input.SN}],请重新扫描[{ExtInfo.FIELD_NAME}]标签条码");
                    action.LocaleMsg = new Locale("MES.WorkAction.WipExtInfo.ScanProdSnError", input.SN, ExtInfo.FIELD_NAME);
                }
 
                //验证通过则保存上料信息
                if (isOK)
                {
                    CurWipExt.GetType().GetProperty($"FIELD_{ExtInfo.SEQ.ToString("00")}")?.SetValue(CurWipExt, input.SN);
                    action = End(input);
                }
 
                //都没有物料验证通过,则返回错误信息
            }
            catch (System.Exception ex)
            {
                CurStep.Message = Biz.L("绑定异常");
                CurStep.Status = StepStatus.Error;
                action.Data.ShortMsg = new("绑定异常", ShortMessage.Types.Exception);
                action.CatchExceptionWithLog(ex, $"扩展信息绑定行为:产品[{CurPosition.CurSN}]绑定[{ExtInfo.FIELD_NAME}]标签条码[{input.SN}]保存异常");
                action.IsSuccessed = false;
                //action.LocaleMsg = new($"产品[{CurPosition.CurWipSN.SN}]绑定[{ExtInfo.FIELD_NAME}]标签条码[{input.SN}]保存异常,工序已重置,请重新扫描进站产品条码");
                action.LocaleMsg = new("MES.WorkAction.WipExtInfo.SaveExtInfoException", CurPosition.CurSN, ExtInfo.FIELD_NAME, input.SN);
                CurPosition.ResetNode();
            }
            return action;
        }
 
        /// <summary>
        /// 结束执行工序行为
        /// </summary>
        /// <returns></returns>
        public ApiAction<SubmitOutput> End(SubmitInput input)
        {
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
 
            //记录行为操作记录
            var wipActs = new List<MES_WIP_ACT>();
            foreach (var wipSn in CurPosition.CurWipSNs)
            {
                var wipAct = new MES_WIP_ACT()
                {
                    AUTH_ORG = CurPosition.WorkBatch.WO.AUTH_ORG,
                    AUTH_PROD = CurPosition.CurLine.LINE_CODE,
                    WIP_ID = wipSn.ID,
                    HIS_ID = CurPosition.CurWipSNHiss.First(q => q.SN == wipSn.SN).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 = CurWipExt.ToJson(),
                    ACT_RESULT = "Y",
                    TRACE_INFO = $"产品[{CurPosition.CurSN}]绑定[{ExtInfo.FIELD_NAME}]标签条码[{CurWipExt.GetType().GetProperty($"FIELD_{ExtInfo.SEQ.ToString("00")}")?.GetValue(CurWipExt)?.ToString()}]保存成功",
                };
                wipActs.Add(wipAct);
            }
 
            //创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
            var _wipActs = wipActs.Clone();
            var _CurWipExt = CurWipExt.Clone();
            //保存数据
            CurStep.DBSubmitAction = () =>
            {
                var db = CurPosition.GetCommitDB();
                db.Storageable(_wipActs, CurPosition.UserCode).ExecuteCommand();
                db.Storageable(_CurWipExt, CurPosition.UserCode).ExecuteCommand();
            };
 
            IsFinished = true;
            CurStep.Message = Biz.L("绑定完成");
            CurStep.Status = StepStatus.Finished;
            action.Data.ShortMsg = new("绑定完成", ShortMessage.Types.Success);
            //action.LocaleMsg = new($"产品[{CurPosition.CurWipSN.SN}]绑定[{ExtInfo.FIELD_NAME}]标签条码[{CurWipExt.GetType().GetProperty($"FIELD_{ExtInfo.SEQ.ToString("00")}")?.GetValue(CurWipExt)?.ToString()}]保存成功");
            action.LocaleMsg = new("MES.WorkAction.WipExtInfo.SaveSuccess", CurPosition.CurSN, ExtInfo.FIELD_NAME, CurWipExt.GetType().GetProperty($"FIELD_{ExtInfo.SEQ.ToString("00")}")?.GetValue(CurWipExt)?.ToString());
            return action;
        }
 
        #endregion Functions
    }
}