schangxiang@126.com
2025-05-09 dffde4019898268b44ddd1e9855f8eb3359d44c4
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
using CMS.Plugin.$NameSpacePath$.Application.Contracts.Dtos.$EntityName$;
using CMS.Plugin.$NameSpacePath$.Application.Contracts.Services;
using CMS.Plugin.$NameSpacePath$.Domain.Shared;
using CmsQueryExtensions;
using CMS.Plugin.$NameSpacePath$.Domain.$EntityName$;
using CmsQueryExtensions.Extension;
using System.Linq.Expressions;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using Volo.Abp.ObjectMapping;
 
namespace CMS.Plugin.$NameSpacePath$.Application.Implements;
 
/// <summary>
/// $ChinaComment$Ó¦Ó÷þÎñ
/// </summary>
public class $EntityName$AppService : CMSPluginAppService, I$EntityName$AppService
{
    private readonly I$EntityName$Repository $EntityInstanceName$Repository;
 
    /// <summary>
    /// Initializes a new instance of the <see cref="$EntityName$AppService"/> class.
    /// </summary>
    /// <param name="$EntityName$Repository">The task job repository.</param>
    public $EntityName$AppService(I$EntityName$Repository _$EntityName$Repository)
    {
        $EntityInstanceName$Repository = _$EntityName$Repository;
    }
 
    /// <summary>
    /// »ñȡָ¶¨$ChinaComment$
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual async Task<$EntityName$Dto> GetAsync(Guid id)
    {
        return ObjectMapper.Map<$EntityName$, $EntityName$Dto>(await $EntityInstanceName$Repository.GetAsync(id));
    }
 
    /// <summary>
    /// ·ÖÒ³»ñÈ¡$ChinaComment$
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public virtual async Task<PagedResultDto<$EntityName$Dto>> GetListAsync(Get$EntityName$Input input)
    {
        Check.NotNull(input, nameof(input));
 
        if (input.Sorting.IsNullOrWhiteSpace())
        {
            input.Sorting = nameof($EntityName$.Sort);
        }
 
        #region ¶¯Ì¬¹¹Ôì²éѯÌõ¼þ 
 
        //¶¯Ì¬¹¹Ôì²éѯÌõ¼þ 
        var whereConditions = DynamicGetQueryParams(input);
 
        #endregion
 
        var count = await $EntityInstanceName$Repository.GetCountAsync(whereConditions);
        var list = await $EntityInstanceName$Repository.GetListAsync(whereConditions, input.Sorting, input.MaxResultCount, input.SkipCount);
 
        return new PagedResultDto<$EntityName$Dto>(count, ObjectMapper.Map<List<$EntityName$>, List<$EntityName$Dto>>(list));
    }
 
    /// <summary> 
    /// ¶¯Ì¬¹¹Ôì²éѯÌõ¼þ 
    /// </summary> 
    /// <param name="input">ÊäÈë²ÎÊý</param> 
    /// <returns></returns> 
    private FunReturnResultModel<Expression<Func<$EntityName$, bool>>> DynamicGetQueryParams(Get$EntityName$Input input)
    {
        //¶¯Ì¬¹¹Ôì²éѯÌõ¼þ 
        var whereConditions = WhereConditionsExtensions.GetWhereConditions<$EntityName$, Get$EntityName$Input>(input);
        if (!whereConditions.IsSuccess)
        {
            throw new Exception("¶¯Ì¬¹¹Ôì²éѯÌõ¼þʧ°Ü:" + whereConditions.ErrMsg);
        }
 
        //Ò²¿ÉÔÙ´Î×Ô¶¨Òå¹¹½¨²éѯÌõ¼þ 
        Expression<Func<$EntityName$, bool>> extendExpression = a => a.IsDeleted == false;
        // Ê¹ÓàSystem.Linq.PredicateBuilder µÄ And
        var pres = (System.Linq.Expressions.Expression<Func<$EntityName$, bool>>)(whereConditions.data);
        whereConditions.data = System.Linq.PredicateBuilder.And(pres, extendExpression);
 
        return whereConditions;
    }
 
