public Result Create(RoleEditDto dto, BaseAppUser user) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } var error = BeforeCrete(dto); if (!string.IsNullOrWhiteSpace(error)) { return(ResultUtil.Fail(error)); } var entity = new RoleEntity { Name = dto.Name, DataPermissionType = dto.DataPermissionType, DepartmentIdJson = JsonConvert.SerializeObject(dto.Departments), }; entity.BeforeCreate(user); using (var conn = _db.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { var id = conn.Create(entity); if (dto.Powers.Any()) { conn.Create(dto.Powers .Select(p => new RolePowerEntity { RoleId = id, PowerId = p }) .ToList()); } trans.Commit(); return(id > 0 ? ResultUtil.Success(id) : ResultUtil.Fail()); } catch (Exception e) { trans.Rollback(); conn.Close(); return(ResultUtil.Fail(e.Message)); } } } }