2023-06-15 08:08:43 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-29 06:19:47 +00:00
|
|
|
"encoding/json"
|
2023-08-24 10:28:01 +00:00
|
|
|
"fmt"
|
2023-08-29 06:19:47 +00:00
|
|
|
|
2023-07-27 11:47:52 +00:00
|
|
|
"fusenapi/utils/auth"
|
2023-09-08 11:11:04 +00:00
|
|
|
"fusenapi/utils/fssql"
|
2023-07-27 11:47:52 +00:00
|
|
|
"time"
|
2023-07-20 02:13:18 +00:00
|
|
|
|
|
|
|
"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-09-05 07:23:43 +00:00
|
|
|
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`email` = ? and is_del = ?", emailname, 0).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) {
|
2023-07-20 02:13:18 +00:00
|
|
|
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`id` = ? and is_del = ?", Id, 0).Take(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:35:57 +00:00
|
|
|
func (u *FsUserModel) FindUserByGoogleId(ctx context.Context, Id string) (resp FsUser, err error) {
|
2023-08-30 03:07:47 +00:00
|
|
|
err = u.db.WithContext(ctx).Model(&FsUser{}).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-09-06 10:35:57 +00:00
|
|
|
func (u *FsUserModel) FindUserByFacebookId(ctx context.Context, Id string) (resp FsUser, err error) {
|
|
|
|
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`facebook_id` = ? and is_del = ?", Id, 0).Take(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2023-07-27 08:48:43 +00:00
|
|
|
func (u *FsUserModel) Transaction(ctx context.Context, fc func(tx *gorm.DB) error) (err error) {
|
2023-09-05 08:16:57 +00:00
|
|
|
return u.db.Model(&FsUser{}).WithContext(ctx).Transaction(fc)
|
2023-07-20 02:13:18 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 10:28:01 +00:00
|
|
|
// 继承guest_id的资源表
|
2023-08-29 06:19:47 +00:00
|
|
|
func InheritGuestIdResource(tx *gorm.DB, userId, guestId int64, afterDo func(txResouce *gorm.DB, txUserMaterial *gorm.DB, txUserInfo *gorm.DB) error) error {
|
2023-08-24 10:28:01 +00:00
|
|
|
var err error
|
2023-08-29 06:57:09 +00:00
|
|
|
txRes := tx.Model(&FsResource{})
|
|
|
|
txUserMaterial := tx.Model(&FsUserMaterial{})
|
|
|
|
txUserInfo := tx.Model(&FsUserInfo{})
|
2023-08-24 10:28:01 +00:00
|
|
|
if guestId != 0 {
|
|
|
|
// 继承guest_id的资源表
|
2023-08-29 06:19:47 +00:00
|
|
|
err = txRes.
|
2023-09-21 16:46:05 +00:00
|
|
|
Where("guest_id = ? and source != 'temp' and version != '0.0.0'", guestId).
|
2023-08-24 10:28:01 +00:00
|
|
|
UpdateColumn("user_id", userId).Error
|
2023-09-21 15:32:58 +00:00
|
|
|
|
2023-09-21 16:18:04 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2023-08-24 10:28:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-08-29 06:57:09 +00:00
|
|
|
|
2023-08-29 06:19:47 +00:00
|
|
|
err = txUserMaterial.
|
2023-09-21 15:47:34 +00:00
|
|
|
Where("guest_id = ? and module != 'clear' and module != 'temp'", guestId).
|
2023-08-24 10:28:01 +00:00
|
|
|
UpdateColumn("user_id", userId).Error
|
|
|
|
|
2023-08-29 06:36:53 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2023-08-24 10:28:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:19:47 +00:00
|
|
|
err = txUserInfo.
|
2023-09-21 16:03:01 +00:00
|
|
|
Where("guest_id = ? and module != 'clear' and module != 'temp'", guestId).
|
2023-08-24 10:28:01 +00:00
|
|
|
UpdateColumn("user_id", userId).Error
|
2023-09-21 15:32:58 +00:00
|
|
|
|
2023-09-21 16:46:05 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2023-08-24 10:28:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-08-29 06:57:09 +00:00
|
|
|
}
|
2023-08-29 06:19:47 +00:00
|
|
|
|
2023-08-29 06:57:09 +00:00
|
|
|
if afterDo != nil {
|
2023-08-29 08:15:34 +00:00
|
|
|
if txRes.Error != nil {
|
|
|
|
txRes = tx.Model(&FsResource{})
|
|
|
|
}
|
|
|
|
if txUserMaterial.Error != nil {
|
|
|
|
txUserMaterial = tx.Model(&FsUserMaterial{})
|
|
|
|
}
|
|
|
|
if txUserInfo.Error != nil {
|
|
|
|
txUserInfo = tx.Model(&FsUserInfo{})
|
|
|
|
}
|
2023-08-29 06:57:09 +00:00
|
|
|
return afterDo(txRes, txUserMaterial, txUserInfo)
|
2023-08-24 10:28:01 +00:00
|
|
|
}
|
2023-09-06 08:33:24 +00:00
|
|
|
return nil
|
2023-08-24 10:28:01 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-09-06 10:35:57 +00:00
|
|
|
googleId := token.Extend["oauth_id"].(string)
|
2023-09-06 04:17:40 +00:00
|
|
|
firstName := token.Extend["first_name"].(string)
|
|
|
|
lastName := token.Extend["last_name"].(string)
|
2023-07-27 11:47:52 +00:00
|
|
|
|
2023-08-30 03:04:54 +00:00
|
|
|
err := tx.Model(&FsUser{}).Where("email = ?", token.Email).Take(user).Error
|
2023-07-27 11:47:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// 没有找到在数据库就创建注册
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
2023-09-26 05:23:49 +00:00
|
|
|
createAt := time.Now().UTC()
|
2023-07-27 11:47:52 +00:00
|
|
|
user.Email = &token.Email
|
|
|
|
user.CreatedAt = &createAt
|
2023-08-29 06:19:47 +00:00
|
|
|
user.GoogleId = &googleId
|
2023-07-27 11:47:52 +00:00
|
|
|
user.PasswordHash = &token.Password
|
2023-09-06 04:17:40 +00:00
|
|
|
user.FirstName = &firstName
|
|
|
|
user.FirstName = &lastName
|
2023-08-30 03:04:54 +00:00
|
|
|
err = tx.Model(&FsUser{}).Create(user).Error
|
2023-07-27 11:47:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-29 06:57:09 +00:00
|
|
|
// 继承guest_id的资源表
|
|
|
|
return InheritGuestIdResource(tx, user.Id, token.GuestId, nil)
|
2023-07-27 11:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果已经存在,把谷歌id 加入到用户信息里
|
2023-08-29 06:19:47 +00:00
|
|
|
user.GoogleId = &googleId
|
2023-09-06 08:21:21 +00:00
|
|
|
return tx.Model(&FsUser{}).Where("id = ?", user.Id).Update("google_id", googleId).Error
|
2023-07-27 11:47:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 09:10:37 +00:00
|
|
|
// SubscriptionStatus 订阅状态
|
|
|
|
type SubscriptionStatus struct {
|
|
|
|
SubEmail bool `json:"all_emails"`
|
2023-09-07 10:39:31 +00:00
|
|
|
ItemMap *struct {
|
2023-09-06 09:10:37 +00:00
|
|
|
} `json:"item_map"`
|
|
|
|
}
|
|
|
|
|
2023-09-22 06:34:23 +00:00
|
|
|
type UserAddress struct {
|
|
|
|
}
|
|
|
|
|
2023-09-22 07:42:09 +00:00
|
|
|
type UserProfile struct {
|
|
|
|
ProfileBase UserProfileBase `json:"base"`
|
|
|
|
SubStatus SubscriptionStatus `json:"sub_status"`
|
|
|
|
}
|
|
|
|
|
2023-09-22 06:34:23 +00:00
|
|
|
// UserProfileBase 个人信息
|
|
|
|
type UserProfileBase struct {
|
2023-09-22 07:42:09 +00:00
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
Resetaurant string `json:"resetaurant"`
|
2023-08-29 06:19:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 10:28:01 +00:00
|
|
|
// 自平台的注册流程
|
2023-08-29 07:39:05 +00:00
|
|
|
func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (user *FsUser, err error) {
|
2023-08-24 10:28:01 +00:00
|
|
|
|
2023-08-29 07:17:51 +00:00
|
|
|
err = u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
2023-08-29 07:15:39 +00:00
|
|
|
|
2023-08-29 07:39:05 +00:00
|
|
|
user = &FsUser{}
|
2023-08-29 07:21:19 +00:00
|
|
|
var err error
|
2023-08-29 08:07:47 +00:00
|
|
|
|
2023-08-30 03:04:54 +00:00
|
|
|
err = tx.Model(&FsUser{}).Where("email = ?", token.Email).Take(user).Error
|
2023-08-29 06:54:53 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2023-08-29 06:44:59 +00:00
|
|
|
|
2023-08-29 06:54:53 +00:00
|
|
|
FirstName := token.Extend["first_name"].(string)
|
|
|
|
LastName := token.Extend["last_name"].(string)
|
|
|
|
Resetaurant := token.Extend["resetaurant"].(string)
|
2023-08-29 06:19:47 +00:00
|
|
|
|
2023-09-26 05:23:49 +00:00
|
|
|
createAt := time.Now().UTC()
|
2023-08-29 06:54:53 +00:00
|
|
|
user.Email = &token.Email
|
|
|
|
user.CreatedAt = &createAt
|
|
|
|
user.PasswordHash = &token.Password
|
|
|
|
user.FirstName = &FirstName
|
|
|
|
user.LastName = &LastName
|
2023-08-29 06:19:47 +00:00
|
|
|
|
2023-08-30 03:04:54 +00:00
|
|
|
err = tx.Model(&FsUser{}).Create(user).Error
|
2023-08-29 07:06:48 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2023-08-29 06:54:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:57:09 +00:00
|
|
|
// 继承guest_id的资源表
|
|
|
|
err = InheritGuestIdResource(tx, user.Id, token.GuestId, func(txResouce, txUserMaterial, txUserInfo *gorm.DB) error {
|
2023-09-22 07:42:09 +00:00
|
|
|
userProfileBase := UserProfileBase{
|
|
|
|
FirstName: FirstName,
|
|
|
|
LastName: LastName,
|
|
|
|
Resetaurant: Resetaurant,
|
2023-08-29 06:49:59 +00:00
|
|
|
}
|
2023-09-22 07:42:09 +00:00
|
|
|
|
|
|
|
userProfile := &UserProfile{
|
|
|
|
ProfileBase: userProfileBase,
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:57:09 +00:00
|
|
|
metadata, err := json.Marshal(userProfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-07 10:39:31 +00:00
|
|
|
|
2023-09-09 19:11:35 +00:00
|
|
|
now := time.Now().UTC()
|
2023-08-29 06:57:09 +00:00
|
|
|
uinfo := &FsUserInfo{
|
|
|
|
Module: FsString("profile"),
|
|
|
|
UserId: &user.Id,
|
|
|
|
GuestId: &token.GuestId,
|
|
|
|
Metadata: &metadata,
|
|
|
|
Ctime: &now,
|
|
|
|
Utime: &now,
|
|
|
|
}
|
2023-08-29 07:21:19 +00:00
|
|
|
|
2023-09-25 07:58:33 +00:00
|
|
|
// logx.Error(metadata)
|
2023-09-22 07:52:20 +00:00
|
|
|
|
2023-09-21 15:39:15 +00:00
|
|
|
err = txUserInfo.Where("module = 'profile' and user_id = ?", *uinfo.UserId).Take(nil).Error
|
2023-09-08 02:52:51 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
2023-09-21 16:35:10 +00:00
|
|
|
err = tx.Model(&FsUserInfo{}).Create(uinfo).Error
|
2023-09-21 16:46:05 +00:00
|
|
|
// logx.Info(err, "*uinfo.UserId:", *uinfo.UserId, " ", uinfo.Id)
|
2023-09-08 02:52:51 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-09 19:09:48 +00:00
|
|
|
err = fssql.MetadataModulePATCH(txUserInfo, "profile", FsUserInfo{}, metadata, "user_id = ?", *uinfo.UserId)
|
2023-09-08 03:08:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-07 10:39:31 +00:00
|
|
|
}
|
2023-08-29 07:21:19 +00:00
|
|
|
return err
|
2023-08-29 06:57:09 +00:00
|
|
|
})
|
2023-08-29 06:49:59 +00:00
|
|
|
|
2023-08-29 06:57:09 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return nil
|
2023-08-24 10:28:01 +00:00
|
|
|
}
|
2023-09-08 02:52:51 +00:00
|
|
|
|
2023-08-29 06:57:09 +00:00
|
|
|
return err
|
2023-08-24 10:28:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 06:54:53 +00:00
|
|
|
return fmt.Errorf("the email had registered")
|
2023-08-24 10:28:01 +00:00
|
|
|
})
|
|
|
|
|
2023-08-29 06:40:08 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2023-08-24 10:28:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-29 07:39:05 +00:00
|
|
|
return
|
2023-08-24 10:28:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2023-08-29 10:06:39 +00:00
|
|
|
|
|
|
|
func (u *FsUserModel) DebugAuthDelete(ctx context.Context, email string) (err error) {
|
|
|
|
|
|
|
|
err = u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
user := &FsUser{}
|
|
|
|
|
2023-08-30 03:04:54 +00:00
|
|
|
txUser := tx.Model(&FsUser{})
|
2023-08-29 10:06:39 +00:00
|
|
|
err = txUser.Where("email = ?", email).Take(user).Error
|
|
|
|
if err == nil {
|
|
|
|
err = txUser.Where("email = ?", email).Delete(user).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txRes := tx.Model(&FsResource{})
|
|
|
|
txUserMaterial := tx.Model(&FsUserMaterial{})
|
|
|
|
txUserInfo := tx.Model(&FsUserInfo{})
|
|
|
|
|
|
|
|
err = txRes.
|
2023-09-14 06:59:44 +00:00
|
|
|
Where("user_id = ?", user.Id).
|
|
|
|
Update("version", "0.0.0").Error
|
2023-08-29 10:06:39 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txUserMaterial.
|
|
|
|
Where("user_id = ?", user.Id).
|
2023-09-14 06:59:44 +00:00
|
|
|
Update("module", "clear").Error
|
2023-08-29 10:06:39 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txUserInfo.
|
|
|
|
Where("user_id = ?", user.Id).
|
|
|
|
Delete(&FsResource{}).Error
|
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 02:10:46 +00:00
|
|
|
return err
|
2023-08-29 10:06:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|