sys_role.go 4.3 KB
package domain

import (
	"context"
	"github.com/samber/lo"
	"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
)

type SysRole struct {
	Id        int64   `json:",omitempty"` // 唯一标识
	CompanyId int64   `json:"companyId"`  // 公司ID
	Name      string  `json:",omitempty"` // 角色名称
	Menus     []int64 `json:",omitempty"` // 菜单列表
	Apps      []int64 `json:",omitempty"` // 应用列表
	AuthUsers []int64 `json:",omitempty"` // 拥护该角色的用户列表
	AuthRange int     `json:",omitempty"` // 权限范围(1:全部权限 2:部分权限)
	Remark    string  `json:",omitempty"` // 备注说明
	CreatedAt int64   `json:",omitempty"`
	UpdatedAt int64   `json:",omitempty"`
	DeletedAt int64   `json:",omitempty"`
	Version   int     `json:",omitempty"`
}

type SysRoleRepository interface {
	Insert(ctx context.Context, conn transaction.Conn, dm *SysRole) (*SysRole, error)
	Update(ctx context.Context, conn transaction.Conn, dm *SysRole) (*SysRole, error)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *SysRole) (*SysRole, error)
	Delete(ctx context.Context, conn transaction.Conn, dm *SysRole) (*SysRole, error)
	FindOne(ctx context.Context, conn transaction.Conn, id int64) (*SysRole, error)
	FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*SysRole, error)
	Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*SysRole, error)
	FindByEmployee(ctx context.Context, conn transaction.Conn, companyId, userId int64, queryOptions map[string]interface{}) (int64, []*SysRole, error)
}

// AddUser 添加用户
func (role *SysRole) AddUser(userId int64) {
	if lo.Contains(role.AuthUsers, userId) {
		return
	}
	role.AuthUsers = append(role.AuthUsers, userId)
}

// RemoveUser 移除用户
func (role *SysRole) RemoveUser(userId int64) {
	if !lo.Contains(role.AuthUsers, userId) {
		return
	}
	role.AuthUsers = lo.Without(role.AuthUsers, userId)
}

func (role *SysRole) AddAuthRange(auth int) {
	if auth == 0 {
		return
	}
	if !(auth == AuthRangeAllMenu || auth == AuthRangeAllApp) {
		return
	}
	if role.AuthRange&auth == 0 {
		role.AuthRange |= auth
	}
}

type SysMenu struct {
	Id         int64  `json:"id,omitempty"`         // 菜单ID
	ParentId   int64  `json:"parentId"`             // 父级ID
	Name       string `json:"name,omitempty"`       // 菜单名称
	Code       string `json:"code,omitempty"`       // 菜单编码
	MenuType   string `json:"menuType,omitempty"`   // 菜单类型 (目录catalog、菜单menu、按钮button)
	Icon       string `json:"icon,omitempty"`       // 图标
	Sort       int64  `json:"sort,omitempty"`       // 排序
	ParentPath string `json:"parentPath,omitempty"` // 父级目录 1,2,3
	Status     int64  `json:"status,omitempty"`     // 启用状态 1:启用 2:禁用
	Version    int    `json:"version,omitempty"`
}

const (
	AuthRangeAllMenu = 1 // 全部菜单权限
	AuthRangeAllApp  = 2 // 全部APP权限
)

var DefaultMenus []*SysMenu = []*SysMenu{
	//NewMenu(10, "工作台", "SYSTEM_WORKBENCH", 0, 1),
	//NewMenu(40, "通知公告", "SYSTEM_NOTICE", 0, 1),
	//NewMenu(60, "系统日志", "SYSTEM_LOG", 0, 1),

	NewMenu(20, "组织架构", "SYSTEM_ORGANIZATION", 0, 2),
	NewMenu(21, "成员与部门", "SYSTEM_ORGANIZATION_EMPLOYEE-DEPARTMENT", 20, 1),
	//NewMenu(22, "部门", "SYSTEM_ORGANIZATION_DEPARTMENT", 20, 2),
	//NewMenu(22, "用户组", "SYSTEM_ORGANIZATION_GROUP", 20, 3),

	NewMenu(30, "应用管理", "SYSTEM_APP-MANAGER", 0, 3),
	NewMenu(31, "应用中心", "SYSTEM_APP-MANAGER_APP-CENTER", 30, 1),
	NewMenu(32, "应用配置", "SYSTEM_APP-MANAGER_APP-CENTER_SET", 30, 1).WithMenuTypeButton(),

	NewMenu(50, "企业设置", "SYSTEM_COMPANY-SETTING", 0, 4),
	NewMenu(51, "企业信息", "SYSTEM_COMPANY-SETTING_INFO", 50, 1),
	NewMenu(52, "管理员权限", "SYSTEM_COMPANY-SETTING_ADMIN-AUTH", 50, 2),
}

func FineOneMenu(ctx context.Context, conn transaction.Conn, menuId int64) (*SysMenu, error) {
	for _, menu := range DefaultMenus {
		if menu.Id == menuId {
			return menu, nil
		}
	}
	return nil, ErrNotFound
}

func NewMenu(id int64, name, code string, pid int64, sort int64) *SysMenu {
	return &SysMenu{
		Id:       id,
		Name:     name,
		Code:     code,
		ParentId: pid,
		Sort:     sort,
		MenuType: "menu",
	}
}

func (m *SysMenu) WithMenuTypeButton() *SysMenu {
	m.MenuType = "button"
	return m
}