OcrChannelController.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 show({ params }: HttpContextContract) {
  22. return await OcrChannel.findOrFail(params.id)
  23. }
  24. public async update({ params, request, bouncer }: HttpContextContract) {
  25. await bouncer.authorize('admin')
  26. const ocrChannel = await OcrChannel.findOrFail(params.id)
  27. const data = await request.validate({
  28. schema: schema.create({
  29. name: schema.string.optional(),
  30. deviceNum: schema.number.optional(),
  31. recordNum: schema.number.optional(),
  32. scanNum: schema.number.optional()
  33. })
  34. })
  35. return await ocrChannel.merge(data).save()
  36. }
  37. public async plusDeviceNum({ request, response }: HttpContextContract) {
  38. try {
  39. const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
  40. if (!ocrChannel) {
  41. return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
  42. }
  43. ocrChannel.deviceNum += 1
  44. await ocrChannel.save()
  45. return ocrChannel
  46. } catch (error) {
  47. return response.internalServerError({
  48. message: '更新设备数量时发生错误',
  49. error: error.message
  50. })
  51. }
  52. }
  53. public async plusRecordNum({ request, response }: HttpContextContract) {
  54. try {
  55. const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
  56. if (!ocrChannel) {
  57. return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
  58. }
  59. ocrChannel.recordNum += 1
  60. await ocrChannel.save()
  61. return ocrChannel
  62. } catch (error) {
  63. return response.internalServerError({
  64. message: '更新记录数量时发生错误',
  65. error: error.message
  66. })
  67. }
  68. }
  69. public async plusScanNum({ request, response }: HttpContextContract) {
  70. const scanCount = Number(request.param('scanCount'))
  71. if (isNaN(scanCount)) {
  72. return response.badRequest({ message: 'scanCount 参数必须是有效数字' })
  73. }
  74. try {
  75. const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
  76. if (!ocrChannel) {
  77. return response.notFound({
  78. message: `未找到 ID 为 ${request.param('id')} 的 OCR 渠道`
  79. })
  80. }
  81. ocrChannel.scanNum = (Number(ocrChannel.scanNum) || 0) + scanCount
  82. await ocrChannel.save()
  83. return response.ok(ocrChannel)
  84. } catch (error) {
  85. return response.internalServerError({
  86. message: '更新扫描数量时发生错误',
  87. error: error.message
  88. })
  89. }
  90. }
  91. }