Files
mayfly-go/server/internal/sys/infrastructure/persistence/role.go

83 lines
2.5 KiB
Go
Raw Normal View History

package persistence
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
2022-09-09 18:26:08 +08:00
type roleRepoImpl struct{}
2022-09-09 18:26:08 +08:00
func newRoleRepo() repository.Role {
return new(roleRepoImpl)
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
2022-10-26 20:49:29 +08:00
return model.GetPage(pageParam, condition, condition, toEntity, orderBy...)
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) Delete(id uint64) {
biz.ErrIsNil(model.DeleteById(new(entity.Role), id), "删除角色失败")
}
// 获取角色拥有的资源id数组从role_resource表获取
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 {
var rrs []entity.RoleResource
condtion := &entity.RoleResource{RoleId: roleId}
model.ListBy(condtion, &rrs, "ResourceId")
var rids []uint64
for _, v := range rrs {
rids = append(rids, v.ResourceId)
}
return rids
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) GetRoleResources(roleId uint64, toEntity interface{}) {
sql := "select rr.creator AS creator, rr.create_time AS CreateTime, rr.resource_id AS id, r.pid AS pid, " +
"r.name AS name, r.type AS type, r.status AS status " +
"FROM t_sys_role_resource rr JOIN t_sys_resource r ON rr.resource_id = r.id " +
"WHERE rr.role_id = ? " +
"ORDER BY r.pid ASC, r.weight ASC"
model.GetListBySql2Model(sql, toEntity, roleId)
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) SaveRoleResource(rr *entity.RoleResource) {
model.Insert(rr)
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
model.DeleteByCondition(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId})
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) GetAccountRoleIds(accountId uint64) []uint64 {
var rrs []entity.AccountRole
condtion := &entity.AccountRole{AccountId: accountId}
model.ListBy(condtion, &rrs, "RoleId")
var rids []uint64
for _, v := range rrs {
rids = append(rids, v.RoleId)
}
return rids
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) SaveAccountRole(ar *entity.AccountRole) {
model.Insert(ar)
}
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) DeleteAccountRole(accountId, roleId uint64) {
model.DeleteByCondition(&entity.AccountRole{RoleId: roleId, AccountId: accountId})
}
// 获取账号角色信息列表
2022-09-09 18:26:08 +08:00
func (m *roleRepoImpl) GetAccountRoles(accountId uint64, toEntity interface{}) {
sql := "SELECT r.status, r.name, ar.create_time AS CreateTime, ar.creator AS creator " +
"FROM t_sys_role r JOIN t_sys_account_role ar ON r.id = ar.role_id AND ar.account_id = ? " +
"ORDER BY ar.create_time DESC"
model.GetListBySql2Model(sql, toEntity, accountId)
}