249 lines
7.5 KiB
Go
249 lines
7.5 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/os/gtime"
|
||
"nl-video-api/internal/model/entity"
|
||
)
|
||
|
||
// PermissionDao 权限数据访问对象
|
||
type PermissionDao struct{}
|
||
|
||
// TableName 获取表名
|
||
func (dao *PermissionDao) TableName() string {
|
||
return "nl_permission"
|
||
}
|
||
|
||
// Create 创建权限
|
||
func (dao *PermissionDao) Create(ctx context.Context, data *entity.Permission) (int, error) {
|
||
result, err := g.DB().Model(dao.TableName()).Ctx(ctx).Data(data).Insert()
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
id, err := result.LastInsertId()
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
return int(id), nil
|
||
}
|
||
|
||
// GetById 根据ID获取权限
|
||
func (dao *PermissionDao) GetById(ctx context.Context, id int) (*entity.Permission, error) {
|
||
var permission *entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).Where("id = ? AND deleted_at = 0", id).Scan(&permission)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return permission, nil
|
||
}
|
||
|
||
// GetByCode 根据权限编码获取权限
|
||
func (dao *PermissionDao) GetByCode(ctx context.Context, code string) (*entity.Permission, error) {
|
||
var permission *entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).Where("code = ? AND deleted_at = 0", code).Scan(&permission)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return permission, nil
|
||
}
|
||
|
||
// Update 更新权限
|
||
func (dao *PermissionDao) Update(ctx context.Context, id int, data g.Map) error {
|
||
data["updated_at"] = gtime.Now().Unix()
|
||
_, err := g.DB().Model(dao.TableName()).Ctx(ctx).Where("id = ?", id).Data(data).Update()
|
||
return err
|
||
}
|
||
|
||
// Delete 删除权限(软删除)
|
||
func (dao *PermissionDao) Delete(ctx context.Context, id int) error {
|
||
data := g.Map{
|
||
"deleted_at": gtime.Now().Unix(),
|
||
"updated_at": gtime.Now().Unix(),
|
||
}
|
||
_, err := g.DB().Model(dao.TableName()).Ctx(ctx).Where("id = ?", id).Data(data).Update()
|
||
return err
|
||
}
|
||
|
||
// GetList 获取权限列表
|
||
func (dao *PermissionDao) GetList(ctx context.Context, req *PermissionListReq) ([]*entity.Permission, int, error) {
|
||
model := g.DB().Model(dao.TableName()).Ctx(ctx).Where("deleted_at = 0")
|
||
|
||
// 条件筛选
|
||
if req.Name != "" {
|
||
model = model.WhereLike("name", "%"+req.Name+"%")
|
||
}
|
||
if req.Code != "" {
|
||
model = model.WhereLike("code", "%"+req.Code+"%")
|
||
}
|
||
if req.Type != "" {
|
||
model = model.Where("type = ?", req.Type)
|
||
}
|
||
if req.Status >= 0 {
|
||
model = model.Where("status = ?", req.Status)
|
||
}
|
||
if req.ParentId >= 0 {
|
||
model = model.Where("parent_id = ?", req.ParentId)
|
||
}
|
||
|
||
// 获取总数
|
||
total, err := model.Count()
|
||
if err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
// 分页和排序
|
||
if req.Page > 0 && req.PageSize > 0 {
|
||
offset := (req.Page - 1) * req.PageSize
|
||
model = model.Limit(req.PageSize).Offset(offset)
|
||
}
|
||
|
||
model = model.OrderAsc("sort").OrderAsc("id")
|
||
|
||
var permissions []*entity.Permission
|
||
err = model.Scan(&permissions)
|
||
if err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
return permissions, total, nil
|
||
}
|
||
|
||
// GetTree 获取权限树形结构
|
||
func (dao *PermissionDao) GetTree(ctx context.Context) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("deleted_at = 0 AND status = 1").
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 构建树形结构
|
||
return dao.buildTree(permissions, 0), nil
|
||
}
|
||
|
||
// buildTree 构建树形结构
|
||
func (dao *PermissionDao) buildTree(permissions []*entity.Permission, parentId int) []*entity.Permission {
|
||
var tree []*entity.Permission
|
||
|
||
for _, permission := range permissions {
|
||
if permission.ParentId == parentId {
|
||
children := dao.buildTree(permissions, permission.Id)
|
||
if len(children) > 0 {
|
||
// 这里需要在Permission实体中添加Children字段
|
||
// permission.Children = children
|
||
}
|
||
tree = append(tree, permission)
|
||
}
|
||
}
|
||
|
||
return tree
|
||
}
|
||
|
||
// GetAll 获取所有权限
|
||
func (dao *PermissionDao) GetAll(ctx context.Context) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("deleted_at = 0 AND status = 1").
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// GetByParentId 根据父级ID获取权限
|
||
func (dao *PermissionDao) GetByParentId(ctx context.Context, parentId int) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("parent_id = ? AND deleted_at = 0 AND status = 1", parentId).
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// GetByType 根据类型获取权限
|
||
func (dao *PermissionDao) GetByType(ctx context.Context, permissionType string) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("type = ? AND deleted_at = 0 AND status = 1", permissionType).
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// CheckCodeExists 检查权限编码是否存在
|
||
func (dao *PermissionDao) CheckCodeExists(ctx context.Context, code string, excludeId int) (bool, error) {
|
||
model := g.DB().Model(dao.TableName()).Ctx(ctx).Where("code = ? AND deleted_at = 0", code)
|
||
if excludeId > 0 {
|
||
model = model.Where("id != ?", excludeId)
|
||
}
|
||
|
||
count, err := model.Count()
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
return count > 0, nil
|
||
}
|
||
|
||
// GetPermissionsByIds 根据ID列表获取权限
|
||
func (dao *PermissionDao) GetPermissionsByIds(ctx context.Context, ids []int) ([]*entity.Permission, error) {
|
||
if len(ids) == 0 {
|
||
return []*entity.Permission{}, nil
|
||
}
|
||
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("id IN (?) AND deleted_at = 0", ids).
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// GetMenuPermissions 获取菜单权限
|
||
func (dao *PermissionDao) GetMenuPermissions(ctx context.Context) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("type = 'menu' AND deleted_at = 0 AND status = 1").
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// GetApiPermissions 获取API权限
|
||
func (dao *PermissionDao) GetApiPermissions(ctx context.Context) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model(dao.TableName()).Ctx(ctx).
|
||
Where("type = 'api' AND deleted_at = 0 AND status = 1").
|
||
OrderAsc("sort").OrderAsc("id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// GetUserPermissions 获取用户权限(通过角色)
|
||
func (dao *PermissionDao) GetUserPermissions(ctx context.Context, userId int) ([]*entity.Permission, error) {
|
||
var permissions []*entity.Permission
|
||
err := g.DB().Model("nl_permission p").Ctx(ctx).
|
||
LeftJoin("nl_role_permission rp", "p.id = rp.permission_id").
|
||
LeftJoin("nl_admin a", "a.role_id = rp.role_id").
|
||
Where("a.id = ? AND p.deleted_at = 0 AND p.status = 1", userId).
|
||
OrderAsc("p.sort").OrderAsc("p.id").
|
||
Scan(&permissions)
|
||
return permissions, err
|
||
}
|
||
|
||
// PermissionListReq 权限列表请求参数
|
||
type PermissionListReq struct {
|
||
Page int `json:"page" v:"min:1#页码最小为1"`
|
||
PageSize int `json:"page_size" v:"min:1,max:100#每页数量范围1-100"`
|
||
Name string `json:"name"` // 权限名称
|
||
Code string `json:"code"` // 权限编码
|
||
Type string `json:"type"` // 权限类型:menu-菜单,button-按钮,api-接口
|
||
Status int `json:"status"` // 状态:-1-全部,0-禁用,1-启用
|
||
ParentId int `json:"parent_id"` // 父级ID:-1-全部,0-顶级,>0-指定父级
|
||
}
|