Parcourir la source

:baby_chick: 首页添加广告和推荐商品

nnkwrik il y a 6 ans
Parent
commit
71c67c284c

+ 1 - 0
dev/mysql/conf/my.cnf

@@ -1,5 +1,6 @@
 [mysqld]
 character-set-server=utf8mb4
+default-time_zone=+8:00
 
 [client]
 default-character-set=utf8mb4

+ 1 - 1
dev/ngrok/start.sh

@@ -1,2 +1,2 @@
 #!/usr/bin/env bash
-./ngrok http 8802
+./ngrok http 8804

+ 3 - 13
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/GoodsController.java

@@ -1,21 +1,16 @@
 package io.github.nnkwrik.goodsservice.controller;
 
-import io.github.nnkwrik.goodsservice.dao.GoodsMapper;
-import io.github.nnkwrik.goodsservice.model.po.Channel;
+import io.github.nnkwrik.goodsservice.dao.CategoryMapper;
 import io.github.nnkwrik.goodsservice.model.vo.CatalogVo;
 import io.github.nnkwrik.goodsservice.model.vo.IndexVO;
 import io.github.nnkwrik.goodsservice.model.vo.ResponseVO;
-import io.github.nnkwrik.goodsservice.model.vo.inner.ChannelVo;
 import io.github.nnkwrik.goodsservice.service.CatalogService;
-import io.github.nnkwrik.goodsservice.util.PO2VO;
 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.RestController;
 
-import java.util.List;
-
 /**
  * @author nnkwrik
  * @date 18/11/14 18:42
@@ -24,19 +19,13 @@ import java.util.List;
 @RestController
 public class GoodsController {
 
-    @Autowired
-    private GoodsMapper goodsMapper;
-
     @Autowired
     private CatalogService catalogService;
 
     @GetMapping("/index/index")
     public ResponseVO<IndexVO> index() {
 
-        List<Channel> channelList = goodsMapper.findChannel();  //service
-        List<ChannelVo> channelVoList = PO2VO.convertList(PO2VO.Channel, channelList);  //service
-
-        IndexVO vo = new IndexVO(null, null, channelVoList);
+        IndexVO vo = catalogService.getIndex();
         log.debug("浏览首页 : 广告 = {},分类 = {}, 商品 = {}", vo.getBanner(), vo.getChannel(), vo.getIndexGoodsList());
 
         return ResponseVO.ok(vo);
@@ -61,4 +50,5 @@ public class GoodsController {
     }
 
 
+
 }

+ 26 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/dao/CategoryMapper.java

@@ -0,0 +1,26 @@
+package io.github.nnkwrik.goodsservice.dao;
+
+import io.github.nnkwrik.goodsservice.model.po.Category;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/14 22:19
+ */
+@Mapper
+public interface CategoryMapper {
+
+    @Select("select id,name from category where parent_id = 0 order by sort_order asc")
+    List<Category> findMainCategory();
+
+    @Select("select id,name,icon_url from category where parent_id = #{parentId} order by sort_order asc")
+    List<Category> findSubCategory(@Param("parentId") int parentId);
+
+    @Select("select id,name from category where id = #{id}")
+    Category findCategoryById(@Param("id") int id);
+
+}

+ 7 - 13
goods-service/src/main/java/io/github/nnkwrik/goodsservice/dao/GoodsMapper.java

@@ -1,29 +1,23 @@
 package io.github.nnkwrik.goodsservice.dao;
 
-import io.github.nnkwrik.goodsservice.model.po.Category;
-import io.github.nnkwrik.goodsservice.model.po.Channel;
+import io.github.nnkwrik.goodsservice.model.po.Goods;
 import org.apache.ibatis.annotations.Mapper;
-import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 
 import java.util.List;
 
 /**
  * @author nnkwrik
- * @date 18/11/14 22:19
+ * @date 18/11/16 19:15
  */
 @Mapper
 public interface GoodsMapper {
-    @Select("select * from channel order by sort_order asc")
-    List<Channel> findChannel();
 
-    @Select("select * from category where parent_id = 0 order by sort_order asc")
-    List<Category> findMainCategory();
+    @Select("select id, `name`, primary_pic_url, price\n" +
+            "from goods\n" +
+            "order by browse_count desc, last_edit desc\n" +
+            "limit 10;")
+    List<Goods> findSimpleGoods();
 
-    @Select("select * from category where parent_id = #{parentId} order by sort_order asc")
-    List<Category> findSubCategory(@Param("parentId") int parentId);
-
-    @Select("select * from category where id = #{id}")
-    Category findCategoryById(@Param("id") int id);
 
 }

+ 26 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/dao/OtherMapper.java

