服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-12-07 790032df98d586627a771159d5b6c7646e3fff35
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
using Rhea.Common;
using Tiger.IBusiness;
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.Model.Sharetronic.Shelf;
using System.ComponentModel;
using Tiger.Model.Entitys.MES.U9C;
using ProdInBatch = Tiger.Model.ProdInBatch;
using Org.BouncyCastle.Ocsp;
using Tiger.Model.Entitys.MES.BizPrintInstoreDoc;
using System.Drawing.Printing;
using Tiger.Model.Entitys.MES.Position;
 
namespace Tiger.Business.MES.Transaction
{
    /// <summary>
    /// 入库单事务
    /// </summary>
    public class InStoreOrderNo : MESTransactionBase, IInStoreOrderNo
    {
        private readonly IMES_U9C _IMES_U9C = DI.Resolve<IMES_U9C>();
        private readonly IBIZ_MES_WO _IBIZ_MES_WO = DI.Resolve<IBIZ_MES_WO>();
        public IInStoreOrderNo Init(string id, string apiHost, InstoreDocInput input)
        {
            TransID = id;
            UserCode = input.UserCode;
            ApiHost = apiHost;
            OrgCode = input.OrgCode;
            pageSize = input.pageSize;
            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 CurSN { get; set; }
        public int pageSize { get; set; }
        public RcvRptDocCreateInput RcvRptInput { get; set; } = new RcvRptDocCreateInput();
        public List<ProdInBatch> ProdInBatchs { get; set; } = new List<ProdInBatch>();
        public List<BIZ_ERP_PROD_IN_BTH> ErpProdInBths { get; set; } = new List<BIZ_ERP_PROD_IN_BTH>();
        /// <summary>
        /// 扫描列表 
        /// </summary>
        public List<InStoreScanInfo> InStoreScanInfos { get; set; } = new List<InStoreScanInfo>();
        public List<BIZ_ERP_PROD_IN_SN> ErpProdInSns { get; set; } = new List<BIZ_ERP_PROD_IN_SN>();
 
        #endregion Propertys & Variables
 
        #region Functions
 
        /// <summary>
        /// 扫描条码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<InStoreInfos>> Submit(string code)
        {
            var action = new ApiAction<InStoreInfos>();
            try
            {
                if (code.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L($"条码不能为空");
                    return action;
                }
                var apiAction = await _IBIZ_MES_WO.GetErpProdInBth(code);
                if (!apiAction.IsSuccessed)
                {
                    return action;
                }
                CurSN = code.Split('|')[0];
                InStoreInfo Info = apiAction.Data;
                //要存入的批次入库单数据 
                int n = InStoreScanInfos.Where(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER).Count();
                if (!ErpProdInBths.Any(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER))
                {
                    Info.ErpProdInBth.AUTH_ORG = OrgCode;
                    Info.ErpProdInBth.CREATE_USER = UserCode;
                    Info.ErpProdInBth.CREATE_TIME = DateTime.Now;
                    Info.ErpProdInBth.SCANED_DATE = DateTime.Now;
                    Info.ErpProdInBth.HANDLED_DATE = DateTime.Now;
                    Info.ErpProdInBth.IS_SCANED = "Y";
                    Info.ErpProdInBth.ORDER_NO = UserCode;
                    Info.ErpProdInBth.IS_HANDLED = "N";
                    Info.ErpProdInBth.CartonQty = n == 0 ? 1 : n;
                    ErpProdInBths.Add(Info.ErpProdInBth);
                }
                else
                {
                    if (!InStoreScanInfos.Any(q => q.WORK_ORDER == Info?.ErpProdInBth.WORK_ORDER && q.SN == CurSN))
                    {
                        var erpbth = ErpProdInBths.Where(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER).FirstOrDefault();
                        if (!erpbth.IsNullOrEmpty())
                        {
                            erpbth.SCAN_QTY += Info.InStoreScanInfo.SCAN_QTY.ToDouble();
                            Info.ErpProdInBth.CartonQty++;
                        }
                    }
                }
 
                //生成打印实体
                if (RcvRptInput.PrintJsons.Count == 0)
                {
                    RcvRptInput.PrintJsons.Add(new()
                    {
                        ID = Guid.NewGuid().ToString("N"),
                        Items = new() {
                            Info.ErpProdInBth
                        }
                    });
                }
                else
                {
                    //拼分页
                    bool isAdded = false;
                    foreach (var item in RcvRptInput.PrintJsons)
                    {
                        if (!item.Items.Any(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER))
                        {
                            if (item.Items.Count < pageSize)
                            {
                                item.Items.Add(Info.ErpProdInBth);
                                isAdded = true;
                            }
                        }
                        else
                        {
                            isAdded = true;
                        }
                    }
                    if (isAdded)
                    {
                        RcvRptInput.PrintJsons.Add(new()
                        {
                            ID = Guid.NewGuid().ToString("N"),
                            Items = new() {
                            Info.ErpProdInBth
                        }
                        });
                    }
                }
 
                //暂存扫码数据
                if (!InStoreScanInfos.Any(q => q.WORK_ORDER == Info?.ErpProdInBth.WORK_ORDER && q.SN == CurSN))
                {
                    InStoreScanInfos.Add(Info.InStoreScanInfo);
                }
                var wipPkgs = Biz.Db.Ado.UseStoredProcedure().SqlQuery<MES_WIP_PKG>("SP_MES_GET_ITEM_PKG", new SugarParameter("root", CurSN));
                int maxLevel = wipPkgs.Max(p => p.LEVEL);
                foreach (var item in wipPkgs.Where(q=> q.LEVEL == maxLevel))
                {
                    var scanResult = new BIZ_ERP_PROD_IN_SN
                    {
                        ID = Guid.NewGuid().ToString(),
                        CARTONNO = CurSN,
                        SN = item.SN,
                        BUSINESSCODE = UserCode,
                        ITEM_CODE = item.ITEM_CODE,
                        Qty = item.QTY.ToDecimal(),
                        SOURCECODE = Info?.ErpProdInBth.WORK_ORDER,
                        STATUS = WMS_ITEM.STATUSs.Counted.GetValue(),
                        CREATE_USER = UserCode,
                        CREATE_TIME = DateTime.Now,
                        UPDATE_USER = UserCode,
                        UPDATE_TIME = DateTime.Now,
                    };
                    ErpProdInSns.Add(scanResult);
                }
 
                //保存数据库
                var db = Business.Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var x = db.Storageable(ErpProdInBths, $"InStoreOrderNo_{UserCode}")
                             .WhereColumns(t => new { t.ORDER_NO, t.WORK_ORDER, t.GHOST_ROW })
                             .ToStorage();
                    x.AsInsertable.ExecuteCommand();
                    x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    var s = db.Storageable(ErpProdInSns, $"InStoreOrderNo_{UserCode}")
                                .WhereColumns(t => new { t.CARTONNO, t.SN, t.GHOST_ROW })
                                .ToStorage();
                    s.AsInsertable.ExecuteCommand();
                    s.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
                action.Data = new() { 
                    ErpProdInBths = ErpProdInBths,
                    InStoreScanInfos = InStoreScanInfos,
                };
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"扫描条码[{code}]复核异常");
            }
            return action;
        }
 
