Skip to content

Commit 5795854

Browse files
committed
feat(next/api): one evaluation stats data source
1 parent 09fc0a8 commit 5795854

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

next/api/src/router/ticket-stats.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ router.get('/', async (ctx) => {
4848
...rest,
4949
});
5050

51-
const created = data?.created || 0;
52-
// 用户未评价记为系统默认好评
53-
evaluationStats.likeCount = created - evaluationStats.dislikeCount;
51+
const evaluationSum = evaluationStats.likeCount + evaluationStats.dislikeCount;
5452

5553
ctx.body = {
5654
...data,
5755
...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,
6058
};
6159
});
6260

@@ -290,37 +288,40 @@ async function getEvaluationStats(
290288
): Promise<EvaluationCounts | EvaluationStats[]> {
291289
const { categoryIds, count, customerServiceIds, from, to, bySelection } = options;
292290

291+
const where = (...clauses: string[]) => {
292+
const clause = clauses.filter(Boolean).join(' AND ');
293+
return clause ? `WHERE ${clause}` : '';
294+
};
295+
293296
const sql = `
294297
SELECT
295298
count(DISTINCT t.objectId) AS count,
296299
${!count && bySelection ? 'selection,' : ''}
297300
${categoryIds ? 'categoryId,' : ''}
298301
${customerServiceIds ? 'customerServiceId,' : ''}
299-
visitParamExtractUInt(t.evaluation, 'star') AS star
302+
if(t.evaluation != '', JSONExtractUInt(t.evaluation, 'star'), 1) AS star
300303
FROM Ticket AS t
301304
${
302305
!count && bySelection
303306
? `LEFT ARRAY JOIN
304307
JSONExtract(t.evaluation, 'selections', 'Array(String)') AS selection`
305308
: ''
306309
}
307-
WHERE t.evaluation != ''
308-
${
310+
${where(
311+
count ? '' : `t.evaluation != ''`,
309312
categoryIds && Array.isArray(categoryIds)
310-
? `AND arrayExists(x -> x = visitParamExtractString(t.category, 'objectId'), [${categoryIds
313+
? `arrayExists(x -> x = visitParamExtractString(t.category, 'objectId'), [${categoryIds
311314
.map((id) => escape(id))
312315
.join(',')}])`
313-
: ''
314-
}
315-
${
316+
: '',
316317
customerServiceIds && Array.isArray(customerServiceIds)
317-
? `AND arrayExists(x -> x = t.\`assignee.objectId\`, [${customerServiceIds
318+
? `arrayExists(x -> x = t.\`assignee.objectId\`, [${customerServiceIds
318319
.map((id) => escape(id))
319320
.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+
)}
324325
GROUP BY
325326
${!count && bySelection ? 'selection,' : ''}
326327
${categoryIds ? "visitParamExtractString(t.category, 'objectId') AS categoryId," : ''}

0 commit comments

Comments
 (0)