新增产品公共内容模板
This commit is contained in:
1 parent
4345bef876
commit
f26b663e46
30 files changed
+1069
-18
No files matched your search
@@ -9,6 +9,8 @@ import com.shoplix.modules.product.repository.service.ProductAttributeRepository
|
||||
import com.shoplix.modules.product.repository.service.ProductAttributeValueRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductMediaRelationRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductCategoryRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductContentTemplateRepositoryService;
|
||||
import com.shoplix.modules.product.enums.ProductTemplatePositionEnum;
|
||||
import com.shoplix.modules.media.repository.service.MediaAssetRepositoryService;
|
||||
import com.shoplix.site.modules.product.res.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -27,18 +29,21 @@ public class SiteProductService {
|
||||
private final ProductMediaRelationRepositoryService productMediaRepository;
|
||||
private final MediaAssetRepositoryService mediaRepository;
|
||||
private final ProductCategoryRepositoryService categoryRepository;
|
||||
private final ProductContentTemplateRepositoryService templateRepository;
|
||||
/** 注入产品数据源。 */
|
||||
public SiteProductService(ProductRepositoryService repository, ProductSpecificationRepositoryService specificationRepository,
|
||||
ProductAttributeRepositoryService attributeRepository,
|
||||
ProductAttributeValueRepositoryService attributeValueRepository,
|
||||
ProductMediaRelationRepositoryService productMediaRepository,
|
||||
MediaAssetRepositoryService mediaRepository,
|
||||
ProductCategoryRepositoryService categoryRepository) {
|
||||
ProductCategoryRepositoryService categoryRepository,
|
||||
ProductContentTemplateRepositoryService templateRepository) {
|
||||
this.repository = repository; this.specificationRepository = specificationRepository;
|
||||
this.attributeRepository = attributeRepository; this.attributeValueRepository = attributeValueRepository;
|
||||
this.productMediaRepository = productMediaRepository;
|
||||
this.mediaRepository = mediaRepository;
|
||||
this.categoryRepository = categoryRepository;
|
||||
this.templateRepository = templateRepository;
|
||||
}
|
||||
/** 查询当前站点已发布产品。 */
|
||||
public ProductListSiteRes list(String categoryCode) {
|
||||
@@ -108,7 +113,29 @@ public class SiteProductService {
|
||||
.filter(image -> image.url() != null).toList();
|
||||
if (images.isEmpty() && item.getCoverMediaCode() != null && mediaUrl != null) images = List.of(new ProductImageSiteRes(item.getCoverMediaCode(), mediaUrl, 0));
|
||||
return new ProductDetailSiteRes(item.getProductCode(), item.getProductName(), item.getSlug(), item.getModel(),
|
||||
item.getSummary(), item.getProductContent(), item.getCoverMediaCode(), mediaUrl, images,
|
||||
item.getSummary(), resolveProductContent(item), item.getCoverMediaCode(), mediaUrl, images,
|
||||
item.getInquiryEnabled() == 1, attributes, specifications);
|
||||
}
|
||||
|
||||
/** 按产品配置将已发布公共模板动态拼接到独立详情前后。 */
|
||||
private String resolveProductContent(ProductEntity product) {
|
||||
String productContent = product.getProductContent() == null ? "" : product.getProductContent();
|
||||
if (product.getContentTemplateCode() == null || product.getContentTemplateCode().isBlank()) {
|
||||
return productContent;
|
||||
}
|
||||
return templateRepository.findPublished(product.getSiteCode(), product.getContentTemplateCode())
|
||||
.map(template -> composeProductContent(productContent, template.getTemplateContent(),
|
||||
ProductTemplatePositionEnum.valueOf(product.getTemplatePosition())))
|
||||
.orElse(productContent);
|
||||
}
|
||||
|
||||
/** 按指定插入位置组合产品独立详情与公共模板内容。 */
|
||||
static String composeProductContent(String productContent, String templateContent,
|
||||
ProductTemplatePositionEnum position) {
|
||||
String safeProductContent = productContent == null ? "" : productContent;
|
||||
String safeTemplateContent = templateContent == null ? "" : templateContent;
|
||||
return ProductTemplatePositionEnum.BEFORE == position
|
||||
? safeTemplateContent + safeProductContent
|
||||
: safeProductContent + safeTemplateContent;
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,16 @@ import com.shoplix.common.context.SiteContext;
|
||||
import com.shoplix.common.context.SiteContextHolder;
|
||||
import com.shoplix.modules.media.repository.entity.MediaAssetEntity;
|
||||
import com.shoplix.modules.media.repository.service.MediaAssetRepositoryService;
|
||||
import com.shoplix.modules.product.repository.entity.ProductEntity;
|
||||
import com.shoplix.modules.product.enums.ProductTemplatePositionEnum;
|
||||
import com.shoplix.modules.product.repository.entity.ProductCategoryEntity;
|
||||
import com.shoplix.modules.product.repository.entity.ProductEntity;
|
||||
import com.shoplix.modules.product.repository.service.ProductAttributeRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductAttributeValueRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductCategoryRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductContentTemplateRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductMediaRelationRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductSpecificationRepositoryService;
|
||||
import com.shoplix.modules.product.repository.service.ProductCategoryRepositoryService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -20,11 +22,11 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
|
||||
/** 前台产品列表业务测试。 */
|
||||
/** 前台产品列表和产品模板业务测试。 */
|
||||
class SiteProductServiceTest {
|
||||
/** 测试站点业务编号。 */
|
||||
private static final String SITE_CODE = "SIT0000000000001";
|
||||
@@ -45,7 +47,8 @@ class SiteProductServiceTest {
|
||||
categoryRepository = mock(ProductCategoryRepositoryService.class);
|
||||
service = new SiteProductService(productRepository, mock(ProductSpecificationRepositoryService.class),
|
||||
mock(ProductAttributeRepositoryService.class), mock(ProductAttributeValueRepositoryService.class),
|
||||
mock(ProductMediaRelationRepositoryService.class), mediaRepository, categoryRepository);
|
||||
mock(ProductMediaRelationRepositoryService.class), mediaRepository, categoryRepository,
|
||||
mock(ProductContentTemplateRepositoryService.class));
|
||||
SiteContextHolder.set(new SiteContext(SITE_CODE, "site.steliro.com"));
|
||||
}
|
||||
|
||||
@@ -96,6 +99,33 @@ class SiteProductServiceTest {
|
||||
&& codes.containsAll(List.of("CAT001", "CAT002", "CAT003"))));
|
||||
}
|
||||
|
||||
/** 验证公共模板能够插入产品独立详情之前。 */
|
||||
@Test
|
||||
void shouldInsertTemplateBeforeProductContent() {
|
||||
String content = SiteProductService.composeProductContent(
|
||||
"<p>产品详情</p>", "<p>公共介绍</p>", ProductTemplatePositionEnum.BEFORE);
|
||||
|
||||
assertThat(content).isEqualTo("<p>公共介绍</p><p>产品详情</p>");
|
||||
}
|
||||
|
||||
/** 验证公共模板能够插入产品独立详情之后。 */
|
||||
@Test
|
||||
void shouldInsertTemplateAfterProductContent() {
|
||||
String content = SiteProductService.composeProductContent(
|
||||
"<p>产品详情</p>", "<p>公共保障</p>", ProductTemplatePositionEnum.AFTER);
|
||||
|
||||
assertThat(content).isEqualTo("<p>产品详情</p><p>公共保障</p>");
|
||||
}
|
||||
|
||||
/** 验证空模板内容不会影响产品独立详情。 */
|
||||
@Test
|
||||
void shouldKeepProductContentWhenTemplateContentIsNull() {
|
||||
String content = SiteProductService.composeProductContent(
|
||||
"<p>产品详情</p>", null, ProductTemplatePositionEnum.AFTER);
|
||||
|
||||
assertThat(content).isEqualTo("<p>产品详情</p>");
|
||||
}
|
||||
|
||||
/** 构造测试产品分类。 */
|
||||
private ProductCategoryEntity category(String categoryCode, String parentCategoryCode) {
|
||||
ProductCategoryEntity category = new ProductCategoryEntity();
|
||||
|
||||
Reference in new issue
Block a user