From 391c4d67ad70df01c3bb849c16e8479edb86846d Mon Sep 17 00:00:00 2001 From: schangxiang@126.com <schangxiang@126.com> Date: 周三, 15 10月 2025 14:04:59 +0800 Subject: [PATCH] qww --- Weben_CMS专用代码生成器/Code/Templete/AppService/AppService模板.txt | 483 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 476 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..789e804 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,488 @@ -using CMS.Plugin.$NameSpacePath$.Domain.Shared.Enums; +锘縰sing 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.$EntityName$; +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.$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 $EntityInstanceName$Repository) { + _$EntityInstanceName$Repository = $EntityInstanceName$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); + + insertObj.CreatorName = input.CreatorName;//鍒涘缓浜� + 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); + + // 鎵归噺璧嬪�兼墍鏈夊彲鏄犲皠瀛楁锛堥�氳繃鍖垮悕瀵硅薄锛� + updateObj = ObjectMapper.Map(input, updateObj); // 绛夋晥浜庢墜鍔ㄨ祴鍊兼墍鏈夊尮閰嶅瓧娈� + + updateObj.LastModifierName = input.LastModifierName;//淇敼浜� + + 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, MyCurrentUser myCurrentUser) + { + //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, MyCurrentUser myCurrentUser) + { + foreach (var id in ids) + { + await DeleteAsync(id); + } + } + + /// <summary> + /// 鐗╃悊鍒犻櫎$ChinaComment$ + /// </summary> + /// <param name="id">涓婚敭ID</param> + /// <param name="cancellationToken"></param> + /// <returns></returns> + public virtual async Task DeletePermanentlyAsync(Guid id, MyCurrentUser myCurrentUser, CancellationToken cancellationToken = default) + { + _$EntityInstanceName$Repository.DeletePermanentlyAsync(id); + } + + /// <summary> + /// 鎵归噺鐗╃悊鍒犻櫎$ChinaComment$锛堢洿鎺ュ垹闄わ紝涓嶈蒋鍒犻櫎锛� + /// </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) + { + _$EntityInstanceName$Repository.BatchDeletePermanentlyAsync(ids); + } + + /// <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, MyCurrentUser myCurrentUser) + { + 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$ 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) + { + old$EntityName$ = ObjectMapper.Map(impItem, old$EntityName$); // 绛夋晥浜庢墜鍔ㄨ祴鍊兼墍鏈夊尮閰嶅瓧娈� + $EntityInstanceName$UpdateDtos.Add((impItem.RowIndex, old$EntityName$.Id, old$EntityName$)); + } + else + { + var $EntityInstanceName$CreateDto = new $EntityName$CreateDto { }; + $EntityInstanceName$CreateDto = ObjectMapper.Map(impItem, $EntityInstanceName$CreateDto); // 绛夋晥浜庢墜鍔ㄨ祴鍊兼墍鏈夊尮閰嶅瓧娈� + $EntityInstanceName$CreateDtos.Add((impItem.RowIndex, $EntityInstanceName$CreateDto)); + } + } + } + + // 鏂板 + foreach (var $EntityInstanceName$Dto in $EntityInstanceName$CreateDtos) + { + try + { + $EntityInstanceName$Dto.Item.CreatorName = myCurrentUser.UserAccount;//鍒涘缓浜� + 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 + { + $EntityInstanceName$Dto.Item.LastModifierName = myCurrentUser.UserAccount;//淇敼浜� + await _$EntityInstanceName$Repository.UpdateAsync($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; + } + + /// <summary> + /// 鏍规嵁鏉′欢鑾峰彇$ChinaComment$鍒楄〃 + /// </summary> + /// <param name="whereConditions"></param> + /// <param name="cancellationToken"></param> + /// <returns></returns> + public async Task<List<$EntityName$>> GetListByFilterAsync(Expression<Func<$EntityName$, bool>> whereConditions, CancellationToken cancellationToken = default) + { + return await _$EntityInstanceName$Repository.GetListByFilterAsync(whereConditions); + } + + /// <summary> + /// 鏍规嵁鏉′欢鑾峰彇鍗曚釜$ChinaComment$ + /// </summary> + /// <param name="whereConditions"></param> + /// <param name="isMultipleThrowException">鏄惁鏌ヨ鍑哄鏉″氨鎶ラ敊</param> + /// <param name="cancellationToken"></param> + /// <returns></returns> + /// <exception cref="UserFriendlyException"></exception> + public async Task<$EntityName$> GetSingleByFilterAsync(Expression<Func<$EntityName$, bool>> whereConditions, bool is鈥婱ultipleThrowException = false, CancellationToken cancellationToken = default) + { + return await _$EntityInstanceName$Repository.GetSingleByFilterAsync(whereConditions, is鈥婱ultipleThrowException); + } + + /// <summary> + /// 鏍规嵁鏉′欢鑾峰彇$ChinaComment$鍒楄〃 + /// </summary> + /// <param name="input"></param> + /// <returns></returns> + public virtual async Task<List<$EntityName$Dto>> FindListByFilterAsync(Get$EntityName$Input input, CancellationToken cancellationToken = default) + { + 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.GetListByFilterAsync(whereConditions?.data); + + return new List<$EntityName$Dto>(ObjectMapper.Map<List<$EntityName$>, List<$EntityName$Dto>>(list)); + } + /// <summary> + /// 鏍规嵁鏉′欢鑾峰彇鍗曚釜$ChinaComment$ + /// </summary> + /// <param name="input"></param> + /// <returns></returns> + public virtual async Task<$EntityName$Dto> FindSingleByFilterAsync(Get$EntityName$Input input, CancellationToken cancellationToken = default) + { + Check.NotNull(input, nameof(input)); + + if (input.Sorting.IsNullOrWhiteSpace()) + { + input.Sorting = nameof($EntityName$.Sort); + } + + #region 鍔ㄦ�佹瀯閫犳煡璇㈡潯浠� + + //鍔ㄦ�佹瀯閫犳煡璇㈡潯浠� + var whereConditions = DynamicGetQueryParams(input); + + #endregion + + var dataObj = await _$EntityInstanceName$Repository.GetSingleByFilterAsync(whereConditions?.data); + + return (ObjectMapper.Map<$EntityName$, $EntityName$Dto>(dataObj)); } } -- Gitblit v1.9.3