You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.6 KiB
Go

package model
import (
"context"
"fmt"
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
"gorm.io/gorm"
)
var _ UserPlatformModel = (*customUserPlatformModel)(nil)
type (
// UserPlatformModel is an interface to be customized, add more methods here,
// and implement the added methods in customUserPlatformModel.
UserPlatformModel interface {
userPlatformModel
// FindOneByPlatformAndPUid 查询平台用户
FindOneByPlatformAndPUid(ctx context.Context, tx *gorm.DB, platform, pUid string) (*UserPlatform, error)
// FindFullByPlatform 查询系统用户信息(包括平台用户)
FindFullByPlatform(ctx context.Context, tx *gorm.DB, platform, pUid string) (*FullUser, error)
// FindEmptyList 查询从未更新过平台信息的用户
FindEmptyList(ctx context.Context, num int64) ([]UserPlatform, error)
// FindUpdatableList 查询过期需要更新信息的用户
FindUpdatableList(ctx context.Context, duration int64, num int64) ([]UserPlatform, error)
// FindDisplayOneByUserId 查找用户信息,用于展示
FindDisplayOneByUserId(ctx context.Context, uid int64) (*UserForDisplay, error)
// FindDisplayOneByPlatform 查找用户信息,用于展示
FindDisplayOneByPlatform(ctx context.Context, tx *gorm.DB, platform string, pUid string) (*UserForDisplay, error)
// UpdateNobilityByPUid 更新贵族数据
UpdateNobilityByPUid(ctx context.Context, pUid int64, nobility int64) error
// FindUserIdByPlatform 根据平台与平台用户ID获取系统用户ID
FindUserIdByPlatform(ctx context.Context, platform, pUid string) (int64, error)
// UpdateNonZero 更新非0值
UpdateNonZero(ctx context.Context, tx *gorm.DB, data *UserPlatform) error
}
UserForDisplay struct {
Id int64 `gorm:"id;primaryKey"` // 主键ID
UserId int64 `gorm:"column:user_id"` // 用户ID
PUname string `gorm:"column:p_uname"` // 平台用户名
PAvatar string `gorm:"column:p_avatar"` // 平台用户头像地址
}
FullUser struct {
UserPlatform
UserNobility UserNobility `gorm:"foreignKey:UserId;references:UserId"`
UserIntegral UserIntegral `gorm:"foreignKey:UserId;references:UserId"`
}
customUserPlatformModel struct {
*defaultUserPlatformModel
}
)
// NewUserPlatformModel returns a model for the database table.
func NewUserPlatformModel(conn *gorm.DB) UserPlatformModel {
return &customUserPlatformModel{
defaultUserPlatformModel: newUserPlatformModel(conn),
}
}
func (m *customUserPlatformModel) FindOneByPlatformAndPUid(ctx context.Context, tx *gorm.DB, platform, pUid string) (*UserPlatform, error) {
db := gormx.WithTx(ctx, m.DB, tx)
var resp UserPlatform
err := db.Model(&UserPlatform{}).Where("`platform` = ? AND `p_uid` = ?", platform, pUid).Take(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return nil, err
}
return &resp, nil
}
func (m *customUserPlatformModel) FindFullByPlatform(ctx context.Context, tx *gorm.DB, platform, pUid string) (*FullUser, error) {
db := gormx.WithTx(ctx, m.DB, tx)
var resp FullUser
err := db.
Joins("UserNobility").
Joins("UserIntegral").
Where("platform = ? AND p_uid = ?", platform, pUid).
Take(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return nil, err
}
return &resp, nil
}
func (m *customUserPlatformModel) FindEmptyList(ctx context.Context, num int64) ([]UserPlatform, error) {
var resp []UserPlatform
err := gormx.WithTx(ctx, m.DB, nil).
Table(m.tableName()).
Where("JSON_LENGTH(`p_info`) = 0 LIMIT ?", num).
Find(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return nil, err
}
return resp, nil
}
func (m *customUserPlatformModel) FindUpdatableList(ctx context.Context, duration int64, num int64) ([]UserPlatform, error) {
var resp []UserPlatform
err := gormx.WithTx(ctx, m.DB, nil).
Table(m.tableName()).
Where("`update_time` < (NOW() - INTERVAL ? HOUR) LIMIT ?", duration, num).
Find(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return nil, err
}
return resp, nil
}
func (m *customUserPlatformModel) FindDisplayOneByUserId(ctx context.Context, uid int64) (*UserForDisplay, error) {
var resp UserForDisplay
err := gormx.WithTx(ctx, m.DB, nil).
Table(m.table).
Select("id, user_id, p_uname, p_avatar").
Where("user_id = ?", uid).
Take(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return nil, err
}
return &resp, nil
}
func (m *customUserPlatformModel) FindDisplayOneByPlatform(ctx context.Context, tx *gorm.DB, platform string, pUid string) (*UserForDisplay, error) {
var resp UserForDisplay
err := gormx.WithTx(ctx, m.DB, tx).
Table(m.table).
Select("id, user_id, p_uname, p_avatar").
Where("platform = ? AND p_uid = ?", platform, pUid).
Take(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return nil, err
}
return &resp, nil
}
func (m *customUserPlatformModel) UpdateNobilityByPUid(ctx context.Context, pUid int64, nobility int64) error {
return gormx.WithTx(ctx, m.DB, nil).
Model(&UserPlatform{}).
Where("p_uid", pUid).
Update("p_nobility_level", nobility).Error
}
func (m *customUserPlatformModel) FindUserIdByPlatform(ctx context.Context, platform, pUid string) (int64, error) {
var resp int64
err := gormx.WithTx(ctx, m.DB, nil).
Table(m.table).
Select(fmt.Sprintf("%s.user_id", m.table)).
Where("platform = ? AND p_uid = ?", platform, pUid).Take(&resp).Error
err = gormx.WrapSelectErr(err)
if err != nil {
return 0, err
}
return resp, nil
}
func (m *customUserPlatformModel) UpdateNonZero(ctx context.Context, tx *gorm.DB, data *UserPlatform) error {
result := gormx.WithTx(ctx, m.DB, tx).Model(&data).Updates(data)
return gormx.WrapUpdateErr(result.Error, result.RowsAffected)
}