2021-06-07 17:22:07 +08:00
|
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"mayfly-go/pkg/gormx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
type roleRepoImpl struct {
|
|
|
|
|
|
base.RepoImpl[*entity.Role]
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func newRoleRepo() repository.Role {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return &roleRepoImpl{base.RepoImpl[*entity.Role]{M: new(entity.Role)}}
|
2022-09-09 18:26:08 +08:00
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
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...)
|
2023-07-01 14:34:42 +08:00
|
|
|
|
return gormx.PageQuery(qd, pageParam, toEntity)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 22:39:32 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 获取角色拥有的资源id数组,从role_resource表获取
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func (m *roleRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
var rrs []entity.RoleResource
|
|
|
|
|
|
|
|
|
|
|
|
condtion := &entity.RoleResource{RoleId: roleId}
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.ListBy(condtion, &rrs, "ResourceId")
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
var rids []uint64
|
|
|
|
|
|
for _, v := range rrs {
|
|
|
|
|
|
rids = append(rids, v.ResourceId)
|
|
|
|
|
|
}
|
|
|
|
|
|
return rids
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (m *roleRepoImpl) GetRoleResources(roleId uint64, toEntity any) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
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 " +
|
2021-07-28 18:03:19 +08:00
|
|
|
|
"FROM t_sys_role_resource rr JOIN t_sys_resource r ON rr.resource_id = r.id " +
|
2023-07-20 22:41:13 +08:00
|
|
|
|
"WHERE rr.role_id = ? AND rr.is_deleted = 0 AND r.is_deleted = 0 " +
|
2021-06-07 17:22:07 +08:00
|
|
|
|
"ORDER BY r.pid ASC, r.weight ASC"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
gormx.GetListBySql2Model(sql, toEntity, roleId)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-20 22:41:13 +08:00
|
|
|
|
func (m *roleRepoImpl) SaveRoleResource(rr []*entity.RoleResource) {
|
|
|
|
|
|
gormx.BatchInsert(rr)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func (m *roleRepoImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
gormx.DeleteBy(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId})
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|