package model import ( "context" "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 // FindOneByPlatformAndPUid 查询平台用户 FindOneByPlatformAndPUid(ctx context.Context, platform, pUid string) (*UserPlatform, error) FindUpdatableList(ctx context.Context, duration int64, num int64) ([]UserPlatform, error) } 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, platform, pUid string) (*UserPlatform, error) { var resp UserPlatform err := m.conn.WithContext(ctx).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) FindUpdatableList(ctx context.Context, duration int64, num int64) ([]UserPlatform, error) { var resp []UserPlatform err := m.conn.WithContext(ctx). Where("`p_info = ? AND `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 } }