服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
19 小时以前 a960900364d19bbf0ad7923a57989609e7fce798
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
using Rhea.Common;
using Microsoft.AspNetCore.Http;
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 Apache.NMS;
using System.Drawing.Drawing2D;
using Tiger.Model.Sharetronic.Shelf;
using Tiger.IBusiness;
using Tiger.Model.MES.Yada;
using Apache.NMS.ActiveMQ.Commands;
using System.Diagnostics;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// 查储位补印条码事务
    /// </summary>
    public class RePrintLabel : WMSTransactionBase, IRePrintLabel
    {
        public IRePrintLabel Init(string id, string userCode, string apiHost, string orgCode)
        {
            TransID = id;
            UserCode = userCode;
            ApiHost = apiHost;
            OrgCode = orgCode;
            Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
            return this;
        }
 
        #region Propertys & Variables
        public string UserCode { get; set; }
        public long UserId { get; set; }
        public string OrgCode { get; set; }
        public string LocationCode { get; set; }
        public List<V_WMS_ITEM> Vitem { get; set; } = new();
        public ScanShelfInfo CurScanShelf { get; set; }
 
        #endregion
 
        #region Functions
        /// <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("WMS.RePrint.ScanItem.SnEmptyFailure");
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                var LotNos = Biz.DataSource["YadaU9C"].Client.Ado.SqlQuery<mes_WhLotCodeQtyInfo>($"select *,'' as ID from mes_WhLotCodeQtyInfo where Bin = '{input.SN}'"); 
                //Biz.DataSource["YadaU9C"].Client.Queryable<mes_WhLotCodeQtyInfo>().Where(t => t.Bin == input.SN).ToList();
                CurScanShelf = new ScanShelfInfo();
                // 查询货架信息
                var whUnit = await Biz.Db.Queryable<V_WH_UNIT>().Where(t => (t.SHELF_CODE.ToUpper() == input.SN || t.LOCATION_CODE.ToUpper() == input.SN) && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
                if (!whUnit.IsNullOrEmpty() && whUnit.LOCATION_CODE == input.SN)
                {
                    if (whUnit.IS_ACTIVE == "N")
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("扫描的储位[{0}]未启用");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfOrLocationDisabled", input.SN);
                        return action;
                    }
                    if (whUnit.Shelf.IsLightShelf || whUnit.Location.IS_SINGLE == "Y")
                    {
                        var locationData = Biz.Db.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == whUnit.LOCATION_ID && q.AUTH_ORG == OrgCode).First();
                        if (!locationData.IsNullOrEmpty())
                        {
                            action.IsSuccessed = false;
                            //action.LocaleMsg = Biz.L("储位[{0}]已存有物料[{1}],请检查系统库存信息");
                            action.LocaleMsg = Biz.L($"WMS.Default.ScanShelf.ItemAlreadyExistsInLocation", input.SN, locationData.SN);
                            return action;
                        }
                    }
                    CurScanShelf.Shelf = whUnit.Shelf;
                    CurScanShelf.Location = whUnit.Location;
                    CurScanShelf.WarehouseCode = whUnit.WH_CODE;
                    CurScanShelf.RegionCode = whUnit.REGION_CODE;
                    CurScanShelf.ShelfCode = whUnit.SHELF_CODE;
                    CurScanShelf.LocationCode = whUnit.LOCATION_CODE;
                    CurScanShelf.IsSmartRack = false;
                    LocationCode = whUnit.LOCATION_CODE;
                }
                else {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
                    action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
                    return action;
                }
                var temps = new List<TemplateInput>();
                foreach (var item in LotNos)
                {
                    var temp = new TemplateInput
                    {
                        custCode="",
                        itemCode= item.ItemCode,
                        itemDesc= item.Description,
                        sapItemCode= item.SapCode,
                        WoBatch = item.LotCode,
                        batchQty= item.StoreQty,
                        qrCode="",
                    };
                    temps.Add(temp);
                }
 
                var y = Biz.Db.Storageable(LotNos, UserCode)
                               .WhereColumns(t => new { t.ItemCode, t.LotCode })
                               .ToStorage();
                y.AsInsertable.ExecuteCommand();
                y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                action.Data.Data = temps;
                action.LocaleMsg = Biz.L("WMS.RePrint.ScanItem.ScanSuccessed");
            }
            catch (Exception ex)
            {
                //action.CatchExceptionWithLog(ex, $"扫描[{input.SN}]异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.RePrint.Scan.ScanException", input.SN));
            }
            return SetOutPutMqttMsg(action, input.Locale);
        }
 
        public async Task<ApiAction<ScanOutput>> ScanItem(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput());
            try
            {
                List<WMS_ITEM> items = new List<WMS_ITEM>();
                List<WMS_ITEM_HIS> itemhiss = new List<WMS_ITEM_HIS>();
                List<WMS_ITEM_PKG> itemPkgs = new List<WMS_ITEM_PKG>();
                List<WMS_ITEM_EXT> itemExts = new List<WMS_ITEM_EXT>();
                var rePrints = (input.Data ?? "").JsonToObject<RePrintLabelBase>() ?? new RePrintLabelBase();
                if (!rePrints.SnList.Any() || rePrints.SnList.Any(q => q.Qty <= 0))
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("数量不能小于等于零,请重新确认条码[{0}]的数量");
                    action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ReComfirmQty", input.SN);
                    return action;
                }
                else
                {
                    //if (Biz.Db.Queryable<WMS_ITEM>().Any(q=>q.SUPP_LOTNO == rePrints.BatchNo) && (Biz.Db.Queryable<WMS_ITEM>().Where(q=>q.SUPP_LOTNO == rePrints.BatchNo && q.STATUS == WMS_ITEM.STATUSs.InStore.GetValue()).Sum(x=>x.QTY)>= rePrints.BatchQty)) { 
                    //    action.IsSuccessed = false;
                    //    action.LocaleMsg = Biz.L($"此库位[{LocationCode}]上的批次[{rePrints.BatchNo}]数量已经够数,不能再上架生成标签");
                    //    return action;
                    //}
                    foreach (var ent in rePrints.SnList)
                    {
                        string CurSN = DI.Resolve<ICodeRuleCache>()["InitialBarcode"]?.Generate($"{ent.SN}").Data.ToString() ?? "";
                        ent.SN = CurSN;
 
                        //解析条码
                        Result<IInventory> result = WMS_ITEM_Biz.WmsItem.Get(CurSN, input.AuthOption, true);
                        if (!result.IsSuccessed)
                        {
                            action.IsSuccessed = false;
                            action.LocaleMsg = result.LocaleMsg;
                            return action;
                        }
                        var inv = result.Data as Inventory;
 
                        //验证条码是否正确
                        if (new[] { WMS_ITEM.STATUSs.InStore, WMS_ITEM.STATUSs.Loaded, WMS_ITEM.STATUSs.UseUp }.Contains(inv.Status))
                        {
                            action.IsSuccessed = false;
                            //action.LocaleMsg = Biz.L("状态[{0}]异常,请重新扫描");
                            action.LocaleMsg = Biz.L("WMS.Default.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(q => q.GetDesc())));
                            return action;
                        }
                        //物料验证
                        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, $"储位补印上架入库");
                            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;
                            }
                        }
 
                        //更新已确认物料数量
                        var _item = inv.Items.First(q => q.SN == CurSN);
                        _item.QTY = ent.Qty;
                        var pkg = inv.Packages.First(q => q.SN == CurSN);
                        pkg.QTY = ent.Qty;
                        inv.UpdatePkgQty();
 
                        var nLocation = new WMS_LOCATION();
 
                        if (CurScanShelf.LocationCode.IsNullOrEmpty() || CurScanShelf.WarehouseCode.IsNullOrEmpty())
                        {
                            action.IsSuccessed = false;
                            //action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
                            action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
                            return action;
                        }
 
                        nLocation = await Biz.Db.Queryable<WMS_LOCATION>().Where(t => t.LOCATION_CODE == CurScanShelf.LocationCode && t.AUTH_ORG == OrgCode).FirstAsync();
 
                        //判断储位是否单放
                        //if (nLocation.IS_SINGLE == "Y" && inv.Items.Count > 1)
                        //{
                        //    action.IsSuccessed = false;
                        //    //action.LocaleMsg = Biz.L("储位[{0}]只能存放一个物料");
                        //    action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationSingleFailure", nLocation.LOCATION_CODE);
                        //    CurInvItem = null;
                        //    CurSN = "";
                        //    return action;
                        //}
 
                        //执行上架
                        //foreach (var item in inv.Items)
                        //{
                        //    item.TRANS_CODE = "ReprintLabel";
                        //    item.TRANS_NO = $"ReprintLabel{DateTime.Now:yyyyMMdd}";
                        //}
                        //Result<PutOnInfo> putonResult = WMS_ITEM_Biz.WmsItem.PutOn(inv, input.AuthOption, nLocation.LOCATION_CODE);
                        //if (!putonResult.IsSuccessed)
                        //{
                        //    action.IsSuccessed = false;
                        //    action.LocaleMsg = putonResult.LocaleMsg;
                        //    inv = null;
                        //    CurSN = "";
                        //    return action;
                        //}
                        //PutOnInfo putOnInfo = putonResult.Data;
                        //items.AddRange(putOnInfo.Items);
                        //itemhiss.AddRange(putOnInfo.History);
                        //itemPkgs.AddRange(putOnInfo.Packages);
                        //itemExts.AddRange(putOnInfo.ItemsExt);
                    }
                    //保存到数据库
                    //var db = Business.Biz.Db;
                    //var dbTran = db.UseTran(() =>
                    //{
                    //    //入库
                    //    db.Storageable(items, UserCode).ExecuteCommand();
                    //    db.Storageable(itemPkgs, UserCode).ExecuteCommand();
                    //    db.Insertable(itemhiss, UserCode).ExecuteCommand();
                    //    var x = db.Storageable(itemExts, UserCode).ToStorage();
                    //    x.AsInsertable.ExecuteCommand();//不存在插入
                    //    x.AsUpdateable.ExecuteCommand();//存在更新
                    //});
                    //if (!dbTran.IsSuccess)
                    //{
                    //    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    //    throw dbTran.ErrorException;
                    //}
 
                    action.Data.Data = rePrints;
                    action.LocaleMsg = Biz.L("库位批次条码上架成功");
                }
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, Biz.L("库位批次条码上架失败"));
            }
            return action;
        }
 
        #endregion
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            needSaveHistoryLog = true;
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
 
    }//endClass
}