mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.panzhihua.service_community.service.impl;
 
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.community.convenient.ConvenientProductDTO;
import com.panzhihua.common.model.dtos.community.convenient.ConvenientProductSpecificationDTO;
import com.panzhihua.common.model.dtos.community.convenient.DeleteConvenientProductDTO;
import com.panzhihua.common.model.dtos.community.convenient.OnShelfOrOffShelfProductDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageConvenientProductDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageSearchDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductLevelInfoVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductShelfVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductSpecificationVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductVO;
import com.panzhihua.service_community.dao.ConvenientProductCategoryDAO;
import com.panzhihua.service_community.dao.ConvenientProductDAO;
import com.panzhihua.service_community.dao.ConvenientProductSpecificationDAO;
import com.panzhihua.service_community.model.dos.ConvenientProductCategoryDO;
import com.panzhihua.service_community.model.dos.ConvenientProductDO;
import com.panzhihua.service_community.model.dos.ConvenientProductSpecificationDO;
import com.panzhihua.service_community.service.ConvenientProductService;
 
/**
 * @title: ConvenientProductServiceImpl
 * @projectName: 成都呐喊信息技术有限公司-智慧社区项目
 * @description: 便民服务商家后台产品服务实现类
 * @author: hans
 * @date: 2021/09/21 21:33
 */
@Service
public class ConvenientProductServiceImpl extends ServiceImpl<ConvenientProductDAO, ConvenientProductDO> implements ConvenientProductService {
 
    @Resource
    private ConvenientProductCategoryDAO convenientProductCategoryDAO;
    @Resource
    private ConvenientProductSpecificationDAO convenientProductSpecificationDAO;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addProduct(ConvenientProductDTO convenientProductDTO) {
        ConvenientProductCategoryDO convenientProductCategoryDO = convenientProductCategoryDAO.selectById(convenientProductDTO.getCategoryId());
        if (isNull(convenientProductCategoryDO)) {
            return R.fail("产品分类不存在");
        }
        ConvenientProductDO convenientProductDO = new ConvenientProductDO();
        BeanUtils.copyProperties(convenientProductDTO, convenientProductDO);
        Date nowDate = new Date();
        if (convenientProductDTO.getOnShelf()) {
            convenientProductDO.setOnShelfAt(nowDate);
        }
        convenientProductDO.setCreatedAt(nowDate);
        this.baseMapper.insert(convenientProductDO);
        Long productId = convenientProductDO.getId();
        if (nonNull(productId)) {
            //规格
            List<ConvenientProductSpecificationDTO> productSpecificationDTOList = convenientProductDTO.getProductSpecificationDTOList();
            List<ConvenientProductSpecificationDO> productSpecificationDOList = productSpecificationDTOList.stream().map(specificationDTO -> {
                ConvenientProductSpecificationDO convenientProductSpecificationDO = new ConvenientProductSpecificationDO();
                BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
                convenientProductSpecificationDO.setProductId(productId);
                convenientProductSpecificationDO.setCreatedAt(nowDate);
                convenientProductSpecificationDO.setCreatedBy(convenientProductDTO.getCreatedBy());
                return convenientProductSpecificationDO;
            }).collect(Collectors.toList());
            convenientProductSpecificationDAO.batchInsert(productSpecificationDOList);
        }
        return R.ok();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R putProduct(ConvenientProductDTO convenientProductDTO) {
        Long productId = convenientProductDTO.getId();
        Long updatedBy = convenientProductDTO.getUpdatedBy();
        ConvenientProductDO convenientProductDO = this.baseMapper.selectById(productId);
        Boolean isOnShelf = convenientProductDO.getOnShelf();
        if (isNull(convenientProductDO)) {
            return R.fail("产品不存在");
        }
        BeanUtils.copyProperties(convenientProductDTO, convenientProductDO);
        Date nowDate = new Date();
        if (!isOnShelf && convenientProductDTO.getOnShelf()) {
            convenientProductDO.setOnShelfAt(nowDate);
        }
        convenientProductDO.setUpdatedBy(updatedBy);
        this.baseMapper.updateById(convenientProductDO);
        List<ConvenientProductSpecificationDTO> productSpecificationDTOList = convenientProductDTO.getProductSpecificationDTOList();
        List<Long> notNeedDelIds = new ArrayList<>();
        productSpecificationDTOList.forEach(specificationDTO -> {
            Long specificationId = specificationDTO.getId();
            if (isNull(specificationId)) {
                //新增
                ConvenientProductSpecificationDO convenientProductSpecificationDO = new ConvenientProductSpecificationDO();
                BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
                convenientProductSpecificationDO.setProductId(productId);
                convenientProductSpecificationDO.setCreatedAt(nowDate);
                convenientProductSpecificationDO.setCreatedBy(updatedBy);
                convenientProductSpecificationDAO.insert(convenientProductSpecificationDO);
                notNeedDelIds.add(convenientProductSpecificationDO.getId());
            } else {
                //更新
                ConvenientProductSpecificationDO convenientProductSpecificationDO = convenientProductSpecificationDAO.selectById(specificationId);
                BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
                convenientProductSpecificationDO.setUpdatedBy(updatedBy);
                convenientProductSpecificationDAO.updateById(convenientProductSpecificationDO);
                notNeedDelIds.add(specificationId);
            }
        });
        //删除已失去关联的规格
        convenientProductSpecificationDAO.deleteLoseRelationSpecifications(notNeedDelIds, productId);
        return R.ok();
    }
 
    @Override
    public R getProduct(Long productId) {
        if (isNull(productId)) {
            return R.fail("产品ID不能为空");
        }
        ConvenientProductDO convenientProductDO = this.baseMapper.selectById(productId);
        if (isNull(convenientProductDO)) {
            return R.fail("产品不存在");
        }
        ConvenientProductVO convenientProductVO = new ConvenientProductVO();
        BeanUtils.copyProperties(convenientProductDO, convenientProductVO);
        //查找产品规格
        List<ConvenientProductSpecificationDO> convenientProductSpecificationDOList = convenientProductSpecificationDAO.selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
                .eq(ConvenientProductSpecificationDO::getProductId, productId).eq(ConvenientProductSpecificationDO::getIsDel, false));
        List<ConvenientProductSpecificationVO> convenientProductSpecificationVOList = new ArrayList<>();
        if (!convenientProductSpecificationDOList.isEmpty()) {
            convenientProductSpecificationDOList.forEach(specificationDO -> {
                ConvenientProductSpecificationVO convenientProductSpecificationVO = new ConvenientProductSpecificationVO();
                BeanUtils.copyProperties(specificationDO, convenientProductSpecificationVO);
                convenientProductSpecificationVOList.add(convenientProductSpecificationVO);
            });
        }
        convenientProductVO.setProductSpecificationVOList(convenientProductSpecificationVOList);
        return R.ok(convenientProductVO);
    }
 
