OcrChannelController.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import PaginationService from 'App/Services/PaginationService'
  3. import OcrChannel from 'App/Models/OcrChannel'
  4. import { schema } from '@ioc:Adonis/Core/Validator'
  5. export default class OcrChannelController {
  6. private paginationService = new PaginationService(OcrChannel)
  7. public async index({ request }: HttpContextContract) {
  8. return await this.paginationService.paginate(request.all())
  9. }
  10. public async store({ request, bouncer }: HttpContextContract) {
  11. await request.validate({
  12. schema: schema.create({
  13. name: schema.string.optional(),
  14. deviceNum: schema.number(),
  15. recordNum: schema.number(),
  16. scanNum: schema.number()
  17. })
  18. })
  19. return await OcrChannel.create(request.all())
  20. }
  21. public async update({ request, bouncer }: HttpContextContract) {
  22. const data = await request.validate({
  23. schema: schema.create({
  24. name: schema.string.optional(),
  25. deviceNum: schema.number.optional(),
  26. recordNum: schema.number.optional(),
  27. scanNum: schema.number.optional()
  28. })
  29. })
  30. const ocrChannel = await OcrChannel.findOrFail(request.param('id'))
  31. return await ocrChannel.merge(data).save()
  32. }
  33. public async plusDeviceNum({ request, response }: HttpContextContract) {
  34. try {
  35. const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
  36. if (!ocrChannel) {
  37. return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
  38. }
  39. ocrChannel.deviceNum += 1
  40. await ocrChannel.save()
  41. return ocrChannel
  42. } catch (error) {
  43. return response.internalServerError({
  44. message: '更新设备数量时发生错误',
  45. error: error.message
  46. })
  47. }
  48. }
  49. public async plusRecordNum({ request, response }: HttpContextContract) {
  50. try {
  51. const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
  52. if (!ocrChannel) {
  53. return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
  54. }
  55. ocrChannel.recordNum += 1
  56. await ocrChannel.save()
  57. return ocrChannel
  58. } catch (error) {
  59. return response.internalServerError({
  60. message: '更新记录数量时发生错误',
  61. error: error.message
  62. })
  63. }
  64. }
  65. public async plusScanNum({ request, response }: HttpContextContract) {
  66. const scanCount = Number(request.param('scanCount')) // 确保 scanCount 是数字
  67. if (isNaN(scanCount)) {
  68. return response.badRequest({ message: 'scanCount 参数必须是有效数字' })
  69. }
  70. try {
  71. const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
  72. if (!ocrChannel) {
  73. return response.notFound({
  74. message: `未找到 ID 为 ${request.param('id')} 的 OCR 渠道`
  75. })
  76. }
  77. ocrChannel.scanNum = (Number(ocrChannel.scanNum) || 0) + scanCount // 确保 scanNum 也是数字
  78. await ocrChannel.save()
  79. return response.ok(ocrChannel)
  80. } catch (error) {
  81. return response.internalServerError({
  82. message: '更新扫描数量时发生错误',
  83. error: error.message
  84. })
  85. }
  86. }
  87. }