@@ -3,7 +3,6 @@ import { prisma } from '~/prisma/index'
33import { getKv , setKv } from '~/lib/redis'
44import { PATCH_CACHE_DURATION } from '~/config/cache'
55import { roundOneDecimal } from '~/utils/rating/average'
6- import { KUN_GALGAME_RATING_RECOMMEND_CONST } from '~/constants/galgame'
76import type { Patch } from '~/types/api/patch'
87
98const CACHE_KEY = 'patch'
@@ -60,46 +59,9 @@ export const getPatchById = async (
6059return '未找到对应 Galgame'
6160}
6261
63- const [ ratingAgg , recommendGroups , overallGroups ] = await prisma . $transaction (
64- [
65- prisma . patch_rating . aggregate ( {
66- where :{ patch_id :patch . id } ,
67- _avg :{ overall :true } ,
68- _count :{ _all :true }
69- } ) ,
70- prisma . patch_rating . groupBy ( {
71- by :[ 'recommend' ] ,
72- where :{ patch_id :patch . id } ,
73- _count :{ _all :true } ,
74- orderBy :{ recommend :'asc' }
75- } ) ,
76- prisma . patch_rating . groupBy ( {
77- by :[ 'overall' ] ,
78- where :{ patch_id :patch . id } ,
79- _count :{ _all :true } ,
80- orderBy :{ overall :'asc' }
81- } )
82- ]
83- )
84-
85- const recommendCountMap :Record < string , number > = { }
86- for ( const k of KUN_GALGAME_RATING_RECOMMEND_CONST ) {
87- recommendCountMap [ k ] = 0
88- }
89- for ( const g of recommendGroups ) {
90- const c = ( g as any ) . _count ?. _all ?? 0
91- recommendCountMap [ g . recommend ] = c
92- }
93-
94- const scoreCountMap :Record < number , number > = { }
95- for ( let s = 1 ; s <= 10 ; s ++ ) scoreCountMap [ s ] = 0
96- for ( const g of overallGroups as any [ ] ) {
97- const score = g . overall as number
98- const c = g . _count ?. _all ?? 0
99- if ( typeof score === 'number' && score >= 1 && score <= 10 ) {
100- scoreCountMap [ score ] = c
101- }
102- }
62+ const stat = await prisma . patch_rating_stat . findUnique ( {
63+ where :{ patch_id :patch . id }
64+ } )
10365
10466const response :Patch = {
10567id :patch . id ,
@@ -118,21 +80,45 @@ export const getPatchById = async (
11880alias :patch . alias . map ( ( a ) => a . name ) ,
11981isFavorite :patch . favorite_folder . length > 0 ,
12082contentLimit :patch . content_limit ,
121- ratingSummary :{
122- average :roundOneDecimal ( ratingAgg . _avg . overall ) ,
123- count :ratingAgg . _count . _all ,
124- histogram :Array . from ( { length :10 } , ( _ , i ) => ( {
125- score :i + 1 ,
126- count :scoreCountMap [ i + 1 ]
127- } ) ) ,
128- recommend :{
129- strong_no :recommendCountMap . strong_no ,
130- no :recommendCountMap . no ,
131- neutral :recommendCountMap . neutral ,
132- yes :recommendCountMap . yes ,
133- strong_yes :recommendCountMap . strong_yes
134- }
135- } ,
83+ ratingSummary :stat
84+ ?{
85+ average :roundOneDecimal ( stat . avg_overall ) ,
86+ count :stat . count ,
87+ histogram :[
88+ { score :1 , count :stat . o1 } ,
89+ { score :2 , count :stat . o2 } ,
90+ { score :3 , count :stat . o3 } ,
91+ { score :4 , count :stat . o4 } ,
92+ { score :5 , count :stat . o5 } ,
93+ { score :6 , count :stat . o6 } ,
94+ { score :7 , count :stat . o7 } ,
95+ { score :8 , count :stat . o8 } ,
96+ { score :9 , count :stat . o9 } ,
97+ { score :10 , count :stat . o10 }
98+ ] ,
99+ recommend :{
100+ strong_no :stat . rec_strong_no ,
101+ no :stat . rec_no ,
102+ neutral :stat . rec_neutral ,
103+ yes :stat . rec_yes ,
104+ strong_yes :stat . rec_strong_yes
105+ }
106+ }
107+ :{
108+ average :0 ,
109+ count :0 ,
110+ histogram :Array . from ( { length :10 } , ( _ , i ) => ( {
111+ score :i + 1 ,
112+ count :0
113+ } ) ) ,
114+ recommend :{
115+ strong_no :0 ,
116+ no :0 ,
117+ neutral :0 ,
118+ yes :0 ,
119+ strong_yes :0
120+ }
121+ } ,
136122user :{
137123id :patch . user . id ,
138124name :patch . user . name ,