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
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.List;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.microCommercialStreet.DeleteProductDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.McsProductDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PageMcsProductDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.SetShelfForProductDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsProductVO;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.service_community.dao.McsLabelDAO;
import com.panzhihua.service_community.dao.McsMerchantDAO;
import com.panzhihua.service_community.dao.McsProductDAO;
import com.panzhihua.service_community.dao.McsProductLabelDAO;
import com.panzhihua.service_community.entity.McsLabel;
import com.panzhihua.service_community.entity.McsMerchant;
import com.panzhihua.service_community.entity.McsProduct;
import com.panzhihua.service_community.entity.McsProductLabel;
import com.panzhihua.service_community.service.McsProductService;
 
/**
 * (McsProduct)表服务实现类
 *
 * @author makejava
 * @since 2021-12-28 14:37:12
 */
@Service("mcsProductService")
public class McsProductServiceImpl extends ServiceImpl<McsProductDAO, McsProduct> implements McsProductService {
 
    @Resource
    private McsMerchantDAO mcsMerchantDAO;
    @Resource
    private McsLabelDAO mcsLabelDAO;
    @Resource
    private McsProductLabelDAO mcsProductLabelDAO;
 
    /**
     * 新增产品信息
     * @param mcsProductDTO
     * @return
     */
    @Override
    public R addMcsProduct(McsProductDTO mcsProductDTO) {
        Long userId = mcsProductDTO.getCreatedBy();
        McsMerchant mcsMerchant = mcsMerchantDAO.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        if (isNull(mcsMerchant)) {
            return R.fail("商家信息不存在");
        }
        McsProduct mcsProduct = new McsProduct();
        BeanUtils.copyProperties(mcsProductDTO, mcsProduct);
        mcsProduct.setMerchantId(mcsMerchant.getId());
        mcsProduct.setStatus(McsProduct.Status.yxj);
        mcsProduct.setId(Snowflake.getId());
        int num = this.baseMapper.insert(mcsProduct);
        if (num > 0) {
            List<Long> labelIds = mcsProductDTO.getLabelIds();
            if (nonNull(labelIds) && !labelIds.isEmpty()) {
                List<McsLabel> mcsLabels = mcsLabelDAO.selectBatchIds(labelIds);
                if (nonNull(mcsLabels) && !mcsLabels.isEmpty()) {
                    List<McsProductLabel> mcsProductLabelList = new ArrayList<>();
                    mcsLabels.forEach(e -> {
                        McsProductLabel productLabel = new McsProductLabel();
                        productLabel.setLabelId(e.getId());
                        productLabel.setProductId(mcsProduct.getId());
                        productLabel.setLabelName(e.getName());
                        mcsProductLabelList.add(productLabel);
                    });
                    mcsProductLabelDAO.insertBatch(mcsProductLabelList);
                }
            }
            return R.ok();
        }
        return R.fail("新增失败,请重新尝试");
    }
 
    /**
     * 编辑产品信息
     * @param mcsProductDTO
     * @return
     */
    @Override
    public R putMcsProduct(McsProductDTO mcsProductDTO) {
        McsProduct mcsProduct = this.baseMapper.selectById(mcsProductDTO.getId());
        if (isNull(mcsProduct)) {
            return R.fail("资源不存在");
        }
        BeanUtils.copyProperties(mcsProductDTO, mcsProduct);
        int num = this.baseMapper.updateById(mcsProduct);
        if (num > 0) {
            List<Long> labelIds = mcsProductDTO.getLabelIds();
            mcsProductLabelDAO.delete(new QueryWrapper<McsProductLabel>().lambda().eq(McsProductLabel::getProductId, mcsProduct.getId()));
            if (nonNull(labelIds) && !labelIds.isEmpty()) {
                List<McsLabel> mcsLabels = mcsLabelDAO.selectBatchIds(labelIds);
                if (nonNull(mcsLabels) && !mcsLabels.isEmpty()) {
                    List<McsProductLabel> mcsProductLabelList = new ArrayList<>();
                    mcsLabels.forEach(e -> {
                        McsProductLabel productLabel = new McsProductLabel();
                        productLabel.setLabelId(e.getId());
                        productLabel.setProductId(mcsProduct.getId());
                        productLabel.setLabelName(e.getName());
                        mcsProductLabelList.add(productLabel);
                    });
                    mcsProductLabelDAO.insertBatch(mcsProductLabelList);
                }
            }
            return R.ok();
        }
        return R.fail("编辑失败,请重新尝试");
    }
 
