diff --git a/Haoliang.Core/Services/StubServices.cs b/Haoliang.Core/Services/StubServices.cs index ba7e7e1..813f76b 100644 --- a/Haoliang.Core/Services/StubServices.cs +++ b/Haoliang.Core/Services/StubServices.cs @@ -388,17 +388,101 @@ namespace Haoliang.Core.Services #region ========== 模板服务 ========== public class TemplateService : ITemplateService { - public Task> GetAllTemplatesAsync() => Task.FromResult>(new List()); - public Task GetTemplateByIdAsync(int templateId) => Task.FromResult(null); - public Task CreateTemplateAsync(CNCBrandTemplate template) => Task.FromResult(template); - public Task UpdateTemplateAsync(int templateId, CNCBrandTemplate template) => Task.FromResult(null); - public Task DeleteTemplateAsync(int templateId) => Task.FromResult(true); - public Task EnableTemplateAsync(int templateId) => Task.FromResult(true); - public Task DisableTemplateAsync(int templateId) => Task.FromResult(true); - public Task CloneTemplateAsync(int templateId, string newName) => Task.FromResult(null); + private readonly ITemplateRepository _templateRepository; + + public TemplateService(ITemplateRepository templateRepository) + { + _templateRepository = templateRepository; + } + + public async Task> GetAllTemplatesAsync() + { + return await _templateRepository.GetAllAsync(); + } + + public async Task GetTemplateByIdAsync(int templateId) + { + return await _templateRepository.GetByIdAsync(templateId); + } + + public async Task CreateTemplateAsync(CNCBrandTemplate template) + { + template.CreatedAt = DateTime.Now; + template.UpdatedAt = DateTime.Now; + template.IsEnabled = true; + await _templateRepository.AddAsync(template); + await _templateRepository.SaveAsync(); + return template; + } + + public async Task UpdateTemplateAsync(int templateId, CNCBrandTemplate template) + { + var existing = await _templateRepository.GetByIdAsync(templateId); + if (existing == null) return null; + + existing.BrandName = template.BrandName; + existing.Description = template.Description; + existing.FieldMappings = template.FieldMappings; + existing.UpdatedAt = DateTime.Now; + + _templateRepository.Update(existing); + await _templateRepository.SaveAsync(); + return existing; + } + + public async Task DeleteTemplateAsync(int templateId) + { + var template = await _templateRepository.GetByIdAsync(templateId); + if (template == null) return false; + + _templateRepository.Remove(template); + return await _templateRepository.SaveAsync() > 0; + } + + public async Task EnableTemplateAsync(int templateId) + { + await _templateRepository.UpdateTemplateEnabledAsync(templateId, true); + return true; + } + + public async Task DisableTemplateAsync(int templateId) + { + await _templateRepository.UpdateTemplateEnabledAsync(templateId, false); + return true; + } + + public async Task CloneTemplateAsync(int templateId, string newName) + { + var original = await _templateRepository.GetByIdAsync(templateId); + if (original == null) return null; + + var cloned = new CNCBrandTemplate + { + BrandName = newName, + Description = original.Description + " (Cloned)", + FieldMappings = original.FieldMappings, + IsEnabled = false, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now + }; + + await _templateRepository.AddAsync(cloned); + await _templateRepository.SaveAsync(); + return cloned; + } + public Task TestTemplateAsync(int templateId) => Task.CompletedTask; - public Task> GetTemplatesByBrandAsync(string brandName) => Task.FromResult>(new List()); - public Task> GetActiveTemplatesAsync() => Task.FromResult>(new List()); + + public async Task> GetTemplatesByBrandAsync(string brandName) + { + return await _templateRepository.FindAsync(t => t.BrandName == brandName); + } + + public async Task> GetActiveTemplatesAsync() + { + return await _templateRepository.GetEnabledTemplatesAsync(); + } + public Task ValidateTemplateAsync(CNCBrandTemplate template) => Task.FromResult(true); }