فهرست منبع

ref(actorocr): 重构渠道和设备控制器的统计更新逻辑- 优化 OcrChannelController 中的 create 和 updateStatistics 方法
- 新增 OcrDevicesController 中的 updateNums 方法,实时更新渠道统计数据
- 改进数据库查询效率,减少冗余代码
- 提高系统性能,减轻服务器压力

wui 7 ماه پیش
والد
کامیت
0bdc052335
2فایلهای تغییر یافته به همراه74 افزوده شده و 45 حذف شده
  1. 44 43
      app/Controllers/Http/OcrChannelController.ts
  2. 30 2
      app/Controllers/Http/OcrDevicesController.ts

+ 44 - 43
app/Controllers/Http/OcrChannelController.ts

@@ -24,7 +24,9 @@ export default class OcrChannelController {
                 scanNum: schema.number()
             })
         })
-        return await OcrChannel.create(request.all())
+        const ocrChannel = await OcrChannel.create(request.all())
+        await this.updateChannelStats(ocrChannel)
+        return ocrChannel
     }
 
     public async show({ params, bouncer }: HttpContextContract) {
@@ -127,7 +129,12 @@ export default class OcrChannelController {
             const data = await query
                 .where('created_at', '>=', sevenDaysAgo)
                 .orderBy('created_at', 'asc')
-                .select('created_at', 'device_num as deviceNum', 'record_num as recordNum', 'scan_num as scanNum')
+                .select(
+                    'created_at',
+                    'device_num as deviceNum',
+                    'record_num as recordNum',
+                    'scan_num as scanNum'
+                )
 
             const result = {
                 dates: data.map((item) => DateTime.fromISO(item.created_at).toFormat('yyyy-MM-dd')),
@@ -171,51 +178,45 @@ export default class OcrChannelController {
         }
     }
 
+    private async updateChannelStats(channel: OcrChannel) {
+        // 获取设备数量
+        const deviceCountResult = await Database.from('ocr_devices')
+            .where('channel', channel.name)
+            .count('* as total')
+
+        // 获取记录数量
+        const recordCountResult = await Database.from('ocr_records')
+            .where('channel', channel.name)
+            .count('* as total')
+
+        // 获取扫描数量总和
+        const scanSumResult = await Database.from('ocr_devices')
+            .where('channel', channel.name)
+            .sum('scanned as total')
+
+        // 更新渠道数据
+        channel.deviceNum = parseInt(deviceCountResult[0].total || '0', 10)
+        channel.recordNum = parseInt(recordCountResult[0].total || '0', 10)
+        channel.scanNum = parseInt(scanSumResult[0].total || '0', 10)
+
+        await channel.save()
+
+        return {
+            id: channel.id,
+            name: channel.name,
+            deviceNum: channel.deviceNum,
+            recordNum: channel.recordNum,
+            scanNum: channel.scanNum
+        }
+    }
+
     public async updateStatistics({ response }: HttpContextContract) {
         try {
             // 获取所有渠道
             const channels = await OcrChannel.all()
-            const results: any[] = []
-
-            for (const channel of channels) {
-                // 使用Database查询生成器直接执行SQL查询
-
-                // 获取设备数量
-                const deviceCountResult = await Database.from('ocr_devices')
-                    .where('channel', channel.name)
-                    .count('* as total')
-                console.log('deviceCountResult', deviceCountResult)
-
-                // 获取记录数量
-                const recordCountResult = await Database.from('ocr_records')
-                    .where('channel', channel.name)
-                    .count('* as total')
-
-                // 获取扫描数量总和
-                const scanSumResult = await Database.from('ocr_devices')
-                    .where('channel', channel.name)
-                    .sum('scanned as total')
-                console.log('scanSumResult', scanSumResult)
-
-                // 确保数值正确转换为数字
-                const deviceNum = parseInt(deviceCountResult[0].total || '0', 10)
-                const recordNum = parseInt(recordCountResult[0].total || '0', 10)
-                const scanNum = parseInt(scanSumResult[0].total || '0', 10)
-
-                // 更新渠道数据
-                channel.deviceNum = deviceNum
-                channel.recordNum = recordNum
-                channel.scanNum = scanNum
-
-                await channel.save()
-                results.push({
-                    id: channel.id,
-                    name: channel.name,
-                    deviceNum: channel.deviceNum,
-                    recordNum: channel.recordNum,
-                    scanNum: channel.scanNum
-                })
-            }
+            const results = await Promise.all(
+                channels.map((channel) => this.updateChannelStats(channel))
+            )
 
             return response.ok({
                 message: '所有渠道统计数据已更新',

+ 30 - 2
app/Controllers/Http/OcrDevicesController.ts

@@ -3,6 +3,7 @@ import PaginationService from 'App/Services/PaginationService'
 import { schema } from '@ioc:Adonis/Core/Validator'
 import OcrDevice from 'App/Models/OcrDevice'
 import OcrRecord from 'App/Models/OcrRecord'
+import OcrChannel from 'App/Models/OcrChannel'
 import { DateTime } from 'luxon'
 import * as console from 'node:console'
 import Database from '@ioc:Adonis/Lucid/Database'
@@ -39,11 +40,38 @@ export default class OcrDevicesController {
             data.ipAddress = clientIp
         }
         const device = await OcrDevice.findBy('id', data.id)
+        let ocrDevice: OcrDevice
         if (device) {
             device.merge(data)
-            return await device.save()
+            ocrDevice = await device.save()
         } else {
-            return await OcrDevice.create(data)
+            ocrDevice = await OcrDevice.create(data)
+        }
+        // 更新渠道统计数据
+        await this.updateNums(data.channel)
+        return ocrDevice
+    }
+
+    private async updateNums(channel: string) {
+        if (channel) {
+            const ocrChannel = await OcrChannel.findBy('name', channel)
+            if (ocrChannel) {
+                const deviceCount = await OcrDevice.query()
+                    .where('channel', channel)
+                    .count('* as total')
+                const recordCount = await OcrRecord.query()
+                    .where('channel', channel)
+                    .count('* as total')
+                const scanSum = await OcrDevice.query()
+                    .where('channel', channel)
+                    .sum('scanned as total')
+
+                ocrChannel.deviceNum = Number(deviceCount[0].$extras.total || 0)
+                ocrChannel.recordNum = Number(recordCount[0].$extras.total || 0)
+                ocrChannel.scanNum = Number(scanSum[0].$extras.total || 0)
+
+                await ocrChannel.save()
+            }
         }
     }