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 implements ProductImageService { private final ProductImageMapper productImageMapper; private final BucketStorageService bucketStorageService; @Override public PageResult queryAll(ProductImageQueryCriteria criteria, Page page) { return PageUtil.toPage(productImageMapper.findAll(criteria, page)); } @Override public List queryAll(ProductImageQueryCriteria criteria) { return productImageMapper.findAll(criteria); } @Override public List queryBatchIds(List ids) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.in(ProductImage::getImageId, ids); return productImageMapper.selectList(lambdaQueryWrapper); } @Override public List queryImagesByProductId(Long productId) { if (productId == null) { return ListUtil.empty(); } LambdaQueryWrapper 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 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 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 requests) { List updatedIds = requests.stream().map(ProductImageUpdateRequest::getImageId).collect(Collectors.toList()); List existingProductImages = this.queryBatchIds(updatedIds); if (CollectionUtil.isEmpty(existingProductImages)) { throw new EntityNotFoundException(ProductImage.class, "imageIds", updatedIds.toString()); } Map requestMap = requests.stream().collect(Collectors.toMap( ProductImageUpdateRequest::getImageId, productImageUpdateRequest -> productImageUpdateRequest ) ); List 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 ids) { List existingProductImages = this.queryBatchIds(ids); if (CollectionUtil.isEmpty(existingProductImages)) { throw new EntityNotFoundException(ProductImage.class, "productImageIds", ids.toString()); } productImageMapper.deleteBatchIds(ids); // 删除图片原纪录 List deleteCloudStorageIds = existingProductImages.stream().map(ProductImage::getCloudStorageId).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(deleteCloudStorageIds)) { bucketStorageService.deleteAll(deleteCloudStorageIds); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (ProductImage productImage : all) { Map 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); } }