Selaa lähdekoodia

:thought_balloon: 完善分类页api

nnkwrik 6 vuotta sitten
vanhempi
sitoutus
d27e886c77

+ 18 - 161
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/GoodsController.java

@@ -1,207 +1,64 @@
 package io.github.nnkwrik.goodsservice.controller;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import io.github.nnkwrik.goodsservice.dao.GoodsMapper;
-import io.github.nnkwrik.goodsservice.model.po.Category;
 import io.github.nnkwrik.goodsservice.model.po.Channel;
 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.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 /**
  * @author nnkwrik
  * @date 18/11/14 18:42
  */
+@Slf4j
 @RestController
 public class GoodsController {
 
     @Autowired
     private GoodsMapper goodsMapper;
 
-    //    @GetMapping("/index/index")
-    public String test() throws JsonProcessingException {
-
-//          newGoods: res.data.newGoodsList,
-//          hotGoods: res.data.hotGoodsList,
-//          topics: res.data.topicList,
-//          brand: res.data.brandList,
-//          floorGoods: res.data.categoryList,
-//          banner: res.data.banner,
-//          channel: res.data.channel
-
-        Map<String, Object> json = new HashMap<>();
-
-        Map<String, Object> data = new HashMap<>();
-
-        String img = "https://nnkwrik.github.io/2018/11/12/20181112/1541980429999.png";
-
-        List<Map> newGoodsList = new ArrayList<>();
-        Map<String, Object> newGoodsListIn = new HashMap<>();
-        newGoodsList.add(newGoodsListIn);
-
-        newGoodsListIn.put("id", 1);
-        newGoodsListIn.put("list_pic_url", img);
-        newGoodsListIn.put("name", "newGoods");
-        newGoodsListIn.put("retail_price", 100);
-
-
-        Map<String, Object> hotGoodsList = new HashMap<>();
-        hotGoodsList.put("id", 1);
-        hotGoodsList.put("list_pic_url", img);
-        hotGoodsList.put("name", "hotGoodsList");
-        hotGoodsList.put("retail_price", 100);
-        hotGoodsList.put("goods_brief", "热门铲平");
-
-        Map<String, Object> categoryList = new HashMap<>();
-        Map<String, Object> categoryListIn = new HashMap<>();
-        categoryList.put("categoryList", categoryListIn);
-
-        categoryListIn.put("id", 1);
-        categoryListIn.put("list_pic_url", img);
-        categoryListIn.put("name", "categoryListIn");
-        categoryListIn.put("retail_price", 100);
-
-        data.put("newGoodsList", newGoodsList);
-        data.put("hotGoodsList", hotGoodsList);
-        data.put("categoryList", categoryList);
-
-        json.put("data", data);
-        String res = new ObjectMapper().writeValueAsString(json);
-//        System.out.println(res);
-
-//        "{\"errno\":0," +
-//                "\"errmsg\":\"\"," +
-//                "\"data\":{" +
-//                "\"banner\":[{\"id\":1,\"ad_position_id\":1,\"media_type\":1,\"name\":\"合作 谁是你的菜\",\"link\":\"\",\"image_url\":\"http://yanxuan.nosdn.127.net/65091eebc48899298171c2eb6696fe27.jpg\",\"content\":\"合作 谁是你的菜\",\"end_time\":0,\"enabled\":1}," +
-//                "{\"id\":2,\"ad_position_id\":1,\"media_type\":1,\"name\":\"活动 美食节\",\"link\":\"\",\"image_url\":\"http://yanxuan.nosdn.127.net/bff2e49136fcef1fd829f5036e07f116.jpg\",\"content\":\"活动 美食节\",\"end_time\":0,\"enabled\":1}," +
-//                "{\"id\":3,\"ad_position_id\":1,\"media_type\":1,\"name\":\"活动 母亲节\",\"link\":\"\",\"image_url\":\"http://yanxuan.nosdn.127.net/8e50c65fda145e6dd1bf4fb7ee0fcecc.jpg\",\"content\":\"活动 母亲节\",\"end_time\":0,\"enabled\":1}]," +
-//                "\"channel\":[{\"id\":1,\"name\":\"居家\",\"url\":\"/pages/category/category?id=1005000\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/c031ea3cf575f885cd1c.png\",\"sort_order\":1}," +
-//                "{\"id\":2,\"name\":\"餐厨\",\"url\":\"/pages/category/category?id=1005001\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/4fbe8913819b017ebe1b.png\",\"sort_order\":2}," +
-//                "{\"id\":3,\"name\":\"配件\",\"url\":\"/pages/category/category?id=1008000\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/e8070853e6c6f5627713.png\",\"sort_order\":3}," +
-//                "{\"id\":4,\"name\":\"服装\",\"url\":\"/pages/category/category?id=1005002\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/4fa3c0c72964901c5a45.png\",\"sort_order\":4}," +
-//                "{\"id\":5,\"name\":\"志趣\",\"url\":\"/pages/category/category?id=1019000\",\"icon_url\":\"http://ac-"
-
-        String a = "{\"errno\":0," +
-                "\"errmsg\":\"\"," +
-                "\"data\":{" +
-                "\"newGoodsList\":[{\"id\":1,\"name\":\"居家\",\"list_pic_url\":\"" + img + "\",\"retail_price\":100}," +
-                "{\"id\":1,\"name\":\"居家\",\"list_pic_url\":\"" + img + "\",\"retail_price\":100}]," +
-                "\"hotGoodsList\":[{\"id\":1,\"name\":\"居家\",\"list_pic_url\":\"" + img + "\",\"retail_price\":100,\"goods_brief\":\"热门铲平\"}," +
-                "{\"id\":1,\"name\":\"居家\",\"list_pic_url\":\"" + img + "\",\"retail_price\":100,\"goods_brief\":\"热门铲平\"}]," +
-                "\"categoryList\":[{\"id\":1,\"name\":\"居家\",\"list_pic_url\":\"" + img + "\",\"retail_price\":100}," +
-                "{\"id\":2,\"name\":\"居家\",\"list_pic_url\":\"" + img + "\",\"retail_price\":100}]," +
-                "\"topicList\":[{\"id\":1,\"title\":\"居家\",\"scene_pic_url\":\"" + img + "\",\"price_info\":100,\"subtitle\":\"热门铲平\"}," +
-                "{\"id\":1,\"title\":\"居家\",\"scene_pic_url\":\"" + img + "\",\"price_info\":100,\"subtitle\":\"热门铲平\"}]," +
-                "\"brandList\":[{\"id\":1,\"name\":\"居家\",\"new_pic_url\":\"" + img + "\",\"floor_price\":100}," +
-                "{\"id\":1,\"name\":\"居家\",\"new_pic_url\":\"" + img + "\",\"floor_price\":100}]," +
-                "\"channel\":[{\"id\":1,\"name\":\"居家\",\"url\":\"/pages/category/category?id=1005000\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/c031ea3cf575f885cd1c.png\",\"sort_order\":1}," +
-                "{\"id\":2,\"name\":\"餐厨\",\"url\":\"/pages/category/category?id=1005001\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/4fbe8913819b017ebe1b.png\",\"sort_order\":2}," +
-                "{\"id\":3,\"name\":\"配件\",\"url\":\"/pages/category/category?id=1008000\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/e8070853e6c6f5627713.png\",\"sort_order\":3}," +
-                "{\"id\":2,\"name\":\"餐厨\",\"url\":\"/pages/category/category?id=1005001\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/4fbe8913819b017ebe1b.png\",\"sort_order\":2}," +
-                "{\"id\":3,\"name\":\"配件\",\"url\":\"/pages/category/category?id=1008000\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/e8070853e6c6f5627713.png\",\"sort_order\":3}," +
-                "{\"id\":2,\"name\":\"餐厨\",\"url\":\"/pages/category/category?id=1005001\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/4fbe8913819b017ebe1b.png\",\"sort_order\":2}," +
-                "{\"id\":3,\"name\":\"配件\",\"url\":\"/pages/category/category?id=1008000\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/e8070853e6c6f5627713.png\",\"sort_order\":3}," +
-                "{\"id\":4,\"name\":\"服装\",\"url\":\"/pages/category/category?id=1005002\",\"icon_url\":\"http://ac-3yr0g9cz.clouddn.com/4fa3c0c72964901c5a45.png\",\"sort_order\":4}]," +
-                "\"banner\":[{\"id\":1,\"ad_position_id\":1,\"media_type\":1,\"name\":\"合作 谁是你的菜\",\"link\":\"\",\"image_url\":\"" + img + "\",\"content\":\"合作 谁是你的菜\",\"end_time\":0,\"enabled\":1}," +
-                "{\"id\":1,\"ad_position_id\":1,\"media_type\":1,\"name\":\"合作 谁是你的菜\",\"link\":\"\",\"image_url\":\"" + img + "\",\"content\":\"合作 谁是你的菜\",\"end_time\":0,\"enabled\":1}]" +
-                "}}";
-
-
-        return a;
-
-//        Map<String,Object>  channel = new HashMap<>();
-//
-//
-//
-//        Map<String,Object>  topicList = new HashMap<>();
-//        Map<String,Object>  brandList = new HashMap<>();
-//        Map<String,Object>  banner = new HashMap<>();
-
-    }
+    @Autowired
+    private CatalogService catalogService;
 
     @GetMapping("/index/index")
     public ResponseVO<IndexVO> index() {
 
-        List<Channel> channelList = goodsMapper.findChannel();
-        IndexVO indexVO = IndexVO.createFromPo(channelList);
-//        indexVO.setChannel(channelList);
+        List<Channel> channelList = goodsMapper.findChannel();  //service
+        List<ChannelVo> channelVoList = PO2VO.convertList(PO2VO.Channel, channelList);  //service
 
-        System.out.println(indexVO);
+        IndexVO vo = new IndexVO(null, null, channelVoList);
+        log.debug("浏览首页 : 广告 = {},分类 = {}, 商品 = {}", vo.getBanner(), vo.getChannel(), vo.getIndexGoodsList());
 
-        return ResponseVO.ok(indexVO);
+        return ResponseVO.ok(vo);
     }
 
     @GetMapping("/catalog/index")
     public ResponseVO<CatalogVo> catalog() {
 
-//        navList: [], res.data.categoryList, List[id,name]
-//        categoryList: [],
-//        currentCategory: {}, wap_banner_url,front_name,name,subCategoryList[id,name,wap_banner_url,cat_id]
-//
-//        navList:
-//                currentCategory:
-//goodsList id list_pic_url name retail_price
-
-        List<Category> mainCategory = goodsMapper.findMainCategory();
-        List<Category> subCategory = goodsMapper.findSubCategory(mainCategory.get(0).getId());
-
-        CatalogVo vo = CatalogVo.createFromPo(mainCategory,subCategory);
-
-
-//        Category currentCategory = new Category();
-//        currentCategory.setId(mainCategory.get(0).getId());
-//        currentCategory.setName(mainCategory.get(0).getName());
-//
-//
-//        List<CategoryVo> categoryList = po2voList(mainCategory);
-//        List<CategoryVo> subCategoryVo = po2voList(subCategory);
-//
-//        CategoryVo currentCategory = new CategoryVo();
-//        currentCategory.setId(categoryList.get(0).getId());
-//        currentCategory.setName(categoryList.get(0).getName());
-//        currentCategory.setSubCategoryList(subCategory);
-//
-//        CatalogVo vo = new CatalogVo();
-//        vo.setCategoryList(categoryList);
-//        vo.setCurrentCategory(currentCategory);
-
-
-        System.out.println(vo);
+        CatalogVo vo = catalogService.getCatalogIndex();
+        log.info("浏览分类页 : 主分类 = {}, 展示子分类 = {}", vo.getCategoryList(), vo.getCurrentCategory());
 
         return ResponseVO.ok(vo);
     }
 
     @GetMapping("/catalog/{id}")
-    public ResponseVO<CatalogVo> subCatalog(@PathVariable("id") int id){
-        List<Category> subCategory = goodsMapper.findSubCategory(id);
-//        List<CategoryVo> subCategoryVo = po2voList(subCategory);
-//        Category currentCategory = goodsMapper.findCategoryById(id);
-//        CategoryVo currentCategoryVo = PO2VO.Category.apply(currentCategory);
-//
-//
-//        currentCategoryVo.setSubCategoryList(subCategoryVo);
-//        vo.setCurrentCategory(currentCategoryVo);
-        CatalogVo vo = CatalogVo.createFromPo(subCategory);
+    public ResponseVO<CatalogVo> subCatalog(@PathVariable("id") int id) {
+
+        CatalogVo vo = catalogService.getCatalogById(id);
+        log.debug("筛选分类 : 获取子分类 = {}", vo.getCurrentCategory());
 
         return ResponseVO.ok(vo);
     }
 
-//    private List<CategoryVo> po2voList(List<Category> poList){
-//        return poList.stream()
-//                .map(PO2VO.Category).collect(Collectors.toList());
-//    }
-
-
-
 
 }

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

@@ -1,13 +1,11 @@
 package io.github.nnkwrik.goodsservice.model.vo;
 
-import io.github.nnkwrik.goodsservice.model.po.Category;
+import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import java.util.List;
-import java.util.function.Function;
-import java.util.stream.Collectors;
 
 /**
  * @author nnkwrik
@@ -20,46 +18,6 @@ public class CatalogVo {
     private List<CategoryVo> categoryList;   //所有主分类
     private  CategoryVo currentCategory;    //当前主分类的名字和它的子分类
 
-
-
-
-    //todo
-    public static CatalogVo createFromPo(List<Category> subCategory){
-        return createFromPo(null,subCategory);
-    }
-
-    public static CatalogVo createFromPo(List<Category> mainCategory,List<Category> subCategory){
-        Function<Category, CategoryVo> po2vo =
-                cate -> {
-                    CategoryVo vo = new CategoryVo();
-                    vo.setId(cate.getId());
-                    vo.setName(cate.getName());
-                    vo.setWap_banner_url(cate.getIconUrl());
-                    return vo;
-                };
-        List<CategoryVo> categoryListVo = mainCategory.stream()
-                .map(po2vo).collect(Collectors.toList());
-
-        List<CategoryVo> subCategoryVo = subCategory.stream()
-                .map(po2vo).collect(Collectors.toList());
-
-        CategoryVo currentCategoryVo = new CategoryVo();
-        currentCategoryVo.setId(mainCategory.get(0).getId());
-        currentCategoryVo.setName(mainCategory.get(0).getName());
-        currentCategoryVo.setSubCategoryList(subCategoryVo);
-
-        return new CatalogVo(categoryListVo,currentCategoryVo);
-
-    }
-
-}
-
-@Data
-class CategoryVo {
-    private Integer id;
-    private String name;
-    private String wap_banner_url;
-    private List<CategoryVo> subCategoryList;
 }
 
 

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

@@ -1,13 +1,11 @@
 package io.github.nnkwrik.goodsservice.model.vo;
 
-import io.github.nnkwrik.goodsservice.model.po.Channel;
+import io.github.nnkwrik.goodsservice.model.vo.inner.ChannelVo;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
-import org.springframework.beans.BeanUtils;
 
 import java.util.List;
-import java.util.stream.Collectors;
 
 /**
  * @author nnkwrik
@@ -21,20 +19,6 @@ public class IndexVO {
     private List<BannerVo> banner;
     private List<ChannelVo> channel;
 
-
-    //todo
-    public static IndexVO createFromPo(List<Channel> channelPo) {
-        List<ChannelVo> channelVo = channelPo.stream()
-                .map(po -> {
-                    ChannelVo vo = new ChannelVo();
-                    BeanUtils.copyProperties(po, vo);
-                    vo.setIcon_url(po.getIconUrl());
-                    return vo;
-                }).collect(Collectors.toList());
-
-        return new IndexVO(null,null,channelVo);
-    }
-
 }
 
 @Data
@@ -58,10 +42,3 @@ class BannerVo {
     private Integer enabled = 1;
 }
 
-@Data
-class ChannelVo {
-    private Integer id;
-    private String name;
-    private String url;
-    private String icon_url;
-}

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

@@ -0,0 +1,21 @@
+package io.github.nnkwrik.goodsservice.model.vo.inner;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 15:25
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class CategoryVo {
+    private Integer id;
+    private String name;
+    private String wap_banner_url;
+    private List<CategoryVo> subCategoryList;
+}

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

@@ -0,0 +1,15 @@
+package io.github.nnkwrik.goodsservice.model.vo.inner;
+
+import lombok.Data;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 15:28
+ */
+@Data
+public class ChannelVo {
+    private Integer id;
+    private String name;
+    private String url;
+    private String icon_url;
+}

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

@@ -0,0 +1,15 @@
+package io.github.nnkwrik.goodsservice.service;
+
+import io.github.nnkwrik.goodsservice.model.vo.CatalogVo;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 15:22
+ */
+public interface CatalogService {
+
+    CatalogVo getCatalogIndex();
+
+    CatalogVo getCatalogById(int id);
+
+}

+ 53 - 0
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/CatalogServiceImpl.java

@@ -0,0 +1,53 @@
+package io.github.nnkwrik.goodsservice.service.impl;
+
+import io.github.nnkwrik.goodsservice.dao.GoodsMapper;
+import io.github.nnkwrik.goodsservice.model.po.Category;
+import io.github.nnkwrik.goodsservice.model.vo.CatalogVo;
+import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
+import io.github.nnkwrik.goodsservice.service.CatalogService;
+import io.github.nnkwrik.goodsservice.util.PO2VO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @author nnkwrik
+ * @date 18/11/16 15:23
+ */
+@Service
+public class CatalogServiceImpl implements CatalogService {
+    @Autowired
+    private GoodsMapper goodsMapper;
+
+    @Override
+    public CatalogVo getCatalogIndex(){
+        List<Category> mainCategory = goodsMapper.findMainCategory();
+        Category currentCate = mainCategory.get(0);
+        List<Category> subCategory = goodsMapper.findSubCategory(currentCate.getId());
+
+
+        List<CategoryVo> mainCategoryVo = PO2VO.convertList(PO2VO.Category, mainCategory);
+        List<CategoryVo> subCategoryVo = PO2VO.convertList(PO2VO.Category, subCategory);
+        CategoryVo currentCategoryVo =
+                new CategoryVo(currentCate.getId(), currentCate.getName(), currentCate.getIconUrl(), subCategoryVo);
+
+        return new CatalogVo(mainCategoryVo, currentCategoryVo);
+    }
+
+    @Override
+    public CatalogVo getCatalogById(int id) {
+        List<Category> subCategory = goodsMapper.findSubCategory(id);
+        List<CategoryVo> subCategoryVo = PO2VO.convertList(PO2VO.Category, subCategory);
+        Category currentCategory = goodsMapper.findCategoryById(id);
+        CategoryVo currentCategoryVo = PO2VO.Category.apply(currentCategory);
+        currentCategoryVo.setSubCategoryList(subCategoryVo);
+
+        return new CatalogVo(null, currentCategoryVo);
+    }
+
+
+
+
+
+}

+ 34 - 9
goods-service/src/main/java/io/github/nnkwrik/goodsservice/util/PO2VO.java

@@ -1,18 +1,43 @@
 package io.github.nnkwrik.goodsservice.util;
 
+import io.github.nnkwrik.goodsservice.model.po.Category;
+import io.github.nnkwrik.goodsservice.model.po.Channel;
+import io.github.nnkwrik.goodsservice.model.vo.inner.CategoryVo;
+import io.github.nnkwrik.goodsservice.model.vo.inner.ChannelVo;
+import org.springframework.beans.BeanUtils;
+
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
 /**
  * @author nnkwrik
  * @date 18/11/15 18:00
  */
 public class PO2VO {
-//
-//    public static Function<Category, CategoryVo> Category =
-//            cate -> {
-//                CategoryVo vo = new CategoryVo();
-//                vo.setId(cate.getId());
-//                vo.setName(cate.getName());
-//                vo.setWap_banner_url(cate.getIconUrl());
-//                return vo;
-//            };
+
+    public static Function<Category, CategoryVo> Category =
+            po -> {
+                CategoryVo vo = new CategoryVo();
+                BeanUtils.copyProperties(po,vo);
+                //手动拷贝和po不相同的属性。
+                vo.setWap_banner_url(po.getIconUrl());
+                return vo;
+            };
+
+    public static Function<Channel, ChannelVo> Channel =
+            po -> {
+                ChannelVo vo = new ChannelVo();
+                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){
+        return poList.stream()
+                .map(converter).collect(Collectors.toList());
+    }
+
+
 
 }

+ 6 - 1
goods-service/src/main/resources/application.yml

@@ -11,4 +11,9 @@ server:
 
 mybatis:
   configuration:
-    map-underscore-to-camel-case: true
+    map-underscore-to-camel-case: true
+
+logging:
+  level:
+    org.springframework.web: info
+    io.github.nnkwrik.goodsservice: debug