schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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
401
402
using Admin.NET.Core.Service;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Admin.NET.Core;
using Furion.DatabaseAccessor;
using DocumentFormat.OpenXml.Vml.Spreadsheet;
using AngleSharp.Dom;
 
namespace Admin.NET.Application;
/// <summary>
/// 库区信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.WmsBaseGroupName, Order = 100)]
public class WmsAreaService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<WmsBaseArea> _rep;
    private readonly SqlSugarRepository<WmsBaseWarehouse> _repWmsWarehouse;
    private readonly SqlSugarRepository<WmsBasePlace> _repWmsPlace;
    private readonly WmsPlaceService _wmsPlaceService;
    private readonly WmsContainerService _wmsContainerService;
    private readonly SqlSugarRepository<WmsBaseContainerType> _wmsContainerTypeRep;
    private readonly SqlSugarRepository<WmsContainerPlace> _wmsContainerPlaceRep;
    private readonly SqlSugarRepository<WmsBaseContainer> _wmsContainerRep;
 
    public WmsAreaService(
        SqlSugarRepository<WmsBaseContainer> wmsContainerRep,
        SqlSugarRepository<WmsBaseArea> rep
        , SqlSugarRepository<WmsBaseWarehouse> wmsWarehouseRep,
        SqlSugarRepository<WmsBasePlace> repWmsPlace,
        WmsPlaceService wmsPlaceService,
        WmsContainerService wmsContainerService,
        SqlSugarRepository<WmsBaseContainerType> wmsContainerTypeRep,
        SqlSugarRepository<WmsContainerPlace> wmsContainerPlaceRep)
    {
        _wmsContainerRep = wmsContainerRep;
        _rep = rep;
        _repWmsWarehouse = wmsWarehouseRep;
        _repWmsPlace = repWmsPlace;
        _wmsPlaceService = wmsPlaceService;
        _wmsContainerService = wmsContainerService;
        _wmsContainerTypeRep = wmsContainerTypeRep;
        _wmsContainerPlaceRep = wmsContainerPlaceRep;
    }
 
    /// <summary>
    /// 分页查询库区信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<WmsAreaOutput>> Page(WmsAreaInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.AreaCode.Contains(input.SearchKey.Trim())
                || u.AreaName.Contains(input.SearchKey.Trim())
                || u.ErpCode.Contains(input.SearchKey.Trim())
            )
 
            .WhereIF(input.AreaType != null, u => u.AreaType == input.AreaType)
            .WhereIF(input.IsVirtually != null, u => u.IsVirtually == input.IsVirtually)
            .WhereIF(input.IsDisabled != null, u => u.IsDisabled == input.IsDisabled) //ly-0612下拉数据用
            .WhereIF(!string.IsNullOrWhiteSpace(input.AreaCode), u => u.AreaCode.Contains(input.AreaCode.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.AreaName), u => u.AreaName.Contains(input.AreaName.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ErpCode), u => u.ErpCode.Contains(input.ErpCode.Trim()))
            .WhereIF(input.WarehouseId > 0, u => u.WarehouseId == input.WarehouseId)
            .Select<WmsAreaOutput>();
        return await query.OrderBuilder(input, "", "AreaCode").ToPagedListAsync(input.Page, input.PageSize);
    }
 
    /// <summary>
    /// 增加库区信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [UnitOfWork]
    public async Task<long> Add(AddWmsAreaInput input)
    {
       
        var entity = input.Adapt<WmsBaseArea>();
        if (entity.IsVirtually == null)
        {
            entity.IsVirtually = false;
        }
        //验重
        await CheckExisit(entity);
        //ly-所属仓库
        var Info = await _repWmsWarehouse.GetFirstAsync(u => u.Id == input.WarehouseId);
        entity.WarehouseName = Info.Name;
        entity.WarehouseCode = Info.Code;
        if (Info == null)
        {
            throw Oops.Oh(errorMessage: @$"所属仓库不存在!");
        }
        if (Info.IsDisabled == true)
        {
            throw Oops.Oh($"不能使用已禁用的仓库");
        }
 
        await _rep.InsertAsync(entity);
 
 
        //await CommonAddZT(entity);
 
        WmsContainerPlace willAddWmsContainerPlace = new WmsContainerPlace();
 
        long placeId = 0;
        var exist_PlaceObj = await _repWmsPlace.GetFirstAsync(u => u.PlaceCode == ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode);
        if (exist_PlaceObj == null)
        {
            // 添加在途库区库位
            AddWmsPlaceInput addWmsPlaceInput = new AddWmsPlaceInput
            {
                PlaceCode = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode,
                PlaceName = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaName,
                AreaId = entity.Id,
                AreaCode = entity.AreaCode,
                AreaName = entity.AreaName,
                PlaceType = PlaceTypeEnum.在途库位.ToInt(),
                PlaceStatus = PlaceStatusEnum.正常.ToInt(),
                IsDisabled = false,
                IsDelete = false,
            };
            placeId = await _wmsPlaceService.Add(addWmsPlaceInput);
 
            willAddWmsContainerPlace.PlaceId = placeId;
            willAddWmsContainerPlace.PlaceCode = addWmsPlaceInput.PlaceCode;
            willAddWmsContainerPlace.PlaceName = addWmsPlaceInput.PlaceName;
        }
        else
        {
            placeId = exist_PlaceObj.Id;
 
            willAddWmsContainerPlace.PlaceId = placeId;
            willAddWmsContainerPlace.PlaceCode = exist_PlaceObj.PlaceCode;
            willAddWmsContainerPlace.PlaceName = exist_PlaceObj.PlaceName;
        }
 
 
 
        // 查找虚拟容器容器类型
        var containerType = await _wmsContainerTypeRep
                .GetFirstAsync(n => n.TypeCode == OperationsContainerTypeEnum.VIR.ToString() && n.IsDelete == false);
 
        if (containerType == null) throw Oops.Oh($"{OperationsContainerTypeEnum.VIR.ToString()}虚拟容器类型不存在!");
 
        long containerId = 0;
        var exist_ContainerObj = await _wmsContainerRep.GetFirstAsync(u => u.ContainerCode == ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode);
        if (exist_ContainerObj == null)
        {
            // 添加在途库区容器
            var addWmsContainerInput = new AddWmsContainerInput
            {
                IsVirtually = true,
                ContainerCode = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode,
                ContainerName = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaName,
                ContainerTypeId = containerType.Id,
                ContainerTypeName = containerType.TypeName,
                IsDisabled = false,
            };
            //虚拟容器不需要容器类型
            containerId = await _wmsContainerService.Add(addWmsContainerInput);
 
            willAddWmsContainerPlace.ContainerId = containerId;
            willAddWmsContainerPlace.ContainerCode = addWmsContainerInput.ContainerCode;
            willAddWmsContainerPlace.ContainerName = addWmsContainerInput.ContainerName;
        }
        else
        {
            containerId = exist_ContainerObj.Id;
 
            willAddWmsContainerPlace.ContainerId = containerId;
            willAddWmsContainerPlace.ContainerCode = exist_ContainerObj.ContainerCode;
            willAddWmsContainerPlace.ContainerName = exist_ContainerObj.ContainerName;
        }
 
        var exist_ContainerPlaceObj = await _wmsContainerPlaceRep.GetFirstAsync(u => u.PlaceCode == willAddWmsContainerPlace.PlaceCode
        && u.ContainerCode == willAddWmsContainerPlace.ContainerCode);
        if (exist_ContainerPlaceObj == null)
        {
            // 创建容器库位关系
            await _wmsContainerPlaceRep.InsertAsync(willAddWmsContainerPlace);
        }
 
 
 
        return entity.Id;
    }
 
    /// <summary>
    /// 公共处理 在途库位,在途容器
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    private async Task CommonAddZT(WmsBaseArea entity)
    {
        WmsContainerPlace willAddWmsContainerPlace = new WmsContainerPlace();
 
        long placeId = 0;
        var exist_PlaceObj = await _repWmsPlace.GetFirstAsync(u => u.PlaceCode == ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode);
        if (exist_PlaceObj == null)
        {
            // 添加在途库区库位
            AddWmsPlaceInput addWmsPlaceInput = new AddWmsPlaceInput
            {
                PlaceCode = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaCode,
                PlaceName = ApplicationConst.DefaultZTPlaceCode_Pre + entity.AreaName,
                AreaId = entity.Id,
                AreaCode = entity.AreaCode,
                AreaName = entity.AreaName,
                PlaceType = PlaceTypeEnum.在途库位.ToInt(),
                PlaceStatus = PlaceStatusEnum.正常.ToInt(),
                IsDisabled = false,
                IsDelete = false,
            };
            placeId = await _wmsPlaceService.Add(addWmsPlaceInput);
 
            willAddWmsContainerPlace.PlaceId = placeId;
            willAddWmsContainerPlace.PlaceCode = addWmsPlaceInput.PlaceCode;
            willAddWmsContainerPlace.PlaceName = addWmsPlaceInput.PlaceName;
        }
        else
        {
            placeId = exist_PlaceObj.Id;
 
            willAddWmsContainerPlace.PlaceId = placeId;
            willAddWmsContainerPlace.PlaceCode = exist_PlaceObj.PlaceCode;
            willAddWmsContainerPlace.PlaceName = exist_PlaceObj.PlaceName;
        }
 
 
 
        // 查找虚拟容器容器类型
        var containerType = await _wmsContainerTypeRep
                .GetFirstAsync(n => n.TypeCode == OperationsContainerTypeEnum.VIR.ToString() && n.IsDelete == false);
 
        if (containerType == null) throw Oops.Oh($"{OperationsContainerTypeEnum.VIR.ToString()}虚拟容器类型不存在!");
 
        long containerId = 0;
        var exist_ContainerObj = await _wmsContainerRep.GetFirstAsync(u => u.ContainerCode == ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode);
        if (exist_ContainerObj == null)
        {
            // 添加在途库区容器
            var addWmsContainerInput = new AddWmsContainerInput
            {
                IsVirtually = true,
                ContainerCode = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaCode,
                ContainerName = ApplicationConst.DefaultZTContainerCode_Pre + entity.AreaName,
                ContainerTypeId = containerType.Id,
                ContainerTypeName = containerType.TypeName,
                IsDisabled = false,
            };
            containerId = await _wmsContainerService.Add(addWmsContainerInput);
 
            willAddWmsContainerPlace.ContainerId = containerId;
            willAddWmsContainerPlace.ContainerCode = addWmsContainerInput.ContainerCode;
            willAddWmsContainerPlace.ContainerName = addWmsContainerInput.ContainerName;
        }
        else
        {
            containerId = exist_ContainerObj.Id;
 
            willAddWmsContainerPlace.ContainerId = containerId;
            willAddWmsContainerPlace.ContainerCode = exist_ContainerObj.ContainerCode;
            willAddWmsContainerPlace.ContainerName = exist_ContainerObj.ContainerName;
        }
 
        var exist_ContainerPlaceObj = await _wmsContainerPlaceRep.GetFirstAsync(u => u.PlaceCode == willAddWmsContainerPlace.PlaceCode
        && u.ContainerCode == willAddWmsContainerPlace.ContainerCode);
        if (exist_ContainerPlaceObj == null)
        {
            // 创建容器库位关系
            await _wmsContainerPlaceRep.InsertAsync(willAddWmsContainerPlace);
        }
    }
 
    /// <summary>
    /// 删除库区信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteWmsAreaInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        ////ly-存在绑定关系,不可删除
        //var entityWmsWarehouse = await _repWmsWarehouse.GetFirstAsync(u => u.Code == entity.WarehouseCode);
        //if (entityWmsWarehouse != null)
        //{
        //    throw Oops.Oh("存在绑定关系,不可删除");
        //}
 
        var entityWmsPlace = await _repWmsPlace.GetFirstAsync(u => u.AreaId == entity.Id);
        if (entityWmsPlace != null)
        {
            throw Oops.Oh("存在绑定关系,不可删除");
        }
 
        //await _rep.FakeDeleteAsync(entity);   //假删除
        await _rep.DeleteAsync(entity);   //真删除
    }
 
    /// <summary>
    /// 更新库区信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [UnitOfWork]
    public async Task Update(UpdateWmsAreaInput input)
    {
        var entity = input.Adapt<WmsBaseArea>();
        //验重
        await CheckExisit(entity, true);
        //ly-所属仓库
        var Info = await _repWmsWarehouse.GetFirstAsync(u => u.Id == input.WarehouseId);
        entity.WarehouseName = Info.Name;
        entity.WarehouseCode = Info.Code;
        if (Info == null)
        {
            throw Oops.Oh(errorMessage: @$"所属仓库不存在!");
        }
        if (Info.IsDisabled == true)
        {
            throw Oops.Oh($"不能使用已禁用的仓库");
        }
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
 
        await CommonAddZT(entity);
    }
 
    /// <summary>
    /// 获取库区信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<WmsBaseArea> Detail([FromQuery] QueryByIdWmsAreaInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }
 
    /// <summary>
    /// 获取库区信息列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<WmsAreaOutput>> List([FromQuery] WmsAreaInput input)
    {
        return await _rep.AsQueryable().Select<WmsAreaOutput>().ToListAsync();
    }
 
 
 
    #region 私有方法
 
    /// <summary>
    /// 根据联合主键验证数据是否已存在-数据库
    /// </summary>
    /// <param name="input"></param>
    /// <param name="isEdit"></param>
    /// <returns></returns>
    private async Task CheckExisit(WmsBaseArea input, bool isEdit = false)
    {
        //输出数据已存在错误
        ErrorCodeItemMetadataAttribute metadata = ErrorCodeEnum.D1006.GetErrorCodeItemMetadata();
 
        WmsBaseArea _existItem = null;
        if (!isEdit)//新增
        {
            _existItem = await _rep.GetFirstAsync(u => u.AreaCode.Equals(input.AreaCode));
            if (_existItem != null) throw Oops.Oh($"库区编号[{input.AreaCode}]{metadata.ErrorMessage}");
 
            _existItem = await _rep.GetFirstAsync(u => u.AreaName.Equals(input.AreaName));
            if (_existItem != null) throw Oops.Oh($"库区名称[{input.AreaName}]{metadata.ErrorMessage}");
 
        }
        else//编辑 
        {
            //当前编辑数据以外是否存在重复
            _existItem = await _rep.GetFirstAsync(u => u.Id != input.Id && u.AreaCode.Equals(input.AreaCode));
            if (_existItem != null) throw Oops.Oh($"库区编号[{input.AreaCode}]{metadata.ErrorMessage}");
 
            _existItem = await _rep.GetFirstAsync(u => u.Id != input.Id && u.AreaName.Equals(input.AreaName));
            if (_existItem != null) throw Oops.Oh($"库区名称[{input.AreaName}]{metadata.ErrorMessage}");
 
        }
 
    }
 
    #endregion
 
 
}