puhanshu
2021-09-22 15c1d716e6a89b38e6af36b62fc372b93779df01
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
package com.panzhihua.service_community.service.impl;
 
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.PageMerchantProductDTO;
import com.panzhihua.common.model.vos.R;
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;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
 
/**
 * @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();
        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);
        if (isNull(convenientProductDO)) {
            return R.fail("产品不存在");
        }
        BeanUtils.copyProperties(convenientProductDTO, convenientProductDO);
        convenientProductDO.setUpdatedBy(updatedBy);
        this.baseMapper.updateById(convenientProductDO);
        List<ConvenientProductSpecificationDTO> productSpecificationDTOList = convenientProductDTO.getProductSpecificationDTOList();
        List<Long> notNeedDelIds = productSpecificationDTOList.stream().filter(specificationDTO -> nonNull(specificationDTO.getId()))
                .map(ConvenientProductSpecificationDTO::getId).collect(Collectors.toList());
        //删除已失去关联的规格
        convenientProductSpecificationDAO.deleteLoseRelationSpecifications(notNeedDelIds);
        Date nowDate = new Date();
        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);
            } else {
                //更新
                ConvenientProductSpecificationDO convenientProductSpecificationDO = convenientProductSpecificationDAO.selectById(specificationId);
                BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
                convenientProductSpecificationDO.setUpdatedBy(updatedBy);
                convenientProductSpecificationDAO.updateById(convenientProductSpecificationDO);
            }
        });
        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 -> {
                ConvenientProductSpecificationDO specificationDO = convenientProductSpecificationDAO.selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
                        .eq(ConvenientProductSpecificationDO::getProductId, product.getId())).get(0);
                product.setImage(specificationDO.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 pageMerchantProductDTO
     * @return
     */
    @Override
    public R getMerchantProduct(PageMerchantProductDTO pageMerchantProductDTO) {
        Page page = new Page<>();
        page.setSize(pageMerchantProductDTO.getPageSize());
        page.setCurrent(pageMerchantProductDTO.getPageNum());
        IPage<ConvenientProductVO> productVOIPage = this.baseMapper.getMerchantProduct(page, pageMerchantProductDTO);
        return null;
    }
}