OcrChannelController.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. import { DateTime } from 'luxon'
  6. export default class OcrChannelController {
  7. private paginationService = new PaginationService(OcrChannel)
  8. public async index({ request, bouncer }: HttpContextContract) {
  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. public async getStatistics({ request, response }: HttpContextContract) {
  100. try {
  101. const name = request.input('name')
  102. const query = OcrChannel.query()
  103. if (name) {
  104. query.where('name', name)
  105. }
  106. const sevenDaysAgo = DateTime.now().minus({ days: 7 }).startOf('day').toSQL()
  107. const data = await query
  108. .where('createdAt', '>=', sevenDaysAgo)
  109. .orderBy('createdAt', 'asc')
  110. .select('createdAt', 'deviceNum', 'recordNum', 'scanNum')
  111. const result = {
  112. dates: data.map((item) => item.createdAt.toFormat('yyyy-MM-dd')),
  113. deviceNum: data.map((item) => item.deviceNum),
  114. recordNum: data.map((item) => item.recordNum),
  115. scanNum: data.map((item) => item.scanNum)
  116. }
  117. return response.ok(result)
  118. } catch (error) {
  119. return response.internalServerError({
  120. message: '获取统计数据时发生错误',
  121. error: error.message
  122. })
  123. }
  124. }
  125. public async getChannelNames({ auth, response }: HttpContextContract) {
  126. try {
  127. const user = auth.user
  128. const role = user?.$attributes?.role
  129. // 验证管理员或操作员权限
  130. if (role !== 'admin' && role !== 'operator') {
  131. return response.forbidden({
  132. message: 'unauthorized'
  133. })
  134. }
  135. // 获取所有渠道名称
  136. const channels = await OcrChannel.query().select('name').orderBy('name', 'asc')
  137. return response.ok({
  138. data: channels.map((channel) => channel.name)
  139. })
  140. } catch (error) {
  141. return response.internalServerError({
  142. message: '获取渠道名称列表时发生错误',
  143. error: error.message
  144. })
  145. }
  146. }
  147. }