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
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);
    }
}