using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Haoliang.Models.System; using Haoliang.Data.Repositories; namespace Haoliang.Data.Repositories { public interface ISystemConfigRepository : IRepository { Task GetByKeyAsync(string configKey); Task DeleteByKeyAsync(string configKey); Task KeyExistsAsync(string configKey); Task> GetByCategoryAsync(string category); SystemConfig UpsertAsync(SystemConfig config); Task GetValueAsync(string configKey); Task SetValueAsync(string configKey, string value); Task> GetActiveConfigsAsync(); Task GetDefaultConfigAsync(); Task UpdateConfigValueAsync(string configKey, string value); } public class SystemConfigRepository : Repository, ISystemConfigRepository { private readonly CNCDbContext _context; public SystemConfigRepository(CNCDbContext context) : base(context) { _context = context; } public async Task GetByKeyAsync(string configKey) { return await _context.SystemConfigs .FirstOrDefaultAsync(c => c.ConfigKey == configKey); } public async Task DeleteByKeyAsync(string configKey) { var config = await GetByKeyAsync(configKey); if (config != null) { _context.SystemConfigs.Remove(config); await SaveAsync(); return true; } return false; } public async Task KeyExistsAsync(string configKey) { return await _context.SystemConfigs .AnyAsync(c => c.ConfigKey == configKey); } public async Task> GetByCategoryAsync(string category) { return await _context.SystemConfigs .Where(c => c.Category == category) .OrderBy(c => c.ConfigKey) .ToListAsync(); } public SystemConfig UpsertAsync(SystemConfig config) { var existing = _context.SystemConfigs .FirstOrDefault(c => c.ConfigKey == config.ConfigKey); if (existing != null) { // Update existing existing.ConfigValue = config.ConfigValue; existing.Description = config.Description; existing.UpdatedAt = DateTime.Now; existing.IsActive = config.IsActive; existing.Category = config.Category; _context.SystemConfigs.Update(existing); } else { // Insert new config.CreatedAt = DateTime.Now; config.UpdatedAt = DateTime.Now; _context.SystemConfigs.Add(config); } SaveAsync(); return config; } public async Task GetValueAsync(string configKey) { var config = await GetByKeyAsync(configKey); return config?.ConfigValue; } public async Task SetValueAsync(string configKey, string value) { var config = await GetByKeyAsync(configKey); if (config != null) { config.ConfigValue = value; config.UpdatedAt = DateTime.Now; await SaveAsync(); return true; } return false; } public async Task> GetActiveConfigsAsync() { return await _context.SystemConfigs .Where(c => c.IsActive) .OrderBy(c => c.Category) .ThenBy(c => c.ConfigKey) .ToListAsync(); } public async Task GetDefaultConfigAsync() { return await _context.SystemConfigs .FirstOrDefaultAsync(c => c.IsDefault); } public async Task UpdateConfigValueAsync(string configKey, string value) { var config = await GetByKeyAsync(configKey); if (config != null) { config.ConfigValue = value; config.UpdatedAt = DateTime.Now; await SaveAsync(); return true; } return false; } } }