Kaynağa Gözat

:accept: 推荐相关商品

nnkwrik 6 yıl önce
ebeveyn
işleme
e19b71f1c5

+ 9 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/GoodsController.java

@@ -2,6 +2,7 @@ package io.github.nnkwrik.goodsservice.controller;
 
 import io.github.nnkwrik.goodsservice.model.vo.CategoryPageVo;
 import io.github.nnkwrik.goodsservice.model.vo.GoodsDetailPageVo;
+import io.github.nnkwrik.goodsservice.model.vo.GoodsRelatedVo;
 import io.github.nnkwrik.goodsservice.model.vo.ResponseVO;
 import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.GalleryVo;
@@ -68,5 +69,13 @@ public class GoodsController {
         return ResponseVO.ok(vo);
     }
 
+    @GetMapping("/goods/related/{goodsId}")
+    public ResponseVO<GoodsRelatedVo> getGoodsRelated(@PathVariable("goodsId") int goodsId) {
+        GoodsRelatedVo vo = goodsService.getGoodsRelated(goodsId);
+        log.debug("与 goodsId=[] 相关的商品 : {}", goodsId, vo);
+
+        return ResponseVO.ok(vo);
+    }
+
 
 }

+ 23 - 2
goods-service/src/main/java/io/github/nnkwrik/goodsservice/dao/CategoryMapper.java

@@ -14,19 +14,40 @@ import java.util.List;
 @Mapper
 public interface CategoryMapper {
 
+    /**
+     * 查找所有主分类
+     * @return
+     */
     @Select("select id,name from category where parent_id = 0 order by sort_order asc")
     List<Category> findMainCategory();
 
+    /**
+     * 查找父分类下的所有子分类
+     * @param parentId
+     * @return
+     */
     @Select("select id,name,icon_url from category where parent_id = #{parentId} order by sort_order asc")
     List<Category> findSubCategory(@Param("parentId") int parentId);
 
+    /**
+     * 通过id查找分类名
+     * @param id
+     * @return
+     */
     @Select("select id,name from category where id = #{id}")
     Category findCategoryById(@Param("id") int id);
 
+    /**
+     * 查找同一父分类下的兄弟分类
+     * @param id
+     * @return
+     */
     @Select("select id, name\n" +
             "from category\n" +
             "where parent_id = (select parent_id from category where id = #{id})\n" +
-            "order by sort_order asc;")
-    List<Category> findBrotherCategry(@Param("id") int id);
+            "order by sort_order asc")
+    List<Category> findBrotherCategory(@Param("id") int id);
+
+
 
 }

+ 55 - 3
goods-service/src/main/java/io/github/nnkwrik/goodsservice/dao/GoodsMapper.java

@@ -5,7 +5,6 @@ import io.github.nnkwrik.goodsservice.model.po.GoodsGallery;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
-import org.springframework.stereotype.Service;
 
 import java.util.List;
 
