public async Task <AppSrvResult> UpdateAsync(long id, DeptUpdationDto input) { var oldDeptDto = (await GetAllFromCacheAsync()).FirstOrDefault(x => x.Id == id); if (oldDeptDto.Pid == 0 && input.Pid > 0) { return(Problem(HttpStatusCode.BadRequest, "一级单位不能修改等级")); } var isExists = (await GetAllFromCacheAsync()).Exists(x => x.FullName == input.FullName && x.Id != id); if (isExists) { return(Problem(HttpStatusCode.BadRequest, "该部门全称已经存在")); } var oldPids = oldDeptDto.Pids; var deptEnity = _mapper.Map <SysDept>(input); deptEnity.Id = id; if (oldDeptDto.Pid == input.Pid) { await _deptRepository.UpdateAsync(deptEnity, UpdatingProps <SysDept>(x => x.FullName, x => x.SimpleName, x => x.Tips, x => x.Ordinal)); } else { await this.SetDeptPids(deptEnity); await _usrManager.UpdateDeptAsync(oldPids, deptEnity); } return(AppSrvResult()); }
public async Task <AppSrvResult> UpdateAsync(long id, DeptUpdationDto input) { var allDepts = await _cacheService.GetAllDeptsFromCacheAsync(); var oldDeptDto = allDepts.FirstOrDefault(x => x.Id == id); if (oldDeptDto.Pid == 0 && input.Pid > 0) { return(Problem(HttpStatusCode.BadRequest, "一级单位不能修改等级")); } var isExists = allDepts.Exists(x => x.FullName == input.FullName && x.Id != id); if (isExists) { return(Problem(HttpStatusCode.BadRequest, "该部门全称已经存在")); } var deptEnity = Mapper.Map <SysDept>(input); deptEnity.Id = id; if (oldDeptDto.Pid == input.Pid) { await _deptRepository.UpdateAsync(deptEnity, UpdatingProps <SysDept>(x => x.FullName, x => x.SimpleName, x => x.Tips, x => x.Ordinal)); } else { await this.SetDeptPids(deptEnity); await _deptRepository.UpdateAsync(deptEnity, UpdatingProps <SysDept>(x => x.FullName, x => x.SimpleName, x => x.Tips, x => x.Ordinal, x => x.Pid, x => x.Pids)); //zz.efcore 不支持 //await _deptRepository.UpdateRangeAsync(d => d.Pids.Contains($"[{dept.ID}]"), c => new SysDept { Pids = c.Pids.Replace(oldDeptPids, dept.Pids) }); var originalDeptPids = $"{oldDeptDto.Pids}[{deptEnity.Id}],"; var nowDeptPids = $"{deptEnity.Pids}[{deptEnity.Id}],"; var subDepts = await _deptRepository .Where(d => d.Pids.StartsWith(originalDeptPids)) .Select(d => new { d.Id, d.Pids }) .ToListAsync(); foreach (var c in subDepts) { await _deptRepository.UpdateAsync(new SysDept { Id = c.Id, Pids = c.Pids.Replace(originalDeptPids, nowDeptPids) }, UpdatingProps <SysDept>(c => c.Pids)); } } return(AppSrvResult()); }
public async Task <ActionResult <long> > UpdateAsync([FromRoute] long id, [FromBody] DeptUpdationDto input) { return(Result(await _deptService.UpdateAsync(id, input))); }