'use strict'; const { Controller } = require('egg'); /** * web 主体端:首页统计(`/api/web/stats`) * * 只统计**当前主体**的数据(设备数 / 通道数 / 扫码记录数 + 最近一条扫码记录), * 与运维视角的统计(`service.user.count()` 那种全站数字)区分开。 */ class WebStatsController extends Controller { /** GET /api/web/stats(需要 apiAuth + subjectAuth) */ async index() { const { ctx } = this; const subjectId = ctx.state.subjectId; const [ deviceCount, channelCount, recordCount, latest ] = await Promise.all([ ctx.service.scanner.count({ subjectId }), ctx.service.scan.countChannels({ subjectId }), ctx.service.scan.countRecords({ subjectId }), ctx.service.scan.listRecords({ subjectId, page: 1, pageSize: 1 }), ]); const row = latest.list[0] || null; ctx.body = { ok: true, data: { deviceCount, channelCount, recordCount, latestRecord: row ? { id: row.id, barcode: row.barcode, channelId: row.channelId, deviceSn: row.scanner ? row.scanner.sn : null, scanTime: row.scanTime, } : null, }, }; } } module.exports = WebStatsController;