        /// <summary>
        /// 调用U9C接口生成入库单
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<RcvRptDocCreateInput>> GenerateInStoreOrder(RcvRptDocCreateInput input)
        {
            var action = new ApiAction<RcvRptDocCreateInput>();
            try
            {
                //input.RcvRptDocBases = new()
                //{
                //    new(){
                //    WorkOrder = item.WORK_ORDER,
                //    ItemCode = item.ITEM_CODE,
                //    WhCode = "10105",
                //    PkgQty = item.SCAN_QTY,
                //    CompleteQty = item.SCAN_QTY,
                //    OutputType = 0,
                //    StorageType = 4,
                //    DocState = 1,
                //    Status = "N"
                //    }
                //};
                action = await _IMES_U9C.RcvRptDocCreate(input);
 
                //保存数据库
                var db = Business.Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    //var x = db.Storageable(ErpProdInBths, $"InStoreOrderNo_{UserCode}")
                    //         .WhereColumns(t => new { t.ORDER_NO, t.WORK_ORDER, t.GHOST_ROW })
                    //         .ToStorage();
                    //x.AsInsertable.ExecuteCommand();
                    //x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    //var s = db.Storageable(ErpProdInSns, $"InStoreOrderNo_{UserCode}")
                    //            .WhereColumns(t => new { t.CARTONNO, t.SN, t.GHOST_ROW })
                    //            .ToStorage();
                    //s.AsInsertable.ExecuteCommand();
                    //s.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
 
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"调用U9C接口生成入库单异常");
            }
            return action;
        }
 
        /// <summary>
        /// 查询对应的条码
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction<RcvRptDocCreateInput>> GetScannedBarcodes()
        {
            var action = new ApiAction<RcvRptDocCreateInput>();
            try
            {
                //var query = await Biz.Db.Queryable<BIZ_ERP_RECEIPT_SN, BAS_ITEM>((t, m) =>
                //                        new JoinQueryInfos(
                //                            JoinType.Left, t.ITEM_CODE == m.ITEM_CODE
                //                            ))
                //    .Where((t, m) => t.CREATE_USER == UserCode
                //        //&& t.WAREHOUSECODE.ToUpper() == WarehouseCode.ToUpper()
                //        && t.STATUS == WMS_ITEM.STATUSs.Counted.GetValue()
                //        && t.ITEM_CODE.ToUpper() == MaterialCode.ToUpper()
                //        && t.BILLCODE == UserCode
                //        && t.AUTH_ORG == OrgCode)
                //    .Select((t, m) => new Model.Minsun.CustSupplyCheckDetail
                //    {
                //        Barcode = t.SN,
                //        BarcodeQty = t.SCANQTY,
                //        Unit = m.UNIT
                //    })
                //    .ToListAsync();
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"查询对应的条码异常");
            }
            return action;
        }
 
        #endregion Functions
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            //needSaveHistoryLog = true;
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
    }//endClass
}