debug-print-task.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // 调试打印任务创建问题
  2. const { DataSource } = require('typeorm');
  3. async function debugPrintTask() {
  4. console.log('=== 调试打印任务创建问题 ===\n');
  5. // 创建数据源
  6. const dataSource = new DataSource({
  7. type: 'postgres',
  8. host: '127.0.0.1',
  9. port: 5432,
  10. username: 'postgres',
  11. password: '',
  12. database: 'postgres',
  13. synchronize: false,
  14. logging: false
  15. });
  16. try {
  17. await dataSource.initialize();
  18. console.log('✅ 数据库连接成功\n');
  19. const tenantId = 1;
  20. const orderId = 25;
  21. // 1. 检查订单是否存在
  22. console.log('1. 检查订单是否存在...');
  23. const order = await dataSource.query(
  24. 'SELECT id, order_no, amount, pay_state, state FROM orders_mt WHERE id = $1 AND tenant_id = $2',
  25. [orderId, tenantId]
  26. );
  27. if (order.length === 0) {
  28. console.log('❌ 订单不存在');
  29. return;
  30. }
  31. console.log('✅ 订单存在:', order[0]);
  32. console.log(` 订单号: ${order[0].order_no}`);
  33. console.log(` 金额: ${order[0].amount}`);
  34. console.log(` 支付状态: ${order[0].pay_state}`);
  35. console.log(` 订单状态: ${order[0].state}\n`);
  36. // 2. 检查打印机配置
  37. console.log('2. 检查打印机配置...');
  38. const printer = await dataSource.query(
  39. 'SELECT * FROM feie_printer_mt WHERE tenant_id = $1 AND is_default = 1',
  40. [tenantId]
  41. );
  42. if (printer.length === 0) {
  43. console.log('❌ 没有默认打印机');
  44. return;
  45. }
  46. console.log('✅ 默认打印机存在:', printer[0].printer_name);
  47. console.log(` 打印机SN: ${printer[0].printer_sn}`);
  48. console.log(` 打印机状态: ${printer[0].printer_status}\n`);
  49. // 3. 检查防退款延迟配置
  50. console.log('3. 检查防退款延迟配置...');
  51. const delayConfig = await dataSource.query(
  52. 'SELECT config_value FROM system_config_mt WHERE tenant_id = $1 AND config_key = $2',
  53. [tenantId, 'feie.anti_refund_delay']
  54. );
  55. const delaySeconds = delayConfig.length > 0 ? parseInt(delayConfig[0].config_value) : 120;
  56. console.log(`✅ 防退款延迟时间: ${delaySeconds}秒\n`);
  57. // 4. 检查飞鹅API配置
  58. console.log('4. 检查飞鹅API配置...');
  59. const apiConfigs = await dataSource.query(
  60. 'SELECT config_key, config_value FROM system_config_mt WHERE tenant_id = $1 AND config_key LIKE $2',
  61. [tenantId, 'feie.api.%']
  62. );
  63. if (apiConfigs.length === 0) {
  64. console.log('❌ 飞鹅API配置不存在');
  65. } else {
  66. console.log('✅ 飞鹅API配置存在:');
  67. apiConfigs.forEach(config => {
  68. console.log(` ${config.config_key}: ${config.config_value}`);
  69. });
  70. console.log();
  71. }
  72. // 5. 手动创建打印任务(模拟代码逻辑)
  73. console.log('5. 手动创建打印任务...');
  74. const taskId = `FEIE_${Date.now()}_${Math.floor(Math.random() * 10000)}`;
  75. const scheduledAt = new Date(Date.now() + delaySeconds * 1000);
  76. try {
  77. await dataSource.query(`
  78. INSERT INTO feie_print_task_mt
  79. (tenant_id, task_id, order_id, printer_sn, content, print_type, print_status, scheduled_at, retry_count, max_retries, created_at, updated_at)
  80. VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, NOW(), NOW())
  81. `, [
  82. tenantId,
  83. taskId,
  84. orderId,
  85. printer[0].printer_sn,
  86. `测试打印内容\n订单号: ${order[0].order_no}\n时间: ${new Date().toLocaleString()}`,
  87. 'RECEIPT',
  88. 'DELAYED',
  89. scheduledAt,
  90. 0,
  91. 3
  92. ]);
  93. console.log('✅ 手动创建打印任务成功');
  94. console.log(` 任务ID: ${taskId}`);
  95. console.log(` 计划时间: ${scheduledAt}\n`);
  96. // 6. 验证任务是否创建成功
  97. console.log('6. 验证任务是否创建成功...');
  98. const createdTask = await dataSource.query(
  99. 'SELECT * FROM feie_print_task_mt WHERE tenant_id = $1 AND task_id = $2',
  100. [tenantId, taskId]
  101. );
  102. if (createdTask.length > 0) {
  103. console.log('✅ 任务验证成功:');
  104. console.log(` 任务ID: ${createdTask[0].task_id}`);
  105. console.log(` 订单ID: ${createdTask[0].order_id}`);
  106. console.log(` 状态: ${createdTask[0].print_status}`);
  107. console.log(` 创建时间: ${createdTask[0].created_at}`);
  108. } else {
  109. console.log('❌ 任务验证失败 - 任务未找到');
  110. }
  111. // 7. 清理测试数据
  112. console.log('\n7. 清理测试数据...');
  113. await dataSource.query(
  114. 'DELETE FROM feie_print_task_mt WHERE tenant_id = $1 AND task_id = $2',
  115. [tenantId, taskId]
  116. );
  117. console.log('✅ 测试数据已清理');
  118. } catch (error) {
  119. console.error('❌ 手动创建打印任务失败:', error.message);
  120. console.error('错误堆栈:', error.stack);
  121. }
  122. } catch (error) {
  123. console.error('调试失败:', error);
  124. } finally {
  125. try {
  126. await dataSource.destroy();
  127. console.log('\n🔌 数据库连接已关闭');
  128. } catch (error) {
  129. console.error('关闭数据库连接失败:', error);
  130. }
  131. }
  132. }
  133. // 运行调试
  134. debugPrintTask().catch(error => {
  135. console.error('调试脚本执行失败:', error);
  136. process.exit(1);
  137. });