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);
|
}
|
|
}
|