using WmsLite.Api.Common; using WmsLite.Api.Dtos.Template; using WmsLite.Api.Infrastructure.Auth; using WmsLite.Api.Models; using WmsLite.Api.Repositories.Interfaces; using WmsLite.Api.Services.Interfaces; namespace WmsLite.Api.Services; public class SysTemplateService : ISysTemplateService { private readonly ISysTemplateRepository _repository; private readonly ICurrentUserContext _userContext; public SysTemplateService(ISysTemplateRepository repository, ICurrentUserContext userContext) { _repository = repository; _userContext = userContext; } public async Task> GetPagedListAsync(SysTemplatePagedQuery query) { var page = await _repository.GetPagedListAsync(query); return new PagedResult { PageIndex = page.PageIndex, PageSize = page.PageSize, TotalCount = page.TotalCount, Items = page.Items.ConvertAll(Map) }; } public async Task CreateAsync(SysTemplateSaveRequest request) { TemplateConfigJsonValidator.ValidateAndParse(request.ConfigJson); var bizType = request.BizType.Trim(); if (await _repository.GetByBizTypeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), bizType) is not null) { throw new BusinessException($"Template '{bizType}' already exists."); } var entity = new SysTemplate { TenantId = _userContext.TenantId.Trim(), OwnerCode = _userContext.OwnerCode.Trim(), BizType = bizType, BizName = request.BizName.Trim(), ConfigJson = request.ConfigJson }; await _repository.InsertAsync(entity); return Map(entity); } public async Task UpdateAsync(SysTemplateUpdateRequest request) { TemplateConfigJsonValidator.ValidateAndParse(request.ConfigJson); var entity = await _repository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim()); if (entity is null) { return null; } var bizType = request.BizType.Trim(); var duplicate = await _repository.GetByBizTypeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), bizType); if (duplicate is not null && duplicate.Id != entity.Id) { throw new BusinessException($"Template '{bizType}' already exists."); } entity.BizType = bizType; entity.BizName = request.BizName.Trim(); entity.ConfigJson = request.ConfigJson; await _repository.UpdateAsync(entity); return Map(entity); } public Task DeleteAsync(long id, string tenantId, string ownerCode) { return _repository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim()); } private static SysTemplateViewDto Map(SysTemplate entity) { return new SysTemplateViewDto { Id = entity.Id, TenantId = entity.TenantId, OwnerCode = entity.OwnerCode, BizType = entity.BizType, BizName = entity.BizName, ConfigJson = entity.ConfigJson }; } }