Exemplo n.º 1
0
        /// <summary>
        /// 更新实体属性信息信息
        /// </summary>
        /// <param name="dtos">包含更新信息的实体属性信息输入DTO</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> UpdateCodeProperties(params CodePropertyInputDto[] dtos)
        {
            List <string> names = new List <string>();

            UnitOfWork.EnableTransaction();
            foreach (var dto in dtos)
            {
                dto.Validate();
                CodeEntity entity = await EntityRepository.GetAsync(dto.EntityId);

                if (entity == null)
                {
                    return(new OperationResult(OperationResultType.Error, $"编号为“{dto.EntityId}”的实体信息不存在"));
                }

                if (await CheckCodePropertyExists(m => m.Name == dto.Name && m.EntityId == dto.EntityId, dto.Id))
                {
                    return(new OperationResult(OperationResultType.Error, $"实体“{entity.Name}”中名称为“{dto.Name}”的属性信息已存在"));
                }

                if (dto.Order == 0)
                {
                    dto.Order = PropertyRepository.Query(m => m.EntityId == entity.Id).Count() + 1;
                }
                int count;
                if (dto.Id == default)
                {
                    CodeProperty property = dto.MapTo <CodeProperty>();
                    count = await PropertyRepository.InsertAsync(property);
                }
                else
                {
                    CodeProperty property = await PropertyRepository.GetAsync(dto.Id);

                    property = dto.MapTo(property);
                    count    = await PropertyRepository.UpdateAsync(property);
                }

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

            await UnitOfWork.CommitAsync();

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