fusenapi/model/gmodel/fs_user_info_logic.go
laodaming aa1ab9937a fix
2023-10-17 18:16:11 +08:00

90 lines
2.3 KiB
Go

package gmodel
// TODO: 使用model的属性做你想做的
import (
"context"
"encoding/json"
"fmt"
"fusenapi/utils/fssql"
"fusenapi/utils/handlers"
"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
}
func (m *FsUserInfoModel) MergeMetadata(userId int64, meta any) error {
return fssql.MetadataModulePATCH(m.db, "profile", FsUserInfo{}, meta, "user_id = ?", userId)
}
func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId int64) (map[string]any, error) {
var baseinfo map[string]any
tname := fssql.GetGormTableName(m.db, FsUserInfo{})
if pkey == "." {
pkey = ""
} else {
pkey = "." + pkey
}
rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$%s') as query from %s where user_id = ? and module = 'profile' order by ctime DESC limit 1", pkey, tname)
err := m.db.Raw(rawsql, userId).Take(&baseinfo).Error
if err != nil {
return nil, err
}
v, ok := baseinfo["query"].(string)
if !ok {
return nil, nil
}
var info map[string]any
err = json.Unmarshal([]byte(v), &info)
if err != nil {
return nil, err
}
return info, nil
}
func (m *FsUserInfoModel) FindOneByUser(ctx context.Context, userId, guestId int64, module string) (resp *FsUserInfo, err error) {
if userId > 0 {
err = m.db.WithContext(ctx).Model(&FsUserInfo{}).Where("user_id = ? and module = ?", userId, module).Take(&resp).Error
} else {
err = m.db.WithContext(ctx).Model(&FsUserInfo{}).Where("user_id = ? and guest_id = ?", userId, guestId).Take(&resp).Error
}
return resp, err
}