    /// <summary>
    /// Ð½¨$ChinaComment$
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="UserFriendlyException"></exception>
    public virtual async Task<$EntityName$Dto> CreateAsync($EntityName$CreateDto input)
    {
        await CheckCreateOrUpdateDtoAsync(input);
 
        var exist = await $EntityInstanceName$Repository.NameExistAsync(input.$ValidateRepeatName$);
        if (exist)
        {
            throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.$ValidateRepeatName$]);
        }
 
        var maxSort = await $EntityInstanceName$Repository.GetMaxSortAsync();
        var sort = input.Sort ?? maxSort;
 
        var insertObj = ObjectMapper.Map<$EntityName$CreateDto, $EntityName$>(input);
        insertObj.Sort = sort;
        input.MapExtraPropertiesTo(insertObj, MappingPropertyDefinitionChecks.None);
 
        await $EntityInstanceName$Repository.InsertAsync(insertObj);
 
        //if (input.Sort.HasValue && insertObj.Sort != maxSort)
        //{
        //    await AdjustSortAsync(insertObj.Id, insertObj.Sort);
        //}
 
        return ObjectMapper.Map<$EntityName$, $EntityName$Dto>(insertObj);
    }
 
    /// <summary>
    /// ¸üÐÂ$ChinaComment$
    /// </summary>
    /// <param name="id"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="UserFriendlyException"></exception>
    public virtual async Task<$EntityName$Dto> UpdateAsync(Guid id, $EntityName$UpdateDto input)
    {
        await CheckCreateOrUpdateDtoAsync(input);
 
        var updateObj = await $EntityInstanceName$Repository.GetAsync(id);
        var exist = await $EntityInstanceName$Repository.NameExistAsync(input.$ValidateRepeatName$, updateObj.Id);
        if (exist)
        {
            throw new UserFriendlyException(L[CMSPluginDomainErrorCodes.NameAlreadyExists, input.$ValidateRepeatName$]);
        }
 
        updateObj.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);
        input.MapExtraPropertiesTo(updateObj, MappingPropertyDefinitionChecks.None);
 
        $UpdateAttributes$
 
        await $EntityInstanceName$Repository.UpdateAsync(updateObj);
 
        return ObjectMapper.Map<$EntityName$, $EntityName$Dto>(updateObj);
    }
 
    /// <summary>
    /// ¿Ë¡$ChinaComment$
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task<List<$EntityName$Dto>> CloneAsync(IEnumerable<Guid> ids)
    {
        //var $EntityInstanceName$s = new List<$EntityName$>();
        //if (ids != null)
        //{
        //    var sort = await $EntityInstanceName$Repository.GetMaxSortAsync();
        //    foreach (var id in ids)
        //    {
        //        var $EntityName$ = await $EntityInstanceName$Repository.FindAsync(id);
        //        if ($EntityName$ != null)
        //        {
        //            var name = $EntityName$.Name + $EntityName$Consts.CloneTag;
        //            var notExist = false;
        //            while (!notExist)
        //            {
        //                var exist = await $EntityInstanceName$Repository.NameExistAsync(name);
        //                if (exist || $EntityInstanceName$s.Any(x => x.Name == name))
        //                {
        //                    name += $EntityName$Consts.CloneTag;
        //                    continue;
        //                }
 
        //                notExist = true;
        //            }
 
        //            //$EntityName$ = await $EntityInstanceName$Repository.InsertAsync($EntityName$.Clone(GuidGenerator.Create(), name, sort++));
        //            $EntityInstanceName$s.Add($EntityName$);
        //        }
        //    }
        //}
 
        //return ObjectMapper.Map<List<$EntityName$>, List<$EntityName$Dto>>($EntityInstanceName$s);
        return new List<$EntityName$Dto>();
    }
 
    /// <summary>
    /// É¾³ýµ¥¸ö$ChinaComment$
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual Task DeleteAsync(Guid id)
    {
        return $EntityInstanceName$Repository.DeleteAsync(id);
    }
 
    /// <summary>
    /// É¾³ý¶à¸ö$ChinaComment$
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task DeleteManyAsync(IEnumerable<Guid> ids)
    {
        foreach (var id in ids)
        {
            await DeleteAsync(id);
        }
    }
 
    /// <summary>
    /// µ÷ÕûÅÅÐò$ChinaComment$
    /// </summary>
    /// <param name="id"></param>
    /// <param name="sort"></param>
    /// <returns></returns>
    public virtual async Task AdjustSortAsync(Guid id, int sort)
    {
        var list = await $EntityInstanceName$Repository.GetListAsync(null, nameof($EntityName$.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 $EntityInstanceName$Repository.UpdateManyAsync(list);
    }
 
    /// <summary>
    /// µ¼Èë$ChinaComment$
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="UserFriendlyException"></exception>
    public async Task ImportAsync($EntityName$sImportModel input)
    {
        Check.NotNull(input, nameof(input));
 
        var $EntityInstanceName$CreateDtos = new List<(int RowIndex, $EntityName$CreateDto Item)>();
        var $EntityInstanceName$UpdateDtos = new List<(int RowIndex, Guid Id, $EntityName$UpdateDto Item)>();
        var importItems = input.$EntityName$s;
 
        if (importItems != null && importItems.Any())
        {
            #region µ¼ÈëУÑé
 
            // ÅжÏÃû³ÆÊÇ·ñÖØ¸´£¬²¢Êä³öµÚ¼¸ÐÐÖØ¸´
            var duplicate$EntityName$s = importItems.GroupBy(x => x.$ValidateRepeatName$).Where(x => x.Count() > 1).ToList();
            if (duplicate$EntityName$s?.Any() == true)
            {
                var duplicate$EntityName$Msgs = duplicate$EntityName$s.Select(x => $"µÚ {string.Join(",", x.Select(x => x.RowIndex))} ÐУº{x.Key}  Ãû³ÆÖظ´");
                var errorMsg = $"µ¼Èëʧ°Ü£¡ÅäÖ㬠{string.Join(",", duplicate$EntityName$Msgs)}£¬ÖÕÖ¹µ¼Èë";
                throw new UserFriendlyException(errorMsg);
            }
 
            #endregion
 
            foreach (var impItem in importItems)
            {
                if (impItem.$ValidateRepeatName$.IsNullOrWhiteSpace())
                {
                    continue;
                }
 
                if (impItem.$ValidateRepeatName$.IsNullOrWhiteSpace())
                {
                    var errorMsg = $"µ¼Èëʧ°Ü£¡ÅäÖ㬵Ú{impItem.RowIndex}ÐУº$EntityName$Ãû³Æ²»ÄÜΪ¿Õ";
                    throw new UserFriendlyException(errorMsg);
                }
 
                var old$EntityName$ = await $EntityInstanceName$Repository.FindByNameAsync(impItem.$ValidateRepeatName$);
                if (old$EntityName$ != null)
                {
                    var $EntityInstanceName$UpdateDto = new $EntityName$UpdateDto
                    {
                        $AppService_ImportAsync$
                    };
 
                    $EntityInstanceName$UpdateDtos.Add((impItem.RowIndex, old$EntityName$.Id, $EntityInstanceName$UpdateDto));
                }
                else
                {
                    var $EntityInstanceName$CreateDto = new $EntityName$CreateDto
                    {
                        $AppService_ImportAsync$
                    };
 
                    $EntityInstanceName$CreateDtos.Add((impItem.RowIndex, $EntityInstanceName$CreateDto));
                }
            }
        }
 
        // ÐÂÔö
        foreach (var $EntityInstanceName$Dto in $EntityInstanceName$CreateDtos)
        {
            try
            {
                await CreateAsync($EntityInstanceName$Dto.Item);
            }
            catch (Exception e)
            {
                var errorMsg = $"µ¼Èëʧ°Ü£¡ÅäÖ㬵Ú{$EntityInstanceName$Dto.RowIndex}ÐУº{e.Message}£¬ÖÕÖ¹µ¼Èë";
                throw new UserFriendlyException(errorMsg);
            }
        }
 
        // ¸üÐÂ
        foreach (var $EntityInstanceName$Dto in $EntityInstanceName$UpdateDtos)
        {
            try
            {
                await UpdateAsync($EntityInstanceName$Dto.Id, $EntityInstanceName$Dto.Item);
            }
            catch (Exception e)
            {
                var errorMsg = $"µ¼Èëʧ°Ü£¡ÅäÖ㬵Ú{$EntityInstanceName$Dto.RowIndex}ÐУº{e.Message}£¬ÖÕÖ¹µ¼Èë";
                throw new UserFriendlyException(errorMsg);
            }
        }
    }
 
    /// <summary>
    /// µ¼³ö$ChinaComment$
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<(Dictionary<string, object> Sheets, string FileName)> ExportAsync(Get$EntityName$Input input)
    {
        Check.NotNull(input, nameof(input));
 
        if (input.Sorting.IsNullOrWhiteSpace())
        {
            input.Sorting = nameof($EntityName$.Sort);
        }
 
        #region ¶¯Ì¬¹¹Ôì²éѯÌõ¼þ 
 
        //¶¯Ì¬¹¹Ôì²éѯÌõ¼þ 
        var whereConditions = DynamicGetQueryParams(input);
 
        #endregion
 
 
        var list = await $EntityInstanceName$Repository.GetListAsync(whereConditions, input.Sorting, input.MaxResultCount, input.SkipCount, includeDetails: true);
        var result = ObjectMapper.Map<List<$EntityName$>, List<$EntityName$Dto>>(list);
 
        var sheets = new Dictionary<string, object>
        {
            ["ÅäÖÃ"] = ExportHelper.ConvertListToExportData(result),
        };
 
        var fileName = "$PageMenuName$";
        return (sheets, fileName);
    }
 
    /// <summary>
    /// Ð£Ñé$ChinaComment$£¬µ±Ð½¨»ò¸üÐÂʱ
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    protected Task CheckCreateOrUpdateDtoAsync($EntityName$CreateOrUpdateDtoBase input)
    {
        Check.NotNull(input, nameof(input));
        $UpdateAttributesForCheckCreateOrUpdateDtoAsync$
        return Task.CompletedTask;
    }
}