    @Override
    public R pageProduct(PageConvenientProductDTO pageConvenientProductDTO) {
        Page page = new Page<>();
        page.setSize(pageConvenientProductDTO.getPageSize());
        page.setCurrent(pageConvenientProductDTO.getPageNum());
        IPage<ConvenientProductVO> productVOIPage = this.baseMapper.pageProduct(page, pageConvenientProductDTO);
        List<ConvenientProductVO> convenientProductVOList = productVOIPage.getRecords();
        //图片填充
        if (!convenientProductVOList.isEmpty()) {
            convenientProductVOList.forEach(product -> {
                List<ConvenientProductSpecificationDO> specificationDOList = convenientProductSpecificationDAO.selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
                        .eq(ConvenientProductSpecificationDO::getProductId, product.getId()).isNotNull(ConvenientProductSpecificationDO::getImage));
                product.setImage(specificationDOList.isEmpty() ? null : specificationDOList.get(0).getImage());
            });
        }
        return R.ok(productVOIPage);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteProduct(DeleteConvenientProductDTO deleteConvenientProductDTO) {
        List<Long> needDelIds = deleteConvenientProductDTO.getIds();
        //删除产品
        int result = this.baseMapper.batchDeleteByIds(needDelIds);
        if (result > 0) {
            //删除规格
            convenientProductSpecificationDAO.batchDeleteByProductIds(needDelIds);
        }
        return R.ok();
    }
 
    @Override
    public R onShelfOrOffShelfProduct(OnShelfOrOffShelfProductDTO onShelfOrOffShelfProductDTO) {
        Integer type = onShelfOrOffShelfProductDTO.getType();
        Long updatedBy = onShelfOrOffShelfProductDTO.getUpdatedBy();
        List<Long> needDelIds = onShelfOrOffShelfProductDTO.getIds();
        if (type.intValue() == 1) {
            //上架
            this.baseMapper.batchOnShelfOrOffShelfByIds(needDelIds, updatedBy, true);
        } else if (type.intValue() == 2) {
            //下架
            this.baseMapper.batchOnShelfOrOffShelfByIds(needDelIds, updatedBy, false);
        } else {
            return R.fail("处理类型错误");
        }
        return R.ok();
    }
 
