public Result Update(RoleEditDto dto, BaseAppUser user)
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var error = BeforeUpdate(dto);

            if (!string.IsNullOrWhiteSpace(error))
            {
                return(ResultUtil.Fail(error));
            }

            var emptyArrayJson = JsonConvert.SerializeObject(new int[] { });

            var entity = new RoleEntity
            {
                Id   = dto.Id,
                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 row = conn.Update(entity);
                        conn.Delete <RolePowerEntity>(MySearchUtil.New().AndEqual("RoleId", dto.Id));
                        if (dto.Powers.Any())
                        {
                            conn.Create(dto.Powers
                                        .Select(p => new RolePowerEntity {
                                RoleId = dto.Id, PowerId = p, ColumnCodeJson = emptyArrayJson
                            })
                                        .ToList());
                        }
                        trans.Commit();
                        return(row > 0 ? ResultUtil.Success() : ResultUtil.Fail());
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                        conn.Close();
                        return(ResultUtil.Fail(e.Message));
                    }
                }
            }
        }
        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));
                    }
                }
            }
        }
        public string BeforeCrete(RoleEditDto dto)
        {
            if (string.IsNullOrWhiteSpace(dto.Name))
            {
                return("角色名称不能为空");
            }

            if (dto.DataPermissionType < 0 || dto.DataPermissionType > 5)
            {
                return("数据权限类型不合法");
            }

            return(string.Empty);
        }
 private string BeforeUpdate(RoleEditDto dto)
 {
     return(dto.Id <= 0 ? "实体Id必须大于0" : BeforeCrete(dto));
 }