public async Task <IResultModel> Add(UserAddModel model) { if (await _userRepository.ExistsAsync(m => m.UserCode == model.UserCode && m.IsDel == false)) { return(ResultModel.HasExists); } try { model.Password = _passwordHandler.Encrypt(model.UserCode, model.Password); var entity = _mapper.Map <UserEntity>(model); await _userRepository.InsertAsync(entity, _dbContext.Db); var roles = new List <RoleUserEntity>(); foreach (var roleId in model.RoleIds) { roles.Add(new RoleUserEntity() { RoleId = roleId, UserId = entity.Id }); } if (roles.Any()) { await _roleUserRepository.InsertRangeAsync(roles); } _dbContext.Db.CommitTran(); } catch (Exception ex) { _dbContext.Db.RollbackTran(); return(ResultModel.Failed(ex.Message)); } return(ResultModel.Success()); }
public async Task <IResultModel> Add(RoleUserAddModel model) { if (!await _roleRepository.ExistsAsync(m => m.Id == model.RoleId)) { return(ResultModel.Failed("角色不存在")); } if (!model.UserIds.Any()) { return(ResultModel.Failed("请选择用户")); } var entities = new List <RoleUserEntity>(); foreach (var userId in model.UserIds) { var isExist = await _roleUserRepository.ExistsAsync(m => m.RoleId == model.RoleId && m.UserId == userId && m.IsDel == false); if (isExist) { continue; } entities.Add(new RoleUserEntity() { RoleId = model.RoleId, UserId = userId }); } if (!entities.Any()) { return(ResultModel.Success()); } var result = await _roleUserRepository.InsertRangeAsync(entities); return(ResultModel.Result(result)); }