@@ -16,12 +15,21 @@ import java.util.List;
 @Mapper
 public interface GoodsMapper {
 
+
+    /**
+     * 列出所有简单商品信息
+     * @return
+     */
     @Select("select id, `name`, primary_pic_url, price\n" +
             "from goods\n" +
             "where is_on_sale = 1 and is_delete = 0\n" +
             "order by browse_count desc, last_edit desc")
     List<Goods> findSimpleGoods();
 
+    /**
+     * 根据分类id列出分类下的所有简单商品信息
+     * @return
+     */
     @Select("select id, `name`, primary_pic_url, price\n" +
             "from goods\n" +
             "where category_id = #{cateId}\n" +
@@ -29,6 +37,11 @@ public interface GoodsMapper {
             "order by browse_count desc, last_edit desc")
     List<Goods> findSimpleGoodsByCateId(@Param("cateId") int cateId);
 
+    /**
+     * 通过商品id查找商品的详细信息
+     * @param goodsId
+     * @return
+     */
     @Select("select id,\n" +
             "       seller_id,\n" +
             "       `name`,\n" +
@@ -41,11 +54,50 @@ public interface GoodsMapper {
             "       last_edit\n" +
             "from goods\n" +
             "where id = #{goodsId}")
-    Goods findDetailGoodsByGoodsId(@Param("goodsId") int goodId);
+    Goods findDetailGoodsByGoodsId(@Param("goodsId") int goodsId);
 
+    /**
+     * 通过商品id查找该商品关联的图片
+     * @param goodsId
+     * @return
+     */
     @Select("select id, img_url\n" +
             "from goods_gallery\n" +
             "where goods_id = #{goodsId}")
-    List<GoodsGallery> findGalleryByGoodsId(@Param("goodsId") int goodId);
+    List<GoodsGallery> findGalleryByGoodsId(@Param("goodsId") int goodsId);
+
+
+    /**
+     * 查找与该商品位于同一个子分类的简单商品信息
+     * @param goodsId
+     * @return
+     */
+    @Select("select id, `name`, primary_pic_url, price\n" +
+            "from goods\n" +
+            "where category_id = (select category_id from goods where id = #{goodsId})\n" +
+            "  and id != #{goodsId}\n" +
+            "  and is_on_sale = 1\n" +
+            "  and is_delete = 0\n" +
+            "order by browse_count desc, last_edit desc")
+    List<Goods> findSimpleGoodsInSameCate(@Param("goodsId") int goodsId);
+
+
+    /**
+     * 查找与该商品位于同一个父分类的简单商品信息
+     * @param goodsId
+     * @return
+     */
+    @Select("select id, `name`, primary_pic_url, price\n" +
+            "from goods\n" +
+            "where category_id in (select bar.id\n" +
+            "                      from goods\n" +
+            "                             inner join category as foo on foo.id = goods.category_id\n" +
+            "                             inner join category as bar on foo.parent_id = bar.parent_id\n" +
+            "                      where goods.id = #{goodsId})\n" +
+            "  and id != #{goodsId}\n" +
+            "  and is_on_sale = 1\n" +
+            "  and is_delete = 0\n" +
+            "order by browse_count desc, last_edit desc")
+    List<Goods> findSimpleGoodsInSameParentCate(@Param("goodsId") int goodsId);
 
 }

+ 1 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/vo/GoodsDetailPageVo.java

@@ -20,4 +20,5 @@ public class GoodsDetailPageVo {
     private List<GalleryVo> gallery;
     //TODO comment
 
+
 }

+ 19 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/vo/GoodsRelatedVo.java

@@ -0,0 +1,19 @@
+package io.github.nnkwrik.goodsservice.model.vo;
+
+import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/18 8:43
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class GoodsRelatedVo {
+    private List<GoodsSimpleVo> goodsList;
+}

+ 3 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/GoodsService.java

@@ -1,6 +1,7 @@
 package io.github.nnkwrik.goodsservice.service;
 
 import io.github.nnkwrik.goodsservice.model.vo.CategoryPageVo;
+import io.github.nnkwrik.goodsservice.model.vo.GoodsRelatedVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.GalleryVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsDetailVo;
 
@@ -19,4 +20,6 @@ public interface GoodsService {
     GoodsDetailVo getGoodsDetail(int id);
 
     List<GalleryVo> getGoodsGallery(int goodsId);
+
+    GoodsRelatedVo getGoodsRelated(int goodsId);
 }

+ 21 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/GoodsServiceImpl.java

@@ -7,6 +7,7 @@ import io.github.nnkwrik.goodsservice.model.po.Category;
 import io.github.nnkwrik.goodsservice.model.po.Goods;
 import io.github.nnkwrik.goodsservice.model.po.GoodsGallery;
 import io.github.nnkwrik.goodsservice.model.vo.CategoryPageVo;
+import io.github.nnkwrik.goodsservice.model.vo.GoodsRelatedVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.GalleryVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsDetailVo;
@@ -17,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Optional;
 
 /**
  * @author nnkwrik
@@ -35,7 +37,7 @@ public class GoodsServiceImpl implements GoodsService {
     @Override
     public CategoryPageVo getGoodsAndBrotherCateById(int id, int page, int size) {
 
-        List<Category> brotherCategory = categoryMapper.findBrotherCategry(id);
+        List<Category> brotherCategory = categoryMapper.findBrotherCategory(id);
         List<CategoryVo> brotherCategoryVo = PO2VO.convertList(PO2VO.category, brotherCategory);
         CategoryPageVo vo = getGoodsByCateId(id, page, size);
         vo.setBrotherCategory(brotherCategoryVo);
@@ -68,5 +70,23 @@ public class GoodsServiceImpl implements GoodsService {
         return galleryVo;
     }
 
+    @Override
+    public GoodsRelatedVo getGoodsRelated(int goodsId) {
+        //获取同一子分类下
+        int pageNum = 1;
+        int pageSize = 10;
+        PageHelper.startPage(pageNum, pageSize);
+        List<Goods> simpleGoods = goodsMapper.findSimpleGoodsInSameCate(goodsId);
+
+        //统一子分类下的数量少于10, 查找同一父分类下
+        if (simpleGoods.size() < 10) {
+            PageHelper.startPage(pageNum, pageSize);
+            simpleGoods = goodsMapper.findSimpleGoodsInSameParentCate(goodsId);
+        }
+
+        List<GoodsSimpleVo> goodsSimpleVo = PO2VO.convertList(PO2VO.goodsSimple, simpleGoods);
+        return new GoodsRelatedVo(goodsSimpleVo);
+    }
+
 
 }

+ 3 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/IndexServiceImpl.java

@@ -45,7 +45,9 @@ public class IndexServiceImpl implements IndexService {
         List<ChannelVo> channelVoList = PO2VO.convertList(PO2VO.channel, channelList);
 
         //推荐商品
-        PageHelper.startPage(1, 10);
+        int pageNum = 1;
+        int pageSize = 10;
+        PageHelper.startPage(pageNum, pageSize);
         List<Goods> goodsList = goodsMapper.findSimpleGoods();
         List<GoodsSimpleVo> goodsSimpleVOList = PO2VO.convertList(PO2VO.goodsSimple, goodsList);