package com.oying.modules.pc.product.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.oying.modules.pc.product.converter.ProductImageAssembler; import com.oying.modules.pc.product.converter.ProductLabelAssembler; import com.oying.modules.pc.product.domain.Product; import com.oying.modules.pc.product.domain.ProductImage; import com.oying.modules.pc.product.domain.ProductLabel; import com.oying.modules.pc.product.domain.dto.ProductCreateRequest; import com.oying.modules.pc.product.domain.dto.ProductImageCreateRequest; import com.oying.modules.pc.product.domain.dto.ProductMerchantCreateRequest; import com.oying.modules.pc.product.domain.enums.ProductCreationType; import com.oying.modules.pc.product.domain.enums.ProductStatusEnum; import com.oying.modules.pc.product.service.ProductCreationStrategy; import com.oying.modules.pc.product.service.ProductImageService; import com.oying.modules.pc.product.service.ProductLabelService; import com.oying.modules.pc.product.service.ProductService; import com.oying.modules.pc.utils.ImageUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Component; import java.util.List; import java.util.stream.Collectors; @Slf4j @Component @RequiredArgsConstructor public class ProductDirectCreator implements ProductCreationStrategy { private final ProductService productService; private final ProductImageService productImageService; private final ProductLabelService productLabelService; @Override public ProductCreationType getType() { return ProductCreationType.DIRECT; } @Override public void create(ProductCreateRequest resource) { ProductMerchantCreateRequest request = (ProductMerchantCreateRequest) resource; Product product = new Product(); BeanUtils.copyProperties(request, product); request.getImages().stream().findFirst().map(ProductImageCreateRequest::getUploadFileId) .ifPresent(id -> { product.setMainImageId(id.toString()); product.setMainImageUrl(ImageUtils.getPublicObjectUrl(id)); }); product.setStatus(ProductStatusEnum.DRAFT.getValue()); productService.create(product); List productImages = request.getImages().stream().map(i -> { i.setProductId(product.getProductId()); return ProductImageAssembler.to(i); }).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(productImages)) { productImageService.saveBatch(productImages); } List productLabels = request.getLabels().stream().map(i -> { i.setProductId(product.getProductId()); return ProductLabelAssembler.to(i); }).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(productLabels)) { productLabelService.saveBatch(productLabels); } } @Override public boolean supports(ProductCreationType type) { return ProductCreationType.DIRECT.equals(type); } }