import { Consumer, MSListenerType, RabbitMQListener, Inject } from '@midwayjs/core';
import { Context } from '@midwayjs/rabbitmq';
import { ConsumeMessage } from 'amqplib';
import { AsyncExportService } from '../service/asyncExport.service';

@Consumer(MSListenerType.RABBITMQ)
export class MissionConsumer {
  @Inject()
  ctx: Context;
  // @Inject()
  // aeService: AsyncExportService;

  @RabbitMQListener('mission', {
    exchange: 'missionEx',
    exchangeOptions: {
      type: 'direct',
    },
    consumeOptions: {
      noAck: true,
    },
  })
  async gotData(msg: ConsumeMessage) {
    try {
      const id = msg.content.toString('utf8');
      const asService = await this.ctx.requestContext.getAsync(AsyncExportService);
      // 接 AsyncExportController
      // 3.消费者消费消息,并开始执行任务
      try {
        console.log(`接收到任务id: ${id} 执行命令`)
        await asService.execute(id);
      } catch (error) {
        console.error(error);
      }
    } catch (error) {
      console.log(error);
    }
  }
}
