示例#1
0
        /// <summary>
        /// 更新代码实体信息信息
        /// </summary>
        /// <param name="dtos">包含更新信息的代码实体信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> UpdateCodeEntities(params CodeEntityInputDto[] dtos)
        {
            List <string> names = new List <string>();

            UnitOfWork.EnableTransaction();
            foreach (var dto in dtos)
            {
                dto.Validate();
                CodeModule module = await ModuleRepository.GetAsync(dto.ModuleId);

                if (module == null)
                {
                    return(new OperationResult(OperationResultType.Error, $"编号为“{dto.ModuleId}”的模块信息不存在"));
                }

                if (await CheckCodeEntityExists(m => m.Name == dto.Name && m.ModuleId == dto.ModuleId, dto.Id))
                {
                    return(new OperationResult(OperationResultType.Error, $"模块“{module.Name}”中名称为“{dto.Name}”的实体信息已存在"));
                }

                if (dto.Order == 0)
                {
                    dto.Order = EntityRepository.Query(m => m.ModuleId == module.Id).Count() + 1;
                }
                int count;
                if (dto.Id == default)
                {
                    CodeEntity entity = dto.MapTo <CodeEntity>();
                    count = await EntityRepository.InsertAsync(entity);
                }
                else
                {
                    CodeEntity entity = await EntityRepository.GetAsync(dto.Id);

                    entity = dto.MapTo(entity);
                    count  = await EntityRepository.UpdateAsync(entity);
                }

                if (count > 0)
                {
                    names.Add(dto.Name);
                }
            }

            await UnitOfWork.CommitAsync();

            return(names.Count > 0
                ? new OperationResult(OperationResultType.Success, $"实体“{names.ExpandAndToString()}”保存成功")
                : OperationResult.NoChanged);
        }