@@ -0,0 +1,26 @@
+package io.github.nnkwrik.goodsservice.dao;
+
+import io.github.nnkwrik.goodsservice.model.po.Ad;
+import io.github.nnkwrik.goodsservice.model.po.Channel;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 19:14
+ */
+@Mapper
+public interface OtherMapper {
+
+    @Select("select id,name,url,icon_url from channel order by sort_order asc")
+    List<Channel> findChannel();
+
+    @Select("select id, image_url, link\n" +
+            "from ad\n" +
+            "where enable = true\n" +
+            "order by sort_order asc, `create` desc\n" +
+            "limit 5")
+    List<Ad> findAd();
+}

+ 18 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/po/Ad.java

@@ -0,0 +1,18 @@
+package io.github.nnkwrik.goodsservice.model.po;
+
+import lombok.Data;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 20:01
+ */
+@Data
+public class Ad {
+    private Integer id;
+    private String name;
+    private String imageUrl;
+    private String link;
+    private Integer sortOrder;
+    private Boolean enable;
+    private Data data;
+}

+ 26 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/po/Goods.java

@@ -0,0 +1,26 @@
+package io.github.nnkwrik.goodsservice.model.po;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 19:07
+ */
+@Data
+public class Goods {
+    private Integer id;
+    private Integer categoryId;
+    private Integer sellerId;
+    private String name;
+    private Double price;
+    private Double marketPrice;
+    private String primaryPicUrl;
+    private String desc;
+    private Integer wantCount;
+    private Integer browseCount;
+    private Boolean isOnSale;
+    private Boolean isDelete;
+    private Date lastEdit;
+}

+ 3 - 23
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/vo/IndexVO.java

@@ -1,6 +1,8 @@
 package io.github.nnkwrik.goodsservice.model.vo;
 
+import io.github.nnkwrik.goodsservice.model.vo.inner.BannerVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.ChannelVo;
+import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
@@ -15,30 +17,8 @@ import java.util.List;
 @AllArgsConstructor
 @NoArgsConstructor
 public class IndexVO {
-    private List<GoodsSimpleVO> indexGoodsList;
+    private List<GoodsSimpleVo> indexGoodsList;
     private List<BannerVo> banner;
     private List<ChannelVo> channel;
-
-}
-
-@Data
-class GoodsSimpleVO {
-    private Integer id;
-    private String name;
-    private String list_pic_url;
-    private Double retail_price;
-}
-
-@Data
-class BannerVo {
-    private Integer id;
-    private String name;
-    private String image_url;
-    private Integer ad_position_id = 1;
-    private Integer media_type = 1;
-    private String link;
-    private String content;
-    private Integer end_time = 0;
-    private Integer enabled = 1;
 }
 

+ 14 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/vo/inner/BannerVo.java

@@ -0,0 +1,14 @@
+package io.github.nnkwrik.goodsservice.model.vo.inner;
+
+import lombok.Data;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 20:00
+ */
+@Data
+public class BannerVo {
+    private Integer id;
+    private String image_url;
+    private String link;
+}

+ 15 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/model/vo/inner/GoodsSimpleVo.java

@@ -0,0 +1,15 @@
+package io.github.nnkwrik.goodsservice.model.vo.inner;
+
+import lombok.Data;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 19:05
+ */
+@Data
+public class GoodsSimpleVo {
+    private Integer id;
+    private String name;
+    private String list_pic_url;
+    private Double price;
+}

+ 4 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/CatalogService.java

@@ -1,6 +1,7 @@
 package io.github.nnkwrik.goodsservice.service;
 
 import io.github.nnkwrik.goodsservice.model.vo.CatalogVo;
+import io.github.nnkwrik.goodsservice.model.vo.IndexVO;
 
 /**
  * @author nnkwrik
@@ -8,8 +9,11 @@ import io.github.nnkwrik.goodsservice.model.vo.CatalogVo;
  */
 public interface CatalogService {
 
+    IndexVO getIndex();
+
     CatalogVo getCatalogIndex();
 
     CatalogVo getCatalogById(int id);
 
+
 }

+ 36 - 9
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/CatalogServiceImpl.java

@@ -1,9 +1,18 @@
 package io.github.nnkwrik.goodsservice.service.impl;
 
+import io.github.nnkwrik.goodsservice.dao.CategoryMapper;
 import io.github.nnkwrik.goodsservice.dao.GoodsMapper;
+import io.github.nnkwrik.goodsservice.dao.OtherMapper;
+import io.github.nnkwrik.goodsservice.model.po.Ad;
 import io.github.nnkwrik.goodsservice.model.po.Category;
+import io.github.nnkwrik.goodsservice.model.po.Channel;
+import io.github.nnkwrik.goodsservice.model.po.Goods;
 import io.github.nnkwrik.goodsservice.model.vo.CatalogVo;
