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.
		
		
		
		
		
			
		
			
				
	
	
		
			81 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
package model
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormx"
 | 
						|
	"gorm.io/gorm"
 | 
						|
)
 | 
						|
 | 
						|
var _ RankPvpModel = (*customRankPvpModel)(nil)
 | 
						|
 | 
						|
const MaxRankN = 50
 | 
						|
 | 
						|
type (
 | 
						|
	// RankPvpModel is an interface to be customized, add more methods here,
 | 
						|
	// and implement the added methods in customRankPvpModel.
 | 
						|
	RankPvpModel interface {
 | 
						|
		rankPvpModel
 | 
						|
		RankListByType(ctx context.Context, rankType, topN int64) ([]RankPvpWithPlatformUser, error)
 | 
						|
		UpdateRank(ctx context.Context, rankType int64, data []RankPvp) error
 | 
						|
	}
 | 
						|
 | 
						|
	customRankPvpModel struct {
 | 
						|
		*defaultRankPvpModel
 | 
						|
	}
 | 
						|
 | 
						|
	RankPvpWithPlatformUser struct {
 | 
						|
		RankPvp
 | 
						|
		Platform string // 平台类型
 | 
						|
		PUname   string // 平台用户名
 | 
						|
		PAvatar  string // 平台用户头像地址
 | 
						|
	}
 | 
						|
)
 | 
						|
 | 
						|
// NewRankPvpModel returns a model for the database table.
 | 
						|
func NewRankPvpModel(conn *gorm.DB) RankPvpModel {
 | 
						|
	return &customRankPvpModel{
 | 
						|
		defaultRankPvpModel: newRankPvpModel(conn),
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func (m *customRankPvpModel) RankListByType(ctx context.Context, rankType, topN int64) ([]RankPvpWithPlatformUser, error) {
 | 
						|
	db := m.DB.WithContext(ctx)
 | 
						|
	limit := topN
 | 
						|
	if limit > MaxRankN {
 | 
						|
		limit = MaxRankN
 | 
						|
	}
 | 
						|
	var result []RankPvpWithPlatformUser
 | 
						|
	err := db.Model(&RankPvpWithPlatformUser{}).
 | 
						|
		Select(fmt.Sprintf("%s.*, UP.platform, UP.p_uname, UP.p_avatar", m.table)).
 | 
						|
		Joins(fmt.Sprintf("LEFT JOIN user_platform UP ON UP.user_id = %s.user_id", m.table)).
 | 
						|
		Where(fmt.Sprintf("%s.user_id > 0 AND rank_type = ?", m.table), rankType).
 | 
						|
		Limit(int(limit)).
 | 
						|
		Find(&result).Error
 | 
						|
	err = gormx.WrapSelectErr(err)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	return result, nil
 | 
						|
}
 | 
						|
 | 
						|
func (m *customRankPvpModel) UpdateRank(ctx context.Context, rankType int64, data []RankPvp) error {
 | 
						|
	db := m.DB.WithContext(ctx)
 | 
						|
	return db.Transaction(func(tx *gorm.DB) error {
 | 
						|
		var err error
 | 
						|
		// delete all by rank_type
 | 
						|
		if err = tx.
 | 
						|
			Table(m.table).
 | 
						|
			Where("rank_type = ?", rankType).
 | 
						|
			Delete(&RankPvp{}).Error; err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		// add all
 | 
						|
		if err = tx.
 | 
						|
			Create(data).Error; err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		return nil
 | 
						|
	})
 | 
						|
}
 |