zs
2025-05-15 edba4ede85a3d82a7f0a0a7dccddbc8281862888
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
using CMS.Plugin.HIAWms.Application.Contracts.Dtos.WmsPlace;
using CMS.Plugin.HIAWms.Application.Contracts.Services;
using CMS.Plugin.HIAWms.Domain.Shared;
using CMS.Plugin.HIAWms.Domain.Shared.WmsPlaces;
using CMS.Plugin.HIAWms.Domain.WmsPlaces;
using CmsQueryExtensions;
using System.Text.RegularExpressions;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
 
namespace CMS.Plugin.HIAWms.Application.Implements;
 
/// <inheritdoc />
public class WmsPlaceAppService : CMSPluginAppService, IWmsPlaceAppService
{
    private readonly IWmsPlaceRepository _wmsplaceRepository;
 
    /// <summary>
    /// Initializes a new instance of the <see cref="WmsPlaceAppService"/> class.
    /// </summary>
    /// <param name="wmsplaceRepository">The task job repository.</param>
    public WmsPlaceAppService(IWmsPlaceRepository wmsplaceRepository)
    {
        _wmsplaceRepository = wmsplaceRepository;
    }
 
    /// <inheritdoc />
    public virtual async Task<WmsPlaceDto> GetAsync(Guid id)
    {
        return ObjectMapper.Map<WmsPlace, WmsPlaceDto>(await _wmsplaceRepository.GetAsync(id));
    }
 
    /// <inheritdoc />
    public virtual async Task<PagedResultDto<WmsPlaceDto>> GetListAsync(GetWmsMaterialInfoInput input)
    {
        Check.NotNull(input, nameof(input));
 
        if (input.Sorting.IsNullOrWhiteSpace())
        {
            input.Sorting = nameof(WmsPlace.Sort);
        }
 
        var specification = new WmsPlaceSpecification(input.Name);
        var place = ObjectMapper.Map<GetWmsMaterialInfoInput, WmsPlace>(input);
        var count = await _wmsplaceRepository.GetCountAsync(place,input.Filter, specification);
        var list = await _wmsplaceRepository.GetListAsync(place, input.Sorting, input.MaxResultCount,  input.SkipCount, input.Filter, specification);
 
        return new PagedResultDto<WmsPlaceDto>(count, ObjectMapper.Map<List<WmsPlace>, List<WmsPlaceDto>>(list));
    }
 
