服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2025-03-10 40df5d91f800bb8b7be2638c13b4f9292d461221
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
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.Model;
using Tiger.IBusiness;
using Tiger.Model.Sharetronic.Shelf;
using Tiger.Business.WMS.Sharetronic.Shelf;
using Tiger.Model.MES.Yada;
using static IronPython.SQLite.PythonSQLite;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// 送货单(U9)清点事务
    /// </summary>
    public class Count_BIZ_U9_ASN : WmsTask, ICount_BIZ_U9_ASN
    {
        public ICount_BIZ_U9_ASN Init(string id, string userCode, string apiHost, string orgCode)
        {
            base.Init(id, apiHost, userCode, orgCode);
            //加载已经清点数据
            LoadData();
            Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
            return this;
        }
 
        #region Propertys & Variables
        //private List<WmsOrder<BIZ_U9_ASN>> _OrderList { get; set; } = new();
        private List<string> OrderList = new();
        private BIZ_U9_ASN_SN CurSn { get; set; }
        #endregion
 
        #region Functions
        /// <summary>
        /// 初始化清点数据
        /// </summary>
        private void LoadData()
        {
            var orders = MainDB.Queryable<BIZ_U9_ASN>().Where(q => ("," + q.RECEIVER + ",").Contains("," + UserCode + ",")).ToList();
            OrderList = orders.Select(q => q.ORDER_NO).ToList();
        }
        /// <summary>
        /// 扫描入口
        /// </summary>
        public async Task<ApiAction<ScanOutput>> Scan(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput());
            try
            {
                if (input.SN.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("条码不能为空");
                    action.LocaleMsg = Biz.L("WMS.Default.ScanItem.SnEmptyFailure");
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                //扫描物料并复核
                else //if (Command == "Normal")
                {
                    action = await ScanItem(input);
                }
            }
            catch (Exception ex)
            {
                //取消当前操作
                ResetScan();
                //action.CatchExceptionWithLog(ex, $"扫描[{input.SN}]异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.Scan.ScanException", input.SN));
            }
            return SetOutPutMqttMsg(action, input.Locale);
        }
 
        /// <summary>
        /// 扫描物料并复核
        /// </summary>
        public async Task<ApiAction<ScanOutput>> ScanItem(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput() { Command = Command });
            try
            {
                if (CurInvItem.IsNullOrEmpty())
                {
                    //解析条码
                    Barcode barcode = new Barcode(input.SN);
                    if (barcode.IsException)
                    {
                        throw barcode.AnalyseException;
                    }
                    //非法条码
                    else if (!barcode.IsRegular)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("条码为非法条码");
                        action.LocaleMsg = Biz.L("WMS.WmsItem.Barcode.IllegalFailure", input.SN);
                        return action;
                    }
                    CurSn = MainDB.Queryable<BIZ_U9_ASN_SN>().Where(q => q.SN == barcode.SN).First();
 
                    if (CurSn.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("ASN单中不存在条码[{0}]");
                        action.LocaleMsg = Biz.L("WMS.Count_BIZ_U9_ASN.SnNotExists", input.SN);
                        return action;
                    }
                    if (CurSn.AUTH_ORG != input.AuthOption.CurOrg)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L($"条码所属组织[{0}]与当前登录组织[{1}]不一致,请重新选择组织");
                        action.LocaleMsg = Biz.L("WMS.Count_BIZ_U9_ASN.OrgNotCorrect", CurSn.AUTH_ORG, input.AuthOption.CurOrg);
                        return action;
                    }
                    //验证条码是否正确
                    if (CurSn.STATUS > WMS_ITEM.STATUSs.Incoming.GetValue())
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("状态[{0}]异常,请重新扫描");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.StatusException", CurSn.STATUS.GetEnumDesc<WMS_ITEM.STATUSs>());
                        return action;
                    }
                    CurSn.META_SN = input.SN;
 
                    Result<IInventory> result = WMS_ITEM_Biz.WmsItem.Get(barcode.SN, input.AuthOption, true);
                    if (!result.IsSuccessed)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = result.LocaleMsg;
                        return action;
                    }
                    var inv = result.Data as Inventory;
 
                    //物料验证
                    if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N")
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("物料编码[{0}]不存在或者该物料未启用");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ItemCodeNotExistsOrNotActive", inv.ItemInfo.ITEM_CODE.IsNullOrEmpty(inv.Barcode.ItemCode));
                        return action;
                    }
 
                    if (inv.Items.Count == 0)
                    {
                        List<WMS_ITEM_HIS> ItemHistorys = new List<WMS_ITEM_HIS>();
                        WMS_ITEM Item;
                        WMS_ITEM_PKG ItemPkgs;
 
                        Item = new()
                        {
                            SN = inv.Barcode.SN,
                            ITEM_CODE = inv.Barcode.ItemCode,
                            AUTH_ORG = input.AuthOption.CurOrg,
                            STATUS = WMS_ITEM.STATUSs.WaitIn.GetValue(),
                            QTY = inv.Barcode.Qty.ToDouble(),
                            PROD_DATE = inv.Barcode.DateCode.ToDateTime(),
                            ERP_WH = CurScanShelf.WarehouseCode,
                            UNIT = inv.Barcode.Unit,
                        };
 
                        WMS_ITEM_HIS his = new(Item, $"送货单(U9)清点");
                        ItemHistorys.Add(his);
                        ItemPkgs = new()
                        {
                            SN = inv.Barcode.SN,
                            AUTH_ORG = input.AuthOption.CurOrg,
                            ITEM_CODE = inv.Barcode.ItemCode,
                            QTY = inv.Barcode.Qty.ToDouble(),
                            ERP_WH = CurScanShelf.WarehouseCode,
                            UNIT = inv.Barcode.Unit,
                        };
 
                        var noExt = inv.Items.Where(q => !inv.ItemsExt.Any(s => s.SN == q.SN)).ToList();
                        //扩展表不存在时新建
                        foreach (var item in noExt)
                        {
                            inv.ItemsExt.Add(new()
                            {
                                SN = item.SN,
                                META_SN = inv.Barcode.MetaSn,
                                QR_CODE = inv.Barcode.MetaSn
                            });
                        }
 
                        inv.Items.Add(Item);
                        inv.History.Add(his);
                        inv.Packages.Add(ItemPkgs);
                    }
                    //雅达不从条码上更新数量
                    //else if (inv.Items.Count == 1)
                    //{
                    //    inv.Items.First().QTY = inv.Barcode.Qty.ToDouble();
                    //    inv.Items.First().ERP_WH = CurScanShelf.WarehouseCode;
                    //    inv.Items.First().AUTH_ORG = input.AuthOption.CurOrg;
 
                    //    inv.Packages.First().QTY = inv.Barcode.Qty.ToDouble();
                    //    inv.Packages.First().ERP_WH = CurScanShelf.WarehouseCode;
                    //    inv.Packages.First().AUTH_ORG = input.AuthOption.CurOrg;
                    //}
                    //如果扫描外箱的不能更新数量
                    else
                    {
                        foreach (var item in inv.Items)
                        {
                            item.ERP_WH = CurScanShelf.WarehouseCode;
                            item.AUTH_ORG = input.AuthOption.CurOrg;
                        }
                        foreach (var item in inv.Packages)
                        {
                            item.ERP_WH = CurScanShelf.WarehouseCode;
                            item.AUTH_ORG = input.AuthOption.CurOrg;
                        }
                    }
                    CurInvItem = inv;
 
                    //检查物料数量,如果没有则查询雅达条码主档,把数量返回前端确认
                    if (CurInvItem.Items.Any(q => q.QTY <= 0))
                    {
                        var qtyList = Biz.DataSource["YadaU9C"].Client.Queryable<mes_MaterialBarCode>().Where(q => CurInvItem.Items.Select(q => q.SN).Contains(q.Code)).ToList();
                        foreach (var qty in qtyList)
                        {
                            var item = CurInvItem.Items.First(q => q.SN == qty.Code);
                            item.QTY = qty.WhQty;
                        }
                        action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY });
                        action.Data.Command = Command = "ComfirmQty";
                        //action.LocaleMsg = Biz.L("请确认条码[{0}]的数量是否正确");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ComfirmQty", input.SN);
                        return action;
                    }
                }
 
                //更新已确认物料数量
                if (Command == "ComfirmQty")
                {
                    var qtyList = (input.Data ?? "").JsonToObject<List<WMS_ITEM>>() ?? new List<WMS_ITEM>();
                    if (!qtyList.Any() || qtyList.Any(q => q.QTY <= 0))
                    {
                        action.IsSuccessed = false;
                        action.Data.Data = CurInvItem.Items.Select(q => new { q.SN, q.QTY });
                        action.Data.Command = Command = "ComfirmQty";
                        //action.LocaleMsg = Biz.L("数量不能小于等于零,请重新确认条码[{0}]的数量");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmQty", input.SN); 
                        return action;
                    } 
                    else
                    {
                        foreach (var qty in qtyList)
                        {
                            var item = CurInvItem.Items.First(q => q.SN == qty.SN);
                            item.QTY = qty.QTY;
                            var pkg = CurInvItem.Packages.First(q => q.SN == qty.SN);
                            pkg.QTY = qty.QTY;
                        }
                        CurInvItem.UpdatePkgQty();
 
                        Command = "Normal";
                    }
                }
 
                //执行清点数据处理
                CurSn.STATUS = WMS_ITEM.STATUSs.Counted.GetValue();
                CurSn.QTY = CurInvItem.Items.Sum(q => q.QTY);
                foreach (var item in CurInvItem.Items)
                {
                    item.STATUS = WMS_ITEM.STATUSs.Counted.GetValue();
                    item.TRANS_CODE = nameof(BIZ_U9_ASN);
                    item.TRANS_NO = CurSn.ORDER_NO;
                    item.TRANS_LINE = CurSn.LINE_NO;
                    item.AUTH_ORG = CurSn.AUTH_ORG;
                    CurInvItem.History.Add(new WMS_ITEM_HIS(item, $"条码[{CurInvItem.SN}]清点成功,操作单据[{item.TRANS_NO}]"));
                }
 
                //创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
                var _CurSn = CurSn.Clone();
                var _Items = CurInvItem.Items.Clone();
                var _Packages = CurInvItem.Packages.Clone();
                var _History = CurInvItem.History.Clone();
                AddCommitAction("Counted", () =>
                {
                    //使用统一的事务DB对象
                    var db = GetCommitDB();
                    //数据保存逻辑
                    var t = db.Storageable(_Items, UserCode).ToStorage();
                    t.AsInsertable.ExecuteCommand();
                    t.AsUpdateable.UpdateColumns(q => new { q.STATUS, q.TRANS_CODE, q.TRANS_NO, q.TRANS_LINE, q.AUTH_ORG, q.UPDATE_TIME, q.UPDATE_USER }).ExecuteCommand();
                    db.Storageable(_Packages, UserCode).ToStorage().AsInsertable.ExecuteCommand();
                    db.Insertable(_History, UserCode).ExecuteCommand();
                    db.Storageable(_CurSn, UserCode).ExecuteCommand();
                });
 
 
                //完成所有处理后使用事务保存数据
                action = DoIfFinish(action, input.Locale);
            }
            catch (Exception ex)
            {
                //取消当前操作
                ResetScan();
                //action.CatchExceptionWithLog(ex, $"扫描条码[{input.SN}]复核异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanItem.ScanException", input.SN));
            }
            return action;
        }
 
        /// <summary>
        /// 完成所有处理后使用事务保存数据
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public ApiAction<ScanOutput> DoIfFinish(ApiAction<ScanOutput> action, string locale)
        {
            //保存数据库
            SaveCommitListToDB();
 
            // 返回数据
            action.Data.Data = new DefaultInStoreOutput
            {
                Barcode = CurInvItem.SN,
                MaterialCode = CurInvItem.ItemInfo.ITEM_CODE,
                MaterialName = CurInvItem.ItemInfo.ITEM_NAME,
                CurrentQty = CurInvItem.Items[0].QTY,
                Unit = CurInvItem.Items[0].UNIT,
                DateCode = CurInvItem.Items[0].PROD_DATE,
                WarehouseCode = CurInvItem.Warehouse.WH_CODE,
                RegionCode = CurInvItem.Region.REGION_CODE,
                ShelfCode = CurInvItem.Shelf.SHELF_CODE,
                LocationCode = CurInvItem.Location.LOCATION_CODE,
            };
 
            //action.LocaleMsg = Biz.L($"扫描条码[{0}]上架到储位[{1}]成功");
            action.LocaleMsg = Biz.L("WMS.Default.ScanItem.PutOnSucceeded", CurInvItem.SN, CurInvItem.Location.LOCATION_CODE);
 
            //重置工序
            ResetScan();
            return action;
        }
 
        #endregion
 
        /// <summary>
        /// 重置当前操作,有需要则重写此方法
        /// </summary>
        public override void ResetScan()
        {
            base.ResetScan();
            Command = null;
            CurInvItem = null;
            CurScanShelf = null;
            CurSn = null;
        }
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
 
    }//endClass
}