2023-08-14 07:07:12 +00:00
package gmodel
2023-08-14 06:40:50 +00:00
2023-08-14 07:08:24 +00:00
// TODO: 使用model的属性做你想做的
2023-08-14 06:40:50 +00:00
import (
"context"
2023-09-26 09:16:10 +00:00
"encoding/json"
"fmt"
2023-09-26 07:02:09 +00:00
"fusenapi/utils/fssql"
2023-08-14 06:40:50 +00:00
"fusenapi/utils/handlers"
2023-11-23 09:46:11 +00:00
"log"
2023-08-14 06:40:50 +00:00
"gorm.io/gorm"
)
// TODO: 使用model的属性做你想做的
func ( m * FsUserInfoModel ) BuilderDB ( ctx context . Context , selectData [ ] string ) * gorm . DB {
if selectData != nil {
return m . db . WithContext ( ctx ) . Select ( selectData )
} else {
return m . db . WithContext ( ctx ) . Select ( "*" )
}
}
func ( m * FsUserInfoModel ) FindOne ( gormDB * gorm . DB , filterMap map [ string ] string ) ( * FsUserInfo , error ) {
var resp FsUserInfo
if filterMap != nil {
gormDB = gormDB . Scopes ( handlers . FilterData ( filterMap ) )
}
result := gormDB . Limit ( 1 ) . Find ( & resp )
if result . Error != nil {
return nil , result . Error
} else {
return & resp , nil
}
}
func ( p * FsUserInfoModel ) CreateOrUpdate ( gormDB * gorm . DB , req * FsUserInfo ) ( resp * FsUserInfo , err error ) {
if req . Id > 0 {
err = gormDB . Save ( req ) . Error
} else {
err = gormDB . Create ( req ) . Error
}
return req , err
}
2023-09-26 07:02:09 +00:00
func ( m * FsUserInfoModel ) MergeMetadata ( userId int64 , meta any ) error {
2023-09-28 05:32:37 +00:00
return fssql . MetadataModulePATCH ( m . db , "profile" , FsUserInfo { } , meta , "user_id = ?" , userId )
2023-09-26 07:02:09 +00:00
}
2023-09-26 09:16:10 +00:00
2023-10-26 07:28:44 +00:00
func ( m * FsUserInfoModel ) getDefaultProfile ( ctx context . Context , tname string ) ( map [ string ] any , error ) {
2023-10-26 03:29:58 +00:00
var baseinfo map [ string ] any
condUser := "user_id = 0 and guest_id = 0"
2023-10-26 03:45:52 +00:00
rawsql := fmt . Sprintf ( "select JSON_EXTRACT(metadata,'$') as query from %s where %s and module = 'profile' order by ctime DESC limit 1" , tname , condUser )
2023-10-26 03:29:58 +00:00
err := m . db . WithContext ( ctx ) . Raw ( rawsql ) . Take ( & baseinfo ) . Error
if err != nil {
return nil , err
}
v , ok := baseinfo [ "query" ] . ( string )
if ! ok {
return nil , fmt . Errorf ( "default userinfo profile is not exists" )
}
var info map [ string ] any
err = json . Unmarshal ( [ ] byte ( v ) , & info )
if err != nil {
return nil , err
}
return info , nil
}
func ( m * FsUserInfoModel ) GetProfile ( ctx context . Context , pkey string , userId int64 , guestId int64 ) ( map [ string ] any , error ) {
2023-09-26 09:16:10 +00:00
var baseinfo map [ string ] any
tname := fssql . GetGormTableName ( m . db , FsUserInfo { } )
if pkey == "." {
pkey = ""
} else {
pkey = "." + pkey
}
2023-10-26 03:29:58 +00:00
var condUser string
if userId == 0 {
condUser = fmt . Sprintf ( "user_id = 0 and guest_id = %d" , guestId )
} else {
condUser = fmt . Sprintf ( "user_id = %d" , userId )
}
rawsql := fmt . Sprintf ( "select JSON_EXTRACT(metadata,'$%s') as query from %s where %s and module = 'profile' order by ctime DESC limit 1" , pkey , tname , condUser )
2023-10-26 03:43:19 +00:00
err := m . db . WithContext ( ctx ) . Raw ( rawsql ) . Take ( & baseinfo ) . Error
2023-09-26 09:16:10 +00:00
if err != nil {
return nil , err
}
v , ok := baseinfo [ "query" ] . ( string )
if ! ok {
2023-10-26 07:28:44 +00:00
return m . getDefaultProfile ( ctx , tname )
2023-09-26 09:16:10 +00:00
}
2023-11-23 09:50:12 +00:00
log . Println ( v )
2023-09-26 09:16:10 +00:00
var info map [ string ] any
err = json . Unmarshal ( [ ] byte ( v ) , & info )
if err != nil {
return nil , err
}
2023-10-26 03:29:58 +00:00
if len ( info ) == 0 {
2023-10-26 07:28:44 +00:00
return m . getDefaultProfile ( ctx , tname )
2023-10-26 03:29:58 +00:00
}
2023-11-17 03:03:19 +00:00
if _ , ok := info [ "logo_selected" ] ; ! ok {
defaultUserInfo , err := m . getDefaultProfile ( ctx , tname )
if err != nil {
return nil , err
}
info [ "logo_selected" ] = defaultUserInfo [ "logo_selected" ]
}
2023-09-26 09:16:10 +00:00
return info , nil
}
2023-10-26 07:28:44 +00:00
func ( m * FsUserInfoModel ) GetDefaultProfile ( ctx context . Context ) ( map [ string ] any , error ) {
tname := fssql . GetGormTableName ( m . db , FsUserInfo { } )
return m . getDefaultProfile ( ctx , tname )
}
2023-10-17 10:16:11 +00:00
func ( m * FsUserInfoModel ) FindOneByUser ( ctx context . Context , userId , guestId int64 , module string ) ( resp * FsUserInfo , err error ) {
2023-10-16 04:04:27 +00:00
if userId > 0 {
2023-10-17 10:16:11 +00:00
err = m . db . WithContext ( ctx ) . Model ( & FsUserInfo { } ) . Where ( "user_id = ? and module = ?" , userId , module ) . Take ( & resp ) . Error
2023-10-16 04:04:27 +00:00
} else {
err = m . db . WithContext ( ctx ) . Model ( & FsUserInfo { } ) . Where ( "user_id = ? and guest_id = ?" , userId , guestId ) . Take ( & resp ) . Error
}
2023-10-16 03:16:39 +00:00
return resp , err
}