服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-05-31 d4c326deaa51e7d4897a84afc339684012b8cfbe
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
using Apache.NMS.ActiveMQ.Commands;
using Newtonsoft.Json;
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Entitys.WMS.Api;
using Tiger.Model.Minsun;
 
namespace Tiger.Business.WMS
{
    /// <summary>
    /// 补印条码
    /// </summary>
    public class RePrintBarcode : IRePrintBarcode
    {
        #region Propertys & Variables
        public static string UserCode { get; set; }
        public static long UserId { get; set; }
        public static string OrgCode { get; set; }
        public static List<Inventory> invs { get; set; } = new();
        public static Inventory CurInv { get; set; }
        #endregion
        /// <summary>
        /// 补印条码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiAction<ReprintReturnEntitys> ScanItem(ReprintEntitys input)
        {
            var action = new ApiAction<ReprintReturnEntitys>();
            try
            {
                if (input.SN.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("条码不能为空");
                    return action;
                }
 
                UserCode = input.AuthOption.UserId;
                OrgCode = input.AuthOption.OrgCode;
 
                //解析条码
                Result<Inventory> result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true);
                if (!result.IsSuccessed)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = result.LocaleMsg;
                    return action;
                }
                CurInv = result.Data;
 
                if (!CurInv.isExists)
                {
                    //action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L($"条码不存在");
                    action.Data = new ReprintReturnEntitys()
                    {
                        Barcode = CurInv.Barcode.SN,
                        MaterialCode = CurInv.Barcode.ItemCode,
                        MaterialName = CurInv.Barcode.ItemName,
                        MaterialStandard = CurInv.Barcode.ItemName,
                        DateCode = (DateTime)CurInv.Barcode.ProdDate,
                        ScanQty = (decimal)CurInv.Barcode.Qty,
                        Unit = CurInv.Barcode.Unit,
                        VenderSName = ""
                    };
                    action.LocaleMsg = Biz.L($"条码[{input.SN}]信息返回成功,如果数量不对请输入数量打印");
                    return action;
 
                }
                var suppEnt = Biz.Db.Queryable<BAS_SUPPLIER>().Where(x => x.SUPP_CODE == CurInv.Items[0].SUPP_CODE).First();
                action.Data = new ReprintReturnEntitys()
                {
                    Barcode = CurInv.CurPkg.SN,
                    MaterialCode = CurInv.ItemInfo.ITEM_CODE,
                    MaterialName = CurInv.ItemInfo.ITEM_NAME,
                    MaterialStandard = CurInv.ItemInfo.SPEC,
                    DateCode = CurInv.CurPkg.PRINT_DATE,
                    ScanQty = CurInv.CurPkg.QTY,
                    Unit = CurInv.ItemInfo.UNIT,
                    VenderSName = suppEnt?.SUPP_NAME_CN
                };
                action.LocaleMsg = Biz.L($"条码[{input.SN}]信息返回成功,如果数量不对请输入数量打印");
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"补印异常");
            }
 
            return action;
 
        }
 
        /// <summary>
        /// 供应商退料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiAction<ReturnBarcodeReponse> ReturnBarcodeRecord(ReturnBarcodeInput input)
        {
            var action = new ApiAction<ReturnBarcodeReponse>();
            try
            {
                DbClient db = Biz.DataSource["XCSJ"].Client;
                var findBarcode = db.Queryable<WMS_BARCODE>().Where(t => t.BARCODE.ToUpper() == input.Barcode.ToUpper()).First(); //await _barcodeRepository.FirstOrDefaultAsync(t => t.Barcode.ToUpper() == input.Barcode.ToUpper());
                if (findBarcode == null)
                {
                    action.IsSuccessed = false;
                    action.Message = $"条码信息有误";
                    return action;
                }
 
                //克隆对象
                var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };
                var nReturnBarcodeNo = $"{input.Barcode}-T";
 
                //条码一
                var nReturnBarcode1 = JsonConvert.DeserializeObject<WMS_BARCODE>(JsonConvert.SerializeObject(findBarcode), deserializeSettings);
                nReturnBarcode1.BARCODE = nReturnBarcodeNo;
                //nReturnBarcode1.BarcodeStatus = (int)Domain.InvStorageInfoSides.BarcodeStatus.Outstock;     //出库状态
                nReturnBarcode1.PACKQTY = input.Qty;
                nReturnBarcode1.CURRENTQTY = input.Qty;
                nReturnBarcode1.INITIALQTY = input.Qty;
                nReturnBarcode1.Id = Guid.NewGuid().ToString();
 
                //生成barcode操作
                //await _barcodeRepository.InsertAsync(nReturnBarcode1);
                findBarcode.BARCODESTATUS = WMS_BARCODE.InvStorageInfoSides.BarcodeStatus.InValid.GetValue(); //(int)Domain.InvStorageInfoSides.BarcodeStatus.InValid;//状态为无效
                //await _barcodeRepository.UpdateAsync(findBarcode);
 
                //新增SRMSNList
                var nBarcodeList = new WMS_SRMSNList()
                {
 
                    SOURCETYPE = "WMS",
                    MATERIALCODE = findBarcode.MATERIALCODE,
                    DeliveryNo = findBarcode.SOURCECODE,
                    SmallBarcode = nReturnBarcodeNo,
                    BigBarcode = findBarcode.CARTON,
                    OuterBarcode = findBarcode.PALLET,
                    IncludeQty = input.Qty,
                    production_date = DateTime.Now
                };
                //await _wms_srmsnlistRepository.InsertAsync(nBarcodeList);
 
                //var nWorkShopLotInfo = db.Queryable<WMS_WORKSHOPLOTINFO>() //await _workShopLotInfoRepository.FirstOrDefaultAsync(t => t.Barcode.ToUpper() == input.Barcode.ToUpper());
                //if (nWorkShopLotInfo == null)
                //{
                //    throw new UserFriendlyException("扫描的条码不在线边仓中,无法进行退料");
                //}
                //var _nWorkShopLotInfo = JsonConvert.DeserializeObject<Domain.InvWorkShopLotInfo>(JsonConvert.SerializeObject(nWorkShopLotInfo), deserializeSettings);
                //_nWorkShopLotInfo.Barcode = nReturnBarcodeNo;
                //_nWorkShopLotInfo.LastQty = input.Qty;
                //_nWorkShopLotInfo.Id = new Guid();
                //await _workShopLotInfoRepository.InsertAsync(_nWorkShopLotInfo);
 
                //获取供应商信息
                var vender = db.Queryable<MesVender>().Where(t => t.VenderCode == findBarcode.SUPPLIERCODE).First(); //await _venderRepository.FirstOrDefaultAsync(t => t.VenderCode == findBarcode.SupplierCode);
 
                DbClient _db = Biz.DataSource["XCSJ"].Client;
                //保存到数据库
                var dbTran = _db.UseTran(() =>
                {
                    _db.Insertable(nReturnBarcode1).ExecuteCommand();
                    _db.Updateable(findBarcode).ExecuteCommand();
                    //_db.Utilities.PageEach(pkgs, 1000, pageList =>
                    //{
                    //    var x = _db.Storageable(pageList, "SRM")
                    //       .WhereColumns(t => new { t.SN, t.GHOST_ROW })
                    //       .ToStorage();
                    //    x.BulkCopy();
                    //    x.BulkUpdate(); //.IgnoreColumns(x => x.ID);
                    //});
                    //_db.Fastest<WMS_ITEM_HIS>().BulkCopy(hiss);//插入
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Scheduler.Error($"退料异常,错误:[{dbTran.ErrorException.Message}]");
                }
 
                action.Data = new ReturnBarcodeReponse
                {
                    Barcode = nReturnBarcodeNo,
                    VenderSName = vender != null ? vender.VenderName : ""
                };
            }
            catch (Exception ex)
            {
                Logger.Scheduler.Error($"退料异常,错误:[{ex.Message}]");
            }
            return action;
        }
 
 
        /// <summary>
        /// 退料扫描
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiAction<RePrintScanReponse> ScanReprintBarcode(IncomingInventoryInput input)
        {
            var action = new ApiAction<RePrintScanReponse>();
            try
            {
                DbClient db = Biz.DataSource["XCSJ"].Client;
                if (input.Barcode.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("请输入或扫描正确的条码");
                    return action;
                }
 
                var nDisassembleBarcode = iWMS.SplitFullBarcode(input.Barcode).Data; //await _commonService.SplitFullBarcode(input.Barcode, true);
                WMS_PO_H nOrderHeader = null;
                List<WMS_PO_D> nOrderDetails = new List<WMS_PO_D>();
                if (nDisassembleBarcode.OrderNo != null)
                {
                    //验证订单是否存在
                    nOrderHeader = db.Queryable<WMS_PO_H>().Where(x => x.BILLCODE == nDisassembleBarcode.OrderNo).First(); //await _headerRepository.FirstOrDefaultAsync(x => x.BillCode.ToUpper() == nDisassembleBarcode.OrderNo.ToUpper());
                    //if (nOrderHeader == null)
                    //    throw new UserFriendlyException("条码对应的采购订单不存在");
 
                    if (nOrderHeader != null && nDisassembleBarcode.MaterialCode != null)
                    {
                        //一个订单中存在多行相同物料的情况()
                        nOrderDetails = db.Queryable<WMS_PO_D>().Where(x => x.BILLCODE == nOrderHeader.BILLCODE && x.MATERIALCODE == nDisassembleBarcode.MaterialCode).ToList(); //await _detailRepository.GetAll().Where(x => x.BillCode.ToUpper() == nOrderHeader.BillCode.ToUpper() && x.MaterialCode.ToUpper() == nDisassembleBarcode.MaterialCode.ToUpper()).AsNoTracking().ToListAsync();
                        //if (!nOrderDetails.Any())
                        //    throw new UserFriendlyException("条码对应的订单没有该物料的采购信息");
 
                    }
 
                }
                var t = 1;
                //定义条码值
                var nTempResult = new RePrintScanTempOutput
                {
                    BillCode = nOrderHeader != null ? nOrderHeader.BILLCODE : "",
                    BillLine = nOrderDetails.Count > 0 ? nOrderDetails.First().BILLLINE : 0,  //存在多行相同物料时,这个字段为null,但目前后续的表结构不支持这么操作
                    MaterialCode = nDisassembleBarcode.MaterialCode,
                    MaterialName = nDisassembleBarcode.MaterialName,
                    MaterialStandard = nDisassembleBarcode.MaterialStandard,
                    SupplierCode = nOrderHeader != null ? nOrderHeader.SUPPLIERCODE : "",
                    Barcode = nDisassembleBarcode.Barcode,  //批次号即为条码
                    DateCode = nDisassembleBarcode.LotDate,
                    LotNo = nDisassembleBarcode.Barcode,
                    StateFlag = nDisassembleBarcode.StateFlag,
                    ScanQty = nDisassembleBarcode.LotQty,   //批次数量
                    Unit = nDisassembleBarcode.Unit
                };
 
 
                action.Data = new RePrintScanReponse
                {
                    ActionType = "SUCCESS",
                    ErrorMessage = "验证通过",
                    Result = nTempResult
                };
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"退料打印异常");
            }
 
            return action;
        }
 
        /// <summary>
        /// 新系统退料扫描
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiAction<RePrintScanReponse> ScanReprintBarcodeNew(IncomingInventoryInput input)
        {
            var action = new ApiAction<RePrintScanReponse>();
            try
            {
                DbClient db = Biz.Db;
                if (input.Barcode.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("请输入或扫描正确的条码");
                    return action;
                }
                //解析条码
                Result<Inventory> result = WMS_ITEM_Biz.WmsItem.Get(input.Barcode, new AuthOption { ByOrg = true, CurOrg = "DGXC", UserId = "admin" }, false);
                if (!result.IsSuccessed)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = result.LocaleMsg;
                    return action;
                }
                var CurInv = result.Data;
                var newItem = CurInv.isExists ? CurInv.Items[0] : new WMS_ITEM()
                {
                    AUTH_ORG = "DGXC",
                    SN = CurInv.Barcode.SN,
                    ITEM_CODE = CurInv.Barcode.ItemCode,
                    STATUS = WMS_ITEM.STATUSs.Returned.GetValue(),
                    QTY = input.Qty,
                    PROD_DATE = (DateTime)CurInv.Barcode.ProdDate,
                    FIRST_IN_DATE = DateTime.Now,
                    TRANS_CODE = "供应商退料",
                    TRANS_LINE = "",
                    TRANS_NO = CurInv.Barcode.OrderNo,
                    SUPP_CODE = CurInv.Barcode.SupplierCode,
                    CARTON_NO = "",
                    PALLET_NO = "",
                    IS_LOCKED = "N",
                    SOURCE_CODE = "供应商退料",
                    SOURCE_LINE = "",
                    SOURCE_ORDER = CurInv.Barcode.OrderNo,
                    CREATE_USER = "供应商退料",
                    UPDATE_USER = "供应商退料",
                };
                newItem.TRANS_CODE = "供应商退料";
                newItem.TRANS_LINE = "";
                newItem.SOURCE_CODE = "供应商退料";
                newItem.SOURCE_LINE = "";
                newItem.SOURCE_ORDER = CurInv.Barcode.OrderNo;
                var strSn = newItem.SN.Split("_");
                var cutSerial = strSn.Length == 1 ? "1" : $"{Convert.ToInt32(strSn[1].Substring(2)) + 1}";
                var orglSn = strSn[0];
                var _sn = $"{orglSn}_CT{cutSerial}";
                newItem.SN = _sn;
                newItem.QTY = input.Qty;
                var item = db.Queryable<WMS_ITEM>().Where(x => x.SN == _sn).First();
                newItem.ID = item != null ? item.ID : newItem.ID;
 
                var oPkg = db.Queryable<WMS_ITEM_PKG>().Where(x => x.SN == _sn).First();
                var pkg = CurInv.isExists ? CurInv.CurPkg : new WMS_ITEM_PKG()
                {
                    AUTH_ORG = "DGXC",
                    SN = _sn,
                    PARENT_SN = "",
                    PKG_TYPE = CurInv.Barcode.SN.StartsWith("P") ? WMS_ITEM_PKG.PKG_TYPEs.Pallet.GetValue() : (CurInv.Barcode.SN.StartsWith("C") ? WMS_ITEM_PKG.PKG_TYPEs.Carton.GetValue() : WMS_ITEM_PKG.PKG_TYPEs.OnePiece.GetValue()),
                    ITEM_CODE = CurInv.Barcode.ItemCode,
                    QTY = input.Qty,
                    TRANS_CODE = "供应商退料",
                    TRANS_NO = CurInv.Barcode.OrderNo,
                    SUPP_CODE = CurInv.Barcode.SupplierCode,
                };
                pkg.ID = oPkg != null ? oPkg.ID : pkg.ID;
                pkg.SN = _sn;
                pkg.TRANS_CODE = "供应商退料";
                pkg.TRANS_NO = CurInv.Barcode.OrderNo;
                pkg.QTY = input.Qty;
 
                action.Data = new RePrintScanReponse
                {
                    ActionType = "SUCCESS",
                    ErrorMessage = "验证通过",
                    Result = new RePrintScanTempOutput
                    {
                        BillCode = "",
                        BillLine = 0,  //存在多行相同物料时,这个字段为null,但目前后续的表结构不支持这么操作
                        MaterialCode = !CurInv.isExists ? CurInv.Barcode.ItemCode : CurInv.ItemInfo?.ITEM_CODE,
                        MaterialName = !CurInv.isExists ? CurInv.Barcode.ItemName : CurInv.ItemInfo?.ITEM_NAME,
                        MaterialStandard = !CurInv.isExists ? CurInv.Barcode.ItemName : CurInv.ItemInfo?.ITEM_NAME,
                        SupplierCode = !CurInv.isExists ? CurInv.Barcode.SupplierCode : CurInv.Items[0].SUPP_CODE,
                        Barcode = _sn,  //批次号即为条码
                        DateCode = !CurInv.isExists ? (DateTime)CurInv.Barcode.ProdDate : CurInv.Items[0].PROD_DATE,
                        LotNo = _sn,
                        StateFlag = "",
                        ScanQty = input.Qty,   //批次数量
                        Unit = !CurInv.isExists ? CurInv.Barcode.Unit : CurInv.ItemInfo?.UNIT
                    }
                };
                DbClient _db = Biz.Db;
                //保存到数据库
                var dbTran = _db.UseTran(() =>
                {
                    _db.Storageable(newItem, "Supplier").ExecuteCommand();
                    _db.Insertable(new WMS_ITEM_HIS(newItem, "供应商退料打印"), "Supplier").ExecuteCommand();
                    _db.Storageable(pkg, "Supplier").ExecuteCommand();
 
                    //_db.Fastest<WMS_ITEM_HIS>().BulkCopy(hiss);//插入
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Scheduler.Error($"退料异常,错误:[{dbTran.ErrorException.Message}]");
                }
 
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"退料打印异常");
            }
 
            return action;
        }
    }
}