服务端的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
using Newtonsoft.Json;
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using Tiger.IBusiness;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.Model;
 
namespace Tiger.Business.WMS
{
    public partial class BS : IBS
    {
        /// <summary>
        /// 获取所有仓库机构列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public async Task<string> GetHouseModel(string name, string status)
        {
            var data = new List<OrgItemH>();
            V_WH_TREE func = null;
            if (!string.IsNullOrEmpty(name))
            {
                func = Biz.Db.Queryable<V_WH_TREE>().Where(x => x.NAME == name).First();
            }
            var items = await Biz.Db.Queryable<V_WH_TREE>()
                .Where(q => q.TYPE != "Location")
                .OrderBy(x => x.CODE)
                .Distinct().ToListAsync();
            //没有查询的时候
            if (func == null)
            {
                List<V_WH_TREE> entities = items.OrderBy(x => x.CODE).ToList().FindAll(t => t.PARENT_ID.IsNullOrEmpty());
                foreach (var item in entities)
                {
                    OrgItemH houseItem = new();
                    houseItem.id = item.TYPE.ToString().FirstOrDefault() + item.ID;
                    houseItem.houseCode = item.CODE;
                    houseItem.houseName = item.AUTH_ORG + ":" + item.CODE + "(" + item.NAME + ")";
                    houseItem.PARENT_HOUSE = item.PARENT_ID;
                    houseItem.createTime = DateTime.Now.ToString("yyyy-MM-dd mm:dd:ss");
                    houseItem.houseType = item.TYPE;
                    //hosueItem.remark = item.REMARK;
                    //hosueItem.orderNo = item.SEQ_NO;
                    houseItem.children = new();
                    if (item.TYPE != "Shelf") GetOrgItem(items, item.ID, ref houseItem);
                    data.Add(houseItem);
                }
            }
            //有查询条件的时候
            else
            {
 
            }
 
            //转换Json时,去除值为null的属性
            var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            var json = JsonConvert.SerializeObject(data, Formatting.Indented, jsonSetting);
            return json;
        }
 
        /// <summary>
        /// 递归返回组织机构
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parent"></param>
        /// <param name="pOrgItem"></param>
        private static void GetOrgItem(List<V_WH_TREE> data, string parent, ref OrgItemH pOrgItem)
        {
            List<V_WH_TREE> entities = data.OrderBy(x => x.CODE).ToList().FindAll(t => t.PARENT_ID == parent).Distinct().ToList();
            if (entities.Count > 0)
            {
                foreach (var item in entities)
                {
                    OrgItemH houseItem = new();
                    houseItem.id = item.TYPE.ToString().FirstOrDefault() + item.ID;
                    houseItem.houseCode = item.CODE;
                    houseItem.houseName = item.AUTH_ORG + ":" + item.CODE + "(" + item.NAME + ")";
                    houseItem.PARENT_HOUSE = item.PARENT_ID;
                    houseItem.createTime = DateTime.Now.ToString("yyyy-MM-dd mm:dd:ss");
                    houseItem.houseType = item.TYPE;
                    //houseItem.remark = item.REMARK;
                    //houseItem.orderNo = item.SEQ_NO;
                    houseItem.children = new();
                    if (item.TYPE != "Shelf") GetOrgItem(data, item.ID, ref houseItem);
                    pOrgItem.children.Add(houseItem);
                }
            }
        }
 
        /// <summary>
        /// 获取所有仓库机构列表
        /// </summary>
        /// <param name="listItem"></param>
        /// <returns></returns>
        public async Task<string> GetHouseModelOrg(HouseListItem listItem)
        {
            var data = new List<OrgItemH>();
            V_WH_TREE func = null;
            var items = await Biz.Db.Queryable<V_WH_TREE>()
                .Where(q => q.TYPE != "Location" && q.AUTH_ORG == listItem.AUTH_ORG)
                .OrderBy(x => x.CODE)
                .Distinct().ToListAsync();
            //没有查询的时候
            if (func == null)
            {
                List<V_WH_TREE> entities = items.OrderBy(x => x.CODE).ToList().FindAll(t => t.PARENT_ID.IsNullOrEmpty());
                foreach (var item in entities)
                {
                    OrgItemH houseItem = new();
                    houseItem.id = item.ID;
                    houseItem.houseCode = item.CODE;
                    houseItem.houseName = item.AUTH_ORG + ":" + item.CODE + "(" + item.NAME + ")";
                    houseItem.PARENT_HOUSE = item.PARENT_ID;
                    houseItem.createTime = DateTime.Now.ToString("yyyy-MM-dd mm:dd:ss");
                    houseItem.houseType = item.TYPE;
                    //hosueItem.remark = item.REMARK;
                    //hosueItem.orderNo = item.SEQ_NO;
                    houseItem.children = new();
                    if (item.TYPE != "Shelf") GetOrgItemOrg(items, item.ID, ref houseItem);
                    data.Add(houseItem);
                }
            }
            //有查询条件的时候
            else
            {
 
            }
 
            //转换Json时,去除值为null的属性
            var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            var json = JsonConvert.SerializeObject(data, Formatting.Indented, jsonSetting);
            return json;
        }
 
