config.service.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { DataSource, Repository } from 'typeorm';
  2. import { FeieConfigMt } from '../entities/feie-config.mt.entity';
  3. /**
  4. * 打印配置服务
  5. */
  6. export class ConfigService {
  7. private configRepository: Repository<FeieConfigMt>;
  8. constructor(dataSource: DataSource) {
  9. this.configRepository = dataSource.getRepository(FeieConfigMt);
  10. }
  11. /**
  12. * 获取租户的所有打印配置
  13. */
  14. async getPrintConfigs(tenantId: number): Promise<FeieConfigMt[]> {
  15. try {
  16. const configs = await this.configRepository.find({
  17. where: { tenantId },
  18. order: { configKey: 'ASC' }
  19. });
  20. // 如果没有配置,返回空数组
  21. return configs || [];
  22. } catch (error) {
  23. console.error(`[租户${tenantId}] 获取打印配置失败:`, error);
  24. throw new Error('获取打印配置失败');
  25. }
  26. }
  27. /**
  28. * 获取单个配置项
  29. */
  30. async getPrintConfig(tenantId: number, configKey: string): Promise<FeieConfigMt | null> {
  31. try {
  32. const config = await this.configRepository.findOne({
  33. where: { tenantId, configKey }
  34. });
  35. return config || null;
  36. } catch (error) {
  37. console.error(`[租户${tenantId}] 获取配置项失败,key: ${configKey}:`, error);
  38. throw new Error('获取配置项失败');
  39. }
  40. }
  41. /**
  42. * 更新打印配置
  43. */
  44. async updatePrintConfig(
  45. tenantId: number,
  46. configKey: string,
  47. configValue: string
  48. ): Promise<FeieConfigMt> {
  49. try {
  50. // 查找现有配置
  51. let config = await this.configRepository.findOne({
  52. where: { tenantId, configKey }
  53. });
  54. if (config) {
  55. // 更新现有配置
  56. config.configValue = configValue;
  57. config.updatedAt = new Date();
  58. await this.configRepository.save(config);
  59. } else {
  60. // 创建新配置
  61. config = this.configRepository.create({
  62. tenantId,
  63. configKey,
  64. configValue,
  65. configType: this.guessConfigType(configValue),
  66. description: this.getConfigDescription(configKey)
  67. });
  68. await this.configRepository.save(config);
  69. }
  70. return config;
  71. } catch (error) {
  72. console.error(`[租户${tenantId}] 更新配置失败,key: ${configKey}:`, error);
  73. throw new Error('更新配置失败');
  74. }
  75. }
  76. /**
  77. * 根据配置值猜测配置类型
  78. */
  79. private guessConfigType(configValue: string): string {
  80. // 尝试解析为JSON
  81. try {
  82. JSON.parse(configValue);
  83. return 'JSON';
  84. } catch {
  85. // 不是JSON
  86. }
  87. // 检查是否为布尔值
  88. if (configValue.toLowerCase() === 'true' || configValue.toLowerCase() === 'false') {
  89. return 'BOOLEAN';
  90. }
  91. // 检查是否为数字
  92. if (!isNaN(Number(configValue)) && configValue.trim() !== '') {
  93. return 'NUMBER';
  94. }
  95. // 默认为字符串
  96. return 'STRING';
  97. }
  98. /**
  99. * 获取配置项描述
  100. */
  101. private getConfigDescription(configKey: string): string {
  102. const descriptions: Record<string, string> = {
  103. 'feie.enabled': '是否启用飞鹅打印功能',
  104. 'feie.default_printer_sn': '默认使用的打印机序列号',
  105. 'feie.auto_print_on_payment': '订单支付成功后是否自动打印小票',
  106. 'feie.auto_print_on_shipping': '订单发货时是否自动打印发货单',
  107. 'feie.anti_refund_delay': '支付成功后等待确认无退款的时间(秒)',
  108. 'feie.retry_max_count': '打印失败时的最大重试次数',
  109. 'feie.retry_interval': '打印失败后重试的间隔时间(秒)',
  110. 'feie.task_timeout': '打印任务的最大执行时间(秒)',
  111. 'feie.receipt_template': '小票打印的模板内容',
  112. 'feie.shipping_template': '发货单打印的模板内容'
  113. };
  114. return descriptions[configKey] || '打印配置项';
  115. }
  116. }