schangxiang@126.com
2025-05-21 0e42f871905f207658d822fcbe29aeb57b2156af
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.WorkTask; 
using CMS.Plugin.PipeLineLems.Application.Contracts.Services; 
using CMS.Plugin.PipeLineLems.Domain.Shared; 
using CMS.Plugin.PipeLineLems.Domain.WorkTask; 
using CmsQueryExtensions; 
using CmsQueryExtensions.Entitys; 
using CmsQueryExtensions.Extension; 
using System.Linq.Expressions; 
using Volo.Abp; 
using Volo.Abp.Application.Dtos; 
using Volo.Abp.Data; 
using Volo.Abp.ObjectExtending; 
 
namespace CMS.Plugin.PipeLineLems.Application.Implements; 
 
/// <summary> 
/// 作业任务表应用服务 
/// </summary> 
public class WorkTaskAppService : CMSPluginAppService, IWorkTaskAppService 
    private readonly IWorkTaskRepository _workTaskRepository; 
 
    /// <summary> 
    /// Initializes a new instance of the <see cref="WorkTaskAppService"/> class. 
    /// </summary> 
    /// <param name="WorkTaskRepository">The task job repository.</param> 
    public WorkTaskAppService(IWorkTaskRepository workTaskRepository) 
    { 
        _workTaskRepository = workTaskRepository; 
    } 
 
    /// <summary> 
    /// 获取指定作业任务表 
    /// </summary> 
    /// <param name="id"></param> 
    /// <returns></returns> 
    public virtual async Task<WorkTaskDto> GetAsync(Guid id) 
    { 
        return ObjectMapper.Map<WorkTask, WorkTaskDto>(await _workTaskRepository.GetAsync(id)); 
    } 
 
    /// <summary> 
    /// 分页获取作业任务表 
    /// </summary> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    public virtual async Task<PagedResultDto<WorkTaskDto>> GetListAsync(GetWorkTaskInput input) 
    { 
        Check.NotNull(input, nameof(input)); 
 
        if (input.Sorting.IsNullOrWhiteSpace()) 
        { 
            input.Sorting = nameof(WorkTask.Sort); 
        } 
 
        #region 动态构造查询条件  
 
        //动态构造查询条件  
        var whereConditions = DynamicGetQueryParams(input); 
 
        #endregion 
 
        var count = await _workTaskRepository.GetCountAsync(whereConditions); 
        var list = await _workTaskRepository.GetListAsync(whereConditions, input.Sorting, input.MaxResultCount, input.SkipCount); 
 
        return new PagedResultDto<WorkTaskDto>(count, ObjectMapper.Map<List<WorkTask>, List<WorkTaskDto>>(list)); 
    } 
 
    /// <summary>  
    /// 动态构造查询条件  
    /// </summary>  
    /// <param name="input">输入参数</param>  
    /// <returns></returns>  
    private FunReturnResultModel<Expression<Func<WorkTask, bool>>> DynamicGetQueryParams(GetWorkTaskInput input) 
    { 
        //动态构造查询条件  
        var whereConditions = WhereConditionsExtensions.GetWhereConditions<WorkTask, GetWorkTaskInput>(input); 
        if (!whereConditions.IsSuccess) 
        { 
            throw new Exception("动态构造查询条件失败:" + whereConditions.ErrMsg); 
        } 
 
        //也可再次自定义构建查询条件  
        Expression<Func<WorkTask, bool>> extendExpression = a => a.IsDeleted == false; 
        // 使用 System.Linq.PredicateBuilder 的 And 
        var pres = (System.Linq.Expressions.Expression<Func<WorkTask, bool>>)(whereConditions.data); 
        whereConditions.data = System.Linq.PredicateBuilder.And(pres, extendExpression); 
 
        return whereConditions; 
    } 
 
    /// <summary> 
    /// 新建作业任务表 
    /// </summary> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    /// <exception cref="UserFriendlyException"></exception> 
    public virtual async Task<WorkTaskDto> CreateAsync(WorkTaskCreateDto input) 
    { 
        await CheckCreateOrUpdateDtoAsync(input); 
 
        var exist = await _workTaskRepository.NameExistAsync(input.Son_TaskCode); 
        if (exist) 
        { 
            throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.Son_TaskCode]); 
        } 
 
        var maxSort = await _workTaskRepository.GetMaxSortAsync(); 
        var sort = input.Sort ?? maxSort; 
 
        var insertObj = ObjectMapper.Map<WorkTaskCreateDto, WorkTask>(input); 
        insertObj.Sort = sort; 
        input.MapExtraPropertiesTo(insertObj, MappingPropertyDefinitionChecks.None); 
 
        insertObj.CreatorName = input.CreatorName;//创建人 
        await _workTaskRepository.InsertAsync(insertObj); 
 
        //if (input.Sort.HasValue && insertObj.Sort != maxSort) 
        //{ 
        //    await AdjustSortAsync(insertObj.Id, insertObj.Sort); 
        //} 
 
        return ObjectMapper.Map<WorkTask, WorkTaskDto>(insertObj); 
    } 
 
    /// <summary> 
    /// 更新作业任务表 
    /// </summary> 
    /// <param name="id"></param> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    /// <exception cref="UserFriendlyException"></exception> 
    public virtual async Task<WorkTaskDto> UpdateAsync(Guid id, WorkTaskUpdateDto input) 
    { 
        await CheckCreateOrUpdateDtoAsync(input); 
 
        var updateObj = await _workTaskRepository.GetAsync(id); 
        var exist = await _workTaskRepository.NameExistAsync(input.Son_TaskCode, updateObj.Id); 
        if (exist) 
        { 
            throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.Son_TaskCode]); 
        } 
 
        updateObj.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp); 
        input.MapExtraPropertiesTo(updateObj, MappingPropertyDefinitionChecks.None); 
 
        // 批量赋值所有可映射字段(通过匿名对象) 
        updateObj = ObjectMapper.Map(input, updateObj); // 等效于手动赋值所有匹配字段 
 
        updateObj.LastModifierName = input.LastModifierName;//修改人 
 
        await _workTaskRepository.UpdateAsync(updateObj); 
 
        return ObjectMapper.Map<WorkTask, WorkTaskDto>(updateObj); 
    } 
 
    /// <summary> 
    /// 克隆作业任务表 
    /// </summary> 
    /// <param name="ids"></param> 
    /// <returns></returns> 
    public async Task<List<WorkTaskDto>> CloneAsync(IEnumerable<Guid> ids, MyCurrentUser myCurrentUser) 
    { 
        //var workTasks = new List<WorkTask>(); 
        //if (ids != null) 
        //{ 
        //    var sort = await _workTaskRepository.GetMaxSortAsync(); 
        //    foreach (var id in ids) 
        //    { 
        //        var WorkTask = await _workTaskRepository.FindAsync(id); 
        //        if (WorkTask != null) 
        //        { 
        //            var name = WorkTask.Name + WorkTaskConsts.CloneTag; 
        //            var notExist = false; 
        //            while (!notExist) 
        //            { 
        //                var exist = await _workTaskRepository.NameExistAsync(name); 
        //                if (exist || workTasks.Any(x => x.Name == name)) 
        //                { 
        //                    name += WorkTaskConsts.CloneTag; 
        //                    continue; 
        //                } 
 
        //                notExist = true; 
        //            } 
 
        //            //WorkTask = await _workTaskRepository.InsertAsync(WorkTask.Clone(GuidGenerator.Create(), name, sort++)); 
        //            workTasks.Add(WorkTask); 
        //        } 
        //    } 
        //} 
 
        //return ObjectMapper.Map<List<WorkTask>, List<WorkTaskDto>>(workTasks); 
        return new List<WorkTaskDto>(); 
    } 
 
    /// <summary> 
    /// 删除单个作业任务表 
    /// </summary> 
    /// <param name="id"></param> 
    /// <returns></returns> 
    public virtual Task DeleteAsync(Guid id) 
    { 
        return _workTaskRepository.DeleteAsync(id); 
    } 
 
    /// <summary> 
    /// 删除多个作业任务表 
    /// </summary> 
    /// <param name="ids"></param> 
    /// <returns></returns> 
    public async Task DeleteManyAsync(IEnumerable<Guid> ids, MyCurrentUser myCurrentUser) 
    { 
        foreach (var id in ids) 
        { 
            await DeleteAsync(id); 
        } 
    } 
 
     /// <summary> 
    /// 物理删除作业任务表 
    /// </summary> 
    /// <param name="id">主键ID</param> 
    /// <param name="cancellationToken"></param> 
    /// <returns></returns> 
    public virtual async Task DeletePermanentlyAsync(Guid id, MyCurrentUser myCurrentUser, CancellationToken cancellationToken = default) 
    { 
        _workTaskRepository.DeletePermanentlyAsync(id); 
    } 
 
    /// <summary> 
    /// 批量物理删除作业任务表(直接删除,不软删除) 
    /// </summary> 
    /// <param name="ids">要删除的主键ID列表</param> 
    /// <param name="cancellationToken"></param> 
    /// <returns></returns> 
    public virtual async Task BatchDeletePermanentlyAsync(IEnumerable<Guid> ids, MyCurrentUser myCurrentUser, CancellationToken cancellationToken = default) 
    { 
        _workTaskRepository.BatchDeletePermanentlyAsync(ids); 
    } 
 
    /// <summary> 
    /// 调整排序作业任务表 
    /// </summary> 
    /// <param name="id"></param> 
    /// <param name="sort"></param> 
    /// <returns></returns> 
    public virtual async Task AdjustSortAsync(Guid id, int sort) 
    { 
        var list = await _workTaskRepository.GetListAsync(null, nameof(WorkTask.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 _workTaskRepository.UpdateManyAsync(list); 
    } 
 
    /// <summary> 
    /// 导入作业任务表 
    /// </summary> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    /// <exception cref="UserFriendlyException"></exception> 
    public async Task ImportAsync(WorkTasksImportModel input, MyCurrentUser myCurrentUser) 
    { 
        Check.NotNull(input, nameof(input)); 
 
        var workTaskCreateDtos = new List<(int RowIndex, WorkTaskCreateDto Item)>(); 
        var workTaskUpdateDtos = new List<(int RowIndex, Guid Id, WorkTask Item)>(); 
        var importItems = input.WorkTasks; 
 
        if (importItems != null && importItems.Any()) 
        { 
            #region 导入校验 
 
            // 判断名称是否重复,并输出第几行重复 
            var duplicateWorkTasks = importItems.GroupBy(x => x.Son_TaskCode).Where(x => x.Count() > 1).ToList(); 
            if (duplicateWorkTasks?.Any() == true) 
            { 
                var duplicateWorkTaskMsgs = duplicateWorkTasks.Select(x => $"第 {string.Join(",", x.Select(x => x.RowIndex))} 行:{x.Key}  名称重复"); 
                var errorMsg = $"导入失败!配置, {string.Join(",", duplicateWorkTaskMsgs)},终止导入"; 
                throw new UserFriendlyException(errorMsg); 
            } 
 
            #endregion 
 
            foreach (var impItem in importItems) 
            { 
                if (impItem.Son_TaskCode.IsNullOrWhiteSpace()) 
                { 
                    continue; 
                } 
 
                if (impItem.Son_TaskCode.IsNullOrWhiteSpace()) 
                { 
                    var errorMsg = $"导入失败!配置,第{impItem.RowIndex}行:WorkTask名称不能为空"; 
                    throw new UserFriendlyException(errorMsg); 
                } 
 
                var oldWorkTask = await _workTaskRepository.FindByNameAsync(impItem.Son_TaskCode); 
                if (oldWorkTask != null) 
                { 
                    oldWorkTask = ObjectMapper.Map(impItem, oldWorkTask); // 等效于手动赋值所有匹配字段 
                    workTaskUpdateDtos.Add((impItem.RowIndex, oldWorkTask.Id, oldWorkTask)); 
                } 
                else 
                { 
                    var workTaskCreateDto = new WorkTaskCreateDto { }; 
                    workTaskCreateDto = ObjectMapper.Map(impItem, workTaskCreateDto); // 等效于手动赋值所有匹配字段 
                    workTaskCreateDtos.Add((impItem.RowIndex, workTaskCreateDto)); 
                } 
            } 
        } 
 
        // 新增 
        foreach (var workTaskDto in workTaskCreateDtos) 
        { 
            try 
            { 
                workTaskDto.Item.CreatorName = myCurrentUser.UserAccount;//创建人 
                await CreateAsync(workTaskDto.Item); 
            } 
            catch (Exception e) 
            { 
                var errorMsg = $"导入失败!配置,第{workTaskDto.RowIndex}行:{e.Message},终止导入"; 
                throw new UserFriendlyException(errorMsg); 
            } 
        } 
 
        // 更新 
        foreach (var workTaskDto in workTaskUpdateDtos) 
        { 
            try 
            { 
                workTaskDto.Item.LastModifierName = myCurrentUser.UserAccount;//修改人 
                await _workTaskRepository.UpdateAsync(workTaskDto.Item); 
            } 
            catch (Exception e) 
            { 
                var errorMsg = $"导入失败!配置,第{workTaskDto.RowIndex}行:{e.Message},终止导入"; 
                throw new UserFriendlyException(errorMsg); 
            } 
        } 
    } 
 
    /// <summary> 
    /// 导出作业任务表 
    /// </summary> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    public async Task<(Dictionary<string, object> Sheets, string FileName)> ExportAsync(GetWorkTaskInput input) 
    { 
        Check.NotNull(input, nameof(input)); 
 
        if (input.Sorting.IsNullOrWhiteSpace()) 
        { 
            input.Sorting = nameof(WorkTask.Sort); 
        } 
 
        #region 动态构造查询条件  
 
        //动态构造查询条件  
        var whereConditions = DynamicGetQueryParams(input); 
 
        #endregion 
 
 
        var list = await _workTaskRepository.GetListAsync(whereConditions, input.Sorting, input.MaxResultCount, input.SkipCount, includeDetails: true); 
        var result = ObjectMapper.Map<List<WorkTask>, List<WorkTaskDto>>(list); 
 
        var sheets = new Dictionary<string, object> 
        { 
            ["配置"] = ExportHelper.ConvertListToExportData(result), 
        }; 
 
        var fileName = "作业任务"; 
        return (sheets, fileName); 
    } 
 
    /// <summary> 
    /// 校验作业任务表,当新建或更新时 
    /// </summary> 
    /// <param name="input"></param> 
    /// <returns></returns> 
    protected Task CheckCreateOrUpdateDtoAsync(WorkTaskCreateOrUpdateDtoBase input) 
    { 
        Check.NotNull(input, nameof(input)); 
                Check.NotNullOrWhiteSpace(input.Son_TaskCode, "子任务编码", 256);
        Check.NotNullOrWhiteSpace(input.TaskCode, "任务编码", 256);
        Check.NotNull(input.WorkPlanStatus, "计划状态");
        Check.NotNull(input.CallMaterialStatus, "叫料状态");
        Check.NotNull(input.Length, "长度(mm)");
        Check.NotNull(input.MarkingPosition, "打码位置");
        Check.NotNull(input.CuttingPosition, "切割位置");
        Check.NotNull(input.Quantity, "管段数量");
        Check.NotNull(input.FlangeThickness, "法兰厚度(mm)");
        Check.NotNull(input.FlangeInnerDiameter, "法兰直径(mm)");
        Check.NotNull(input.PipeDiameter, "套管长度(mm)");
        Check.NotNull(input.PipeWallThickness, "套管直径(mm)");
        Check.NotNull(input.OuterDiameter, "外径(mm)");
        Check.NotNull(input.Thickness, "壁厚(mm)");
 
        return Task.CompletedTask; 
    } 
 
     /// <summary> 
    /// 根据条件获取作业任务表列表 
    /// </summary> 
    /// <param name="whereConditions"></param> 
    /// <param name="cancellationToken"></param> 
    /// <returns></returns> 
    public async Task<List<WorkTask>> GetListByFilterAsync(Expression<Func<WorkTask, bool>> whereConditions, CancellationToken cancellationToken = default) 
    { 
        return await _workTaskRepository.GetListByFilterAsync(whereConditions); 
    } 
 
    /// <summary> 
    ///  根据条件获取单个作业任务表 
    /// </summary> 
    /// <param name="whereConditions"></param> 
    /// <param name="isMultipleThrowException">是否查询出多条就报错</param> 
    /// <param name="cancellationToken"></param> 
    /// <returns></returns> 
    /// <exception cref="UserFriendlyException"></exception> 
    public async Task<WorkTask> GetSingleByFilterAsync(Expression<Func<WorkTask, bool>> whereConditions, bool is​MultipleThrowException = false, CancellationToken cancellationToken = default) 
    { 
        return await _workTaskRepository.GetSingleByFilterAsync(whereConditions, is​MultipleThrowException); 
    } 
 
     /// <summary>  
    /// 根据条件获取作业任务表列表 
    /// </summary>  
    /// <param name="input"></param>  
    /// <returns></returns>  
    public virtual async Task<List<WorkTaskDto>> FindListByFilterAsync(GetWorkTaskInput input, CancellationToken cancellationToken = default) 
    { 
        Check.NotNull(input, nameof(input)); 
 
        if (input.Sorting.IsNullOrWhiteSpace()) 
        { 
            input.Sorting = nameof(WorkTask.Sort); 
        } 
 
        #region 动态构造查询条件   
 
        //动态构造查询条件   
        var whereConditions = DynamicGetQueryParams(input); 
 
        #endregion 
 
        var list = await _workTaskRepository.GetListByFilterAsync(whereConditions?.data); 
 
        return new List<WorkTaskDto>(ObjectMapper.Map<List<WorkTask>, List<WorkTaskDto>>(list)); 
    } 
    /// <summary>  
    /// 根据条件获取单个作业任务表 
    /// </summary>  
    /// <param name="input"></param>  
    /// <returns></returns>  
    public virtual async Task<WorkTaskDto> FindSingleByFilterAsync(GetWorkTaskInput input, CancellationToken cancellationToken = default) 
    { 
        Check.NotNull(input, nameof(input)); 
 
        if (input.Sorting.IsNullOrWhiteSpace()) 
        { 
            input.Sorting = nameof(WorkTask.Sort); 
        } 
 
        #region 动态构造查询条件   
 
        //动态构造查询条件   
        var whereConditions = DynamicGetQueryParams(input); 
 
        #endregion 
 
        var dataObj = await _workTaskRepository.GetSingleByFilterAsync(whereConditions?.data); 
 
        return (ObjectMapper.Map<WorkTask, WorkTaskDto>(dataObj)); 
    }