@@ -48,15 +48,13 @@ router.get('/', async (ctx) => {
48
48
...rest ,
49
49
} ) ;
50
50
51
- const created = data ?. created || 0 ;
52
- // 用户未评价记为系统默认好评
53
- evaluationStats . likeCount = created - evaluationStats . dislikeCount ;
51
+ const evaluationSum = evaluationStats . likeCount + evaluationStats . dislikeCount ;
54
52
55
53
ctx . body = {
56
54
...data ,
57
55
...evaluationStats ,
58
- likeRate : created && evaluationStats . likeCount / created ,
59
- dislikeRate : created && evaluationStats . dislikeCount / created ,
56
+ likeRate : evaluationSum && evaluationStats . likeCount / evaluationSum ,
57
+ dislikeRate : evaluationSum && evaluationStats . dislikeCount / evaluationSum ,
60
58
} ;
61
59
} ) ;
62
60
@@ -290,37 +288,40 @@ async function getEvaluationStats(
290
288
) : Promise < EvaluationCounts | EvaluationStats [ ] > {
291
289
const { categoryIds, count, customerServiceIds, from, to, bySelection } = options ;
292
290
291
+ const where = ( ...clauses : string [ ] ) => {
292
+ const clause = clauses . filter ( Boolean ) . join ( ' AND ' ) ;
293
+ return clause ? `WHERE ${ clause } ` : '' ;
294
+ } ;
295
+
293
296
const sql = `
294
297
SELECT
295
298
count(DISTINCT t.objectId) AS count,
296
299
${ ! count && bySelection ? 'selection,' : '' }
297
300
${ categoryIds ? 'categoryId,' : '' }
298
301
${ customerServiceIds ? 'customerServiceId,' : '' }
299
- visitParamExtractUInt (t.evaluation, 'star') AS star
302
+ if (t.evaluation != '', JSONExtractUInt(t.evaluation, 'star'), 1 ) AS star
300
303
FROM Ticket AS t
301
304
${
302
305
! count && bySelection
303
306
? `LEFT ARRAY JOIN
304
307
JSONExtract(t.evaluation, 'selections', 'Array(String)') AS selection`
305
308
: ''
306
309
}
307
- WHERE t.evaluation != ''
308
- ${
310
+ ${ where (
311
+ count ? '' : `t.evaluation != ''` ,
309
312
categoryIds && Array . isArray ( categoryIds )
310
- ? `AND arrayExists(x -> x = visitParamExtractString(t.category, 'objectId'), [${ categoryIds
313
+ ? `arrayExists(x -> x = visitParamExtractString(t.category, 'objectId'), [${ categoryIds
311
314
. map ( ( id ) => escape ( id ) )
312
315
. join ( ',' ) } ])`
313
- : ''
314
- }
315
- ${
316
+ : '' ,
316
317
customerServiceIds && Array . isArray ( customerServiceIds )
317
- ? `AND arrayExists(x -> x = t.\`assignee.objectId\`, [${ customerServiceIds
318
+ ? `arrayExists(x -> x = t.\`assignee.objectId\`, [${ customerServiceIds
318
319
. map ( ( id ) => escape ( id ) )
319
320
. join ( ',' ) } ])`
320
- : ''
321
- }
322
- ${ from ? `AND t.createdAt > = parseDateTimeBestEffortOrNull(${ escape ( from ) } )` : '' }
323
- ${ to ? `AND t.createdAt <= parseDateTimeBestEffortOrNull( ${ escape ( to ) } )` : '' }
321
+ : '' ,
322
+ from ? `t.createdAt >= parseDateTimeBestEffortOrNull( ${ escape ( from ) } )` : '' ,
323
+ to ? `t.createdAt < = parseDateTimeBestEffortOrNull(${ escape ( to ) } )` : ''
324
+ ) }
324
325
GROUP BY
325
326
${ ! count && bySelection ? 'selection,' : '' }
326
327
${ categoryIds ? "visitParamExtractString(t.category, 'objectId') AS categoryId," : '' }
0 commit comments