mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-10-13 01:28:15 +03:00
[-] QueryDSL
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package aquadx.sega.maimai2.handler
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory
|
||||
import aquadx.sega.maimai2.model.Mai2UserPlaylogRepo
|
||||
import ext.logger
|
||||
import ext.thread
|
||||
import aquadx.sega.allnet.TokenChecker
|
||||
import aquadx.sega.general.BaseHandler
|
||||
import aquadx.sega.maimai2.model.userdata.QMai2UserPlaylog
|
||||
import org.springframework.data.domain.PageRequest
|
||||
import org.springframework.scheduling.annotation.Scheduled
|
||||
import org.springframework.stereotype.Component
|
||||
import java.time.LocalDateTime
|
||||
@@ -17,7 +17,7 @@ import kotlin.concurrent.Volatile
|
||||
*/
|
||||
@Component("Maimai2GetGameRankingHandler")
|
||||
class GetGameRankingHandler(
|
||||
private val queryFactory: JPAQueryFactory
|
||||
private val playlogRepo: Mai2UserPlaylogRepo
|
||||
) : BaseHandler {
|
||||
private data class MusicRankingItem(val musicId: Int, val weight: Long)
|
||||
|
||||
@@ -37,18 +37,8 @@ class GetGameRankingHandler(
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
val queryAfterStr = queryAfter.format(formatter)
|
||||
|
||||
val qPlaylog = QMai2UserPlaylog.mai2UserPlaylog
|
||||
val cMusicId = qPlaylog.musicId
|
||||
val cUserCount = qPlaylog.user.id.countDistinct()
|
||||
musicRankingCache = queryFactory
|
||||
.select(cMusicId, cUserCount)
|
||||
.from(qPlaylog)
|
||||
.where(qPlaylog.userPlayDate.stringValue().goe(queryAfterStr))
|
||||
.groupBy(cMusicId)
|
||||
.orderBy(cUserCount.desc())
|
||||
.limit(QUERY_LIMIT)
|
||||
.fetch()
|
||||
.map { MusicRankingItem(it.get(cMusicId)!!, it.get(cUserCount)!!) }
|
||||
musicRankingCache = playlogRepo.getMusicRanking(queryAfterStr, PageRequest.of(0, QUERY_LIMIT.toInt()))
|
||||
.map { MusicRankingItem(it.musicId, it.userCount) }
|
||||
|
||||
log.info("Refreshed music ranking cache: ${musicRankingCache.size} items")
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.data.domain.Page
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Modifying
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.NoRepositoryBean
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
@@ -105,6 +106,14 @@ interface Mai2UserPlaylogRepo : GenericPlaylogRepo<Mai2UserPlaylog>, Mai2UserLin
|
||||
userPlayDate: String
|
||||
): MutableList<Mai2UserPlaylog>
|
||||
fun findByUserAndUserPlayDateAfter(user: Mai2UserDetail, userPlayDate: String): List<Mai2UserPlaylog>
|
||||
|
||||
@Query("select p.musicId as musicId, count(distinct p.user.id) as userCount from Mai2UserPlaylog p where p.userPlayDate >= :date group by p.musicId order by userCount desc")
|
||||
fun getMusicRanking(date: String, pageable: Pageable): List<Mai2MusicRanking>
|
||||
}
|
||||
|
||||
interface Mai2MusicRanking {
|
||||
val musicId: Int
|
||||
val userCount: Long
|
||||
}
|
||||
|
||||
interface Mai2UserPrintDetailRepo : JpaRepository<Mai2UserPrintDetail, Long>
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package aquadx.spring
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory
|
||||
import jakarta.persistence.EntityManager
|
||||
import jakarta.persistence.PersistenceContext
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
class QuerydslConfig {
|
||||
@PersistenceContext
|
||||
private lateinit var entityManager: EntityManager
|
||||
|
||||
@Bean
|
||||
fun jpaQueryFactory(): JPAQueryFactory {
|
||||
return JPAQueryFactory(entityManager)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user