'use strict'; const { Controller } = require('egg'); /** * web 用户端:首页统计(`/api/web/stats`) * * 只统计**当前用户名下**的数据(设备数 / 通道数 / 扫码记录数 + 最近一条扫码记录), * 与运维视角的统计(`service.user.count()` 那种全站数字)区分开。 */ class WebStatsController extends Controller { /** GET /api/web/stats */ async index() { const { ctx } = this; const userId = ctx.state.user.id; const ids = await ctx.service.scanner.idsOfUser(userId); const [ deviceCount, channelCount, recordCount, latest ] = await Promise.all([ ctx.service.scanner.count({ userId }), ctx.service.scan.countChannels({ scannerIds: ids }), ctx.service.scan.countRecords({ scannerIds: ids }), ctx.service.scan.listRecords({ scannerIds: ids, 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;