package domain

import "time"

const (
	StatusEnable  int64 = 1
	StatusDisable int64 = 2
)
const (
	SuperAdministrator      = 1
	EnterpriseAdministrator = 10
	NormalUser              = 100
)

// UserType
const (
	Manager = 1
	Partner = 2
	Guest   = 3
)

// 用户实体
type User struct {
	// 用户id
	UserId int64 `json:"userId"`
	// 1.高管 2.合伙人 4:游客
	UserType int `json:"userType"`
	// 用户权限
	Permissions []int `json:"permissions"`
	// 公司Id
	CompanyId int64 `json:"companyId"`
	// 用户信息
	UserInfo *UserInfo `json:"userInfo"`
	// 合伙人信息
	PartnerInfo *PartnerInfo `json:"partnerInfo"`
	// 状态 1正常 2禁用
	Status int64 `json:"status"`
	// 管理员类型 1.超级管理员  10:企业管理员 100:普通用户
	AdminType int `json:"adminType"`
	// 合伙人列表 用户类型为高管时有效
	AccessPartners []int64 `json:"accessPartners"`
	// 创建时间
	CreateAt time.Time `json:"createAt"`
	// 更新时间
	UpdateAt time.Time `json:"updateAt"`
	// 删除时间
	DeleteAt time.Time `json:"deleteAt"`
}

type UserRepository interface {
	Save(user *User) (*User, error)
	Remove(user *User) (*User, error)
	FindOne(queryOptions map[string]interface{}) (*User, error)
	Find(queryOptions map[string]interface{}) (int64, []*User, error)
}

func (user *User) Identify() interface{} {
	if user.UserId == 0 {
		return nil
	}
	return user.UserId
}

func (user *User) Update(data map[string]interface{}) error {
	if userId, ok := data["userId"]; ok {
		user.UserId = userId.(int64)
	}
	if userType, ok := data["userType"]; ok {
		user.UserType = userType.(int)
	}
	if permissions, ok := data["permissions"]; ok {
		user.Permissions = permissions.([]int)
	}
	if companyId, ok := data["companyId"]; ok {
		user.CompanyId = companyId.(int64)
	}
	if isPrincipal, ok := data["isPrincipal"]; ok {
		user.UserInfo.IsPrincipal = isPrincipal.(bool)
	}
	if uid, ok := data["uid"]; ok {
		user.UserInfo.Uid = uid.(int64)
	}
	if userAccount, ok := data["userAccount"]; ok {
		user.UserInfo.UserAccount = userAccount.(string)
	}
	if userAvatarUrl, ok := data["userAvatarUrl"]; ok {
		user.UserInfo.UserAvatarUrl = userAvatarUrl.(string)
	}
	if userName, ok := data["userName"]; ok {
		user.UserInfo.UserName = userName.(string)
	}
	if email, ok := data["email"]; ok {
		user.UserInfo.Email = email.(string)
	}
	if gender, ok := data["gender"]; ok {
		user.UserInfo.Gender = gender.(int)
	}
	if entryTime, ok := data["entryTime"]; ok {
		user.UserInfo.EntryTime = entryTime.(time.Time)
	}
	if extension, ok := data["extension"]; ok {
		user.UserInfo.Extension = extension.(string)
	}
	if workplace, ok := data["workplace"]; ok {
		user.UserInfo.Workplace = workplace.(string)
	}
	if privateNumber, ok := data["privateNumber"]; ok {
		user.UserInfo.PrivateNumber = privateNumber.(string)
	}
	if jobNumber, ok := data["jobNumber"]; ok {
		user.UserInfo.JobNumber = jobNumber.(string)
	}
	if partnerAccount, ok := data["partnerAccount"]; ok {
		user.PartnerInfo.PartnerAccount = partnerAccount.(string)
	}
	if partnerName, ok := data["partnerName"]; ok {
		user.PartnerInfo.PartnerName = partnerName.(string)
	}
	if regionName, ok := data["regionName"]; ok {
		user.PartnerInfo.RegionInfo.RegionName = regionName.(string)
	}
	if status, ok := data["status"]; ok {
		user.PartnerInfo.Status = status.(int64)
	}
	return nil
}