user_base.go 2.2 KB
package domain

import "time"

// 用户基础
type UserBase struct {
	// 用户基础数据id
	UserBaseId int64 `json:"userBaseId,omitempty"`
	// 用户信息
	UserInfo *UserInfo `json:"userInfo,omitempty"`
	// 手机号码
	Account string `json:"phone,omitempty"`
	// 密码
	Password string `json:"password,omitempty"`
	// IM信息
	Im *Im `json:"im,omitempty"`
	// 关联的用户 (冗余)
	RelatedUsers []int64 `json:"relatedUsers,omitempty"`
	// 账号状态 1:正常 2.禁用 3:注销
	Status int `json:"status,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
}

type UserBaseRepository interface {
	Save(userBase *UserBase) (*UserBase, error)
	Remove(userBase *UserBase) (*UserBase, error)
	FindOne(queryOptions map[string]interface{}) (*UserBase, error)
	Find(queryOptions map[string]interface{}) (int64, []*UserBase, error)
}

func (userBase *UserBase) Identify() interface{} {
	if userBase.UserBaseId == 0 {
		return nil
	}
	return userBase.UserBaseId
}

func (userBase *UserBase) Update(data map[string]interface{}) error {
	if userName, ok := data["userName"]; ok {
		userBase.UserInfo.UserName = userName.(string)
	}
	if phone, ok := data["phone"]; ok {
		userBase.UserInfo.Phone = phone.(string)
	}
	if avatar, ok := data["avatar"]; ok {
		userBase.UserInfo.Avatar = avatar.(string)
	}
	if email, ok := data["email"]; ok {
		userBase.UserInfo.Email = email.(string)
	}
	if account, ok := data["account"]; ok {
		userBase.Account = account.(string)
	}
	if password, ok := data["password"]; ok {
		userBase.Password = password.(string)
	}
	if accid, ok := data["accid"]; ok {
		userBase.Im.Accid = accid.(string)
	}
	if imToken, ok := data["imToken"]; ok {
		userBase.Im.ImToken = imToken.(string)
	}
	if csAccountId, ok := data["csAccountId"]; ok {
		userBase.Im.CsAccountId = csAccountId.(string)
	}
	if relatedUser, ok := data["relatedUser"]; ok {
		userBase.RelatedUsers = relatedUser.([]int64)
	}
	if status, ok := data["status"]; ok {
		userBase.Status = status.(int)
	}
	if createdAt, ok := data["createdAt"]; ok {
		userBase.CreatedAt = createdAt.(time.Time)
	}
	if updatedAt, ok := data["updatedAt"]; ok {
		userBase.UpdatedAt = updatedAt.(time.Time)
	}
	return nil
}