zepengdev
2025-07-19 f6239d1029da4c51ba3b2cec790f7399a210a3df
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.oying.modules.pc.product.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.exception.EntityExistException;
import com.oying.exception.EntityNotFoundException;
import com.oying.modules.pc.common.ValueUpdate;
import com.oying.modules.pc.product.converter.ProductImageAssembler;
import com.oying.modules.pc.product.domain.ProductImage;
import com.oying.modules.pc.product.domain.dto.ProductImageCreateRequest;
import com.oying.modules.pc.product.domain.dto.ProductImageQueryCriteria;
import com.oying.modules.pc.product.domain.dto.ProductImageUpdateRequest;
import com.oying.modules.pc.product.mapper.ProductImageMapper;
import com.oying.modules.pc.product.service.ProductImageService;
import com.oying.modules.pc.store.domain.StoreQualification;
import com.oying.service.BucketStorageService;
import com.oying.utils.FileUtil;
import com.oying.utils.PageResult;
import com.oying.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author lzp
 * @description 服务实现
 * @date 2025-05-28
 **/
@Service
@RequiredArgsConstructor
public class ProductImageServiceImpl extends ServiceImpl<ProductImageMapper, ProductImage> implements ProductImageService {
 
    private final ProductImageMapper productImageMapper;
    private final BucketStorageService bucketStorageService;
 
    @Override
    public PageResult<ProductImage> queryAll(ProductImageQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(productImageMapper.findAll(criteria, page));
    }
 
    @Override
    public List<ProductImage> queryAll(ProductImageQueryCriteria criteria) {
        return productImageMapper.findAll(criteria);
    }
 
    @Override
    public List<ProductImage> queryBatchIds(List<Long> ids) {
        LambdaQueryWrapper<ProductImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ProductImage::getImageId, ids);
        return productImageMapper.selectList(lambdaQueryWrapper);
    }
 
    @Override
    public List<ProductImage> queryImagesByProductId(Long productId) {
        if (productId == null) {
            return ListUtil.empty();
        }
        LambdaQueryWrapper<ProductImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductImage::getProductId, productId);
        return productImageMapper.selectList(wrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProductImageCreateRequest request) {
        productImageMapper.insert(ProductImageAssembler.to(request));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchCreate(List<ProductImageCreateRequest> requests) {
        this.saveBatch(ProductImageAssembler.toProducts(requests));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductImageUpdateRequest request) {
 
        Long updatedImageId = request.getImageId();
        ProductImage existingProductImage = this.getById(updatedImageId);
        if (ObjUtil.isEmpty(existingProductImage)) {
            throw new EntityNotFoundException(ProductImage.class, "imageId", Optional.ofNullable(updatedImageId).map(Object::toString).orElse("null"));
        }
 
        // 新的图片数据
        ProductImage newProductImage = ProductImageAssembler.to(request);
        // 新的图片数据
        ValueUpdate<Long> cloudStorageUpdate = new ValueUpdate<>(newProductImage.getCloudStorageId(), existingProductImage.getCloudStorageId());
        // 填充新的数据
        existingProductImage.copy(newProductImage);
        productImageMapper.updateById(existingProductImage);
        // 删除旧图片原纪录
        if (cloudStorageUpdate.isChangeAndOldValueNotEmpty()) {
            bucketStorageService.deleteAll(ListUtil.toList(cloudStorageUpdate.getOldValue()));
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(List<ProductImageUpdateRequest> requests) {
 
        List<Long> updatedIds = requests.stream().map(ProductImageUpdateRequest::getImageId).collect(Collectors.toList());
        List<ProductImage> existingProductImages = this.queryBatchIds(updatedIds);
        if (CollectionUtil.isEmpty(existingProductImages)) {
            throw new EntityNotFoundException(ProductImage.class, "imageIds", updatedIds.toString());
        }
 
        Map<Long, ProductImageUpdateRequest> requestMap = requests.stream().collect(Collectors.toMap(
                                ProductImageUpdateRequest::getImageId,
                                productImageUpdateRequest -> productImageUpdateRequest
                        )
                );
 
        List<Long> deleteCloudStorageIds = existingProductImages.stream().filter(i -> {
            Long newCloudStorageId = requestMap.get(i.getImageId()).getUploadFileId();
            return ValueUpdate.isChangeAndOldValueNotEmpty(newCloudStorageId, i.getCloudStorageId());
        }).map(ProductImage::getImageId).collect(Collectors.toList());
 
        // 填充新的数据
        for (ProductImage existingProductImage : existingProductImages) {
            ProductImage newProductImage = ProductImageAssembler.to(requestMap.get(existingProductImage.getImageId()));
            existingProductImage.copy(newProductImage);
        }
        this.updateBatchById(existingProductImages);
        // 删除旧图片原纪录
        if (CollectionUtil.isNotEmpty(deleteCloudStorageIds)) {
            bucketStorageService.deleteAll(deleteCloudStorageIds);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        List<ProductImage> existingProductImages = this.queryBatchIds(ids);
        if (CollectionUtil.isEmpty(existingProductImages)) {
            throw new EntityNotFoundException(ProductImage.class, "productImageIds", ids.toString());
        }
        productImageMapper.deleteBatchIds(ids);
        // 删除图片原纪录
        List<Long> deleteCloudStorageIds = existingProductImages.stream().map(ProductImage::getCloudStorageId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deleteCloudStorageIds)) {
            bucketStorageService.deleteAll(deleteCloudStorageIds);
        }
    }
 
    @Override
    public void download(List<ProductImage> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProductImage productImage : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("商品ID", productImage.getProductId());
            map.put("桶ID", productImage.getCloudStorageId());
            map.put("图片key", productImage.getImageType());
            map.put("图片地址", productImage.getImageUrl());
            map.put("排序权重", productImage.getSortWeight());
            map.put("创建人", productImage.getCreateBy());
            map.put("创建日期", productImage.getCreateTime());
            map.put("修改人", productImage.getUpdateBy());
            map.put("修改时间", productImage.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}