Skip to content

Commit

Permalink
feature: 商城 BFF 层开发查询商品分页功能 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Apr 20, 2023
1 parent 076ef99 commit a4b5619
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public class PanelProductRelationDO extends BaseDO {
*/
private Long productId;

/**
* 商品大图
*/
private String bigPic;

/**
* 商品图
*/
private String pic;

/**
* 排序
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.opengoofy.congomall.bff.biz.dto.resp.adapter;

import lombok.Data;

/**
* 商品适配返回对象
*
* @author chen.ma
* @github <a href="https://github.com/opengoofy" />
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册
*/
@Data
public class GoodsAdapterRespDTO {

private String productId;

private String productImageBig;

private String productName;

private Integer salePrice;

private String subTitle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.opengoofy.congomall.bff.biz.dto.resp.adapter;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* 商品返回包装适配返回
*
* @author chen.ma
* @github <a href="https://github.com/opengoofy" />
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodsResultAdapterRespDTO {

private Long total;

private List<GoodsAdapterRespDTO> data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.opengoofy.congomall.bff.biz.service;

import org.opengoofy.congomall.bff.biz.dto.resp.adapter.GoodsResultAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomePanelAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomeProductDetailAdapterRespDTO;

Expand Down Expand Up @@ -52,4 +53,16 @@ public interface GoodsService {
* @return 为您推荐板块返回数据
*/
HomePanelAdapterRespDTO recommend();

/**
* 全部商品集合
*
* @param page 当前页
* @param size 每页多少条
* @param sort 排序方式
* @param priceGt 价格区间开始
* @param priceLte 价格区间结束
* @return 全部商品返回数据
*/
GoodsResultAdapterRespDTO allGoods(Integer page, Integer size, Integer sort, Integer priceGt, Integer priceLte);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opengoofy.congomall.bff.biz.dao.entity.PanelDO;
import org.opengoofy.congomall.bff.biz.dao.entity.PanelProductRelationDO;
import org.opengoofy.congomall.bff.biz.dao.mapper.PanelMapper;
import org.opengoofy.congomall.bff.biz.dao.mapper.PanelProductRelationMapper;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.GoodsAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.GoodsResultAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomePanelAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomePanelContentAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomeProductDetailAdapterRespDTO;
Expand All @@ -33,6 +36,8 @@
import org.opengoofy.congomall.bff.remote.resp.ProductRespDTO;
import org.opengoofy.congomall.bff.remote.resp.ProductSpuRespDTO;
import org.opengoofy.congomall.springboot.starter.common.toolkit.BeanUtil;
import org.opengoofy.congomall.springboot.starter.convention.exception.ServiceException;
import org.opengoofy.congomall.springboot.starter.convention.page.PageResponse;
import org.opengoofy.congomall.springboot.starter.convention.result.Result;
import org.springframework.stereotype.Service;

Expand All @@ -49,6 +54,7 @@
* @github <a href="https://github.com/opengoofy" />
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class GoodsServiceImpl implements GoodsService {
Expand All @@ -58,7 +64,7 @@ public class GoodsServiceImpl implements GoodsService {
private final ProductRemoteService productRemoteService;

private static final int PRODUCT_LIMIT_CART = 1;
private static final List<String> TYPE_TWO_LIST = Lists.newArrayList("1647777981810081792", "1647788115844136960", "1647794693754322944");
private static final List<String> TYPE_TWO_LIST = Lists.newArrayList("1647777981810081792", "1647788115844136960", "1647794693754322944");

private final String cache = "[\n" +
" {\n" +
Expand Down Expand Up @@ -750,32 +756,29 @@ public List<HomePanelAdapterRespDTO> listHomePanel() {
panelProductRelationList.forEach(item -> {
Result<ProductRespDTO> productResult = productRemoteService.getProductBySpuId(String.valueOf(item.getProductId()));
if (productResult.isSuccess() && productResult.getData() != null) {
HomePanelContentAdapterRespDTO productRespDTO = new HomePanelContentAdapterRespDTO();
ProductRespDTO resultData = productResult.getData();
ProductSpuRespDTO productSpu = resultData.getProductSpu();
HomePanelContentAdapterRespDTO productRespDTO = new HomePanelContentAdapterRespDTO();
productRespDTO.setProductId(String.valueOf(productSpu.getId()));
productRespDTO.setProductName(productSpu.getName());
productRespDTO.setId(String.valueOf(productSpu.getId()));
productRespDTO.setProductId(String.valueOf(productSpu.getId()));
productRespDTO.setSalePrice(productSpu.getPrice().intValue());
productRespDTO.setId(String.valueOf(productSpu.getId()));
productRespDTO.setSortOrder(item.getSort());
productRespDTO.setSubTitle(productSpu.getSubTitle());
productRespDTO.setPanelId(each.getId());
productRespDTO.setType(0);
productRespDTO.setCreated(new Date());
productRespDTO.setUpdated(new Date());
List<String> pics = StrUtil.split(productSpu.getPic(), ",");
if (pics.size() == 1) {
productRespDTO.setProductImageBig(pics.get(0));
List<String> pics = StrUtil.split(Optional.ofNullable(item.getPic()).orElse(productSpu.getPic()), ",");
if (CollUtil.isNotEmpty(pics)) {
productRespDTO.setProductImageBig(Optional.ofNullable(item.getBigPic()).orElse(pics.get(0)));
productRespDTO.setPicUrl(pics.get(0));
}
if (pics.size() > 1) {
productRespDTO.setPicUrl(pics.get(1));
}
if (pics.size() > 2) {
productRespDTO.setPicUrl2(pics.get(2));
}
if (pics.size() > 3) {
productRespDTO.setPicUrl3(pics.get(3));
if (pics.size() > 1) {
productRespDTO.setPicUrl2(pics.get(1));
}
if (pics.size() > 2) {
productRespDTO.setPicUrl3(pics.get(2));
}
}
if (TYPE_TWO_LIST.contains(String.valueOf(productSpu.getId()))) {
productRespDTO.setType(2);
Expand Down Expand Up @@ -844,4 +847,30 @@ public HomePanelAdapterRespDTO recommend() {
}
return result;
}

@Override
public GoodsResultAdapterRespDTO allGoods(Integer page, Integer size, Integer sort, Integer priceGt, Integer priceLte) {
Result<PageResponse<ProductRespDTO>> pageResponseResult = null;
try {
pageResponseResult = productRemoteService.pageQueryProduct(page, size, sort, priceGt, priceLte);
if (!pageResponseResult.isSuccess() || pageResponseResult.getData() == null) {
throw new ServiceException("调用商品服务分页查询商品失败");
}
} catch (Throwable ex) {
log.error("调用商品服务分页查询商品失败", ex);
}
PageResponse<ProductRespDTO> pageResponse = pageResponseResult.getData();
List<ProductRespDTO> records = pageResponse.getRecords();
List<GoodsAdapterRespDTO> goodsAdapter = new ArrayList<>();
records.stream().map(ProductRespDTO::getProductSpu).forEach(each -> {
GoodsAdapterRespDTO item = new GoodsAdapterRespDTO();
item.setProductId(String.valueOf(each.getId()));
item.setProductName(each.getName());
item.setSubTitle(each.getSubTitle());
item.setSalePrice(each.getPrice().intValue());
item.setProductImageBig(each.getPic());
goodsAdapter.add(item);
});
return new GoodsResultAdapterRespDTO(pageResponse.getTotal(), goodsAdapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package org.opengoofy.congomall.bff.remote;

import org.opengoofy.congomall.bff.remote.resp.ProductRespDTO;
import org.opengoofy.congomall.springboot.starter.convention.page.PageResponse;
import org.opengoofy.congomall.springboot.starter.convention.result.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
* 商品服务远程调用
Expand All @@ -38,4 +40,14 @@ public interface ProductRemoteService {
*/
@GetMapping("/api/product/spu/{spuId}")
Result<ProductRespDTO> getProductBySpuId(@PathVariable("spuId") String spuId);

/**
* 商品分页查询返回 SPU 信息
*/
@GetMapping("/api/product/page")
Result<PageResponse<ProductRespDTO>> pageQueryProduct(@RequestParam("current") Integer page,
@RequestParam("size") Integer size,
@RequestParam(value = "sort", required = false) Integer sort,
@RequestParam(value = "priceGt", required = false) Integer priceGt,
@RequestParam(value = "priceLte", required = false) Integer priceLte);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.opengoofy.congomall.bff.remote.req;

import lombok.Data;
import org.opengoofy.congomall.springboot.starter.convention.page.PageRequest;

/**
* 商品分页查询
*
* @author chen.ma
* @github <a href="https://github.com/opengoofy" />
* @公众号 马丁玩编程,关注回复:资料,领取后端技术专家成长手册
*/
@Data
public class ProductPageQuery extends PageRequest {

private Integer sort;

private Integer priceGt;

private Integer priceLte;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.opengoofy.congomall.bff.biz.common.ResultT;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.GoodsResultAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomePanelAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.dto.resp.adapter.HomeProductDetailAdapterRespDTO;
import org.opengoofy.congomall.bff.biz.service.GoodsService;
Expand Down Expand Up @@ -68,4 +69,20 @@ public ResultT<List<HomePanelAdapterRespDTO>> recommend() {
return ResultT.success(Lists.newArrayList(goodsService.recommend()));
}

@GetMapping("/goods/allGoods")
@ApiOperation(value = "全部商品", notes = "全部商品")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "全部商品列表第几页", required = true, example = "1"),
@ApiImplicitParam(name = "size", value = "全部商品列表每页多少条数据", required = true, example = "10"),
@ApiImplicitParam(name = "sort", value = "排序方式", example = "1"),
@ApiImplicitParam(name = "priceGt", value = "价格区间开始", example = "1"),
@ApiImplicitParam(name = "priceLte", value = "价格区间结束", example = "1")
})
public ResultT<GoodsResultAdapterRespDTO> allGoods(@RequestParam("page") Integer page,
@RequestParam("size") Integer size,
@RequestParam(value = "sort", required = false) Integer sort,
@RequestParam(value = "priceGt", required = false) Integer priceGt,
@RequestParam(value = "priceLte", required = false) Integer priceLte) {
return ResultT.success(goodsService.allGoods(page, size, sort, priceGt, priceLte));
}
}

0 comments on commit a4b5619

Please sign in to comment.