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
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
package com.sinata.rest.modular.mall.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinata.common.enums.EnumIsDelete;
import com.sinata.common.enums.mall.EnumMallGoodsGroupType;
import com.sinata.rest.common.ApiUtils;
import com.sinata.rest.modular.mall.controller.vo.VoGoods;
import com.sinata.rest.modular.mall.controller.vo.VoGoodsDetail;
import com.sinata.rest.modular.mall.controller.vo.VoGoodsSpecSet;
import com.sinata.rest.modular.mall.model.MallGoods;
import com.sinata.rest.modular.mall.model.MallGoodsSku;
import com.sinata.rest.modular.mall.model.MallTag;
import com.sinata.rest.modular.mall.service.IMallGoodsService;
import com.sinata.rest.modular.mall.service.IMallGoodsSkuService;
import com.sinata.rest.modular.mall.service.IMallGoodsSpecService;
import com.sinata.rest.modular.mall.service.IMallTagService;
import com.sinata.rest.modular.member.model.MemMerchant;
import com.sinata.rest.modular.member.service.IMemMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 商品接口
 * @author goku
 * @date 2023/3/11
 */
@Slf4j
@RestController
@RequestMapping("/mall/goods")
@Api(tags = "商城-商品接口", description = "搜索、列表、详情、规格")
public class MallGoodsController {
 
    @Autowired
    private IMallGoodsService goodsService;
    @Autowired
    private IMallGoodsSkuService goodsSkuService;
    @Autowired
    private IMallGoodsSpecService goodsSpecService;
    @Autowired
    private IMallTagService mallTagService;
    @Autowired
    private IMemMerchantService memMerchantService;
 
