예제 #1
0
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="baseTreeDto"></param>
        /// <returns></returns>
        public virtual async Task <object> Submit(BaseTreeDto baseTreeDto)
        {
            BaseTree baseTree = null;

            if (string.IsNullOrWhiteSpace(baseTreeDto.DisplayName) || baseTreeDto.TreeNodeType == TreeNodeType.Knowledge)
            {
                baseTreeDto.DisplayName = baseTreeDto.Name;
            }
            if (baseTreeDto.Id == 0)
            {
                baseTree = baseTreeDto.MapTo <BaseTree>();
                await BaseTreeManager.CreateAsync(baseTree);
            }
            else
            {
                baseTree = await BaseTreeManager.GetByIdAsync(baseTreeDto.Id);

                //仅当父级变动
                if (baseTreeDto.ParentId != baseTree.ParentId)
                {
                    var childIds = (await BaseTreeManager.FindChildrenAsync(baseTree.Id, true)).Select(o => o.Id).ToList();
                    if (baseTree.Id == baseTreeDto.ParentId)
                    {
                        throw new UserFriendlyException("不允许设置父级为自己");
                    }
                    else if (baseTreeDto.ParentId != null && childIds.Contains(baseTreeDto.ParentId.Value))
                    {
                        throw new UserFriendlyException("不允许设置父级为子级");
                    }
                    if (baseTreeDto.ParentId == null)
                    {
                        await BaseTreeManager.MoveAsync(baseTree.Id, null);
                    }
                    else
                    {
                        await BaseTreeManager.MoveAsync(baseTree.Id, baseTreeDto.ParentId.Value);
                    }
                }
                baseTreeDto.MapTo(baseTree);
                await BaseTreeManager.UpdateAsync(baseTree);
            }
            await CurrentUnitOfWork.SaveChangesAsync();

            return(new
            {
                baseTree.Id,
                baseTree.ParentId,
                baseTree.Name,
                baseTree.DisplayName,
                baseTree.Code,
                baseTree.RelativeNodeId,
                baseTree.Sort,
                baseTree.TreeNodeType
            });
        }
예제 #2
0
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="baseTreeDto"></param>
        /// <returns></returns>
        public virtual async Task Submit(BaseTreeDto baseTreeDto)
        {
            BaseTree baseTree = null;

            if (baseTreeDto.Id == 0)
            {
                baseTree = baseTreeDto.MapTo <BaseTree>();
                if (baseTreeDto.ParentId.HasValue)
                {
                    var parentNode = await Manager.GetByIdAsync(baseTreeDto.ParentId.Value);

                    if (parentNode == null)
                    {
                        baseTree.ParentId = null;
                    }
                }

                await BaseTreeManager.CreateAsync(baseTree);
            }
            else
            {
                baseTree = await BaseTreeManager.GetByIdAsync(baseTreeDto.Id);

                //仅当父级变动
                if (baseTreeDto.ParentId != baseTree.ParentId)
                {
                    var childIds = (await BaseTreeManager.FindChildrenAsync(baseTree.Id, baseTree.Discriminator, true)).Select(o => o.Id).ToList();
                    if (baseTree.Id == baseTreeDto.ParentId)
                    {
                        throw new UserFriendlyException("不允许设置父级为自己");
                    }
                    else if (baseTreeDto.ParentId != null && childIds.Contains(baseTreeDto.ParentId.Value))
                    {
                        throw new UserFriendlyException("不允许设置父级为子级");
                    }
                    if (baseTreeDto.ParentId == null)
                    {
                        await BaseTreeManager.MoveAsync(baseTree.Id, null);
                    }
                    else
                    {
                        await BaseTreeManager.MoveAsync(baseTree.Id, baseTreeDto.ParentId.Value);
                    }
                }
                baseTreeDto.MapTo(baseTree);
                await BaseTreeManager.UpdateAsync(baseTree);
            }
        }