| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
- import PaginationService from 'App/Services/PaginationService'
- import OcrChannel from 'App/Models/OcrChannel'
- import { schema } from '@ioc:Adonis/Core/Validator'
- import { DateTime } from 'luxon'
- export default class OcrChannelController {
- private paginationService = new PaginationService(OcrChannel)
- public async index({ request, bouncer }: HttpContextContract) {
- return await this.paginationService.paginate(request.all())
- }
- public async store({ request, bouncer }: HttpContextContract) {
- await request.validate({
- schema: schema.create({
- name: schema.string.optional(),
- deviceNum: schema.number(),
- recordNum: schema.number(),
- scanNum: schema.number()
- })
- })
- return await OcrChannel.create(request.all())
- }
- public async show({ params, bouncer }: HttpContextContract) {
- await bouncer.authorize('admin')
- return await OcrChannel.findOrFail(params.id)
- }
- public async update({ params, request, bouncer }: HttpContextContract) {
- await bouncer.authorize('admin')
- const ocrChannel = await OcrChannel.findOrFail(params.id)
- const data = await request.validate({
- schema: schema.create({
- name: schema.string.optional(),
- deviceNum: schema.number.optional(),
- recordNum: schema.number.optional(),
- scanNum: schema.number.optional()
- })
- })
- return await ocrChannel.merge(data).save()
- }
- public async findApiChannel({ auth, response }: HttpContextContract) {
- if (!auth.user) {
- return response.unauthorized({ message: 'unauthorized' })
- }
- return await OcrChannel.findBy('name', auth.user.username)
- }
- public async plusDeviceNum({ request, response }: HttpContextContract) {
- try {
- const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
- if (!ocrChannel) {
- return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
- }
- ocrChannel.deviceNum += 1
- await ocrChannel.save()
- return ocrChannel
- } catch (error) {
- return response.internalServerError({
- message: '更新设备数量时发生错误',
- error: error.message
- })
- }
- }
- public async plusRecordNum({ request, response }: HttpContextContract) {
- try {
- const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
- if (!ocrChannel) {
- return response.notFound({ message: `未找到ID为 ${request.param('id')} 的OCR渠道` })
- }
- ocrChannel.recordNum += 1
- await ocrChannel.save()
- return ocrChannel
- } catch (error) {
- return response.internalServerError({
- message: '更新记录数量时发生错误',
- error: error.message
- })
- }
- }
- public async plusScanNum({ request, response }: HttpContextContract) {
- const scanCount = Number(request.param('scanCount'))
- if (isNaN(scanCount)) {
- return response.badRequest({ message: 'scanCount 参数必须是有效数字' })
- }
- try {
- const ocrChannel = await OcrChannel.findBy('id', request.param('id'))
- if (!ocrChannel) {
- return response.notFound({
- message: `未找到 ID 为 ${request.param('id')} 的 OCR 渠道`
- })
- }
- ocrChannel.scanNum = (Number(ocrChannel.scanNum) || 0) + scanCount
- await ocrChannel.save()
- return response.ok(ocrChannel)
- } catch (error) {
- return response.internalServerError({
- message: '更新扫描数量时发生错误',
- error: error.message
- })
- }
- }
- public async getStatistics({ request, response }: HttpContextContract) {
- try {
- const name = request.input('name')
- const query = OcrChannel.query()
- if (name) {
- query.where('name', name)
- }
- const sevenDaysAgo = DateTime.now().minus({ days: 7 }).startOf('day').toSQL()
- const data = await query
- .where('createdAt', '>=', sevenDaysAgo)
- .orderBy('createdAt', 'asc')
- .select('createdAt', 'deviceNum', 'recordNum', 'scanNum')
- const result = {
- dates: data.map((item) => item.createdAt.toFormat('yyyy-MM-dd')),
- deviceNum: data.map((item) => item.deviceNum),
- recordNum: data.map((item) => item.recordNum),
- scanNum: data.map((item) => item.scanNum)
- }
- return response.ok(result)
- } catch (error) {
- return response.internalServerError({
- message: '获取统计数据时发生错误',
- error: error.message
- })
- }
- }
- public async getChannelNames({ auth, response }: HttpContextContract) {
- try {
- const user = auth.user
- const role = user?.$attributes?.role
- // 验证管理员或操作员权限
- if (role !== 'admin' && role !== 'operator') {
- return response.forbidden({
- message: 'unauthorized'
- })
- }
- // 获取所有渠道名称
- const channels = await OcrChannel.query().select('name').orderBy('name', 'asc')
- return response.ok({
- data: channels.map((channel) => channel.name)
- })
- } catch (error) {
- return response.internalServerError({
- message: '获取渠道名称列表时发生错误',
- error: error.message
- })
- }
- }
- }
|