From e6528353a31a64b6ca43a9db7ebf575e8d4d711a Mon Sep 17 00:00:00 2001 From: "821644@qq.com" <821644@qq.com> Date: Mon, 13 Apr 2026 11:40:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0TemplateService=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=9C=8D=E5=8A=A1=E6=95=B0=E6=8D=AE=E5=BA=93=E9=9B=86?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TemplateService使用ITemplateRepository实现模板CRUD操作 - 实现GetAllTemplatesAsync、GetTemplateByIdAsync、CreateTemplateAsync等 - 实现EnableTemplateAsync、DisableTemplateAsync、CloneTemplateAsync等 - 修正CNCBrandTemplate属性名(无TemplateName,使用BrandName) - dotnet build 0 Error --- Haoliang.Core/Services/StubServices.cs | 104 ++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 10 deletions(-) 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); }