ark-member/internal/logic/bind_user_info_logic.go

100 lines
2.5 KiB
Go

package logic
import (
"context"
"errors"
"member/internal/domain"
ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/model"
"time"
"github.com/go-sql-driver/mysql"
"member/gen_result/pb/member"
"member/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type BindUserInfoLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewBindUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindUserInfoLogic {
return &BindUserInfoLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
type createUserInfo struct {
Uid string `validate:"required"`
VerifyType int32 `validate:"required,oneof=0 1 2 3"`
AlarmType int32 `validate:"required,oneof=0 1 2"`
Status int32 `validate:"required,oneof=1 2 3 4 5 6"`
RoleId string `validate:"required"`
Language string `validate:"required"`
Currency string `validate:"required"`
NickName string
Gender int8
Birthday int64
}
// BindUserInfo 初次,綁定 User Info
func (l *BindUserInfoLogic) BindUserInfo(in *member.CreateUserInfoReq) (*member.Response, error) {
// 驗證資料
err := required.ValidateAll(l.svcCtx.Validate, &createUserInfo{
Uid: in.GetUid(),
VerifyType: int32(in.GetVerifyType()),
AlarmType: int32(in.GetAlarmType()),
Status: int32(in.GetStatus()),
RoleId: in.GetRoleId(),
Language: in.GetLanguage(),
Currency: in.GetCurrency(),
NickName: in.GetNickName(),
Gender: int8(in.GetGender()),
Birthday: in.GetBirthday(),
})
if err != nil {
return nil, ers.InvalidFormat(err.Error())
}
now := time.Now().UTC().Unix()
_, err = l.svcCtx.UserModel.Insert(l.ctx, &model.UserTable{
Uid: in.GetUid(),
VerifyType: int64(in.GetVerifyType()),
AlarmType: int64(in.GetAlarmType()),
Status: int64(in.GetStatus()),
RoleId: in.GetRoleId(),
Language: in.GetLanguage(),
Currency: in.GetCurrency(),
NickName: in.GetNickName(),
Gender: int64(in.GetGender()),
Birthday: in.GetBirthday(),
CreateTime: now,
UpdateTime: now,
})
if err != nil {
// 新增進去
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {
// 處理重複條目錯誤
return nil, ers.DBDuplicate(in.GetUid())
}
return nil, ers.DBError(err.Error())
}
return &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
}, nil
}