package logic

import (
	sysModel "sundynix-micro-go/app/system/model"
	"sundynix-micro-go/app/system/rpc/system"
)

// convertUserToProto 将 model 转为 proto UserInfo
func convertUserToProto(u *sysModel.SundynixUser) *system.UserInfo {
	info := &system.UserInfo{
		Id:         u.ID,
		TenantId:   u.TenantID,
		ClientId:   u.ClientID,
		Name:       u.Name,
		Account:    u.Account,
		NickName:   u.NickName,
		Phone:      u.Phone,
		SessionKey: u.SessionKey,
		UnionId:    u.UnionID,
		OpenId:     u.OpenID,
		SaOpenId:   u.SaOpenID,
		AvatarId:   u.AvatarID,
		Gender:     int32(u.Gender),
		CreatedAt:  u.CreatedAt.Unix(),
		UpdatedAt:  u.UpdatedAt.Unix(),
	}
	if u.LastLoginAt != nil {
		info.LastLoginAt = u.LastLoginAt.Unix()
	}

	for _, r := range u.Roles {
		info.Roles = append(info.Roles, &system.RoleInfo{
			Id:   r.ID,
			Name: r.Name,
			Code: r.Code,
			Sort: int32(r.Sort),
		})
	}

	return info
}
