mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
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
package com.sinata.modular.mall.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sinata.common.enums.EnumIsSystemNotice;
import com.sinata.core.shiro.ShiroKit;
import com.sinata.modular.mall.dao.MallGoodsMapper;
import com.sinata.modular.mall.model.MallGoods;
import com.sinata.modular.mall.model.MallGoodsDetail;
import com.sinata.modular.mall.model.MallGoodsSet;
import com.sinata.modular.mall.model.MallGoodsSku;
import com.sinata.modular.mall.service.IMallGoodsDetailService;
import com.sinata.modular.mall.service.IMallGoodsService;
import com.sinata.modular.mall.service.IMallGoodsSetService;
import com.sinata.modular.mall.service.IMallGoodsSkuService;
import com.sinata.modular.system.model.SystemNotice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author goku
 * @since 2023-03-11
 */
@Service
public class MallGoodsServiceImpl extends ServiceImpl<MallGoodsMapper, MallGoods> implements IMallGoodsService {
 
    @Resource
    private IMallGoodsDetailService goodsDetailService;
 
    @Resource
    private IMallGoodsSetService goodsSetService;
 
    @Resource
    private IMallGoodsSkuService mallGoodsSkuService;
 
    @Override
    public void wrapperMapGoods(List<Map<String, Object>> list, String goodsKey) {
        // 用户ID串
        Object[] goodsIds = list.stream().map(o -> o.get(goodsKey)).collect(Collectors.toList()).toArray();
        // 用户列表
        List<MallGoods> goodsList = baseMapper.selectList(
                new EntityWrapper<MallGoods>()
                        .setSqlSelect("id,goods_name goodsName")
                        .in("id", goodsIds)
        );
 
        // 封装数据
        for (Map<String, Object> map : list) {
            for (MallGoods u : goodsList) {
                if (u.getId().toString().equals(map.get(goodsKey) + "")) {
                    map.put(goodsKey + "_merchantName", u.getGoodsName());
                }
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMallGoods(MallGoods mallGoods) {
        // 库存求和
        mallGoods.setStock(mallGoods.getGoodsSkus().stream().mapToInt(MallGoodsSku::getStock).sum());
        if (StrUtil.isNotBlank(mallGoods.getIntroImage())) {
            mallGoods.setGoodsImage(mallGoods.getIntroImage().split(",")[0]);
        }
 
        // 保存基本信息
        this.insert(mallGoods);
        // 保存详情
        MallGoodsDetail goodsDetail = new MallGoodsDetail()
                .setId(mallGoods.getId())
                .setIntroH5(mallGoods.getIntroH5())
                .setPurchaseH5(mallGoods.getPurchaseH5())
                .setIntroImage(mallGoods.getIntroImage());
        this.goodsDetailService.insert(goodsDetail);
        // 保存商品系数
        MallGoodsSet goodsSet = mallGoods.getGoodsSet();
        goodsSet.setId(mallGoods.getId());
        this.goodsSetService.insert(goodsSet);
        // 保存规格
        if (CollUtil.isNotEmpty(mallGoods.getGoodsSkus())) {
            List<MallGoodsSku> goodsSkus = mallGoods.getGoodsSkus().stream().peek(sku -> {
                sku.setGoodsId(mallGoods.getId());
                sku.setPrice(mallGoods.getPrice());
            }).collect(Collectors.toList());
            this.mallGoodsSkuService.insertBatch(goodsSkus);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMallGoods(MallGoods mallGoods) {
        if (mallGoods != null && mallGoods.getId() != null) {
            // 添加后台消息通知
            addSystemNotice(this.selectById(mallGoods.getId()), mallGoods);
        }
 
        // 库存求和
        mallGoods.setStock(mallGoods.getGoodsSkus().stream().mapToInt(MallGoodsSku::getStock).sum());
        if (StrUtil.isNotBlank(mallGoods.getIntroImage())) {
            mallGoods.setGoodsImage(mallGoods.getIntroImage().split(",")[0]);
        }
 
        // 修改基本信息
        this.updateById(mallGoods);
        // 修改详情信息
        MallGoodsDetail goodsDetail = new MallGoodsDetail()
                .setId(mallGoods.getId())
                .setImgList(mallGoods.getGoodsImage())
                .setIntroH5(mallGoods.getIntroH5())
                .setPurchaseH5(mallGoods.getPurchaseH5())
                .setIntroImage(mallGoods.getIntroImage());
        this.goodsDetailService.updateById(goodsDetail);
        // 保存商品系数
        MallGoodsSet goodsSet = mallGoods.getGoodsSet();
        goodsSet.setId(mallGoods.getId());
        this.goodsSetService.updateById(goodsSet);
        // 重新保存规格
        List<Integer> exitSkuIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(mallGoods.getGoodsSkus())) {
            exitSkuIds = mallGoods.getGoodsSkus().stream()
                    .peek(sku -> {
                        sku.setGoodsId(mallGoods.getId());
                        sku.setPrice(mallGoods.getPrice());
                        sku.setPriceSale(mallGoods.getPriceSale());
                    })
                    .map(MallGoodsSku::getId)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
        }
        Wrapper<MallGoodsSku> wrapper = new EntityWrapper<MallGoodsSku>()
                .eq("goods_id", mallGoods.getId());
        if (CollUtil.isNotEmpty(exitSkuIds)) {
            wrapper.notIn("id", exitSkuIds);
        }
        this.mallGoodsSkuService.delete(wrapper);
        if (CollUtil.isNotEmpty(mallGoods.getGoodsSkus())) {
            this.mallGoodsSkuService.insertOrUpdateBatch(mallGoods.getGoodsSkus());
        }
    }
 
    /**
     * 添加后台消息通知
     * @param oldGoods
     * @param newGoods
     */
    public void addSystemNotice(MallGoods oldGoods, MallGoods newGoods) {
        try {
            StringBuffer updateStr = new StringBuffer();
            if (oldGoods.getPrice() != null && newGoods.getPrice() != null &&
                    oldGoods.getPrice().compareTo(newGoods.getPrice()) != 0) {
                updateStr.append("将市场价" + oldGoods.getPrice() + "元修改为" + newGoods.getPrice() + "元。");
            }
            if (oldGoods.getPriceSale() != null && newGoods.getPriceSale() != null &&
                    oldGoods.getPriceSale().compareTo(newGoods.getPriceSale()) != 0) {
                updateStr.append("将美天销售价" + oldGoods.getPriceSale() + "元修改为" + newGoods.getPriceSale() + "元。");
            }
            if (oldGoods.getPriceMember() != null && newGoods.getPriceMember() != null &&
                    oldGoods.getPriceMember().compareTo(newGoods.getPriceMember()) != 0) {
                updateStr.append("将会员价" + oldGoods.getPriceMember() + "元修改为" + newGoods.getPriceMember() + "元。");
            }
 
            if (updateStr.length() != 0) {
                // 添加后台通知
                SystemNotice.builder()
                        .param(oldGoods.getId())
                        .type(EnumIsSystemNotice.TYPE_8.index)
                        .content(ShiroKit.getUser().getName() + " 管理员修改 " + oldGoods.getGoodsName() + " 商品。" + updateStr)
                        .build()
                        .insert();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}