xin
2 hours ago 501eba8c1c5b58fb3e5f442dcf892a460148e0c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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<ProductImage> 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<ProductLabel> 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);
    }
}