222 lines
9.8 KiB
C#
222 lines
9.8 KiB
C#
using System;
|
||
using System.Configuration;
|
||
using System.IO;
|
||
using XP.Common.Logging.Interfaces;
|
||
|
||
namespace XP.ReportEngine.Configs
|
||
{
|
||
/// <summary>
|
||
/// 报告引擎配置加载器(读取 App.config)| Report engine configuration loader (reads from App.config)
|
||
/// </summary>
|
||
public class ConfigLoader
|
||
{
|
||
private readonly ILoggerService _logger;
|
||
|
||
/// <summary>
|
||
/// 构造函数 | Constructor
|
||
/// </summary>
|
||
/// <param name="logger">日志服务 | Logger service</param>
|
||
public ConfigLoader(ILoggerService logger)
|
||
{
|
||
_logger = logger.ForModule<ConfigLoader>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从 App.config 加载报告引擎配置 | Load report engine configuration from App.config
|
||
/// </summary>
|
||
/// <param name="prefix">配置前缀,默认为 "Report" | Configuration prefix, default is "Report"</param>
|
||
/// <returns>报告配置对象 | Report configuration object</returns>
|
||
public ReportConfig LoadReportConfig(string prefix = "Report")
|
||
{
|
||
try
|
||
{
|
||
_logger.Info("开始从 App.config 加载报告引擎配置,前缀: {Prefix} | Loading report config from App.config, prefix: {Prefix}", prefix);
|
||
|
||
var config = new ReportConfig();
|
||
|
||
// 读取输出目录 | Read output directory
|
||
var outputDirectory = ConfigurationManager.AppSettings[$"{prefix}:OutputDirectory"];
|
||
if (!string.IsNullOrEmpty(outputDirectory))
|
||
{
|
||
config.OutputDirectory = outputDirectory;
|
||
}
|
||
|
||
// 读取模板路径 | Read template path
|
||
var templatePath = ConfigurationManager.AppSettings[$"{prefix}:TemplatePath"];
|
||
if (!string.IsNullOrEmpty(templatePath))
|
||
{
|
||
config.TemplatePath = templatePath;
|
||
}
|
||
|
||
// 读取文件名模式 | Read file name pattern
|
||
var fileNamePattern = ConfigurationManager.AppSettings[$"{prefix}:FileNamePattern"];
|
||
if (!string.IsNullOrEmpty(fileNamePattern))
|
||
{
|
||
config.FileNamePattern = fileNamePattern;
|
||
}
|
||
|
||
// 读取重复文件名自动累加设置 | Read auto-increment on duplicate setting
|
||
var autoIncrement = ConfigurationManager.AppSettings[$"{prefix}:AutoIncrementOnDuplicate"];
|
||
if (bool.TryParse(autoIncrement, out var autoIncrementValue))
|
||
{
|
||
config.AutoIncrementOnDuplicate = autoIncrementValue;
|
||
}
|
||
|
||
// 读取自动打开设置 | Read auto-open setting
|
||
var autoOpen = ConfigurationManager.AppSettings[$"{prefix}:AutoOpenAfterGenerate"];
|
||
if (bool.TryParse(autoOpen, out var autoOpenValue))
|
||
{
|
||
config.AutoOpenAfterGenerate = autoOpenValue;
|
||
}
|
||
|
||
// 读取页面尺寸 | Read page size
|
||
var pageSize = ConfigurationManager.AppSettings[$"{prefix}:DefaultPageSize"];
|
||
if (!string.IsNullOrEmpty(pageSize))
|
||
{
|
||
config.DefaultPageSize = pageSize;
|
||
}
|
||
|
||
// 读取页面方向 | Read page orientation
|
||
var orientation = ConfigurationManager.AppSettings[$"{prefix}:DefaultOrientation"];
|
||
if (!string.IsNullOrEmpty(orientation))
|
||
{
|
||
config.DefaultOrientation = orientation;
|
||
}
|
||
|
||
// 读取边距配置 | Read margin configuration
|
||
var marginTop = ConfigurationManager.AppSettings[$"{prefix}:MarginTop"];
|
||
if (float.TryParse(marginTop, out var marginTopValue))
|
||
{
|
||
config.MarginTop = marginTopValue;
|
||
}
|
||
|
||
var marginBottom = ConfigurationManager.AppSettings[$"{prefix}:MarginBottom"];
|
||
if (float.TryParse(marginBottom, out var marginBottomValue))
|
||
{
|
||
config.MarginBottom = marginBottomValue;
|
||
}
|
||
|
||
var marginLeft = ConfigurationManager.AppSettings[$"{prefix}:MarginLeft"];
|
||
if (float.TryParse(marginLeft, out var marginLeftValue))
|
||
{
|
||
config.MarginLeft = marginLeftValue;
|
||
}
|
||
|
||
var marginRight = ConfigurationManager.AppSettings[$"{prefix}:MarginRight"];
|
||
if (float.TryParse(marginRight, out var marginRightValue))
|
||
{
|
||
config.MarginRight = marginRightValue;
|
||
}
|
||
|
||
// 读取公司名称 | Read company name
|
||
var companyName = ConfigurationManager.AppSettings[$"{prefix}:CompanyName"];
|
||
if (!string.IsNullOrEmpty(companyName))
|
||
{
|
||
config.CompanyName = companyName;
|
||
}
|
||
|
||
// 读取公司 Logo 路径 | Read company logo path
|
||
var companyLogo = ConfigurationManager.AppSettings[$"{prefix}:CompanyLogo"];
|
||
if (!string.IsNullOrEmpty(companyLogo))
|
||
{
|
||
config.CompanyLogo = companyLogo;
|
||
}
|
||
|
||
// 读取软件名称 | Read software name
|
||
var softwareName = ConfigurationManager.AppSettings[$"{prefix}:SoftwareName"];
|
||
if (!string.IsNullOrEmpty(softwareName))
|
||
{
|
||
config.SoftwareName = softwareName;
|
||
}
|
||
|
||
// 读取软件 Logo 路径 | Read software logo path
|
||
var softwareLogo = ConfigurationManager.AppSettings[$"{prefix}:SoftwareLogo"];
|
||
if (!string.IsNullOrEmpty(softwareLogo))
|
||
{
|
||
config.SoftwareLogo = softwareLogo;
|
||
}
|
||
|
||
// 验证配置 | Validate configuration
|
||
ValidateConfig(config);
|
||
|
||
_logger.Info("报告引擎配置加载成功:输出目录={OutputDir}, 模板={Template} | Report config loaded: OutputDir={OutputDir}, Template={Template}",
|
||
config.OutputDirectory, config.TemplatePath);
|
||
|
||
return config;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.Warn("加载报告引擎配置失败,使用默认配置 | Failed to load report config, using defaults: {Message}", ex.Message);
|
||
return new ReportConfig();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证配置参数 | Validate configuration parameters
|
||
/// </summary>
|
||
/// <param name="config">报告配置对象 | Report configuration object</param>
|
||
private void ValidateConfig(ReportConfig config)
|
||
{
|
||
// 验证输出目录(不存在则尝试创建)| Validate output directory (create if not exists)
|
||
if (string.IsNullOrWhiteSpace(config.OutputDirectory))
|
||
{
|
||
_logger.Warn("OutputDirectory 为空,使用默认值 | OutputDirectory is empty, using default");
|
||
config.OutputDirectory = new ReportConfig().OutputDirectory;
|
||
}
|
||
|
||
// 验证模板路径 | Validate template path
|
||
if (string.IsNullOrWhiteSpace(config.TemplatePath))
|
||
{
|
||
_logger.Warn("TemplatePath 为空,使用默认值 | TemplatePath is empty, using default");
|
||
config.TemplatePath = @"Templates\StandardReportTemplate.json";
|
||
}
|
||
|
||
// 验证文件名模式 | Validate file name pattern
|
||
if (string.IsNullOrWhiteSpace(config.FileNamePattern))
|
||
{
|
||
_logger.Warn("FileNamePattern 为空,使用默认值 | FileNamePattern is empty, using default");
|
||
config.FileNamePattern = "{ReportId}";
|
||
}
|
||
|
||
// 验证页面方向 | Validate page orientation
|
||
if (config.DefaultOrientation != "Portrait" && config.DefaultOrientation != "Landscape")
|
||
{
|
||
_logger.Warn("DefaultOrientation 无效: {Value},使用默认值 Portrait | DefaultOrientation invalid: {Value}, using default Portrait", config.DefaultOrientation);
|
||
config.DefaultOrientation = "Portrait";
|
||
}
|
||
|
||
// 验证边距范围(0-100mm)| Validate margin range (0-100mm)
|
||
config.MarginTop = ClampMargin(config.MarginTop, "MarginTop");
|
||
config.MarginBottom = ClampMargin(config.MarginBottom, "MarginBottom");
|
||
config.MarginLeft = ClampMargin(config.MarginLeft, "MarginLeft");
|
||
config.MarginRight = ClampMargin(config.MarginRight, "MarginRight");
|
||
|
||
// 验证 Logo 路径(如果配置了则检查文件是否存在)| Validate logo path (check file exists if configured)
|
||
if (!string.IsNullOrEmpty(config.CompanyLogo))
|
||
{
|
||
var logoPath = Path.IsPathRooted(config.CompanyLogo)
|
||
? config.CompanyLogo
|
||
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.CompanyLogo);
|
||
|
||
if (!File.Exists(logoPath))
|
||
{
|
||
_logger.Warn("公司 Logo 文件不存在: {Path},将不显示 Logo | Company logo file not found: {Path}, logo will not be displayed", logoPath);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将边距值限制在有效范围内 | Clamp margin value to valid range
|
||
/// </summary>
|
||
private float ClampMargin(float value, string name)
|
||
{
|
||
if (value < 0 || value > 100)
|
||
{
|
||
_logger.Warn("{Name} 超出有效范围 [0, 100]: {Value},使用默认值 20 | {Name} out of valid range [0, 100]: {Value}, using default 20", name, value);
|
||
return 20f;
|
||
}
|
||
return value;
|
||
}
|
||
}
|
||
}
|