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; using CmsQueryExtensions.Entitys; namespace CMS.Plugin.$NameSpacePath$.Application.Implements; /// /// $ChinaComment$应用服务 /// public class $EntityName$AppService : CMSPluginAppService, I$EntityName$AppService { private readonly I$EntityName$Repository _$EntityInstanceName$Repository; /// /// Initializes a new instance of the class. /// /// The task job repository. public $EntityName$AppService(I$EntityName$Repository $EntityInstanceName$Repository) { _$EntityInstanceName$Repository = $EntityInstanceName$Repository; } /// /// 获取指定$ChinaComment$ /// /// /// public virtual async Task<$EntityName$Dto> GetAsync(Guid id) { return ObjectMapper.Map<$EntityName$, $EntityName$Dto>(await _$EntityInstanceName$Repository.GetAsync(id)); } /// /// 分页获取$ChinaComment$ /// /// /// public virtual async Task> 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$Dto>>(list)); } /// /// 动态构造查询条件 /// /// 输入参数 /// private FunReturnResultModel>> DynamicGetQueryParams(Get$EntityName$Input input) { //动态构造查询条件 var whereConditions = WhereConditionsExtensions.GetWhereConditions<$EntityName$, Get$EntityName$Input>(input); if (!whereConditions.IsSuccess) { throw new Exception("动态构造查询条件失败:" + whereConditions.ErrMsg); } //也可再次自定义构建查询条件 Expression> extendExpression = a => a.IsDeleted == false; // 使用 System.Linq.PredicateBuilder 的 And var pres = (System.Linq.Expressions.Expression>)(whereConditions.data); whereConditions.data = System.Linq.PredicateBuilder.And(pres, extendExpression); return whereConditions; } /// /// 新建$ChinaComment$ /// /// /// /// 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); } /// /// 更新$ChinaComment$ /// /// /// /// /// 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$ updateObj.LastModifierName = input.LastModifierName;//修改人 await _$EntityInstanceName$Repository.UpdateAsync(updateObj); return ObjectMapper.Map<$EntityName$, $EntityName$Dto>(updateObj); } /// /// 克隆$ChinaComment$ /// /// /// public async Task> CloneAsync(IEnumerable 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$Dto>>($EntityInstanceName$s); return new List<$EntityName$Dto>(); } /// /// 删除单个$ChinaComment$ /// /// /// public virtual Task DeleteAsync(Guid id) { return _$EntityInstanceName$Repository.DeleteAsync(id); } /// /// 删除多个$ChinaComment$ /// /// /// public async Task DeleteManyAsync(IEnumerable ids, MyCurrentUser myCurrentUser) { foreach (var id in ids) { await DeleteAsync(id); } } /// /// 物理删除$ChinaComment$ /// /// 主键ID /// /// public virtual async Task DeletePermanentlyAsync(Guid id, MyCurrentUser myCurrentUser, CancellationToken cancellationToken = default) { _$EntityInstanceName$Repository.DeletePermanentlyAsync(id); } /// /// 批量物理删除$ChinaComment$(直接删除,不软删除) /// /// 要删除的主键ID列表 /// /// public virtual async Task BatchDeletePermanentlyAsync(IEnumerable ids, MyCurrentUser myCurrentUser, CancellationToken cancellationToken = default) { _$EntityInstanceName$Repository.BatchDeletePermanentlyAsync(ids); } /// /// 调整排序$ChinaComment$ /// /// /// /// 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); } /// /// 导入$ChinaComment$ /// /// /// /// 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$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 { $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 UpdateAsync($EntityInstanceName$Dto.Id, $EntityInstanceName$Dto.Item); } catch (Exception e) { var errorMsg = $"导入失败!配置,第{$EntityInstanceName$Dto.RowIndex}行:{e.Message},终止导入"; throw new UserFriendlyException(errorMsg); } } } /// /// 导出$ChinaComment$ /// /// /// public async Task<(Dictionary 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$Dto>>(list); var sheets = new Dictionary { ["配置"] = ExportHelper.ConvertListToExportData(result), }; var fileName = "$PageMenuName$"; return (sheets, fileName); } /// /// 校验$ChinaComment$,当新建或更新时 /// /// /// protected Task CheckCreateOrUpdateDtoAsync($EntityName$CreateOrUpdateDtoBase input) { Check.NotNull(input, nameof(input)); $UpdateAttributesForCheckCreateOrUpdateDtoAsync$ return Task.CompletedTask; } /// /// 根据条件获取$ChinaComment$列表 /// /// /// /// public async Task> GetListByFilterAsync(Expression> whereConditions, CancellationToken cancellationToken = default) { return await _$EntityInstanceName$Repository.GetListByFilterAsync(whereConditions); } /// /// 根据条件获取单个$ChinaComment$ /// /// /// 是否查询出多条就报错 /// /// /// public async Task<$EntityName$> GetSingleByFilterAsync(Expression> whereConditions, bool is​MultipleThrowException = false, CancellationToken cancellationToken = default) { return await _$EntityInstanceName$Repository.GetSingleByFilterAsync(whereConditions, is​MultipleThrowException); } }