服务端的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
using Rhea.Common;
using Tiger.Model;
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.Sharetronic.Shelf;
using Tiger.Business.WMS.Sharetronic.Shelf;
using Tiger.IBusiness;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// 一般移库事务
    /// </summary>
    public class LocationTransfer : WMSTransactionBase, ILocationTransfer
    {
        public ILocationTransfer Init(string id, string userCode, string apiHost)
        {
            TransID = id;
            UserCode = userCode;
            ApiHost = apiHost;
            Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
            return this;
        }
 
        #region Propertys & Variables
        public string UserId { get; set; }
        public string UserCode { get; set; }
        public string OrgCode { get; set; }
        public WMS_SHELF CurShelf { get; set; }
        public WMS_LOCATION CurLocation { get; set; }
        #endregion
 
        #region Functions
        /// <summary>
        /// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
        /// </summary>
        public async Task<ApiAction<LocationTransferReturn>> ScanItem(BaseInput input)
        {
            var action = new ApiAction<LocationTransferReturn>();
            try
            {
                if (string.IsNullOrEmpty(input.SN))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.SnEmptyFailure"); // $"条码不能为空!";
                    return action;
                }
                input.AuthOption.OrgCode = input.AuthOption.OrgCode.IsNullOrEmpty() ? input.AuthOption.CurOrg : input.AuthOption.OrgCode;
                OrgCode =input.AuthOption.OrgCode;
                var _action = await ScanShelf(input.SN);
        
                //货架为空则扫描的认为是货架
                if ((CurLocation.IsNullOrEmpty() && CurShelf.IsNullOrEmpty()) || _action.IsSuccessed)
                {
                    if (!_action.IsSuccessed)
                    {
                        action.LocaleMsg = _action.LocaleMsg;
                        action.IsSuccessed = _action.IsSuccessed;
                    }
                    // 扫描货架代码,且为智能货架
                    if (_action.Data != null && _action.Data.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() && _action.Data.SHELF_CODE == input.SN)
                    {
                        CurShelf = _action.Data.Shelf;
                        CurLocation = null;
                    }
                    // 扫描库位代码
                    else// if (_action.Data != null && _action.Data.LOCATION_CODE == input.SN)
                    {
                        if (_action.Data.IsNullOrEmpty())
                        {
                            action.IsSuccessed = false;
                            action.LocaleMsg = Biz.L($"当前储位不存在{input.SN}");
                            return action;
                        }
                        if (_action.Data.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() || _action.Data.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue())
                        {
                            var locationData = Biz.Db.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == _action.Data.LOCATION_ID && q.AUTH_ORG == _action.Data.AUTH_ORG).First();
                            if (!locationData.IsNullOrEmpty())
                            {
                                action.IsSuccessed = false;
                                action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息");
                                return action;
 
                            }
                        }
                        CurShelf = _action.Data?.Shelf;
                        CurLocation = _action.Data?.Location;
                    }
                    action.Data = new()
                    {
                        LocationCode = input.SN,
                    };
                    return action;
                }
 
                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;
                }
                var inv = result.Data;
 
                //验证条码是否正确
                if (!inv.isNormalStatus || !new[] { WMS_ITEM.STATUSs.InStore }.Contains(inv.Status))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(x => x.GetDesc())));
                    return action;
                }
 
                //原储位灭灯
                if (inv?.Shelf?.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() && !inv.Location.IsNullOrEmpty())
                {
                    await Share.Shelf.DownSingle(TransID, inv.Location);
                }
                //物料验证
                if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N")
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.ItemCodeNotExistsOrNotActive", inv.ItemInfo?.ITEM_CODE.IsNullOrEmpty(inv.Barcode.ItemCode));
                    return action;
                }
                // 判断是否智能货架
                WMS_LOCATION target = null;
                if (CurLocation.IsNullOrEmpty() && (CurShelf.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue()|| CurShelf.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue()))
                {
                    ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, CurShelf, inv.Items[0]);
                    if (!shelfApiResult.IsSuccess)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L(shelfApiResult.GetData<string>());
                        return action;
                    }
                    var reaultShelf = shelfApiResult.GetData<ShelfChangeModel>();
 
                    target = reaultShelf.GetLocation();
                    if (target.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L($"系统不存在储位,请先维护货架信息");
                        return action;
                    }
                    var locationData = Biz.Db.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == target.ID && q.AUTH_ORG==target.AUTH_ORG).First();
                    if (!locationData.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息");
                        return action;
 
                    }
                }
                else
                {
                    if (CurLocation.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.LocationEmptyFailure"); //$"请先扫描库位码";
                        return action;
                    }
                    target = CurLocation;
                }
 
                //执行入库
                Result<PutOnInfo> onResult = WMS_ITEM_Biz.WmsItem.PutOn(inv, input.AuthOption, target?.LOCATION_CODE, true);
                if (!onResult.IsSuccessed)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = onResult.LocaleMsg;
                    return action;
                }
                PutOnInfo putOnInfo = onResult.Data;
 
                //保存到数据库
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    //入库
                    db.Updateable(putOnInfo.Items, UserCode).ExecuteCommand();
                    db.Insertable(putOnInfo.History, UserCode).ExecuteCommand();
                    db.Updateable(putOnInfo.Packages, UserCode).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
                action.LocaleMsg = Biz.L($"储位调拨成功");
                //if (inv?.Shelf?.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue())
                //{
                //    //CurLocation = null;
                //}
                action.Data = new()
                {
                    ItemCode = inv.ItemInfo.ITEM_CODE,
                    ItemName = inv.ItemInfo.ITEM_NAME,
                    SN = inv.SN,
                    Qty = inv.CurPkg.QTY,
                    LocationCode = putOnInfo.Location.LOCATION_CODE,
                };
            }
            catch (Exception ex)
            {
                //取消当前操作
                action.CatchExceptionWithLog(ex, $"扫描条码[{input.SN}]复核异常");
            }
            return action;
        }
 
        /// <summary>
        /// 扫描货架或者储位
        /// </summary>
        public async Task<ApiAction<V_WH_UNIT>> ScanShelf(string code)
        {
            var action = new ApiAction<V_WH_UNIT>();
            try
            {
                if (code.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfCanNotEmpty");
                    return action;
                }
 
                // 查询货架信息
                var whUnit = await Biz.Db.Queryable<V_WH_UNIT>().Where(t => (t.SHELF_CODE.ToUpper() == code.ToUpper() || t.LOCATION_CODE.ToUpper() == code.ToUpper())&& t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
                if (whUnit.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotExist", code);
                    return action;
                }
 
                if (whUnit.IS_ACTIVE == "N")
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfOrLocationDisabled");
                    return action;
                }
                action.Data = whUnit;
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"扫描货架或者储位[{code}]异常");
            }
            return action;
        }
 
        #endregion
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
 
    }//endClass
}