    /// <inheritdoc />
    public virtual async Task<WmsPlaceDto> CreateAsync(WmsPlaceCreateDto input)
    {
        await CheckCreateOrUpdateDtoAsync(input);
 
        var exist = await _wmsplaceRepository.NameExistAsync(input.PlaceNo);
        if (exist)
        {
            throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.PlaceNo]);
        }
 
        var maxSort = await _wmsplaceRepository.GetMaxSortAsync();
        var sort = input.Sort ?? maxSort;
        var wmsplace = ObjectMapper.Map<WmsPlaceCreateDto,WmsPlace>(input);
        input.MapExtraPropertiesTo(wmsplace, MappingPropertyDefinitionChecks.None);
        wmsplace.Sort = sort;
        await _wmsplaceRepository.InsertAsync(wmsplace);
 
        if (input.Sort.HasValue && wmsplace.Sort != maxSort)
        {
            await AdjustSortAsync(wmsplace.Id, wmsplace.Sort);
        }
 
        return ObjectMapper.Map<WmsPlace, WmsPlaceDto>(wmsplace);
    }
 
    /// <inheritdoc />
    public virtual async Task<WmsPlaceDto> UpdateAsync(Guid id, WmsPlaceUpdateDto input)
    {
        await CheckCreateOrUpdateDtoAsync(input);
 
        var wmsplace = await _wmsplaceRepository.GetAsync(id);
        var exist = await _wmsplaceRepository.NameExistAsync(input.PlaceNo, wmsplace.Id);
        if (exist)
        {
            throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.PlaceNo]);
        }
 
        wmsplace.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);
        input.MapExtraPropertiesTo(wmsplace, MappingPropertyDefinitionChecks.None);
 
        wmsplace.StorageTypeNo = input.StorageTypeNo;
        wmsplace.PlaceStatus = input.PlaceStatus;
        wmsplace.AreaCode = input.AreaCode;
        wmsplace.Aisle = input.Aisle;
        wmsplace.RowNo = input.RowNo;
        wmsplace.ColumnNo = input.ColumnNo;
        wmsplace.LayerNo = input.LayerNo;
        wmsplace.Islock = input.Islock;
        wmsplace.EmptyContainer = input.EmptyContainer;
        wmsplace.RedundantField1 = input.RedundantField1;
        wmsplace.RedundantField2 = input.RedundantField2;
        wmsplace.RedundantField3 = input.RedundantField3;
        wmsplace.Remark = input.Remark;
        wmsplace.IsDisabled = input.IsDisabled;
 
        await _wmsplaceRepository.UpdateAsync(wmsplace);
 
        return ObjectMapper.Map<WmsPlace, WmsPlaceDto>(wmsplace);
    }
 
    /// <inheritdoc />
    public async Task<List<WmsPlaceDto>> CloneAsync(IEnumerable<Guid> ids)
    {
        var wmsplaces = new List<WmsPlace>();
        if (ids != null)
        {
            var sort = await _wmsplaceRepository.GetMaxSortAsync();
            foreach (var id in ids)
            {
                var wmsplace = await _wmsplaceRepository.FindAsync(id);
                if (wmsplace != null)
                {
                    var name = wmsplace.PlaceNo + WmsPlaceConsts.CloneTag;
                    var notExist = false;
                    while (!notExist)
                    {
                        var exist = await _wmsplaceRepository.NameExistAsync(name);
                        if (exist || wmsplaces.Any(x => x.PlaceNo == name))
                        {
                            name += WmsPlaceConsts.CloneTag;
                            continue;
                        }
 
                        notExist = true;
                    }
 
                    //wmsplace = await _wmsplaceRepository.InsertAsync(wmsplace.Clone(GuidGenerator.Create(), name, sort++));
                    wmsplaces.Add(wmsplace);
                }
            }
        }
 
        return ObjectMapper.Map<List<WmsPlace>, List<WmsPlaceDto>>(wmsplaces);
    }
 
    /// <inheritdoc />
    public virtual Task DeleteAsync(Guid id)
    {
        return _wmsplaceRepository.DeleteAsync(id);
    }
 
    /// <inheritdoc />
    public async Task DeleteManyAsync(IEnumerable<Guid> ids)
    {
        foreach (var id in ids)
        {
            await DeleteAsync(id);
        }
    }
 
    /// <inheritdoc />
    public virtual async Task AdjustSortAsync(Guid id, int sort)
    {
        var list = await _wmsplaceRepository.GetListAsync(null,nameof(WmsPlace.Sort));
        if (list != null && list.Any())
        {
            var initSort = 1;
            list.ForEach(x => x.AdjustSort(initSort++));
            var entity = list.FirstOrDefault(x => x.Id == id);
            if (entity != null)
            {
                if (sort == 1)
                {
                    list.Where(x => x.Id != id).ToList()?.ForEach(x => x.AdjustSort(x.Sort + 1));
                }
                else if (entity.Sort > sort)
                {
                    list.Where(x => x.Id != id && x.Sort >= sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort + 1));
                    list.Where(x => x.Id != id && x.Sort < sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort - 1));
                }
                else if (entity.Sort < sort)
                {
                    list.Where(x => x.Id != id && x.Sort > sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort + 1));
                    list.Where(x => x.Id != id && x.Sort <= sort).ToList()?.ForEach(x => x.AdjustSort(x.Sort - 1));
                }
 
                entity.AdjustSort(sort);
            }
        }
 
        await _wmsplaceRepository.UpdateManyAsync(list);
    }
 
    /// <inheritdoc />
    public async Task ImportAsync(WmsPlacesImportModel input)
    {
        Check.NotNull(input, nameof(input));
 
        var wmsplaceCreateDtos = new List<(int RowIndex, WmsPlaceCreateDto Item)>();
        var wmsplaceUpdateDtos = new List<(int RowIndex, Guid Id, WmsPlaceUpdateDto Item)>();
        var wmsplaces = input.WmsPlaces;
 
        if (wmsplaces != null && wmsplaces.Any())
        {
            #region 导入校验
 
            // 判断名称是否重复,并输出第几行重复
            var duplicateWmsPlaces = wmsplaces.GroupBy(x => x.PlaceNo).Where(x => x.Count() > 1).ToList();
            if (duplicateWmsPlaces?.Any() == true)
            {
                var duplicateWmsPlaceMsgs = duplicateWmsPlaces.Select(x => $"第 {string.Join(",", x.Select(x => x.RowIndex))} 行:{x.Key}  名称重复");
                var errorMsg = $"导入失败!配置, {string.Join(",", duplicateWmsPlaceMsgs)},终止导入";
                throw new UserFriendlyException(errorMsg);
            }
 
            #endregion
 
            foreach (var wmsplace in wmsplaces)
            {
                if (wmsplace.PlaceNo.IsNullOrWhiteSpace())
                {
                    continue;
                }
 
                if (wmsplace.PlaceNo.IsNullOrWhiteSpace())
                {
                    var errorMsg = $"导入失败!配置,第{wmsplace.RowIndex}行:WmsPlace名称不能为空";
                    throw new UserFriendlyException(errorMsg);
                }
 
                var oldWmsPlace = await _wmsplaceRepository.FindByNameAsync(wmsplace.PlaceNo);
                if (oldWmsPlace != null)
                {
                    var wmsplaceUpdateDto = new WmsPlaceUpdateDto
                    {
                        PlaceNo = wmsplace.PlaceNo,
                        PlaceStatus = wmsplace.PlaceStatus,
                        Remark = wmsplace.Remark,
                    };
 
                    wmsplaceUpdateDtos.Add((wmsplace.RowIndex, oldWmsPlace.Id, wmsplaceUpdateDto));
                }
                else
                {
                    var wmsplaceCreateDto = new WmsPlaceCreateDto
                    {
                        PlaceNo = wmsplace.PlaceNo,
                        PlaceStatus = wmsplace.PlaceStatus,
                        Remark = wmsplace.Remark,
                    };
 
                    wmsplaceCreateDtos.Add((wmsplace.RowIndex, wmsplaceCreateDto));
                }
            }
        }
 
        // 新增
        foreach (var wmsplaceDto in wmsplaceCreateDtos)
        {
            try
            {
                await CreateAsync(wmsplaceDto.Item);
            }
            catch (Exception e)
            {
                var errorMsg = $"导入失败!配置,第{wmsplaceDto.RowIndex}行:{e.Message},终止导入";
                throw new UserFriendlyException(errorMsg);
            }
        }
 
        // 更新
        foreach (var wmsplaceDto in wmsplaceUpdateDtos)
        {
            try
            {
                await UpdateAsync(wmsplaceDto.Id, wmsplaceDto.Item);
            }
            catch (Exception e)
            {
                var errorMsg = $"导入失败!配置,第{wmsplaceDto.RowIndex}行:{e.Message},终止导入";
                throw new UserFriendlyException(errorMsg);
            }
        }
    }
 
    /// <inheritdoc />
    public async Task<(Dictionary<string, object> Sheets, string FileName)> ExportAsync(GetWmsMaterialInfoInput input)
    {
        Check.NotNull(input, nameof(input));
 
        if (input.Sorting.IsNullOrWhiteSpace())
        {
            input.Sorting = nameof(WmsPlace.Sort);
        }
 
        var specification = new WmsPlaceSpecification(input.Name);
        var place = ObjectMapper.Map<GetWmsMaterialInfoInput, WmsPlace>(input);
        var list = await _wmsplaceRepository.GetListAsync(place, input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, specification, includeDetails: true);
        var result = ObjectMapper.Map<List<WmsPlace>, List<WmsPlaceDto>>(list);
 
        var sheets = new Dictionary<string, object>
        {
            ["配置"] = ExportHelper.ConvertListToExportData(result),
        };
 
        var fileName = result.Count > 1 ? "WmsPlace列表" : result.Count == 1 ? result.First()?.PlaceNo : "WmsPlace模版";
        return (sheets, fileName);
    }
 
    /// <summary>
    /// Checks the create or update dto asynchronous.
    /// </summary>
    /// <param name="input">The input.</param>
    protected Task CheckCreateOrUpdateDtoAsync(WmsPlaceCreateOrUpdateDtoBase input)
    {
        Check.NotNull(input, nameof(input));
        Check.NotNullOrWhiteSpace(input.PlaceNo, "编号", WmsPlaceConsts.MaxCodeLength);
        string pattern = @"^\d+-\d+$";
        if (input.StorageTypeNo == Domain.Shared.Enums.PlaceTypeEnum.YUANLIAOKUWEI && !Regex.IsMatch(input.PlaceNo, pattern))
        {
            throw new UserFriendlyException("库位编号格式错误");
        }
        Check.Length(input.Remark, "备注", WmsPlaceConsts.MaxRemarkLength);
        return Task.CompletedTask;
    }
}