debug-print-task-issue.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // 调试打印任务创建问题
  2. const { DataSource } = require('typeorm');
  3. const path = require('path');
  4. // 设置模块路径
  5. const projectRoot = path.resolve(__dirname);
  6. const sharedUtilsPath = path.join(projectRoot, 'packages/shared-utils/dist/index.js');
  7. async function debugPrintTaskIssue() {
  8. console.log('=== 调试打印任务创建问题 ===\n');
  9. try {
  10. // 1. 检查数据源连接
  11. console.log('1. 检查数据源连接...');
  12. if (!AppDataSource.isInitialized) {
  13. console.log(' 数据源未初始化,正在初始化...');
  14. await AppDataSource.initialize();
  15. console.log(' 数据源初始化成功');
  16. } else {
  17. console.log(' 数据源已初始化');
  18. }
  19. // 2. 检查订单ID 25是否存在
  20. console.log('\n2. 检查订单ID 25...');
  21. const orderRepository = AppDataSource.getRepository('OrderMt');
  22. const order = await orderRepository.findOne({
  23. where: { id: 25, tenantId: 1 }
  24. });
  25. if (!order) {
  26. console.log(' ❌ 订单ID 25不存在');
  27. return;
  28. }
  29. console.log(` ✅ 订单存在: ${order.orderNo}, 支付状态: ${order.payState}, 订单状态: ${order.state}`);
  30. // 3. 检查飞鹅配置
  31. console.log('\n3. 检查飞鹅配置...');
  32. const configRepository = AppDataSource.getRepository('FeieConfigMt');
  33. const configKeys = [
  34. 'feie.api.user',
  35. 'feie.api.ukey',
  36. 'feie.api.base_url',
  37. 'feie.anti_refund_delay'
  38. ];
  39. for (const key of configKeys) {
  40. const config = await configRepository.findOne({
  41. where: { tenantId: 1, configKey: key }
  42. });
  43. console.log(` ${key}: ${config ? config.configValue : '未配置'}`);
  44. }
  45. // 4. 检查默认打印机
  46. console.log('\n4. 检查默认打印机...');
  47. const printerRepository = AppDataSource.getRepository('FeiePrinterMt');
  48. const defaultPrinter = await printerRepository.findOne({
  49. where: { tenantId: 1, isDefault: true }
  50. });
  51. if (!defaultPrinter) {
  52. console.log(' ❌ 未找到默认打印机');
  53. // 检查是否有任何打印机
  54. const printers = await printerRepository.find({
  55. where: { tenantId: 1 }
  56. });
  57. console.log(` 租户1共有 ${printers.length} 台打印机`);
  58. } else {
  59. console.log(` ✅ 默认打印机: ${defaultPrinter.printerSn} (${defaultPrinter.printerName})`);
  60. }
  61. // 5. 检查打印任务表结构
  62. console.log('\n5. 检查打印任务表结构...');
  63. const printTaskRepository = AppDataSource.getRepository('FeiePrintTaskMt');
  64. const tableMetadata = printTaskRepository.metadata;
  65. console.log(` 表名: ${tableMetadata.tableName}`);
  66. console.log(` 列数: ${tableMetadata.columns.length}`);
  67. // 6. 尝试手动创建打印任务
  68. console.log('\n6. 尝试手动创建打印任务...');
  69. try {
  70. // 动态导入飞鹅打印模块
  71. const { PrintTriggerService } = await import('@d8d/feie-printer-module-mt');
  72. // 创建配置
  73. const feieConfig = {
  74. user: 'test',
  75. ukey: 'test',
  76. baseUrl: 'http://api.feieyun.cn/Api/Open/',
  77. timeout: 10000,
  78. maxRetries: 3
  79. };
  80. // 创建服务实例
  81. const printTriggerService = new PrintTriggerService(AppDataSource, feieConfig);
  82. // 调用处理支付成功事件
  83. console.log(' 调用handleOrderPaymentSuccess...');
  84. await printTriggerService.handleOrderPaymentSuccess(1, 25);
  85. console.log(' ✅ 调用成功');
  86. } catch (error) {
  87. console.log(` ❌ 调用失败: ${error.message}`);
  88. console.log(` 错误堆栈: ${error.stack}`);
  89. }
  90. // 7. 检查是否创建了记录
  91. console.log('\n7. 检查是否创建了记录...');
  92. const tasks = await printTaskRepository.find({
  93. where: { tenantId: 1, orderId: 25 }
  94. });
  95. if (tasks.length === 0) {
  96. console.log(' ❌ 未找到订单ID 25的打印任务');
  97. } else {
  98. console.log(` ✅ 找到 ${tasks.length} 个打印任务`);
  99. tasks.forEach(task => {
  100. console.log(` - 任务ID: ${task.taskId}, 状态: ${task.printStatus}, 创建时间: ${task.createdAt}`);
  101. });
  102. }
  103. } catch (error) {
  104. console.error('调试过程中发生错误:', error);
  105. console.error('错误堆栈:', error.stack);
  106. }
  107. }
  108. // 运行调试
  109. debugPrintTaskIssue().catch(error => {
  110. console.error('调试脚本执行失败:', error);
  111. process.exit(1);
  112. });