From fddc6be72153b6e22bcd77952ff471bcf98a808d Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周二, 06 5月 2025 14:35:43 +0800
Subject: [PATCH] 22

---
 Weben_CMS专用代码生成器/Code/Templete/AppService/AppService模板.txt |  388 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 381 insertions(+), 7 deletions(-)

diff --git "a/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/AppService/AppService\346\250\241\346\235\277.txt" "b/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/AppService/AppService\346\250\241\346\235\277.txt"
index 27e202b..f56427c 100644
--- "a/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/AppService/AppService\346\250\241\346\235\277.txt"
+++ "b/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/AppService/AppService\346\250\241\346\235\277.txt"
@@ -1,19 +1,393 @@
-using CMS.Plugin.$NameSpacePath$.Domain.Shared.Enums;
+using CMS.Plugin.$NameSpacePath$.Application.Contracts.Dtos.$EntityName$;
+using CMS.Plugin.$NameSpacePath$.Application.Contracts.Services;
+using CMS.Plugin.$NameSpacePath$.Domain.Shared;
+using CMS.Plugin.$NameSpacePath$.Domain.Shared.Util;
+using CMS.Plugin.$NameSpacePath$.Domain.Shared.$EntityName$;
+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.Contracts.Dtos.$EntityName$;
+namespace CMS.Plugin.$NameSpacePath$.Application.Implements;
 
 /// <summary>
-/// $ChinaComment$创建或更新基类
+/// $ChinaComment$应用服务
 /// </summary>
-public abstract class $EntityName$CreateOrUpdateDtoBase : ExtensibleEntityDto
+public class $EntityName$AppService : CMSPluginAppService, I$EntityName$AppService
 {
-    $AddAttributes$
+    private readonly I$EntityName$Repository $EntityInstanceName$Repository;
 
     /// <summary>
-    /// Initializes a new instance of the <see cref="$EntityName$CreateOrUpdateDtoBase"/> class.
+    /// Initializes a new instance of the <see cref="$EntityName$AppService"/> class.
     /// </summary>
-    public $EntityName$CreateOrUpdateDtoBase() : base(false)
+    /// <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() && impItem.MaterialName.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 = result.Count > 1 ? "$ChinaComment$列表" : result.Count == 1 ? result[0]?.$ValidateRepeatName$ : "$EntityName$模版";
+        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;
     }
 }

--
Gitblit v1.9.3