第三章

1. 热搜词

1.1 接口说明

接口url:/buyer/goods/hot-words

请求方式:GET

请求参数:

参数名称参数类型说明
startint开始
endint结束

返回数据:list<String>

{
 "success":true,
 "message":"success",
 "code":2000000000,
 "result":["鼠标垫","丝袜","手机"]
}
1
2
3
4
5
6

1.2 技术点

使用redis的,zset数据结构完成。 key score value

当用户搜索的时候,搜索词为value,搜索次数为score,按照score排序即可。

1.3 前端分析

Search.vue:

<template v-if="showTag">
        <div style="height:12px" v-if="promotionTags.length === 0"></div>
        <div v-else>
          <Tag
            v-for="(item, index) in promotionTags"
            :key="index"
          >
            <span class="hover-color" @click="selectTags(item)">{{ item }}</span>
          </Tag>
        </div>
      </template>
1
2
3
4
5
6
7
8
9
10
11
  hotWords({start: 1, end: 5}).then(res => {
      if (res.success) this.promotionTags = res.result
    })
1
2
3

1.4 Controller

package com.mszlu.shop.buyer.controller.goods;

import com.mszlu.shop.buyer.service.goods.GoodsSearchService;
import com.mszlu.shop.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "买家端,商品接口")
@RestController
@RequestMapping("/goods")
public class GoodsBuyerController {

    @Autowired
    private GoodsSearchService goodsSearchService;