    /**
     * 小程序获取商家产品
     * @param merchantId
     * @return
     */
    @Override
    public R getMerchantProduct(Long merchantId) {
        List<ConvenientProductVO> productVOList = this.baseMapper.getMerchantProduct(merchantId);
        List<ConvenientProductSpecificationVO> specificationVOList = this.baseMapper.getProductSpecifications(merchantId);
        List<ConvenientProductLevelInfoVO> levelInfoVOList = new ArrayList<>();
        List<String> categoryList = new ArrayList<>();
        if (!productVOList.isEmpty()) {
            productVOList.forEach(productVO -> {
                List<ConvenientProductSpecificationVO> currentProductSpecificationVOList = new ArrayList<>();
                specificationVOList.forEach(specificationVO -> {
                    if (productVO.getId().equals(specificationVO.getProductId())) {
                        currentProductSpecificationVOList.add(specificationVO);
                    }
                });
                productVO.setProductSpecificationVOList(currentProductSpecificationVOList);
                if (!categoryList.contains(productVO.getCategoryName())) {
                    categoryList.add(productVO.getCategoryName());
                }
            });
            //分类
            Map<String, List<ConvenientProductVO>> levelGroup = productVOList.stream()
                    .collect(Collectors.groupingBy(ConvenientProductVO::getCategoryName));
            categoryList.forEach(category -> {
                ConvenientProductLevelInfoVO levelInfoVOLevelInfoVO = new ConvenientProductLevelInfoVO();
                levelInfoVOLevelInfoVO.setCategoryName(category);
                List<ConvenientProductVO> productVOS = levelGroup.get(category);
                List<ConvenientProductVO> sortedProductVOS = productVOS.stream()
                        .sorted(Comparator.comparing(ConvenientProductVO::getOnShelfAt).reversed()).collect(Collectors.toList());
                levelInfoVOLevelInfoVO.setProductVOList(sortedProductVOS);
                levelInfoVOList.add(levelInfoVOLevelInfoVO);
            });
        }
        return R.ok(levelInfoVOList);
    }
 
    @Override
    public R pageSearchProduct(PageSearchDTO pageSearchDTO) {
        Page page = new Page<>();
        page.setSize(pageSearchDTO.getPageSize());
        page.setCurrent(pageSearchDTO.getPageNum());
        IPage<ConvenientProductVO> productVOIPage = this.baseMapper.pageSearchProduct(page, pageSearchDTO);
        List<ConvenientProductVO> records = productVOIPage.getRecords();
        if (!records.isEmpty()) {
            List<Long> productIds = records.stream().map(ConvenientProductVO::getId).collect(Collectors.toList());
            List<ConvenientProductSpecificationDO> specificationDOS = convenientProductSpecificationDAO
                    .selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
                    .eq(ConvenientProductSpecificationDO::getIsDel, false).in(ConvenientProductSpecificationDO::getProductId, productIds));
            List<ConvenientProductSpecificationVO> specificationVOList = new ArrayList<>();
            if (!specificationDOS.isEmpty()) {
                specificationDOS.forEach(specificationDO -> {
                    ConvenientProductSpecificationVO specificationVO = new ConvenientProductSpecificationVO();
                    BeanUtils.copyProperties(specificationDO, specificationVO);
                    specificationVOList.add(specificationVO);
                });
                Map<Long, List<ConvenientProductSpecificationVO>> collect = specificationVOList
                        .stream().collect(Collectors.groupingBy(ConvenientProductSpecificationVO::getProductId));
                Set<Long> keySet = collect.keySet();
                Iterator<Long> iterator = keySet.iterator();
                while (iterator.hasNext()) {
                    Long productId = iterator.next();
                    records.forEach(record -> {
                        if (record.getId().equals(productId)) {
                            record.setProductSpecificationVOList(collect.get(productId));
                        }
                    });
                }
            }
        }
        return R.ok(productVOIPage);
    }
 
    @Override
    public R incrProductView(Long productId) {
        this.baseMapper.incrProductView(productId);
        return R.ok();
    }
 
    @Override
    public R getProductShelfNum(Long merchantId) {
        int onShelfTotalNum = this.baseMapper.selectCount(new LambdaQueryWrapper<ConvenientProductDO>().eq(ConvenientProductDO::getMerchantId, merchantId)
                .eq(ConvenientProductDO::getIsDel, false).eq(ConvenientProductDO::getOnShelf, true));
        int offShelfTotalNum = this.baseMapper.selectCount(new LambdaQueryWrapper<ConvenientProductDO>().eq(ConvenientProductDO::getMerchantId, merchantId)
                .eq(ConvenientProductDO::getIsDel, false).eq(ConvenientProductDO::getOnShelf, false));
        ConvenientProductShelfVO convenientProductShelfVO = new ConvenientProductShelfVO();
        convenientProductShelfVO.setOnShelfTotalNum(onShelfTotalNum);
        convenientProductShelfVO.setOffShelfTotalNum(offShelfTotalNum);
        return R.ok(convenientProductShelfVO);
    }
}