+import io.github.nnkwrik.goodsservice.model.vo.IndexVO;
+import io.github.nnkwrik.goodsservice.model.vo.inner.BannerVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
+import io.github.nnkwrik.goodsservice.model.vo.inner.ChannelVo;
+import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
 import io.github.nnkwrik.goodsservice.service.CatalogService;
 import io.github.nnkwrik.goodsservice.util.PO2VO;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -17,14 +26,35 @@ import java.util.List;
  */
 @Service
 public class CatalogServiceImpl implements CatalogService {
+    @Autowired
+    private CategoryMapper categoryMapper;
+    @Autowired
+    private OtherMapper otherMapper;
     @Autowired
     private GoodsMapper goodsMapper;
 
     @Override
-    public CatalogVo getCatalogIndex(){
-        List<Category> mainCategory = goodsMapper.findMainCategory();
+    public IndexVO getIndex() {
+        //广告
+        List<Ad> adList = otherMapper.findAd();
+        List<BannerVo> bannerVoList = PO2VO.convertList(PO2VO.Banner, adList);
+
+        //分类
+        List<Channel> channelList = otherMapper.findChannel();
+        List<ChannelVo> channelVoList = PO2VO.convertList(PO2VO.Channel, channelList);
+
+        //推荐商品
+        List<Goods> goodsList = goodsMapper.findSimpleGoods();
+        List<GoodsSimpleVo> goodsSimpleVOList = PO2VO.convertList(PO2VO.GoodsSimple, goodsList);
+
+        return new IndexVO(goodsSimpleVOList, bannerVoList, channelVoList);
+    }
+
+    @Override
+    public CatalogVo getCatalogIndex() {
+        List<Category> mainCategory = categoryMapper.findMainCategory();
         Category currentCate = mainCategory.get(0);
-        List<Category> subCategory = goodsMapper.findSubCategory(currentCate.getId());
+        List<Category> subCategory = categoryMapper.findSubCategory(currentCate.getId());
 
 
         List<CategoryVo> mainCategoryVo = PO2VO.convertList(PO2VO.Category, mainCategory);
@@ -37,17 +67,14 @@ public class CatalogServiceImpl implements CatalogService {
 
     @Override
     public CatalogVo getCatalogById(int id) {
-        List<Category> subCategory = goodsMapper.findSubCategory(id);
+        List<Category> subCategory = categoryMapper.findSubCategory(id);
         List<CategoryVo> subCategoryVo = PO2VO.convertList(PO2VO.Category, subCategory);
-        Category currentCategory = goodsMapper.findCategoryById(id);
-        CategoryVo currentCategoryVo = PO2VO.Category.apply(currentCategory);
+        Category currentCategory = categoryMapper.findCategoryById(id);
+        CategoryVo currentCategoryVo = PO2VO.convert(PO2VO.Category, currentCategory);
         currentCategoryVo.setSubCategoryList(subCategoryVo);
 
         return new CatalogVo(null, currentCategoryVo);
     }
 
 
-
-
-
 }

+ 27 - 4
goods-service/src/main/java/io/github/nnkwrik/goodsservice/util/PO2VO.java

@@ -1,9 +1,13 @@
 package io.github.nnkwrik.goodsservice.util;
 
+import io.github.nnkwrik.goodsservice.model.po.Ad;
 import io.github.nnkwrik.goodsservice.model.po.Category;
 import io.github.nnkwrik.goodsservice.model.po.Channel;
+import io.github.nnkwrik.goodsservice.model.po.Goods;
+import io.github.nnkwrik.goodsservice.model.vo.inner.BannerVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.ChannelVo;
+import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
 import org.springframework.beans.BeanUtils;
 
 import java.util.List;
@@ -19,7 +23,7 @@ public class PO2VO {
     public static Function<Category, CategoryVo> Category =
             po -> {
                 CategoryVo vo = new CategoryVo();
-                BeanUtils.copyProperties(po,vo);
+                BeanUtils.copyProperties(po, vo);
                 //手动拷贝和po不相同的属性。
                 vo.setWap_banner_url(po.getIconUrl());
                 return vo;
@@ -28,16 +32,35 @@ public class PO2VO {
     public static Function<Channel, ChannelVo> Channel =
             po -> {
                 ChannelVo vo = new ChannelVo();
-                BeanUtils.copyProperties(po,vo);
+                BeanUtils.copyProperties(po, vo);
                 vo.setIcon_url(po.getIconUrl());
                 return vo;
             };
 
-    public static <T,R> List<R> convertList(Function<T,R> converter, List<T> poList){
+    public static Function<Goods, GoodsSimpleVo> GoodsSimple =
+            po -> {
+                GoodsSimpleVo vo = new GoodsSimpleVo();
+                BeanUtils.copyProperties(po, vo);
+                vo.setList_pic_url(po.getPrimaryPicUrl());
+                return vo;
+            };
+
+    public static Function<Ad, BannerVo> Banner =
+            po -> {
+                BannerVo vo = new BannerVo();
+                BeanUtils.copyProperties(po, vo);
+                vo.setImage_url(po.getImageUrl());
+                return vo;
+            };
+
+    public static <T, R> R convert(Function<T, R> converter, T po) {
+        return converter.apply(po);
+    }
+
+    public static <T, R> List<R> convertList(Function<T, R> converter, List<T> poList) {
         return poList.stream()
                 .map(converter).collect(Collectors.toList());
     }
 
 
-
 }