| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- // 简单测试延迟打印任务流程
- const { DataSource } = require('typeorm');
- async function testDelayedPrintSimple() {
- console.log('=== 简单测试延迟打印任务流程 ===\n');
- // 创建数据源
- const dataSource = new DataSource({
- type: 'postgres',
- host: '127.0.0.1',
- port: 5432,
- username: 'postgres',
- password: '',
- database: 'postgres',
- synchronize: false,
- logging: false
- });
- try {
- await dataSource.initialize();
- console.log('✅ 数据库连接成功\n');
- const tenantId = 1;
- const testPrinterSn = 'TEST' + Math.floor(Math.random() * 1000);
- const testOrderId = Math.floor(Math.random() * 10000) + 10000;
- // 1. 确保打印机存在
- console.log('1. 创建测试打印机记录...');
- await dataSource.query(`
- INSERT INTO feie_printer_mt (tenant_id, printer_sn, printer_key, printer_name, printer_status, printer_type, is_default, created_at, updated_at)
- VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), NOW())
- ON CONFLICT (tenant_id, printer_sn) DO NOTHING
- `, [tenantId, testPrinterSn, 'test-key', '测试打印机', 'ACTIVE', '58mm', 0]);
- console.log('✅ 测试打印机记录已创建\n');
- // 2. 创建延迟打印任务
- console.log('2. 创建延迟打印任务...');
- const taskId = `FEIE_${Date.now()}_${Math.floor(Math.random() * 10000)}`;
- const delaySeconds = 5;
- const scheduledAt = new Date(Date.now() + delaySeconds * 1000);
- await dataSource.query(`
- INSERT INTO feie_print_task_mt
- (tenant_id, task_id, order_id, printer_sn, content, print_type, print_status, scheduled_at, retry_count, max_retries, created_at, updated_at)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, NOW(), NOW())
- `, [
- tenantId,
- taskId,
- testOrderId,
- testPrinterSn,
- `测试延迟打印内容\n订单号: ${testOrderId}\n时间: ${new Date().toLocaleString()}`,
- 'RECEIPT',
- 'DELAYED',
- scheduledAt,
- 0,
- 3
- ]);
- console.log(`✅ 延迟打印任务创建成功`);
- console.log(` 任务ID: ${taskId}`);
- console.log(` 订单ID: ${testOrderId}`);
- console.log(` 打印机SN: ${testPrinterSn}`);
- console.log(` 延迟秒数: ${delaySeconds}`);
- console.log(` 计划时间: ${scheduledAt}\n`);
- // 3. 查询任务
- console.log('3. 查询任务状态...');
- const tasks = await dataSource.query(
- 'SELECT * FROM feie_print_task_mt WHERE tenant_id = $1 AND task_id = $2',
- [tenantId, taskId]
- );
- if (tasks.length > 0) {
- const task = tasks[0];
- console.log(`✅ 任务查询成功`);
- console.log(` 任务状态: ${task.print_status}`);
- console.log(` 计划时间: ${task.scheduled_at}`);
- console.log(` 创建时间: ${task.created_at}\n`);
- }
- // 4. 模拟调度器处理(查询待处理任务)
- console.log('4. 模拟调度器查询待处理任务...');
- console.log(`等待 ${delaySeconds + 2} 秒让任务到达执行时间...`);
- await new Promise(resolve => setTimeout(resolve, (delaySeconds + 2) * 1000));
- const pendingTasks = await dataSource.query(`
- SELECT * FROM feie_print_task_mt
- WHERE tenant_id = $1
- AND (print_status = 'DELAYED' OR print_status = 'PENDING')
- AND scheduled_at <= NOW()
- ORDER BY scheduled_at ASC
- `, [tenantId]);
- console.log(`找到 ${pendingTasks.length} 个待处理任务`);
- if (pendingTasks.length > 0) {
- const pendingTask = pendingTasks[0];
- console.log(` 任务ID: ${pendingTask.task_id}`);
- console.log(` 状态: ${pendingTask.print_status}`);
- console.log(` 计划时间: ${pendingTask.scheduled_at}`);
- console.log(` 当前时间: ${new Date()}`);
- console.log(` 是否已到执行时间: ${new Date() >= new Date(pendingTask.scheduled_at) ? '是' : '否'}\n`);
- }
- // 5. 模拟执行打印(更新状态)
- console.log('5. 模拟执行打印任务...');
- if (pendingTasks.length > 0) {
- await dataSource.query(`
- UPDATE feie_print_task_mt
- SET print_status = 'PRINTING', updated_at = NOW()
- WHERE tenant_id = $1 AND task_id = $2
- `, [tenantId, taskId]);
- console.log('✅ 任务状态更新为PRINTING');
- // 模拟打印失败(测试环境)
- await new Promise(resolve => setTimeout(resolve, 1000));
- await dataSource.query(`
- UPDATE feie_print_task_mt
- SET print_status = 'PENDING',
- error_message = $3,
- retry_count = retry_count + 1,
- updated_at = NOW()
- WHERE tenant_id = $1 AND task_id = $2
- `, [tenantId, taskId, '飞鹅API测试环境失败']);
- console.log('✅ 模拟打印失败,任务进入重试状态');
- console.log(` 错误信息: 飞鹅API测试环境失败`);
- console.log(` 重试次数: 1/3\n`);
- }
- // 6. 查询最终状态
- console.log('6. 查询最终任务状态...');
- const finalTasks = await dataSource.query(
- 'SELECT * FROM feie_print_task_mt WHERE tenant_id = $1 AND task_id = $2',
- [tenantId, taskId]
- );
- if (finalTasks.length > 0) {
- const finalTask = finalTasks[0];
- console.log(`最终任务状态:`);
- console.log(` 任务ID: ${finalTask.task_id}`);
- console.log(` 状态: ${finalTask.print_status}`);
- console.log(` 错误信息: ${finalTask.error_message || '无'}`);
- console.log(` 重试次数: ${finalTask.retry_count}/${finalTask.max_retries}`);
- console.log(` 创建时间: ${finalTask.created_at}`);
- console.log(` 更新时间: ${finalTask.updated_at}\n`);
- }
- // 7. 清理测试数据
- console.log('7. 清理测试数据...');
- await dataSource.query(
- 'DELETE FROM feie_print_task_mt WHERE tenant_id = $1 AND task_id = $2',
- [tenantId, taskId]
- );
- console.log('✅ 测试数据已清理');
- console.log('\n🎉 === 测试完成 ===');
- console.log('测试流程验证:');
- console.log('1. ✅ 数据库连接正常');
- console.log('2. ✅ 打印机记录创建正常');
- console.log('3. ✅ 延迟打印任务创建正常');
- console.log('4. ✅ 任务状态查询正常');
- console.log('5. ✅ 调度器查询逻辑正常');
- console.log('6. ✅ 任务状态更新正常');
- console.log('7. ✅ 数据清理正常');
- } catch (error) {
- console.error('\n❌ 测试失败:', error);
- console.error('错误堆栈:', error.stack);
- } finally {
- try {
- await dataSource.destroy();
- console.log('\n🔌 数据库连接已关闭');
- } catch (error) {
- console.error('关闭数据库连接失败:', error);
- }
- }
- }
- // 运行测试
- testDelayedPrintSimple().catch(error => {
- console.error('测试脚本执行失败:', error);
- process.exit(1);
- });
|