|
@@ -0,0 +1,98 @@
|
|
|
|
|
+import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
|
|
|
+import PaginationService from 'App/Services/PaginationService'
|
|
|
|
|
+import OcrChannel from 'App/Models/OcrChannel'
|
|
|
|
|
+import { schema } from '@ioc:Adonis/Core/Validator'
|
|
|
|
|
+
|
|
|
|
|
+export default class OcrChannelController {
|
|
|
|
|
+ private paginationService = new PaginationService(OcrChannel)
|
|
|
|
|
+
|
|
|
|
|
+ public async index({ request }: HttpContextContract) {
|
|
|
|
|
+ return await this.paginationService.paginate(request.all())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async store({ request, bouncer }: HttpContextContract) {
|
|
|
|
|
+ await request.validate({
|
|
|
|
|
+ schema: schema.create({
|
|
|
|
|
+ name: schema.string.optional(),
|
|
|
|
|
+ deviceNum: schema.number(),
|
|
|
|
|
+ recordNum: schema.number(),
|
|
|
|
|
+ scanNum: schema.number()
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ return await OcrChannel.create(request.all())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async update({ request, bouncer }: HttpContextContract) {
|
|
|
|
|
+ const data = await request.validate({
|
|
|
|
|
+ schema: schema.create({
|
|
|
|
|
+ name: schema.string.optional(),
|
|
|
|
|
+ deviceNum: schema.number.optional(),
|
|
|
|
|
+ recordNum: schema.number.optional(),
|
|
|
|
|
+ scanNum: schema.number.optional()
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ const ocrChannel = await OcrChannel.findOrFail(request.param('id'))
|
|
|
|
|
+ return await ocrChannel.merge(data).save()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async plusDeviceNum({ request, response }: HttpContextContract) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
|
|
|
|
|
+ if (!ocrChannel) {
|
|
|
|
|
+ return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ocrChannel.deviceNum += 1
|
|
|
|
|
+ await ocrChannel.save()
|
|
|
|
|
+ return ocrChannel
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ return response.internalServerError({
|
|
|
|
|
+ message: '更新设备数量时发生错误',
|
|
|
|
|
+ error: error.message
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async plusRecordNum({ request, response }: HttpContextContract) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
|
|
|
|
|
+ if (!ocrChannel) {
|
|
|
|
|
+ return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
|
|
|
|
|
+ }
|
|
|
|
|
+ ocrChannel.recordNum += 1
|
|
|
|
|
+ await ocrChannel.save()
|
|
|
|
|
+ return ocrChannel
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ return response.internalServerError({
|
|
|
|
|
+ message: '更新记录数量时发生错误',
|
|
|
|
|
+ error: error.message
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async plusScanNum({ request, response }: HttpContextContract) {
|
|
|
|
|
+ const scanCount = Number(request.param('scanCount')) // 确保 scanCount 是数字
|
|
|
|
|
+ if (isNaN(scanCount)) {
|
|
|
|
|
+ return response.badRequest({ message: 'scanCount 参数必须是有效数字' })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
|
|
|
|
|
+ if (!ocrChannel) {
|
|
|
|
|
+ return response.notFound({
|
|
|
|
|
+ message: `未找到 ID 为 ${request.param('id')} 的 OCR 渠道`
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ocrChannel.scanNum = (Number(ocrChannel.scanNum) || 0) + scanCount // 确保 scanNum 也是数字
|
|
|
|
|
+ await ocrChannel.save()
|
|
|
|
|
+
|
|
|
|
|
+ return response.ok(ocrChannel)
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ return response.internalServerError({
|
|
|
|
|
+ message: '更新扫描数量时发生错误',
|
|
|
|
|
+ error: error.message
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|