fusenapi/model/gmodel/fs_user_logic.go

182 lines
5.2 KiB
Go
Raw Normal View History

2023-06-15 08:08:43 +00:00
package gmodel
import (
"context"
2023-08-24 10:28:01 +00:00
"fmt"
2023-07-27 11:47:52 +00:00
"fusenapi/utils/auth"
"time"
"gorm.io/gorm"
2023-06-15 08:08:43 +00:00
)
// TODO: 使用model的属性做你想做的
2023-06-15 09:51:06 +00:00
type UserBasicInfoForSave struct {
ID uint `gorm:"primary_key" json:"id"`
2023-06-16 11:26:48 +00:00
FirstName string `gorm:"-" json:"first_name"`
LastName string `gorm:"-" json:"last_name"`
Mobile string `gorm:"-" json:"mobile"`
Company string `gorm:"-" json:"company"`
IsOrderStatusEmail int64 `gorm:"-" json:"is_order_status_email"`
IsEmailAdvertisement int64 `gorm:"-" json:"is_email_advertisement"`
IsOrderStatusPhone int64 `gorm:"-" json:"is_order_status_phone"`
IsPhoneAdvertisement int64 `gorm:"-" json:"is_phone_advertisement"`
Type int64 `gorm:"-" json:"type"`
IsOpenRender int64 `gorm:"-" json:"is_open_render"`
IsLowRendering int64 `gorm:"-" json:"is_low_rendering"`
IsRemoveBg int64 `gorm:"-" json:"is_remove_bg"`
2023-06-15 08:08:43 +00:00
}
2023-06-15 09:51:06 +00:00
func (u *FsUserModel) FindUserByEmail(ctx context.Context, emailname string) (resp FsUser, err error) {
2023-07-10 09:38:44 +00:00
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`email` = ?", emailname).Take(&resp).Error
2023-06-15 08:08:43 +00:00
return resp, err
}
2023-06-15 09:51:06 +00:00
func (u *FsUserModel) FindUserById(ctx context.Context, Id int64) (resp FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`id` = ? and is_del = ?", Id, 0).Take(&resp).Error
return resp, err
}
func (u *FsUserModel) FindUserByGoogleId(ctx context.Context, Id int64) (resp *FsUser, err error) {
err = u.db.WithContext(ctx).Model(resp).Where("`google_id` = ? and is_del = ?", Id, 0).Take(resp).Error
2023-06-15 08:08:43 +00:00
return resp, err
}
2023-06-15 09:51:06 +00:00
2023-07-27 08:48:43 +00:00
func (u *FsUserModel) Transaction(ctx context.Context, fc func(tx *gorm.DB) error) (err error) {
return u.db.WithContext(ctx).Transaction(fc)
}
2023-08-24 10:28:01 +00:00
// 继承guest_id的资源表
func InheritGuestIdResource(tx *gorm.DB, userId, guestId int64) error {
var err error
if guestId != 0 {
// 继承guest_id的资源表
err = tx.Model(&FsResource{}).
Where("guest_id = ?", guestId).
UpdateColumn("user_id", userId).Error
if err != nil {
return err
}
err = tx.Model(&FsUserMaterial{}).
Where("guest_id = ?", guestId).
UpdateColumn("user_id", userId).Error
if err != nil {
return err
}
err = tx.Model(&FsUserInfo{}).
Where("guest_id = ?", guestId).
UpdateColumn("user_id", userId).Error
if err != nil {
return err
}
}
return fmt.Errorf("guest_id must not be 0")
}
2023-07-27 11:47:52 +00:00
// 谷歌平台的注册流程
func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
user := &FsUser{}
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
err := tx.Model(user).Where("email = ?", token.Email).Take(user).Error
if err != nil {
// 没有找到在数据库就创建注册
if err == gorm.ErrRecordNotFound {
2023-08-28 06:21:06 +00:00
createAt := time.Now().UTC().Unix()
2023-07-27 11:47:52 +00:00
user.Email = &token.Email
user.CreatedAt = &createAt
user.GoogleId = &token.Id
user.PasswordHash = &token.Password
err = tx.Model(user).Create(user).Error
if err != nil {
return err
}
if token.GuestId != 0 {
// 继承guest_id的资源表
2023-08-24 10:28:01 +00:00
return InheritGuestIdResource(tx, user.Id, token.GuestId)
2023-07-27 11:47:52 +00:00
}
return err
}
return err
}
// 如果已经存在,把谷歌id 加入到用户信息里
user.GoogleId = &token.Id
return tx.Model(user).Update("google_id", user).Error
})
if err != nil {
return nil, err
}
return user, nil
}
2023-08-24 10:28:01 +00:00
// 自平台的注册流程
func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
user := &FsUser{}
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
err := tx.Model(user).Where("email = ?", token.Email).Take(user).Error
if err != nil {
// 没有找到在数据库就创建注册
if err == gorm.ErrRecordNotFound {
2023-08-28 06:21:06 +00:00
createAt := time.Now().UTC().Unix()
2023-08-24 10:28:01 +00:00
user.Email = &token.Email
user.CreatedAt = &createAt
user.PasswordHash = &token.Password
err = tx.Model(user).Create(user).Error
if err != nil {
return err
}
if token.GuestId != 0 {
// 继承guest_id的资源表
return InheritGuestIdResource(tx, user.Id, token.GuestId)
}
return err
}
return err
}
if err == nil {
return fmt.Errorf("the email had registered")
}
return err
})
if err != nil {
return nil, err
}
return user, nil
}
2023-06-15 09:51:06 +00:00
func (u *FsUserModel) UpdateUserBasicInfoById(ctx context.Context, Id int64, user *UserBasicInfoForSave) (err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).
Where("`id` = ? and is_del = ? and status = ?", Id, 0, 1).
Updates(map[string]interface{}{
"first_name": user.FirstName,
"last_name": user.LastName,
"mobile": user.Mobile,
"company": user.Company,
"is_order_status_email": user.IsOrderStatusEmail,
"is_email_advertisement": user.IsEmailAdvertisement,
"is_order_status_phone": user.IsOrderStatusPhone,
"is_phone_advertisement": user.IsPhoneAdvertisement,
"type": user.Type,
"is_open_render": user.IsOpenRender,
"is_low_rendering": user.IsLowRendering,
"is_remove_bg": user.IsRemoveBg,
}).Error
return err
}