| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import { DataSource, Repository } from 'typeorm';
- import { FeieConfigMt } from '../entities/feie-config.mt.entity';
- /**
- * 打印配置服务
- */
- export class ConfigService {
- private configRepository: Repository<FeieConfigMt>;
- constructor(dataSource: DataSource) {
- this.configRepository = dataSource.getRepository(FeieConfigMt);
- }
- /**
- * 获取租户的所有打印配置
- */
- async getPrintConfigs(tenantId: number): Promise<FeieConfigMt[]> {
- try {
- const configs = await this.configRepository.find({
- where: { tenantId },
- order: { configKey: 'ASC' }
- });
- // 如果没有配置,返回空数组
- return configs || [];
- } catch (error) {
- console.error(`[租户${tenantId}] 获取打印配置失败:`, error);
- throw new Error('获取打印配置失败');
- }
- }
- /**
- * 获取单个配置项
- */
- async getPrintConfig(tenantId: number, configKey: string): Promise<FeieConfigMt | null> {
- try {
- const config = await this.configRepository.findOne({
- where: { tenantId, configKey }
- });
- return config || null;
- } catch (error) {
- console.error(`[租户${tenantId}] 获取配置项失败,key: ${configKey}:`, error);
- throw new Error('获取配置项失败');
- }
- }
- /**
- * 更新打印配置
- */
- async updatePrintConfig(
- tenantId: number,
- configKey: string,
- configValue: string
- ): Promise<FeieConfigMt> {
- try {
- // 查找现有配置
- let config = await this.configRepository.findOne({
- where: { tenantId, configKey }
- });
- if (config) {
- // 更新现有配置
- config.configValue = configValue;
- config.updatedAt = new Date();
- await this.configRepository.save(config);
- } else {
- // 创建新配置
- config = this.configRepository.create({
- tenantId,
- configKey,
- configValue,
- configType: this.guessConfigType(configValue),
- description: this.getConfigDescription(configKey)
- });
- await this.configRepository.save(config);
- }
- return config;
- } catch (error) {
- console.error(`[租户${tenantId}] 更新配置失败,key: ${configKey}:`, error);
- throw new Error('更新配置失败');
- }
- }
- /**
- * 根据配置值猜测配置类型
- */
- private guessConfigType(configValue: string): string {
- // 尝试解析为JSON
- try {
- JSON.parse(configValue);
- return 'JSON';
- } catch {
- // 不是JSON
- }
- // 检查是否为布尔值
- if (configValue.toLowerCase() === 'true' || configValue.toLowerCase() === 'false') {
- return 'BOOLEAN';
- }
- // 检查是否为数字
- if (!isNaN(Number(configValue)) && configValue.trim() !== '') {
- return 'NUMBER';
- }
- // 默认为字符串
- return 'STRING';
- }
- /**
- * 获取配置项描述
- */
- private getConfigDescription(configKey: string): string {
- const descriptions: Record<string, string> = {
- 'feie.enabled': '是否启用飞鹅打印功能',
- 'feie.default_printer_sn': '默认使用的打印机序列号',
- 'feie.auto_print_on_payment': '订单支付成功后是否自动打印小票',
- 'feie.auto_print_on_shipping': '订单发货时是否自动打印发货单',
- 'feie.anti_refund_delay': '支付成功后等待确认无退款的时间(秒)',
- 'feie.retry_max_count': '打印失败时的最大重试次数',
- 'feie.retry_interval': '打印失败后重试的间隔时间(秒)',
- 'feie.task_timeout': '打印任务的最大执行时间(秒)',
- 'feie.receipt_template': '小票打印的模板内容',
- 'feie.shipping_template': '发货单打印的模板内容'
- };
- return descriptions[configKey] || '打印配置项';
- }
- }
|