    /**
     * 删除产品信息
     * @param deleteProductDTO
     * @return
     */
    @Override
    public R deleteMcsProduct(DeleteProductDTO deleteProductDTO) {
        List<McsProduct> mcsProducts = this.baseMapper.selectBatchIds(deleteProductDTO.getProductIds());
        if (nonNull(mcsProducts) && !mcsProducts.isEmpty()) {
            mcsProducts.forEach(e -> {
                e.setIsDel(true);
                e.setUpdatedBy(deleteProductDTO.getUpdatedBy());
            });
            this.updateBatchById(mcsProducts);
        }
        return R.ok();
    }
 
    /**
     * 上架/下架产品信息
     * @param setShelfForProductDTO
     * @return
     */
    @Override
    public R setShelfForMcsProduct(SetShelfForProductDTO setShelfForProductDTO) {
        Integer type = setShelfForProductDTO.getType();
        List<McsProduct> mcsProducts = this.baseMapper.selectBatchIds(setShelfForProductDTO.getProductIds());
        if (nonNull(mcsProducts) && !mcsProducts.isEmpty()) {
            if (type.equals(1)) {
                //上架
                mcsProducts.forEach(e -> {
                    e.setStatus(McsProduct.Status.sjz);
                    e.setUpdatedBy(setShelfForProductDTO.getUpdatedBy());
                });
            } else if (type.equals(2)) {
                //下架
                mcsProducts.forEach(e -> {
                    e.setStatus(McsProduct.Status.yxj);
                    e.setUpdatedBy(setShelfForProductDTO.getUpdatedBy());
                });
            } else {
                return R.fail("未知错误");
            }
            this.updateBatchById(mcsProducts);
        }
        return R.ok();
    }
 
    /**
     * 分页查询产品信息
     * @param pageMcsProductDTO
     * @return
     */
    @Override
    public R pageMcsProduct(PageMcsProductDTO pageMcsProductDTO) {
        Page page = new Page<>();
        page.setSize(pageMcsProductDTO.getPageSize());
        page.setCurrent(pageMcsProductDTO.getPageNum());
        IPage<McsProductVO> mcsProduct = this.baseMapper.pageMcsProduct(page, pageMcsProductDTO);
        return R.ok(mcsProduct);
    }
 
    /**
     * 获取产品信息详情
     * @param productId
     * @return
     */
    @Override
    public R getMcsProduct(Long productId) {
        McsProduct mcsProduct = this.baseMapper.selectById(productId);
        if (isNull(mcsProduct)) {
            return R.fail("资源不存在");
        }
        McsProductVO mcsProductVO = new McsProductVO();
        BeanUtils.copyProperties(mcsProduct, mcsProductVO);
        List<McsProductLabel> mcsProductLabelList = mcsProductLabelDAO.selectList(new QueryWrapper<McsProductLabel>().lambda()
                .eq(McsProductLabel::getProductId, productId));
        if (nonNull(mcsProductLabelList) && !mcsProductLabelList.isEmpty()) {
            List<Long> labelIds = mcsProductLabelList.stream().map(McsProductLabel::getLabelId).collect(Collectors.toList());
            mcsProductVO.setLabelIds(labelIds);
        }
        return R.ok(mcsProductVO);
    }
}