public async Task UpdateAsync(DepartmentUDto dto) { var parent = await _dbContent.Departments.FindAsync(dto.Pid); if (parent == null && dto.Pid != default(Guid)) { throw new UserOperationException("上级部门不存在"); } var entity = await _dbContent.Departments.FindAsync(dto.Id); if (entity == null) { throw new UserOperationException("数据不存在"); } var oldEntityFullPath = entity.FullPath; //A_B entity.Pid = dto.Pid; entity.Name = dto.Name; entity.Order = dto.Order; entity.Description = dto.Description; if (parent == null) { entity.FullPath = entity.Name; } else { entity.FullPath = parent.FullPath + "/" + dto.Name; } //检查当前节点的子节点 var children = await _dbContent.Departments .Where(b => b.FullPath.StartsWith(oldEntityFullPath) && b.Id != entity.Id).ToListAsync(); foreach (var item in children) { item.FullPath = item.FullPath.Replace(oldEntityFullPath, entity.FullPath); } _dbContent.Departments.UpdateRange(entity); _dbContent.Departments.UpdateRange(children); await _dbContent.SaveChangesAsync(); }
public async Task <IActionResult> UpdateAsync([FromBody] DepartmentUDto dto) { await _departmentService.UpdateAsync(dto); return(Ok("更新成功")); }