package model import ( "context" "fmt" "git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc" "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 // Transaction 开启事务,传入方法即可 Transaction(ctx context.Context, tx *gorm.DB, fn func(tx *gorm.DB) error) error // InsertTx 事务插入 InsertTx(ctx context.Context, tx *gorm.DB, data *UserPlatform) error // 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) // FindOneForRankByUserId 查找用户信息 用于排行榜展示 FindOneForRankByUserId(ctx context.Context, uid int64) (*UserPlatformForRank, error) // UpdateNobilityByPUid 更新贵族数据 UpdateNobilityByPUid(ctx context.Context, pUid int64, nobility int64) error // FindUserIdByPlatform 根据平台与平台用户ID获取系统用户ID FindUserIdByPlatform(ctx context.Context, platform, pUid string) (int64, error) } UserPlatformForRank struct { UserId int64 // 用户ID PUname string // 平台用户名 PAvatar string // 平台用户头像地址 } 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) Transaction(ctx context.Context, tx *gorm.DB, fn func(tx *gorm.DB) error) error { return withTx(ctx, m.conn, tx).Transaction(func(tx *gorm.DB) error { return fn(tx) }) } func (m *customUserPlatformModel) InsertTx(ctx context.Context, tx *gorm.DB, data *UserPlatform) error { return withTx(ctx, m.conn, tx).Create(data).Error } func (m *customUserPlatformModel) FindOneByPlatformAndPUid(ctx context.Context, tx *gorm.DB, platform, pUid string) (*UserPlatform, error) { db := withTx(ctx, m.conn, tx) var resp UserPlatform err := db.Model(&UserPlatform{}).Where("`platform` = ? AND `p_uid` = ?", platform, pUid).Take(&resp).Error switch err { case nil: return &resp, nil case gormc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *customUserPlatformModel) FindFullByPlatform(ctx context.Context, tx *gorm.DB, platform, pUid string) (*FullUser, error) { db := withTx(ctx, m.conn, tx) var resp FullUser err := db. Joins("UserNobility"). Joins("UserIntegral"). Where("platform = ? AND p_uid = ?", platform, pUid). Take(&resp).Error switch err { case nil: return &resp, nil case gormc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *customUserPlatformModel) FindEmptyList(ctx context.Context, num int64) ([]UserPlatform, error) { var resp []UserPlatform err := m.conn.WithContext(ctx). Table(m.tableName()). Where("JSON_LENGTH(`p_info`) = 0 LIMIT ?", num). Find(&resp).Error switch err { case nil: return resp, nil case gormc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *customUserPlatformModel) FindUpdatableList(ctx context.Context, duration int64, num int64) ([]UserPlatform, error) { var resp []UserPlatform err := m.conn.WithContext(ctx). Table(m.tableName()). Where("`update_time` < (NOW() - INTERVAL ? HOUR) LIMIT ?", duration, num). Find(&resp).Error switch err { case nil: return resp, nil case gormc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *customUserPlatformModel) FindOneForRankByUserId(ctx context.Context, uid int64) (*UserPlatformForRank, error) { var resp UserPlatformForRank err := m.conn.WithContext(ctx). Table(m.table). Select(fmt.Sprintf("%s.user_id, %s.p_uname, %s.p_avatar", m.table, m.table, m.table)). Where("user_id = ?", uid). Take(&resp).Error switch err { case nil: return &resp, nil case gormc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *customUserPlatformModel) UpdateNobilityByPUid(ctx context.Context, pUid int64, nobility int64) error { return m.conn.WithContext(ctx). 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 := m.conn.WithContext(ctx). Table(m.table). Select(fmt.Sprintf("%s.user_id", m.table)). Where("platform = ? AND p_uid = ?", platform, pUid).Take(&resp).Error switch err { case nil: return resp, nil case gormc.ErrNotFound: return 0, ErrNotFound default: return 0, err } }