using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Haoliang.Core.Services; using Haoliang.Models.Models.System; using Haoliang.Models.Models.Production; using Haoliang.Models.Models.Template; using Haoliang.Models.Common; 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.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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>.Success(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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"Error updating alert configuration: {ex.Message}")); } } /// /// Get business rules configuration /// [HttpGet("rules")] public async Task>>> GetBusinessRules() { try { var rules = await _rulesService.GetAllRulesAsync(); return Ok(ApiResponse>.Success(rules)); } 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] BusinessRuleConfig rule) { try { var result = await _rulesService.CreateOrUpdateRuleAsync(rule); return Ok(ApiResponse.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"Error deleting business rule: {ex.Message}")); } } /// /// Get statistical analysis rules /// [HttpGet("statistics-rules")] public async Task>>> GetStatisticsRules() { try { var rules = await _rulesService.GetStatisticsRulesAsync(); return Ok(ApiResponse>.Success(rules)); } 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 result = await _rulesService.UpdateStatisticsRulesAsync(rules); return Ok(ApiResponse.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"Error updating dashboard config: {ex.Message}")); } } /// /// Get export configuration /// [HttpGet("export")] public async Task>> GetExportConfig() { try { var config = await _systemService.GetExportConfigAsync(); return Ok(ApiResponse.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"Error updating export config: {ex.Message}")); } } /// /// Get collection configuration /// [HttpGet("collection")] public async Task>> GetCollectionConfig() { try { var config = await _systemService.GetCollectionConfigAsync(); return Ok(ApiResponse.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"Error updating collection config: {ex.Message}")); } } /// /// Get notification configuration /// [HttpGet("notifications")] public async Task>> GetNotificationConfig() { try { var config = await _systemService.GetNotificationConfigAsync(); return Ok(ApiResponse.Success(config)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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 result = await _systemService.ImportConfigurationAsync(configuration); return Ok(ApiResponse.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"Error importing configuration: {ex.Message}")); } } /// /// Reset to default configuration /// [HttpPost("reset")] public async Task>> ResetToDefault() { try { var result = await _systemService.ResetToDefaultConfigurationAsync(); return Ok(ApiResponse.Success(result)); } catch (Exception ex) { return StatusCode(500, ApiResponse.InternalServerError($"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>.Success(history)); } catch (Exception ex) { return StatusCode(500, ApiResponse>.InternalServerError($"Error getting configuration history: {ex.Message}")); } } } }