package users

import "time"

// 用户实体
type Users struct {
	// dcc
	Id int64 `json:"id"`
	// 名称
	Name string `json:"name"`
	// 手机号
	Phone string `json:"phone"`
	// 密码
	Passwd string `json:"passwd"`
	// 用户角色
	Roles []int64 `json:"roles"`
	// 状态 1-启用 0-禁用
	Status int `json:"status"`
	// 1:管理员  2:普通员工
	AdminType int `json:"adminType"`
	// 创建时间
	CreateTime time.Time `json:"createTime"`
	// 更新时间
	UpdateTime time.Time `json:"updateTime"`
}

type UsersRepository interface {
	Save(users *Users) (*Users, error)
	Remove(users *Users) (*Users, error)
	FindOne(queryOptions map[string]interface{}) (*Users, error)
	Find(queryOptions map[string]interface{}) (int64, []*Users, error)
}

func (users *Users) Identify() interface{} {
	if users.Id == 0 {
		return nil
	}
	return users.Id
}

func (users *Users) GetRoles() []int {
	var ret []int
	for _, v := range users.Roles {
		ret = append(ret, int(v))
	}
	return ret
}

func (users *Users) Update(data map[string]interface{}) error {
	if Id, ok := data["Id"]; ok {
		users.Id = Id.(int64)
	}
	if Name, ok := data["Name"]; ok {
		users.Name = Name.(string)
	}
	if Phone, ok := data["Phone"]; ok {
		users.Phone = Phone.(string)
	}
	if Passwd, ok := data["Passwd"]; ok {
		users.Passwd = Passwd.(string)
	}
	if Roles, ok := data["Roles"]; ok {
		users.Roles = Roles.([]int64)
	}
	if Status, ok := data["Status"]; ok {
		users.Status = Status.(int)
	}
	if AdminType, ok := data["AdminType"]; ok {
		users.AdminType = AdminType.(int)
	}
	if CreateTime, ok := data["CreateTime"]; ok {
		users.CreateTime = CreateTime.(time.Time)
	}
	if UpdateTime, ok := data["UpdateTime"]; ok {
		users.UpdateTime = UpdateTime.(time.Time)
	}
	return nil
}