zepengdev
2025-06-14 6e0a83c55db4bae4d23a4c281946bda1d610f678
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.oying.modules.pc.product.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.oying.exception.EntityExistException;
import com.oying.exception.EntityNotFoundException;
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.ProductImageCreateRequest;
import com.oying.modules.pc.product.domain.dto.ProductLabelCreateRequest;
import com.oying.modules.pc.product.domain.dto.ProductMerchantCreateRequest;
import com.oying.modules.pc.product.domain.dto.ProductMerchantUpdateRequest;
import com.oying.modules.pc.product.domain.enums.ProductStatusEnum;
import com.oying.modules.pc.product.service.ProductImageService;
import com.oying.modules.pc.product.service.ProductLabelService;
import com.oying.modules.pc.product.service.ProductMerchantService;
import com.oying.modules.pc.product.service.ProductService;
import com.oying.modules.pc.utils.ImageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class ProductMerchantServiceImpl implements ProductMerchantService {
 
    private final ProductService productService;
    private final ProductImageService productImageService;
    private final ProductLabelService productLabelService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProductMerchantCreateRequest request) {
        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
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductMerchantUpdateRequest request) {
        Product existingProduct = this.findOrThrow(request.getProductId());
        this.processImagesUpdate(request);
        this.processLabelsUpdate(request);
        BeanUtils.copyProperties(request, existingProduct);
        productService.update(existingProduct);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateImages(ProductMerchantUpdateRequest request) {
        this.findOrThrow(request.getProductId());
        this.processImagesUpdate(request);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLabels(ProductMerchantUpdateRequest request) {
        this.findOrThrow(request.getProductId());
        this.processImagesUpdate(request);
    }
 
    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        productService.deleteAll(ids);
    }
 
    @Override
    public void putOnShelf(Long productId) {
        LambdaUpdateWrapper<Product> wrapper = new LambdaUpdateWrapper<Product>()
                .eq(Product::getProductId, productId)
                .set(Product::getStatus, ProductStatusEnum.AVAILABLE.getValue());
        productService.update(wrapper);
    }
 
    @Override
    public void takeOffShelf(Long productId) {
        LambdaUpdateWrapper<Product> wrapper = new LambdaUpdateWrapper<Product>()
                .eq(Product::getProductId, productId)
                .set(Product::getStatus, ProductStatusEnum.NO_AVAILABLE.getValue());
        productService.update(wrapper);
    }
 
    private Product findOrThrow(Long productId) {
        Product existingProduct = productService.getById(productId);
        if (ObjUtil.isEmpty(existingProduct)) {
            throw new EntityNotFoundException(Product.class, "id", Optional.ofNullable(productId).map(Object::toString).orElse("null"));
        }
        return existingProduct;
    }
 
    private void processImagesUpdate(ProductMerchantUpdateRequest request) {
        if (CollectionUtil.isNotEmpty(request.getDeletedImageIds())) {
            productImageService.deleteAll(request.getDeletedImageIds());
        }
        if (CollectionUtil.isNotEmpty(request.getUpdatedImages())) {
            productImageService.batchUpdate(request.getUpdatedImages());
        }
        if (CollectionUtil.isNotEmpty(request.getNewImages())) {
            List<ProductImageCreateRequest> newImages = request.getNewImages().stream().peek(i->{i.setProductId(request.getProductId());}).collect(Collectors.toList());
            productImageService.batchCreate(newImages);
        }
    }
 
    private void processLabelsUpdate(ProductMerchantUpdateRequest request) {
        if (CollectionUtil.isNotEmpty(request.getDeletedLabelIds())) {
            productLabelService.deleteAll(request.getDeletedLabelIds());
        }
        if (CollectionUtil.isNotEmpty(request.getUpdatedLabels())) {
            productLabelService.batchUpdate(request.getUpdatedLabels());
        }
        if (CollectionUtil.isNotEmpty(request.getNewLabels())) {
            List<ProductLabel> newLabels = request.getNewLabels().stream().peek(i->{i.setProductId(request.getProductId());}).collect(Collectors.toList());
            productLabelService.batchCreate(newLabels);
        }
    }
}