using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Haoliang.Core.Services;
using Haoliang.Models.System;
using Haoliang.Models.Production;
using Haoliang.Models.Template;
using Haoliang.Models.Common;
using Haoliang.Models.Models.System;
namespace Haoliang.Api.Controllers
{
[Route("api/v1/config")]
[ApiController]
public class ConfigController : ControllerBase
{
private readonly ISystemConfigService _systemService;
private readonly ITemplateService _templateService;
private readonly IRulesService _rulesService;
private readonly IProductionStatisticsService _statisticsService;
public ConfigController(
ISystemConfigService systemService,
ITemplateService templateService,
IRulesService rulesService,
IProductionStatisticsService statisticsService)
{
_systemService = systemService;
_templateService = templateService;
_rulesService = rulesService;
_statisticsService = statisticsService;
}
///
/// Get all system configuration
///
[HttpGet]
public async Task>> GetSystemConfiguration()
{
try
{
var config = await _systemService.GetSystemConfigurationAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting system configuration: {ex.Message}"));
}
}
///
/// Update system configuration
///
[HttpPut]
public async Task>> UpdateSystemConfiguration([FromBody] SystemConfiguration configuration)
{
try
{
var result = await _systemService.UpdateSystemConfigurationAsync(configuration);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating system configuration: {ex.Message}"));
}
}
///
/// Get production target configuration
///
[HttpGet("production-targets")]
public async Task>>> GetProductionTargets()
{
try
{
var targets = await _systemService.GetProductionTargetsAsync();
return Ok(ApiResponse>.Ok(targets));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse>.InternalServerError($"Error getting production targets: {ex.Message}"));
}
}
///
/// Update production targets
///
[HttpPut("production-targets")]
public async Task>> UpdateProductionTargets([FromBody] List targets)
{
try
{
var result = await _systemService.UpdateProductionTargetsAsync(targets);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating production targets: {ex.Message}"));
}
}
///
/// Get working hours configuration
///
[HttpGet("working-hours")]
public async Task>> GetWorkingHoursConfig()
{
try
{
var config = await _systemService.GetWorkingHoursConfigAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting working hours config: {ex.Message}"));
}
}
///
/// Update working hours configuration
///
[HttpPut("working-hours")]
public async Task>> UpdateWorkingHoursConfig([FromBody] WorkingHoursConfig config)
{
try
{
var result = await _systemService.UpdateWorkingHoursConfigAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating working hours config: {ex.Message}"));
}
}
///
/// Get alert configuration
///
[HttpGet("alerts")]
public async Task>> GetAlertConfiguration()
{
try
{
var config = await _systemService.GetAlertConfigurationAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting alert configuration: {ex.Message}"));
}
}
///
/// Update alert configuration
///
[HttpPut("alerts")]
public async Task>> UpdateAlertConfiguration([FromBody] AlertConfiguration config)
{
try
{
var result = await _systemService.UpdateAlertConfigurationAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating alert configuration: {ex.Message}"));
}
}
///
/// Get business rules configuration
///
[HttpGet("rules")]
public async Task>>> GetBusinessRules()
{
try
{
var rules = await _rulesService.GetAllRulesAsync();
var ruleConfigs = rules.Select(r => new Haoliang.Models.System.BusinessRuleConfig
{
RuleId = r.RuleId,
RuleName = r.RuleName,
RuleType = r.RuleType,
Category = r.Category,
IsEnabled = r.IsEnabled,
Configuration = r.Configuration,
CreatedAt = r.CreatedAt,
UpdatedAt = r.UpdatedAt
}).ToList();
return Ok(ApiResponse>.Ok(ruleConfigs));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse>.InternalServerError($"Error getting business rules: {ex.Message}"));
}
}
///
/// Create or update business rule
///
[HttpPost("rules")]
public async Task>> CreateOrUpdateBusinessRule([FromBody] Haoliang.Models.System.BusinessRuleConfig rule)
{
try
{
var businessRule = new Haoliang.Core.Services.BusinessRule
{
RuleId = rule.RuleId,
RuleName = rule.RuleName,
RuleType = rule.RuleType,
Category = rule.Category,
IsEnabled = rule.IsEnabled,
Configuration = rule.Configuration,
CreatedAt = rule.CreatedAt,
UpdatedAt = rule.UpdatedAt
};
var result = await _rulesService.CreateOrUpdateRuleAsync(businessRule);
var resultConfig = new Haoliang.Models.System.BusinessRuleConfig
{
RuleId = result.RuleId,
RuleName = result.RuleName,
RuleType = result.RuleType,
Category = result.Category,
IsEnabled = result.IsEnabled,
Configuration = result.Configuration,
CreatedAt = result.CreatedAt,
UpdatedAt = result.UpdatedAt
};
return Ok(ApiResponse.Ok(resultConfig));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error creating/updating business rule: {ex.Message}"));
}
}
///
/// Delete business rule
///
[HttpDelete("rules/{ruleId}")]
public async Task>> DeleteBusinessRule(int ruleId)
{
try
{
var result = await _rulesService.DeleteRuleAsync(ruleId);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error deleting business rule: {ex.Message}"));
}
}
///
/// Get statistical analysis rules
///
[HttpGet("statistics-rules")]
public async Task>>> GetStatisticsRules()
{
try
{
var rules = await _rulesService.GetStatisticsRulesAsync();
var ruleConfigs = rules.Select(r => new Haoliang.Models.System.StatisticsRuleConfig
{
ConfigId = r.RuleId,
ConfigName = r.RuleName,
ConfigType = r.RuleType,
Formula = r.Configuration,
GroupBy = "",
IsActive = r.IsEnabled,
CreatedAt = r.CreatedAt,
UpdatedAt = r.UpdatedAt
}).ToList();
return Ok(ApiResponse>.Ok(ruleConfigs));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse>.InternalServerError($"Error getting statistics rules: {ex.Message}"));
}
}
///
/// Update statistics rules
///
[HttpPut("statistics-rules")]
public async Task>> UpdateStatisticsRules([FromBody] List rules)
{
try
{
var businessRules = rules.Select(r => new Haoliang.Core.Services.BusinessRule
{
RuleId = r.ConfigId,
RuleName = r.ConfigName,
RuleType = r.ConfigType,
Category = r.GroupBy,
IsEnabled = r.IsActive,
Configuration = r.Formula,
CreatedAt = r.CreatedAt,
UpdatedAt = r.UpdatedAt
}).ToList();
var result = await _rulesService.UpdateStatisticsRulesAsync(businessRules);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating statistics rules: {ex.Message}"));
}
}
///
/// Get data retention configuration
///
[HttpGet("data-retention")]
public async Task>> GetDataRetentionConfig()
{
try
{
var config = await _systemService.GetDataRetentionConfigAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting data retention config: {ex.Message}"));
}
}
///
/// Update data retention configuration
///
[HttpPut("data-retention")]
public async Task>> UpdateDataRetentionConfig([FromBody] DataRetentionConfig config)
{
try
{
var result = await _systemService.UpdateDataRetentionConfigAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating data retention config: {ex.Message}"));
}
}
///
/// Get dashboard configuration
///
[HttpGet("dashboard")]
public async Task>> GetDashboardConfig()
{
try
{
var config = await _systemService.GetDashboardConfigAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting dashboard config: {ex.Message}"));
}
}
///
/// Update dashboard configuration
///
[HttpPut("dashboard")]
public async Task>> UpdateDashboardConfig([FromBody] DashboardConfig config)
{
try
{
var result = await _systemService.UpdateDashboardConfigAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating dashboard config: {ex.Message}"));
}
}
///
/// Get export configuration
///
[HttpGet("export")]
public async Task>> GetExportConfig()
{
try
{
var config = await _systemService.GetExportConfigAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting export config: {ex.Message}"));
}
}
///
/// Update export configuration
///
[HttpPut("export")]
public async Task>> UpdateExportConfig([FromBody] ExportConfig config)
{
try
{
var result = await _systemService.UpdateExportConfigAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating export config: {ex.Message}"));
}
}
///
/// Get collection configuration
///
[HttpGet("collection")]
public async Task>> GetCollectionConfig()
{
try
{
var config = await _systemService.GetCollectionConfigAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting collection config: {ex.Message}"));
}
}
///
/// Update collection configuration
///
[HttpPut("collection")]
public async Task>> UpdateCollectionConfig([FromBody] CollectionConfig config)
{
try
{
var result = await _systemService.UpdateCollectionConfigAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating collection config: {ex.Message}"));
}
}
///
/// Get notification configuration
///
[HttpGet("notifications")]
public async Task>> GetNotificationConfig()
{
try
{
var config = await _systemService.GetNotificationConfigAsync();
return Ok(ApiResponse.Ok(config));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error getting notification config: {ex.Message}"));
}
}
///
/// Update notification configuration
///
[HttpPut("notifications")]
public async Task>> UpdateNotificationConfig([FromBody] NotificationConfig config)
{
try
{
var result = await _systemService.UpdateNotificationConfigAsync(config);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error updating notification config: {ex.Message}"));
}
}
///
/// Validate configuration
///
[HttpPost("validate")]
public async Task>> ValidateConfiguration([FromBody] object configuration)
{
try
{
var result = await _systemService.ValidateConfigurationAsync(configuration);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error validating configuration: {ex.Message}"));
}
}
///
/// Export configuration
///
[HttpGet("export")]
public async Task> ExportConfiguration()
{
try
{
var config = await _systemService.GetSystemConfigurationAsync();
var json = System.Text.Json.JsonSerializer.Serialize(config, new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true
});
return File(System.Text.Encoding.UTF8.GetBytes(json), "application/json", "system-configuration.json");
}
catch (Exception ex)
{
return StatusCode(500, new { error = $"Error exporting configuration: {ex.Message}" });
}
}
///
/// Import configuration
///
[HttpPost("import")]
public async Task>> ImportConfiguration([FromBody] SystemConfiguration configuration)
{
try
{
var json = System.Text.Json.JsonSerializer.Serialize(configuration);
var result = await _systemService.ImportConfigurationAsync(json);
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error importing configuration: {ex.Message}"));
}
}
///
/// Reset to default configuration
///
[HttpPost("reset")]
public async Task>> ResetToDefault()
{
try
{
var result = await _systemService.ResetToDefaultConfigurationAsync();
return Ok(ApiResponse.Ok(result));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse.InternalServerErrorResult($"Error resetting configuration: {ex.Message}"));
}
}
///
/// Get configuration change history
///
[HttpGet("history")]
public async Task>>> GetConfigurationHistory([FromQuery] DateTime? startDate = null, [FromQuery] DateTime? endDate = null)
{
try
{
var history = await _systemService.GetConfigurationChangeHistoryAsync(startDate, endDate);
return Ok(ApiResponse>.Ok(history));
}
catch (Exception ex)
{
return StatusCode(500, ApiResponse>.InternalServerError($"Error getting configuration history: {ex.Message}"));
}
}
}
}