package domain

import "time"

//用户是否可用状态:【1:正常】【 2:禁用】
const (
	userStatusUsable   int8 = 1
	userStatusUnusable int8 = 2
)

//用户是否是主管 :【1:是主管】【 2:不是主管】
const (
	userIsCharge    int8 = 1
	userIsNotCharge int8 = 2
)

//Users 企业平台的用户
type Users struct {
	Id             int64     //用户id
	CompanyId      int64     //公司id
	OpenId         int64     //统一用户中心
	Name           string    //用户名称
	Sex            int8      //性别:【0:未知】【1:男】【2:女】
	JobNum         string    //工号
	Phone          string    //手机号,同账号
	PrivatePhone   string    //私人手机号
	Email          string    //邮件
	ExtensionNum   string    //分机号
	EntryTime      time.Time //入职时间
	Workspace      string    //工作地
	Status         int8      //状态:【1:正常】【 2:禁用】
	Avatar         string    ///头像
	Remarks        string    //备注
	ChargeStatus   int8      //是否为当前公司主管 【1:是】【2:否】
	CreateAt       time.Time
	UpdateAt       time.Time
	Permission     []AdminPermissionBase //权限
	AccessPartners []Partner
}

//IsUsable 用户是否可用
func (u Users) IsUsable() bool {
	return u.Status == userStatusUsable
}

//IsCharge 用户是否是主管
func (u Users) IsCharge() bool {
	return u.ChargeStatus == userIsCharge
}

func (u Users) InCompany(companyid int64) bool {
	return u.CompanyId == companyid
}

func (u *Users) Update(m map[string]interface{}) error {
	if v, ok := m["CompanyId"]; ok {
		u.CompanyId = v.(int64)
	}
	if v, ok := m["OpenId"]; ok {
		u.OpenId = v.(int64)
	}
	if v, ok := m["Name"]; ok {
		u.Name = v.(string)
	}
	if v, ok := m["Sex"]; ok {
		u.Sex = v.(int8)
	}
	if v, ok := m["JobNum"]; ok {
		u.JobNum = v.(string)
	}
	if v, ok := m["Phone"]; ok {
		u.Phone = v.(string)
	}
	if v, ok := m["PrivatePhone"]; ok {
		u.PrivatePhone = v.(string)
	}
	if v, ok := m["Email"]; ok {
		u.Email = v.(string)
	}
	if v, ok := m["ExtensionNum"]; ok {
		u.ExtensionNum = v.(string)
	}
	if v, ok := m["EntryTime"]; ok {
		u.EntryTime = v.(time.Time)
	}
	if v, ok := m["Workspace"]; ok {
		u.Workspace = v.(string)
	}
	if v, ok := m["Status"]; ok {
		u.Status = v.(int8)
	}
	if v, ok := m["Avatar"]; ok {
		u.Avatar = v.(string)
	}
	if v, ok := m["Remarks"]; ok {
		u.Remarks = v.(string)
	}
	if v, ok := m["ChargeStatus"]; ok {
		u.ChargeStatus = v.(int8)
	}
	if v, ok := m["Permission"]; ok {
		u.Permission = v.([]AdminPermissionBase)
	}
	if v, ok := m["AccessPartners"]; ok {
		u.AccessPartners = v.([]Partner)
	}
	return nil
}

type UsersFindOneQuery struct {
	Id    int64
	Phone string
}

type UsersFindQuery struct {
	Ids    []int64
	Offset int
	Limit  int
}
type UsersRepository interface {
	Add(*Users) error
	Edit(*Users) error
	Remove([]int64) error
	FindOne(UsersFindOneQuery) (Users, error)
	Find(UsersFindQuery) (int, []Users, error)
}