|
|
@@ -5,28 +5,34 @@ import { DelaySchedulerService } from './delay-scheduler.service';
|
|
|
import { FeieApiConfig, PrintType, PrintStatus, CancelReason } from '../types/feie.types';
|
|
|
import { FeieConfigMt } from '../entities/feie-config.mt.entity';
|
|
|
import { OrderMt } from '@d8d/orders-module-mt';
|
|
|
+import { getFeieApiConfig } from '../routes/utils/feie-config.util';
|
|
|
|
|
|
/**
|
|
|
* 打印触发服务
|
|
|
* 负责处理订单支付成功等事件触发的打印任务
|
|
|
*/
|
|
|
export class PrintTriggerService {
|
|
|
- private printTaskService: PrintTaskService;
|
|
|
- private printerService: PrinterService;
|
|
|
private dataSource: DataSource;
|
|
|
- private feieConfig: FeieApiConfig;
|
|
|
private configRepository: any;
|
|
|
private orderRepository: any;
|
|
|
|
|
|
- constructor(dataSource: DataSource, feieConfig: FeieApiConfig) {
|
|
|
+ constructor(dataSource: DataSource) {
|
|
|
this.dataSource = dataSource;
|
|
|
- this.feieConfig = feieConfig;
|
|
|
- this.printTaskService = new PrintTaskService(dataSource, feieConfig);
|
|
|
- this.printerService = new PrinterService(dataSource, feieConfig);
|
|
|
this.configRepository = dataSource.getRepository(FeieConfigMt);
|
|
|
this.orderRepository = dataSource.getRepository(OrderMt);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取飞鹅API配置(内部方法)
|
|
|
+ */
|
|
|
+ private async getFeieConfig(tenantId: number): Promise<FeieApiConfig> {
|
|
|
+ const config = await getFeieApiConfig(tenantId, this.dataSource);
|
|
|
+ if (!config) {
|
|
|
+ throw new Error(`[租户${tenantId}] 飞鹅API配置未找到或不完整`);
|
|
|
+ }
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 处理订单支付成功事件
|
|
|
* @param tenantId 租户ID
|
|
|
@@ -40,6 +46,9 @@ export class PrintTriggerService {
|
|
|
try {
|
|
|
console.debug(`[租户${tenantId}] 处理订单支付成功事件,订单ID: ${orderId}`);
|
|
|
|
|
|
+ // 获取飞鹅API配置
|
|
|
+ const feieConfig = await this.getFeieConfig(tenantId);
|
|
|
+
|
|
|
// 1. 获取完整的订单信息
|
|
|
const fullOrderInfo = await this.getFullOrderInfo(tenantId, orderId);
|
|
|
if (!fullOrderInfo) {
|
|
|
@@ -51,7 +60,8 @@ export class PrintTriggerService {
|
|
|
const delaySeconds = await this.getAntiRefundDelaySeconds(tenantId);
|
|
|
|
|
|
// 3. 获取默认打印机
|
|
|
- const defaultPrinter = await this.printerService.getDefaultPrinter(tenantId);
|
|
|
+ const printerService = new PrinterService(this.dataSource, feieConfig);
|
|
|
+ const defaultPrinter = await printerService.getDefaultPrinter(tenantId);
|
|
|
if (!defaultPrinter) {
|
|
|
console.warn(`[租户${tenantId}] 未找到默认打印机,跳过打印任务`);
|
|
|
return;
|
|
|
@@ -61,7 +71,8 @@ export class PrintTriggerService {
|
|
|
const printContent = await this.generateReceiptContent(tenantId, fullOrderInfo);
|
|
|
|
|
|
// 5. 创建延迟打印任务
|
|
|
- await this.printTaskService.createPrintTask(tenantId, {
|
|
|
+ const printTaskService = new PrintTaskService(this.dataSource, feieConfig);
|
|
|
+ await printTaskService.createPrintTask(tenantId, {
|
|
|
orderId,
|
|
|
printerSn: defaultPrinter.printerSn,
|
|
|
content: printContent,
|
|
|
@@ -79,7 +90,7 @@ export class PrintTriggerService {
|
|
|
|
|
|
try {
|
|
|
// 动态创建DelaySchedulerService实例
|
|
|
- const delaySchedulerService = new DelaySchedulerService(this.dataSource, this.feieConfig, tenantId);
|
|
|
+ const delaySchedulerService = new DelaySchedulerService(this.dataSource, feieConfig, tenantId);
|
|
|
|
|
|
// 检查并启动调度器
|
|
|
const status = delaySchedulerService.getStatus();
|
|
|
@@ -103,7 +114,7 @@ export class PrintTriggerService {
|
|
|
// 对于有延迟的任务,也启动调度器(如果未运行)
|
|
|
try {
|
|
|
console.debug(`[租户${tenantId}] 检查并启动延迟调度器...`);
|
|
|
- const delaySchedulerService = new DelaySchedulerService(this.dataSource, this.feieConfig, tenantId);
|
|
|
+ const delaySchedulerService = new DelaySchedulerService(this.dataSource, feieConfig, tenantId);
|
|
|
|
|
|
// 检查调度器状态
|
|
|
const status = delaySchedulerService.getStatus();
|
|
|
@@ -136,8 +147,12 @@ export class PrintTriggerService {
|
|
|
try {
|
|
|
console.debug(`[租户${tenantId}] 处理订单退款事件,订单ID: ${orderId}`);
|
|
|
|
|
|
+ // 获取飞鹅API配置
|
|
|
+ const feieConfig = await this.getFeieConfig(tenantId);
|
|
|
+ const printTaskService = new PrintTaskService(this.dataSource, feieConfig);
|
|
|
+
|
|
|
// 1. 查找关联的打印任务
|
|
|
- const { tasks: printTasks } = await this.printTaskService.getPrintTasks(tenantId, {
|
|
|
+ const { tasks: printTasks } = await printTaskService.getPrintTasks(tenantId, {
|
|
|
orderId,
|
|
|
printStatus: PrintStatus.PENDING // 先查询PENDING状态的任务
|
|
|
});
|
|
|
@@ -149,7 +164,7 @@ export class PrintTriggerService {
|
|
|
|
|
|
// 2. 取消所有关联的打印任务
|
|
|
for (const task of printTasks) {
|
|
|
- await this.printTaskService.cancelPrintTask(tenantId, task.taskId, CancelReason.REFUND);
|
|
|
+ await printTaskService.cancelPrintTask(tenantId, task.taskId, CancelReason.REFUND);
|
|
|
console.debug(`[租户${tenantId}] 打印任务已取消,任务ID: ${task.taskId}, 订单ID: ${orderId}`);
|
|
|
}
|
|
|
|