mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package persistence
|
||
|
||
import (
|
||
"mayfly-go/internal/sys/domain/entity"
|
||
"mayfly-go/internal/sys/domain/repository"
|
||
"mayfly-go/pkg/base"
|
||
"mayfly-go/pkg/gormx"
|
||
"mayfly-go/pkg/model"
|
||
)
|
||
|
||
type roleRepoImpl struct {
|
||
base.RepoImpl[*entity.Role]
|
||
}
|
||
|
||
func newRoleRepo() repository.Role {
|
||
return &roleRepoImpl{base.RepoImpl[*entity.Role]{M: new(entity.Role)}}
|
||
}
|
||
|
||
func (m *roleRepoImpl) GetPageList(condition *entity.RoleQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||
qd := gormx.NewQuery(new(entity.Role)).
|
||
Like("name", condition.Name).
|
||
Like("code", condition.Code).
|
||
In("id", condition.Ids).
|
||
NotIn("id", condition.NotIds).
|
||
WithOrderBy(orderBy...)
|
||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||
}
|
||
|
||
func (m *roleRepoImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) {
|
||
var res []*entity.Role
|
||
qd := gormx.NewQuery(new(entity.Role)).
|
||
Like("name", condition.Name).
|
||
Like("code", condition.Code).
|
||
In("id", condition.Ids).
|
||
NotIn("id", condition.NotIds).
|
||
OrderByDesc("id")
|
||
err := gormx.ListByQueryCond(qd, &res)
|
||
return res, err
|
||
}
|
||
|
||
// 获取角色拥有的资源id数组,从role_resource表获取
|
||
func (m *roleRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
||
var rrs []entity.RoleResource
|
||
|
||
condtion := &entity.RoleResource{RoleId: roleId}
|
||
gormx.ListBy(condtion, &rrs, "ResourceId")
|
||
|
||
var rids []uint64
|
||
for _, v := range rrs {
|
||
rids = append(rids, v.ResourceId)
|
||
}
|
||
return rids
|
||
}
|
||
|
||
func (m *roleRepoImpl) GetRoleResources(roleId uint64, toEntity any) {
|
||
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 = ? AND rr.is_deleted = 0 AND r.is_deleted = 0 " +
|
||
"ORDER BY r.pid ASC, r.weight ASC"
|
||
gormx.GetListBySql2Model(sql, toEntity, roleId)
|
||
}
|
||
|
||
func (m *roleRepoImpl) SaveRoleResource(rr []*entity.RoleResource) {
|
||
gormx.BatchInsert(rr)
|
||
}
|
||
|
||
func (m *roleRepoImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
|
||
gormx.DeleteBy(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId})
|
||
}
|