        /// <summary>
        /// 递归返回组织机构
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parent"></param>
        /// <param name="pOrgItem"></param>
        private static void GetOrgItemOrg(List<V_WH_TREE> data, string parent, ref OrgItemH pOrgItem)
        {
            List<V_WH_TREE> entities = data.OrderBy(x => x.CODE).ToList().FindAll(t => t.PARENT_ID == parent).Distinct().ToList();
            if (entities.Count > 0)
            {
                foreach (var item in entities)
                {
                    OrgItemH houseItem = new();
                    houseItem.id = item.ID;
                    houseItem.houseCode = item.CODE;
                    houseItem.houseName = item.AUTH_ORG + ":" + item.CODE + "(" + item.NAME + ")";
                    houseItem.PARENT_HOUSE = item.PARENT_ID;
                    houseItem.createTime = DateTime.Now.ToString("yyyy-MM-dd mm:dd:ss");
                    houseItem.houseType = item.TYPE;
                    //houseItem.remark = item.REMARK;
                    //houseItem.orderNo = item.SEQ_NO;
                    houseItem.children = new();
                    if (item.TYPE != "Shelf") GetOrgItemOrg(data, item.ID, ref houseItem);
                    pOrgItem.children.Add(houseItem);
                }
            }
        }
 
        /// <summary>
        /// 根据仓库代码和类型删除仓库机构列表
        /// </summary>
        /// <param name="wareHouse"></param>
        /// <returns></returns>
        public ApiAction DelWareHouseModel(DelWareHouseModelEntity wareHouse)
        {
            var action = new ApiAction();
            List<string> locaIds = new();
            List<string> shelfIds = new();
            List<string> regIds = new();
            List<string> wareIds = new();
            var db = Biz.Db;
            List<V_WH_UNIT> uNITs = new List<V_WH_UNIT>();
            if (wareHouse.Type == "Warehouse")
            {
                var res = db.Queryable<WMS_ITEM>().Where(q => q.STATUS > 2 && q.WH_ID == wareHouse.ID).ToList();
                if (res.IsNullOrEmpty())
                {
                    var unit = db.Queryable<V_WH_UNIT>().Where(q => q.WH_ID == wareHouse.ID).ToList();
                    locaIds.AddRange(unit.Where(q => !q.LOCATION_ID.IsNullOrEmpty()).Select(s => s.LOCATION_ID).Distinct());
                    shelfIds.AddRange(unit.Where(q => !q.SHELF_ID.IsNullOrEmpty()).Select(s => s.SHELF_ID).Distinct());
                    regIds.AddRange(unit.Where(q => !q.REGION_ID.IsNullOrEmpty()).Select(s => s.REGION_ID).Distinct());
                    wareIds.AddRange(unit.Where(q => !q.WH_ID.IsNullOrEmpty()).Select(s => s.WH_ID).Distinct());
                }
                else
                {
                    action.IsSuccessed = false;
                    action.Message = "仓库里面有东西,不能删除!";
                }
            }
            else if (wareHouse.Type == "Region")
            {
                var res = db.Queryable<WMS_ITEM>().Where(q => q.STATUS > 2 && q.REGION_ID == wareHouse.ID).ToList();
                if (res.IsNullOrEmpty())
                {
                    var unit = db.Queryable<V_WH_UNIT>().Where(q => q.REGION_ID == wareHouse.ID).ToList();
                    locaIds.AddRange(unit.Where(q => !q.LOCATION_ID.IsNullOrEmpty()).Select(s => s.LOCATION_ID).Distinct());
                    shelfIds.AddRange(unit.Where(q => !q.SHELF_ID.IsNullOrEmpty()).Select(s => s.SHELF_ID).Distinct());
                    regIds.AddRange(unit.Where(q => !q.REGION_ID.IsNullOrEmpty()).Select(s => s.REGION_ID).Distinct());
                }
                else
                {
                    action.IsSuccessed = false;
                    action.Message = "储区里面有东西,不能删除!";
                }
            }
            else if (wareHouse.Type == "Shelf")
            {
                var res = db.Queryable<WMS_ITEM>().Where(q => q.STATUS > 2 && q.SHELF_ID == wareHouse.ID).ToList();
                if (res.IsNullOrEmpty())
                {
                    var unit = db.Queryable<V_WH_UNIT>().Where(q => q.SHELF_ID == wareHouse.ID).ToList();
                    locaIds.AddRange(unit.Where(q => !q.LOCATION_ID.IsNullOrEmpty()).Select(s => s.LOCATION_ID).Distinct());
                    shelfIds.AddRange(unit.Where(q => !q.SHELF_ID.IsNullOrEmpty()).Select(s => s.SHELF_ID).Distinct());
                }
                else
                {
                    action.IsSuccessed = false;
                    action.Message = "货架里面有东西,不能删除!";
                }
            }
            else
            {
 
            }
            //删储位
            if (locaIds.Any())
            {
                db.Deleteable<Model.WMS_LOCATION>().Where(it => locaIds.Contains(it.ID)).ExecuteCommand();
            }
            //删货架
            if (shelfIds.Any())
            {
                db.Deleteable<Model.WMS_SHELF>().Where(it => shelfIds.Contains(it.ID)).ExecuteCommand();
            }
            //删储区
            if (regIds.Any())
            {
                db.Deleteable<Model.WMS_REGION>().Where(it => regIds.Contains(it.ID)).ExecuteCommand();
            }
            //删仓库
            if (wareIds.Any())
            {
                db.Deleteable<Model.WMS_WAREHOUSE>().Where(it => wareIds.Contains(it.ID)).ExecuteCommand();
            }
            return action;
        }
        /// <summary>
        /// 生成储位列表
        /// </summary>
        /// <param name="locations"></param>
        /// <returns></returns>
        public ApiAction AddLocationModel(List<AddLocationModelEntity> locations)
        {
            var action = new ApiAction();
            List<WMS_LOCATION> Locations = new List<WMS_LOCATION>();
            foreach (var item in locations)
            {
                WMS_LOCATION location = new WMS_LOCATION()
                {
                    LOCATION_CODE = item.LOCATION_CODE,
                    LOCATION_NAME = item.LOCATION_CODE,
                    IS_ACTIVE = "Y",
                    AUTH_ORG = item.AUTH_ORG,
                    ORG_CODE = item.ORG_CODE,
                    SHELF_ID = item.SHELF_ID,
                    IS_MIX = item.ISMIX,
                    IS_SINGLE = item.ISMIX == "N" ? "Y" : "N",
                    FLOOR_NO = item.FLOOR_NO,
                    SEQ_NO = item.SEQ_NO,
                    LEDID = item.LEDID
                };
                Locations.Add(location);
            }
            var db = Biz.Db;
            var dbTran = db.UseTran(() =>
            {
                db.Deleteable<WMS_LOCATION>().Where(q => q.SHELF_ID == locations[0].SHELF_ID).ExecuteCommand();
                db.Insertable<WMS_LOCATION>(Locations).ExecuteCommand();
            });
            if (!dbTran.IsSuccess)
            {
                action.GetResponse().CatchExceptionWithLog(dbTran.ErrorException, $"新增储位异常");
            }
 
            action.LocaleMsg = Biz.L("WMS.WareHouse.VerifyException", Locations[0].ORG_CODE);
            return action;
        }
        /// <summary>
        /// 查询货架是否有东西
        /// </summary>
        /// <param name="shelf"></param>
        /// <returns></returns>
        public ApiAction CheckLocation(DelWareHouseModelEntity shelf)
        {
            var action = new ApiAction();
            var db = Biz.Db;
            if (shelf.Type == "shelf")
            {
                var res = db.Queryable<WMS_ITEM>().Where(q => q.STATUS > 2 && q.SHELF_ID == shelf.ID).ToList();
                if (res.IsNullOrEmpty())
                {
                    action.IsSuccessed = true;
                }
                else
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.WareHouse.检查储位初始化", shelf.ID);
                }
            }
            return action;
        }
        /// <summary>
        /// 修改仓库据点,同下级全部修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> GetUpdateOrg(string id)
        {
            var db = Biz.Db;
            List<WMS_SHELF> shelfs = new();
            List<WMS_LOCATION> locations = new();
            var warehouse = db.Queryable<WMS_WAREHOUSE>().Where(q => q.ID == id).First();
            if (warehouse != null)
            {
                var unit = db.Queryable<V_WH_UNIT>().Where(q => q.WH_ID == id).ToList();
                var regionids = unit.Select(q => q.REGION_ID).Distinct().ToList();
                var shelfids = unit.Select(q => q.SHELF_ID).Distinct().ToList();
                //var locationids=unit.Select(q=>q.LOCATION_ID).Distinct().ToList();
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable<WMS_REGION>().SetColumns(q => q.AUTH_ORG == warehouse.AUTH_ORG).Where(q => q.WH_ID == warehouse.ID).ExecuteCommand();
 
                    db.Updateable<WMS_SHELF>().SetColumns(q => q.AUTH_ORG == warehouse.AUTH_ORG)
                    .Where(q => regionids.Any(o => o == q.REGION_ID)).ExecuteCommand();
                    db.Updateable<WMS_LOCATION>().SetColumns(q => q.AUTH_ORG == warehouse.AUTH_ORG)
                    .Where(q => SqlFunc.Subqueryable<WMS_SHELF>().Where(s => regionids.Any(o => o == s.REGION_ID) && q.SHELF_ID == s.ID).Any()).ExecuteCommand();
                });
            }
            return "";
        }
 
        /// <summary>
        /// 根据id获取Code
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> GetWmsCode(string id)
        {
            var db = Biz.Db;
            var ID = "";
            var warehouse = db.Queryable<WMS_WAREHOUSE>().Where(q => q.WH_CODE == id).First();
            if (warehouse != null)
            {
                ID = warehouse.ID;
            }
            return ID;
        }
 
    }
}