package domain

import "time"

// 角色类型  1.普通角色  1024:超级管理员
const (
	RoleTypeNormal = 1
	RoleTypeAdmin  = 1024
)

// 角色 (base)
type Role struct {
	// 角色ID
	RoleId int64 `json:"roleId,omitempty"`
	// 企业id
	CompanyId int64 `json:"companyId,omitempty"`
	// 组织ID
	OrgId int64 `json:"orgId,omitempty"`
	// 角色类型  1.普通角色  1024:超级管理员
	RoleType int `json:"roleType,omitempty"`
	// 角色名称
	RoleName string `json:"roleName,omitempty"`
	// 有权限的菜单
	AccessMenus []int64 `json:"accessMenus,omitempty"`
	// 描述
	Desc int64 `json:"desc,omitempty"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
}

type RoleRepository interface {
	Save(role *Role) (*Role, error)
	Remove(role *Role) (*Role, error)
	FindOne(queryOptions map[string]interface{}) (*Role, error)
	Find(queryOptions map[string]interface{}) (int64, []*Role, error)
}

func (role *Role) Identify() interface{} {
	if role.RoleId == 0 {
		return nil
	}
	return role.RoleId
}

func (role *Role) Update(data map[string]interface{}) error {
	if companyId, ok := data["companyId"]; ok {
		role.CompanyId = companyId.(int64)
	}
	if orgId, ok := data["orgId"]; ok {
		role.OrgId = orgId.(int64)
	}
	if roleType, ok := data["roleType"]; ok {
		role.RoleType = roleType.(int)
	}
	if roleName, ok := data["roleName"]; ok {
		role.RoleName = roleName.(string)
	}
	if accessMenus, ok := data["accessMenus"]; ok {
		role.AccessMenus = accessMenus.([]int64)
	}
	if desc, ok := data["desc"]; ok {
		role.Desc = desc.(int64)
	}
	if userName, ok := data["userName"]; ok {
		role.Ext.UserName = userName.(string)
	}
	if orgName, ok := data["orgName"]; ok {
		role.Ext.OrgName = orgName.(string)
	}
	if phone, ok := data["phone"]; ok {
		role.Ext.Phone = phone.(string)
	}
	if depName, ok := data["depName"]; ok {
		role.Ext.DepName = depName.(string)
	}
	if parentDepName, ok := data["parentDepName"]; ok {
		role.Ext.ParentDepName = parentDepName.(string)
	}
	if createdAt, ok := data["createdAt"]; ok {
		role.CreatedAt = createdAt.(time.Time)
	}
	if updatedAt, ok := data["updatedAt"]; ok {
		role.UpdatedAt = updatedAt.(time.Time)
	}
	return nil
}