OcrChannelController.ts 4.0 KB

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