OcrChannelController.ts 4.0 KB

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