package com.oying.modules.pc.product.service.impl;
|
|
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.product.domain.ProductLabel;
|
import com.oying.modules.pc.product.domain.dto.ProductLabelQueryCriteria;
|
import com.oying.modules.pc.product.mapper.ProductLabelMapper;
|
import com.oying.modules.pc.product.service.ProductLabelService;
|
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.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author lzp
|
* @description 服务实现
|
* @date 2025-05-28
|
**/
|
@Service
|
@RequiredArgsConstructor
|
public class ProductLabelServiceImpl extends ServiceImpl<ProductLabelMapper, ProductLabel> implements ProductLabelService {
|
|
private final ProductLabelMapper productLabelMapper;
|
|
@Override
|
public PageResult<ProductLabel> queryAll(ProductLabelQueryCriteria criteria, Page<Object> page) {
|
return PageUtil.toPage(productLabelMapper.findAll(criteria, page));
|
}
|
|
@Override
|
public List<ProductLabel> queryAll(ProductLabelQueryCriteria criteria) {
|
return productLabelMapper.findAll(criteria);
|
}
|
|
@Override
|
public List<ProductLabel> queryBatchIds(List<Long> ids) {
|
LambdaQueryWrapper<ProductLabel> wrapper = new LambdaQueryWrapper<>();
|
wrapper.in(ProductLabel::getLabelId, ids);
|
return productLabelMapper.selectList(wrapper);
|
}
|
|
@Override
|
public List<ProductLabel> queryLabelsByProductId(Long productId) {
|
if (productId == null) {
|
return ListUtil.empty();
|
}
|
LambdaQueryWrapper<ProductLabel> wrapper = new LambdaQueryWrapper<>();
|
wrapper.eq(ProductLabel::getProductId, productId);
|
return productLabelMapper.selectList(wrapper);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void create(ProductLabel resources) {
|
productLabelMapper.insert(resources);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void batchCreate(List<ProductLabel> resources) {
|
this.saveBatch(resources);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(ProductLabel resources) {
|
ProductLabel productLabel = getById(resources.getLabelId());
|
productLabel.copy(resources);
|
productLabelMapper.updateById(productLabel);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void batchUpdate(List<ProductLabel> resources) {
|
|
List<Long> updateIds = resources.stream().map(ProductLabel::getLabelId).collect(Collectors.toList());
|
List<ProductLabel> existingProductLabels = this.queryBatchIds(updateIds);
|
if (ObjUtil.isEmpty(existingProductLabels)) {
|
throw new EntityNotFoundException(ProductLabel.class, "labelId", updateIds.toString());
|
}
|
|
Map<Long, ProductLabel> longProductLabelMap = resources.stream()
|
.collect(Collectors.toMap(
|
ProductLabel::getLabelId,
|
productLabel -> productLabel)
|
);
|
|
for (ProductLabel existingProductLabel : existingProductLabels) {
|
existingProductLabel.copy(longProductLabelMap.get(existingProductLabel.getLabelId()));
|
}
|
|
this.updateBatchById(existingProductLabels);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void deleteAll(List<Long> ids) {
|
productLabelMapper.deleteBatchIds(ids);
|
}
|
|
@Override
|
public void download(List<ProductLabel> all, HttpServletResponse response) throws IOException {
|
List<Map<String, Object>> list = new ArrayList<>();
|
for (ProductLabel productLabel : all) {
|
Map<String, Object> map = new LinkedHashMap<>();
|
map.put("商品ID", productLabel.getProductId());
|
map.put(" categoryName", productLabel.getCategoryName());
|
map.put("标签名称", productLabel.getLabelName());
|
map.put("标签值", productLabel.getLabelValue());
|
map.put("创建人", productLabel.getCreateBy());
|
map.put("创建时间", productLabel.getCreateTime());
|
map.put("修改人", productLabel.getUpdateBy());
|
map.put("修改时间", productLabel.getUpdateTime());
|
list.add(map);
|
}
|
FileUtil.downloadExcel(list, response);
|
}
|
}
|