    @GetMapping(value = "/list")
    @ApiOperation(value = "商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantId", value = "门店id", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "firstClassifyId", value = "分类ID", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "筛选条件(1最新上架 2销量 3价格)", defaultValue = "0", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "sortType", value = "价格(1升序 2降序)", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页数", defaultValue = "1", dataType = "Int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", defaultValue = "20", dataType = "Int", required = true, paramType = "query"),
    })
    public ApiUtils<List<VoGoods>> list(Integer merchantId, String goodsName, Integer firstClassifyId, Integer type, Integer sortType, Integer current, Integer size) {
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
 
        Page page = new Page<VoGoods>(current, size);
        // 查询商品信息
        List<VoGoods> list = goodsService.listGoods(EnumMallGoodsGroupType.GOODS.index, merchantId, goodsName, firstClassifyId, null, type, sortType, page);
        Map<Integer, BigDecimal> minPriceSale = goodsSkuService.queryPlatMinSalePrice(null).stream()
                .collect(Collectors.toMap(MallGoodsSku::getGoodsId, MallGoodsSku::getPriceSale));
        return ApiUtils.returnOK(
                list.stream()
                        .map(o -> {
                            BigDecimal money = minPriceSale.get(o.getId());
                            o.setTagList(mallTagService.getTagListByGoodsId(mallTags, o.getTagIds()));
                            o.setPriceSale(Optional.ofNullable(money).orElse(o.getPriceSale()));
                            return o;
                        })
                        .collect(Collectors.toList())
        );
    }
 
    @GetMapping(value = "/shareGoods")
    @ApiOperation(value = "推广商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
            @ApiImplicitParam(name = "cityCode", value = "城市code", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "current", value = "当前页数", defaultValue = "1", dataType = "Int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", defaultValue = "20", dataType = "Int", required = true, paramType = "query"),
    })
    public ApiUtils<List<VoGoods>> shareGoods(Integer userId, String cityCode, Integer current, Integer size) {
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
 
        Page page = new Page<VoGoods>(current, size);
        // 查询商品信息
        List<VoGoods> list = goodsService.listGoods(12, null, null, null, null, null, null, page);
        return ApiUtils.returnOK(
                list.stream()
                        .map(o -> {
                            o.setTagList(mallTagService.getTagListByGoodsId(mallTags, o.getTagIds()));
                            return o;
                        })
                        .collect(Collectors.toList())
        );
    }
 
    @GetMapping(value = "/detail/{id}")
    @ApiOperation(value = "获取商品详情", notes = "获取商品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", dataType = "Int", defaultValue = "1", paramType = "path"),
    })
    public ApiUtils<VoGoodsDetail> detail(@PathVariable("id") Integer id) {
        VoGoodsDetail o = goodsService.goodsDetail(id, null);
        List<MallGoodsSku> skuList = goodsSkuService.queryPlatMinSalePrice(null);
        Map<Integer, BigDecimal> minPriceSale = skuList.stream()
                .collect(Collectors.toMap(MallGoodsSku::getGoodsId, MallGoodsSku::getPriceSale));
        Map<Integer, BigDecimal> minPriceMember = skuList.stream()
                .collect(Collectors.toMap(MallGoodsSku::getGoodsId, MallGoodsSku::getPriceMember));
        if (o != null) {
            BigDecimal money = minPriceSale.get(o.getId());
            BigDecimal minMemberMoney = minPriceMember.get(o.getId());
            o.setTagList(mallTagService.getTagListByGoodsId(null, o.getTagIds()));
            o.setPriceSale(Optional.ofNullable(money).orElse(o.getPriceSale()));
            o.setPriceMember(Optional.ofNullable(minMemberMoney).orElse(o.getPriceMerchant()));
        }
        return ApiUtils.returnOK(o);
    }
 
    @GetMapping(value = "/merchant/{id}")
    @ApiOperation(value = "获取商品适用门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", dataType = "Int", defaultValue = "1", paramType = "path"),
            @ApiImplicitParam(name = "cityCode", value = "城市code", dataType = "Int", paramType = "query")
    })
    public ApiUtils<List<MemMerchant>> merchant(@PathVariable("id") Integer id, String cityCode) {
        MallGoods goods = goodsService.getById(id);
        if (goods != null && goods.getGroupType() != EnumMallGoodsGroupType.GOODS.index) {
            // 套餐商品获取所有门店
            return ApiUtils.returnOK(memMerchantService.list(
                    Wrappers.<MemMerchant>query().lambda()
                            .eq(MemMerchant::getIsDelete, EnumIsDelete.EXISTED.index)
            ));
        }
 
        return ApiUtils.returnOK(memMerchantService.getMerchantListByGoodsId(id, cityCode));
    }
 
    @GetMapping(value = "/goodsSpec/{id}")
    @ApiOperation(value = "商品规格", notes = "商品规格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", dataType = "Int", defaultValue = "1", paramType = "path"),
            @ApiImplicitParam(name = "merchantId", value = "门店ID", dataType = "Int", defaultValue = "1", paramType = "query")
    })
    public ApiUtils<VoGoodsSpecSet> goodsSpec(@PathVariable("id") Integer id, Integer merchantId) {
        VoGoodsSpecSet goodsSpecSet = new VoGoodsSpecSet();
 
//        //获取商品所有规格值id组合
//        List<VoGoodsSpec> voGoodsSpecs = new ArrayList<>();
        QueryWrapper<MallGoodsSku> wrapper = new QueryWrapper<MallGoodsSku>().eq("goods_id", id);
        if (merchantId != null) {
            wrapper.eq("merchant_id", merchantId);
        } else {
            wrapper.eq("merchant_id", 0);
        }
        List<MallGoodsSku> goodsSkuList = goodsSkuService.list(wrapper);
        //获取商品规格组sku信息
        goodsSpecSet.setGoodsSkuList(goodsSkuList);
//        if (ToolUtil.isNotEmpty(goodsSkuList)) {
//            //存储所有规格值
//            Set<Integer> specValueSet = new HashSet<>();
//            goodsSkuList.forEach(sku -> {
//                String[] specGrepArray = sku.getSpecGrep().split(",");
//                for (String s:specGrepArray) {
//                    if (!s.equals("0")) {
//                        specValueSet.add(Integer.parseInt(s));
//                    }
//                }
//            });
//
//            List<Integer> specValueList = new ArrayList<>(specValueSet);
//            List<Map<String, Object>> specGroupList = goodsSpecService.listGoodsSpecGroup(specValueList);
//            if (ToolUtil.isNotEmpty(specGroupList)) {
//                specGroupList.forEach(sp -> {
//                    VoGoodsSpec goodsSpec = new VoGoodsSpec();
//                    Integer spId = (Integer) sp.get("specId");
//                    goodsSpec.setSpecId(spId);
//                    goodsSpec.setSpecName((String) sp.get("spec_name"));
//
//                    if (!(voGoodsSpecs.stream().filter(w-> w.getSpecId() == spId).findAny().isPresent())){
//                        voGoodsSpecs.add(goodsSpec);
//                    }
//                });
//                voGoodsSpecs.forEach(goodsSpec -> {
//                    List<MallGoodsSpecValue> goodsSpecValues = new ArrayList<>();
//                    specGroupList.forEach(sv -> {
//                        Integer svId = (Integer) sv.get("spec_id");
//                        if (svId == goodsSpec.getSpecId()) {
//                            MallGoodsSpecValue specValue = new MallGoodsSpecValue();
//                            specValue.setId((Integer) sv.get("id"));
//                            specValue.setSpecId(goodsSpec.getSpecId());
//                            specValue.setSpecValue((String) sv.get("spec_value"));
//                            goodsSpecValues.add(specValue);
//                        }
//                    });
//                    goodsSpec.setGoodsSpecValueList(goodsSpecValues);
//                });
//            }
//        }
//        goodsSpecSet.setVoGoodsSpecList(voGoodsSpecs);
        return ApiUtils.returnOK(goodsSpecSet);
    }
 
}