    @ApiOperation(value = "获取搜索热词")
    @GetMapping("/hot-words")
    public Result<List<String>> getGoodsHotWords(Integer start, Integer end) {
        List<String> hotWords = goodsSearchService.getHotWords(start, end);
        return Result.success(hotWords);
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

1.5 Service

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
1
2
3
4
package com.mszlu.shop.buyer.service.goods;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class GoodsSearchService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public List<String> getHotWords(Integer start, Integer end) {
        List<String> searchWords = new ArrayList<>();
        start = (start - 1) * end;
        end = start + end;
        Set<ZSetOperations.TypedTuple<String>> goodsHotWords = redisTemplate.opsForZSet().reverseRangeWithScores("goods_hot_words", start, end);
        if (goodsHotWords == null){
            return searchWords;
        }
        for (ZSetOperations.TypedTuple<String> goodsHotWord : goodsHotWords) {
            String value = goodsHotWord.getValue();
            searchWords.add(value);
        }
        return searchWords;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

1.6 测试

2. 常见问题

2.1 接口说明

接口url:/buyer/article

请求方式:GET

请求参数:

参数名称参数类型说明
pageNumberint开始
pageSizeint每页显示数量
typeString文章类型,ANNOUNCEMENT:通知,USER_AGREEMENT:用户协议,OTHER:其他,PRIVACY_POLICY:隐私,ABOUT:关于我们
sortstring排序

返回数据:

{
    "success":true,
    "message":"success",
    "code":2000000000,
    "result":
    {
        "records":
        [
            {"id":"1364722349428965376",
             "title":"促销计算规则",
             "content":null,
             "articleCategoryName":null,
             "sort":21,
             "openStatus":true
            },
            {"id":"1369922111602163712","title":"店铺与平台的结算规则","content":null,"articleCategoryName":null,"sort":3,"openStatus":true},{"id":"1369922025446965248","title":"一个秒杀活动能参与多次吗?","content":null,"articleCategoryName":null,"sort":2,"openStatus":true},{"id":"1369921919213633536","title":"一个商品可以设置几个满额活动?","content":null,"articleCategoryName":null,"sort":1,"openStatus":true},{"id":"1370177884043345920","title":"多人拼团有虚拟成团的功能吗?","content":null,"articleCategoryName":null,"sort":1,"openStatus":true}
        ]
        ,"total":5,
        "size":5,
        "current":1,
        "orders":[],
        "optimizeCountSql":true,
        "searchCount":true,
        "countId":null,
        "maxLimit":null,
        "pages":1}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

表结构:

见资料

package com.mszlu.shop.pojo;

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

import java.io.Serializable;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Article implements Serializable {

    private Long id;

    private Long categoryId;

    private String content;

    private Integer sort;

    private String title;

    private String type;

    private Boolean openStatus;

    private Boolean deleteFlag;

    private String createBy;

    private Date createTime;

    private Date updateTime;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

2.2 前端分析

carousel.vue:

  <div class="shop-msg">
          <div>
            <span>常见问题</span>
            <ul class="article-list">
              <li class="ellipsis" :alt="article.title" v-for="(article, index) in articleList" :key="index" @click="goArticle(article.id)">
                {{article.title}}
              </li>
            </ul>
          </div>
        </div>


getArticleList () { // 获取常见问题列表
      articleList(this.params).then(res => {
        if (res.success) {
          this.articleList = res.result.records
        }
      })
    },
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

2.3 Controller

package com.mszlu.shop.buyer.controller.article;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.shop.buyer.service.article.BuyerArticleService;
import com.mszlu.shop.common.vo.Result;
import com.mszlu.shop.model.buyer.params.ArticleSearchParams;
import com.mszlu.shop.model.buyer.vo.article.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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;

@RestController
@Api(tags = "买家端,文章接口")
@RequestMapping("/article")
public class ArticleBuyerController {

    @Autowired
    private BuyerArticleService buyerArticleService;

    @ApiOperation(value = "分页获取")
    @GetMapping
    public Result<Page<ArticleVO>> getByPage(ArticleSearchParams articleSearchParams) {
        return Result.success(buyerArticleService.articlePage(articleSearchParams));
    }


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.mszlu.shop.buyer.article.params;

import com.mszlu.shop.buyer.PageParams;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class ArticleSearchParams extends PageParams implements Serializable {

    @ApiModelProperty(value = "分类ID")
    private String categoryId;

    @ApiModelProperty(value = "标题")
    private String title;

    @ApiModelProperty(value = "分类类型")
    private String type;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mszlu.shop.buyer;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageParams implements Serializable {

    @ApiModelProperty(value = "页号")
    private Integer pageNumber = 1;

    @ApiModelProperty(value = "页面大小")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段")
    private String sort;

    @ApiModelProperty(value = "排序方式 asc/desc")
    private String order;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.mszlu.shop.buyer.article;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ArticleVO implements Serializable {

    @ApiModelProperty(value = "文章ID")
    private String id;

    @ApiModelProperty(value = "文章标题")
    private String title;

    @ApiModelProperty(value = "文章内容")
    private String content;

    @ApiModelProperty(value = "分类名称")
    private String articleCategoryName;

    @ApiModelProperty(value = "文章排序")
    private Integer sort;

    @ApiModelProperty(value = "状态, allowableValues = OPEN,CLOSE")
    private Boolean openStatus;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

2.4 Service

package com.mszlu.shop.buyer.service.article;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.shop.buyer.service.ArticleService;
import com.mszlu.shop.model.buyer.params.ArticleSearchParams;
import com.mszlu.shop.model.buyer.vo.article.ArticleVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuyerArticleService {

    @DubboReference(version = "1.0.0")
    private ArticleService articleService;

    public Page<ArticleVO> articlePage(ArticleSearchParams articleSearchParams) {

        return articleService.articlePage(articleSearchParams);
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

2.5 Dubbo服务

package com.mszlu.shop.buyer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.shop.buyer.mapper.ArticleMapper;
import com.mszlu.shop.buyer.service.ArticleService;
import com.mszlu.shop.model.buyer.params.ArticleSearchParams;
import com.mszlu.shop.model.buyer.pojo.Article;
import com.mszlu.shop.model.buyer.vo.article.ArticleVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@DubboService(version = "1.0.0",interfaceClass = ArticleService.class)
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Page<ArticleVO> articlePage(ArticleSearchParams articleSearchParams) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        String categoryId = articleSearchParams.getCategoryId();
        if (StringUtils.isNotBlank(categoryId)) {
            queryWrapper.eq(Article::getCategoryId, categoryId);
        }
        String title = articleSearchParams.getTitle();
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.eq(Article::getTitle, title);
        }
        String type = articleSearchParams.getType();
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.eq(Article::getType, type);
        }
        queryWrapper.orderByDesc(Article::getSort);

        queryWrapper.select(Article::getCategoryId,Article::getId,Article::getOpenStatus,Article::getTitle,Article::getSort);
        Page<Article> articlePage = new Page<>(articleSearchParams.getPageNumber(),articleSearchParams.getPageSize());
        Page<Article> articlePage1 = articleMapper.selectPage(articlePage, queryWrapper);

        Page<ArticleVO> articleVOPage = new Page<>();
        BeanUtils.copyProperties(articlePage1,articleVOPage);
        List<Article> records = articlePage1.getRecords();
        articleVOPage.setRecords(copyList(records));
        return articleVOPage;
    }



    public ArticleVO copy(Article article){
        if (article == null){
            return null;
        }
        ArticleVO articleVO = new ArticleVO();

        BeanUtils.copyProperties(article,articleVO);
        articleVO.setId(article.getId().toString());
        return articleVO;
    }
    public  List<ArticleVO> copyList(List<Article> articleList){
        List<ArticleVO> articleVOList = new ArrayList<>();

        for (Article article : articleList) {
            articleVOList.add(copy(article));
        }
        return articleVOList;
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.mszlu.shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mszlu.shop.pojo.Article;

public interface ArticleMapper extends BaseMapper<Article> {
}

1
2
3
4
5
6
7
8
package com.mszlu.shop.pojo;

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

import java.io.Serializable;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Article implements Serializable {

    private Long id;

    private Long categoryId;

    private String content;

    private Integer sort;

    private String title;

    private String type;

    private Boolean openStatus;

    private Boolean deleteFlag;

    private String createBy;

    private Date createTime;

    private Date updateTime;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

2.6 测试

3. 文章详情

3.1 接口说明

接口url:/buyer/article/get/{id}

请求方式:GET

请求参数:

参数名称参数类型说明
idlong文章id,路径参数

返回数据:

{
  "success": true,
  "message": "success",
  "code": 2000000000,
  "result": {
    "id": "1369921919213633536",
    "title": "一个商品可以设置几个满额活动?",
    "content": "<p><span style=\"font-size: 1em;\">一个商品同一时间段只能设置 1 个满额活动,不同时段可以设置不同的满额活动。</span><br></p>",
    "articleCategoryName": null,
    "sort": 1,
    "openStatus": true
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

3.2 前端分析

pages/article/index.vue:

  <transition mode="out-in">
              <div v-show="!showList">
                <a class="back-btn" @click="showList = true">&lt;返回上一级</a>
                <h2 class="mt_10 mb_10">{{detail.title}}</h2>
                <div class="mt_10 mb_10" v-html="detail.content"></div>
              </div>
 </transition>

async getDetail (id) { // 文章详情
      await articleDetail(id).then(res => {
        if (res.success) {
          this.detail = res.result
          this.showList = false
        }
      })
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

3.3 Controller

 	@ApiOperation(value = "通过id获取文章")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, paramType = "path")
    @GetMapping(value = "/get/{id}")
    public Result<ArticleVO> get(@PathVariable("id") Long id) {
        return Result.success(buyerArticleService.customGet(id));
    }
1
2
3
4
5
6

3.4 Service

 public ArticleVO customGet(Long id) {
        return articleService.findArticleById(id);
    }
1
2
3

3.5 Dubbo服务

  @Override
    public ArticleVO findArticleById(Long id) {
        Article article = this.articleMapper.selectById(id);
        return copy(article);
    }
1
2
3
4
5

3.6 测试

4. 文章分类列表

4.1 接口说明

接口url:/buyer/article/articleCategory/list

请求方式:GET

请求参数:

返回数据:

{
  "success": true,
  "message": "success",
  "code": 2000000000,
  "result": [
    {
      "id": "1347433752083324928",
      "level": 0,
      "parentId": "0",
      "sort": 1,
      "articleCategoryName": "商家中心常见问题",
      "type": "OTHER",
      "children": [
        {
          "id": "1347456734864367616",
          "level": 1,
          "parentId": "1347433752083324928",
          "sort": 1,
          "articleCategoryName": "店铺开店",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347450302295203840",
          "level": 1,
          "parentId": "1347433752083324928",
          "sort": 2,
          "articleCategoryName": "商品管理",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347450353352466432",
          "level": 1,
          "parentId": "1347433752083324928",
          "sort": 3,
          "articleCategoryName": "订单管理",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347450411015757824",
          "level": 1,
          "parentId": "1347433752083324928",
          "sort": 4,
          "articleCategoryName": "营销管理",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347458431707799552",
          "level": 1,
          "parentId": "1347433752083324928",
          "sort": 5,
          "articleCategoryName": "设置",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347469221429010432",
          "level": 1,
          "parentId": "1347433752083324928",
          "sort": 6,
          "articleCategoryName": "财务管理",
          "type": "OTHER",
          "children": []
        }
      ]
    },
    {
      "id": "1347730672995557376",
      "level": 0,
      "parentId": "0",
      "sort": 2,
      "articleCategoryName": "平台管理常见问题",
      "type": "OTHER",
      "children": [
        {
          "id": "1347730780659146752",
          "level": 1,
          "parentId": "1347730672995557376",
          "sort": 1,
          "articleCategoryName": "订单",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347730985055969280",
          "level": 1,
          "parentId": "1347730672995557376",
          "sort": 2,
          "articleCategoryName": "商品",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347732008847826944",
          "level": 1,
          "parentId": "1347730672995557376",
          "sort": 3,
          "articleCategoryName": "营销",
          "type": "OTHER",
          "children": []
        },
        {
          "id": "1347732307880730624",
          "level": 1,
          "parentId": "1347730672995557376",
          "sort": 4,
          "articleCategoryName": "财务",
          "type": "OTHER",
          "children": []
        }
      ]
    },
    {
      "id": "1369921726825103362",
      "level": 0,
      "parentId": "0",
      "sort": 2,
      "articleCategoryName": "商家公告",
      "type": "STORE_ARTICLE",
      "children": []
    },
    {
      "id": "1390968079827075072",
      "level": 0,
      "parentId": "0",
      "sort": 1,
      "articleCategoryName": "关于我们",
      "type": "ABOUT",
      "children": [
        {
          "id": "1369921726825103360",
          "level": 1,
          "parentId": "1390968079827075072",
          "sort": 3,
          "articleCategoryName": "平台公告",
          "type": "ANNOUNCEMENT",
          "children": []
        },
        {
          "id": "1371779742369316864",
          "level": 1,
          "parentId": "1390968079827075072",
          "sort": 4,
          "articleCategoryName": "平台信息",
          "type": "PLATFORM_INFORMATION",
          "children": []
        }
      ]
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

4.2 表结构

见资料

package com.mszlu.shop.pojo;

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

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ArticleCategory {

    private Long id;

    private Integer level;

    private Long parentId;

    private Integer sort;

    private String articleCategoryName;

    private String type;

    private Boolean deleteFlag;

    private Date createTime;

    private Date updateTime;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

4.3 前端分析

在 pages/article/index.vue:

 <Sider class="side-bar" ref="side" :collapsed-width="78">
          <div class="article-cate">文章分类列表</div>
          <Menu
            class="side-menu"
            theme="light"
            width="auto"
            ref='menu'
            :active-name="activeName"
            :open-names="openName"
            @on-select="onSelect"
          >
            <!--   循环导航栏       -->
            <Submenu
              v-show="menu.children"
              v-for="(menu, index) in list"
              :key="index"
              :name="menu.articleCategoryName"
            >
              <template slot="title">
                <span>{{ menu.articleCategoryName }}</span>
              </template>
              <MenuItem
                v-for="(chlidren, i) in menu.children"
                :key="i"
                :name="chlidren.id"
                >{{ chlidren.articleCategoryName }}</MenuItem
              >
            </Submenu>

          </Menu>
        </Sider>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

有子分类,是一个树结构,实现方案还是 先把所有的文章分类查出来,代码中进行递归操作,组成树结构

4.4 Controller

	@ApiOperation(value = "获取文章分类列表")
    @GetMapping(value = "/articleCategory/list")
    public Result<List<ArticleCategoryVO>> getArticleCategoryList() {
        return Result.success(buyerArticleService.allChildren());
    }
1
2
3
4
5
package com.mszlu.shop.buyer.article;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ArticleCategoryVO implements Serializable {

    private String id;

    private Integer level;

    private String parentId;

    private Integer sort;

    private String articleCategoryName;

    private String type;

    @ApiModelProperty(value = "子菜单")
    private List<ArticleCategoryVO> children = new ArrayList<>();

    public List<ArticleCategoryVO> getChildren() {
        if (children != null) {
            children.sort(new Comparator<ArticleCategoryVO>() {
                @Override
                public int compare(ArticleCategoryVO o1, ArticleCategoryVO o2) {
                    return o1.getSort().compareTo(o2.getSort());
                }
            });
            return children;
        }
        return null;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

4.5 Service

public List<ArticleCategoryVO> allChildren() {
        return articleService.findAllArticleCategory();
    }
1
2
3

4.6 Dubbo服务

@Override
    public List<ArticleCategoryVO> findAllArticleCategory() {
        LambdaQueryWrapper<ArticleCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleCategory::getDeleteFlag,false);
        List<ArticleCategory> articleCategories = articleCategoryMapper.selectList(queryWrapper);
        List<ArticleCategoryVO> articleCategoryVOs = copyCategoryList(articleCategories);
        List<ArticleCategoryVO> articleCategoryVOList = new ArrayList<>();
        for (ArticleCategoryVO articleCategoryVO : articleCategoryVOs) {
            if (articleCategoryVO.getLevel() == 0){
                addCategoryChild(articleCategoryVO,articleCategoryVOs);
                articleCategoryVOList.add(articleCategoryVO);
            }
        }
        return articleCategoryVOList;
    }

    private void addCategoryChild(ArticleCategoryVO articleCategoryVO, List<ArticleCategoryVO> articleCategoryVOs) {
        List<ArticleCategoryVO> articleCategoryVOList = new ArrayList<>();
        for (ArticleCategoryVO categoryVO : articleCategoryVOs) {
            if (categoryVO.getParentId().equals(articleCategoryVO.getId())){
                addCategoryChild(categoryVO,articleCategoryVOs);
                articleCategoryVOList.add(categoryVO);
            }
        }
        articleCategoryVO.setChildren(articleCategoryVOList);
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public ArticleCategoryVO copyCategory(ArticleCategory article){
    if (article == null){
        return null;
    }
    ArticleCategoryVO articleVO = new ArticleCategoryVO();

    BeanUtils.copyProperties(article,articleVO);
    articleVO.setId(article.getId().toString());
    articleVO.setParentId(article.getParentId().toString());
    return articleVO;
}

public  List<ArticleCategoryVO> copyCategoryList(List<ArticleCategory> articleList){
    List<ArticleCategoryVO> articleVOList = new ArrayList<>();

    for (ArticleCategory article : articleList) {
        articleVOList.add(copyCategory(article));
    }
    return articleVOList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

4.7 测试