package sys_role_admin import ( "github.com/beego/beego/v2/client/orm" "time" ) type SysRoleAdmin struct { Id int `orm:"column(id);pk" description:"自增ID"` AdminId int `orm:"column(admin_id);" description:"系统用户id"` RoleId int `orm:"column(role_id);" description:"角色ID"` CreateTime time.Time `orm:"column(create_time);" description:"创建时间"` } type RoleAdminItem struct { SysRoleAdminId int `description:"管理员账号和角色映射表id"` AdminId int `description:"系统用户id"` AdminName string `description:"系统用户名称"` RealName string `description:"系统用户姓名"` Enabled int `description:"1:有效,0:禁用"` Mobile string `description:"手机号"` RoleId int `description:"角色Id"` RoleTypeCode string `description:"角色编码"` DepartmentId int `description:"部门id"` DepartmentName string `description:"部门名称"` DepartmentGroup string `description:"部门分组"` TeamId int `description:"小组id"` GroupId int `description:"分组id"` GroupName string `description:"分组名称"` Authority int `description:"管理权限,0:无,1:部门负责人,2:小组负责人,或者ficc销售主管,4:ficc销售组长"` LabelVal int `description:"标签:1:超级管理员,2:管理员,3:部门经理,4:组长,5:ficc销售主管"` } func (sa *SysRoleAdmin) TableName() string { return "sys_role_admin" } // GetRoleIdsByAdminId 根据管理员账号查询角色ID func GetRoleIdsByAdminId(adminId int) (items []*SysRoleAdmin, err error) { sql := `SELECT * from sys_role_admin where admin_id = ?` _, err = orm.NewOrm().Raw(sql, adminId).QueryRows(&items) return } // GetRoleIdsByAdminIdRoleId 根据管理员账号和角色查询 func GetRoleIdsByAdminIdRoleId(adminId int, roleId int) (item *SysRoleAdmin, err error) { sql := `SELECT * from sys_role_admin where admin_id = ? and role_id=?` err = orm.NewOrm().Raw(sql, adminId, roleId).QueryRow(&item) return } // GetRoleAdminList 查询对应角色绑定的管理员列表 func GetRoleAdminList(condition string, pars []interface{}, startSize, pageSize int) (list []*RoleAdminItem, err error) { sql := `SELECT s.id as sys_role_admin_id, a.admin_id, a.admin_name, a.real_name, a.mobile, a.department_id, a.department_name, a.enabled, a.role_id, a.role_type_code , a.group_id, a.authority, a.group_name FROM sys_role_admin s INNER JOIN admin a ON s.admin_id = a.admin_id where 1=1` if condition != "" { sql += condition } sql += ` ORDER BY s.id DESC limit ?, ?` _, err = orm.NewOrm().Raw(sql, pars, startSize, pageSize).QueryRows(&list) return } // GetRoleAdminListTotal 查询对应角色绑定的管理员列表总数 func GetRoleAdminListTotal(condition string, pars []interface{}) (total int64, err error) { sql := `SELECT count(*) FROM sys_role_admin s INNER JOIN admin a ON s.admin_id = a.admin_id where 1=1` if condition != "" { sql += condition } err = orm.NewOrm().Raw(sql, pars).QueryRow(&total) return } // CheckRoleAdminByAdminIds 判断是否已经绑定过该角色 func CheckRoleAdminByAdminIds(adminIds string, roleId int) (list []*SysRoleAdmin, err error) { sql := `SELECT s.* FROM sys_role_admin s LEFT JOIN admin a ON s.admin_id = a.admin_id where s.role_id=? and s.admin_id in (` + adminIds + `)` _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&list) return } // AddRoleAdminBatch 批量新增留言版管理员 func AddRoleAdminBatch(admins []*SysRoleAdmin) (err error) { o := orm.NewOrm() _, err = o.InsertMulti(len(admins), admins) return } // DeleteRoleAdmin 删除管理员和角色的绑定 func DeleteRoleAdmin(SysRoleAdminId int) (err error) { sql := `DELETE FROM sys_role_admin WHERE id=? ` o := orm.NewOrm() _, err = o.Raw(sql, SysRoleAdminId).Exec() return } // GetRoleAdminById 根据ID查询绑定记录 func GetRoleAdminById(SysRoleAdminId int) (item *SysRoleAdmin, err error) { sql := `SELECT * FROM sys_role_admin where id=?` err = orm.NewOrm().Raw(sql, SysRoleAdminId).QueryRow(&item) return }