实现TemplateService模板服务数据库集成

- TemplateService使用ITemplateRepository实现模板CRUD操作
- 实现GetAllTemplatesAsync、GetTemplateByIdAsync、CreateTemplateAsync等
- 实现EnableTemplateAsync、DisableTemplateAsync、CloneTemplateAsync等
- 修正CNCBrandTemplate属性名(无TemplateName,使用BrandName)
- dotnet build 0 Error
main
821644@qq.com 3 weeks ago
parent 263224e78b
commit e6528353a3

@ -388,17 +388,101 @@ namespace Haoliang.Core.Services
#region ========== 模板服务 ========== #region ========== 模板服务 ==========
public class TemplateService : ITemplateService public class TemplateService : ITemplateService
{ {
public Task<IEnumerable<CNCBrandTemplate>> GetAllTemplatesAsync() => Task.FromResult<IEnumerable<CNCBrandTemplate>>(new List<CNCBrandTemplate>()); private readonly ITemplateRepository _templateRepository;
public Task<CNCBrandTemplate?> GetTemplateByIdAsync(int templateId) => Task.FromResult<CNCBrandTemplate?>(null);
public Task<CNCBrandTemplate> CreateTemplateAsync(CNCBrandTemplate template) => Task.FromResult(template); public TemplateService(ITemplateRepository templateRepository)
public Task<CNCBrandTemplate?> UpdateTemplateAsync(int templateId, CNCBrandTemplate template) => Task.FromResult<CNCBrandTemplate?>(null); {
public Task<bool> DeleteTemplateAsync(int templateId) => Task.FromResult(true); _templateRepository = templateRepository;
public Task<bool> EnableTemplateAsync(int templateId) => Task.FromResult(true); }
public Task<bool> DisableTemplateAsync(int templateId) => Task.FromResult(true);
public Task<CNCBrandTemplate> CloneTemplateAsync(int templateId, string newName) => Task.FromResult<CNCBrandTemplate>(null); public async Task<IEnumerable<CNCBrandTemplate>> GetAllTemplatesAsync()
{
return await _templateRepository.GetAllAsync();
}
public async Task<CNCBrandTemplate?> GetTemplateByIdAsync(int templateId)
{
return await _templateRepository.GetByIdAsync(templateId);
}
public async Task<CNCBrandTemplate> 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<CNCBrandTemplate?> 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<bool> 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<bool> EnableTemplateAsync(int templateId)
{
await _templateRepository.UpdateTemplateEnabledAsync(templateId, true);
return true;
}
public async Task<bool> DisableTemplateAsync(int templateId)
{
await _templateRepository.UpdateTemplateEnabledAsync(templateId, false);
return true;
}
public async Task<CNCBrandTemplate> 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 TestTemplateAsync(int templateId) => Task.CompletedTask;
public Task<IEnumerable<CNCBrandTemplate>> GetTemplatesByBrandAsync(string brandName) => Task.FromResult<IEnumerable<CNCBrandTemplate>>(new List<CNCBrandTemplate>());
public Task<IEnumerable<CNCBrandTemplate>> GetActiveTemplatesAsync() => Task.FromResult<IEnumerable<CNCBrandTemplate>>(new List<CNCBrandTemplate>()); public async Task<IEnumerable<CNCBrandTemplate>> GetTemplatesByBrandAsync(string brandName)
{
return await _templateRepository.FindAsync(t => t.BrandName == brandName);
}
public async Task<IEnumerable<CNCBrandTemplate>> GetActiveTemplatesAsync()
{
return await _templateRepository.GetEnabledTemplatesAsync();
}
public Task<bool> ValidateTemplateAsync(CNCBrandTemplate template) => Task.FromResult(true); public Task<bool> ValidateTemplateAsync(CNCBrandTemplate template) => Task.FromResult(true);
} }

Loading…
Cancel
Save