package models

import coreTypes "git.apinb.com/bsm-sdk/core/types"

// 应用模型
type MgtApplication struct {
	coreTypes.Std_IICUDS                 // 标准字段
	Title                string          `gorm:"column:title;type:varchar(255);" json:"title,omitempty"`                                                                        // 应用名称
	Workspace            string          `gorm:"column:workspace;uniqueIndex" json:"workspace,omitempty"`                                                                       // 应用编码
	Description          string          `gorm:"column:description;type:text" json:"description,omitempty"`                                                                     // 应用描述
	Permissions          []MgtPermission `gorm:"foreignKey:Appid;references:ID" json:"permissions,omitempty"`                                                                   // 应用的所有权限
	UsersPmn             []MgtPermission `gorm:"many2many:mgt_link_user_pmn;foreignKey:ID;joinForeignKey:app_id;References:ID;joinReferences:pmn_id" json:"user_pmn,omitempty"` // 用户-权限中间的应用层（用于树形结构查询）
	Users                []MgtUser       `gorm:"many2many:mgt_link_user_app;foreignKey:ID;joinForeignKey:app_id;References:ID;joinReferences:user_id" json:"user,omitempty"`    // 用户关联的角色
	Roles                []MgtRole       `gorm:"many2many:mgt_link_role_app;foreignKey:ID;joinForeignKey:app_id;References:ID;joinReferences:role_id" json:"roles,omitempty"`
	RolesPmn             []MgtPermission `gorm:"many2many:mgt_link_role_pmn;foreignKey:ID;joinForeignKey:app_id;References:ID;joinReferences:pmn_id" json:"role_pmn,omitempty"`       // 角色-权限按应用（树形）
	DepartmentsPmn       []MgtPermission `gorm:"many2many:mgt_link_dpt_pmn;foreignKey:ID;joinForeignKey:app_id;References:ID;joinReferences:pmn_id" json:"departments_pmn,omitempty"` // 部门-权限按应用（树形）
}

// func init() {
// 	database.AppendMigrate(&Application{})
// }

func (table *MgtApplication) WorkspaceToId() (uint, error) {
	if err := DBService.Where("workspace = ?", table.Workspace).First(table).Error; err != nil {
		return 0, err
	}
	return table.ID, nil
}
