1. 平台基本信息 26


准备工具
安装一个mybatis的插件
将这个插件直接拖到idea中,会弹出一个安装的提示框,统一安装即可


出现绿色箭头即代表成功

1.1 业务接口
操作micr-api模块 提供业务接口
1.1.1 定义平台详细信息service接口
接口PlatBaseInfoService
package com.bjpowernode.api.service;
import com.bjpowernode.api.pojo.BaseInfo;
//平台详细信息接口   26
public interface PlatBaseInfoService {
    // 计算利率, 注册人数, 累计成交金额    26
    BaseInfo queryPlatBaseInfo();
}1.1.2平台详细信息实体类 26
BaseInfo
package com.bjpowernode.api.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
/**
 * 平台详细信息实体类  26
 */
public class BaseInfo implements Serializable {
    /*收益率平均值*/
    private BigDecimal historyAvgRate;
    /*累计成交金额*/
    private BigDecimal sumBidMoney;
    /*注册人数*/
    private Integer registerUsers;
    public BaseInfo() {
    }
    public BaseInfo(BigDecimal historyAvgRate, BigDecimal sumBidMoney, Integer registerUsers) {
        this.historyAvgRate = historyAvgRate;
        this.sumBidMoney = sumBidMoney;
        this.registerUsers = registerUsers;
    }
    public BigDecimal getHistoryAvgRate() {
        return historyAvgRate;
    }
    public void setHistoryAvgRate(BigDecimal historyAvgRate) {
        this.historyAvgRate = historyAvgRate;
    }
    public BigDecimal getSumBidMoney() {
        return sumBidMoney;
    }
    public void setSumBidMoney(BigDecimal sumBidMoney) {
        this.sumBidMoney = sumBidMoney;
    }
    public Integer getRegisterUsers() {
        return registerUsers;
    }
    public void setRegisterUsers(Integer registerUsers) {
        this.registerUsers = registerUsers;
    }
}1.2 业务接口实现类 26
操作micr-dataservice模块充当提供者
1.2.1 平台详细信息service接口实现类
接口实现类PlatBaseInfoServiceImpl
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.pojo.BaseInfo;
import com.bjpowernode.api.service.PlatBaseInfoService;
import com.bjpowernode.dataservice.mapper.BidInfoMapper;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import com.bjpowernode.dataservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
import java.math.BigDecimal;
/**
 * 平台详细信息service接口实现类   26
 */
@DubboService(interfaceClass = PlatBaseInfoService.class,version = "1.0")
public class PlatBaseInfoServiceImpl implements PlatBaseInfoService {
    //注入Mapper   26
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;
    @Resource
    private BidInfoMapper bidInfoMapper;
    /*平台基本信息*/
    @Override
    public BaseInfo queryPlatBaseInfo() {
        //获取注册人数, 收益率平均值, 累计成交金额
        int registerUser = userMapper.selectCountUser();
        //收益率平均值   27
        BigDecimal avgRate = productInfoMapper.selectAvgRate();
        //累计成交金额   28
        BigDecimal sumBidMoney = bidInfoMapper.selectSumBidMoney();
        BaseInfo baseInfo = new BaseInfo(avgRate,sumBidMoney,registerUser);
        return baseInfo;
    }
}1.2.2 定义mapper中的接口方法
UserMapper 26

//统计注册人数   26
int selectCountUser();ProductInfoMapper
//利率平均值  27
    BigDecimal selectAvgRate();BidInfoMapper 28
//累计成交金额  28
    BigDecimal selectSumBidMoney();1.2.3 编写sql 27
利用我们之前安装的插件

UserMapper.xml
<!--  注册的用户数   27-->
  <select id="selectCountUser" resultType="java.lang.Integer">
    select count(id) as ct from u_user
  </select>ProductInfoMapper.xml
<!--    利率平均值  27-->
  <select id="selectAvgRate" resultType="java.math.BigDecimal">
        select round(avg(rate),2) as avgRate  from b_product_info
    </select>BidInfoMapper.xml
<!--  累计成交金额  28-->
  <select id="selectSumBidMoney" resultType="java.math.BigDecimal">
    select  sum(bid_money) ad sumBidMoney from b_bid_info
  </select>1.3 控制层消费者controller 28
操作micr-web模块 充当消费者
1.3.1 BaseController 公共controller
BaseController 公共controller,定义这个类就是为了方便,定义一些常量啊,引用啊啥的
package com.bjpowernode.front.controller;
import com.bjpowernode.api.service.PlatBaseInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
/**
 * 公共controller   28
 */
public class BaseController {
    //声明公共的方法,属性的等
    @DubboReference(interfaceClass = PlatBaseInfoService.class,version = "1.0")
    protected PlatBaseInfoService platBaseInfoService;
}同一的应答结果类RespResult
package com.bjpowernode.front.view;
/**
 * 同一的应答结果。 controller方法的返回值都是它   28
 */
public class RespResult {
    //应答码,自定义的数字
    private int code;
    //code的文字说明,一般做提示给用户看
    private String msg;
    //单个数据
    private Object data;
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
}1.3.2 消费者PlatInfoController
package com.bjpowernode.front.controller;
import com.bjpowernode.api.pojo.BaseInfo;
import com.bjpowernode.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//平台基本信息   28
@Api(tags = "平台信息功能")
@RestController
@RequestMapping("/v1")
public class PlatInfoController extends BaseController {
    /*平台基本信息*/
    @ApiOperation(value = "平台三项基本信息",notes = "注册人数,平均的利率,总投资金额")
    @GetMapping("/plat/info")
    public RespResult queryPlatBaseInfo(){
        //调用远程服务,展示平台基本信息
        BaseInfo baseInfo = platBaseInfoService.queryPlatBaseInfo();
        RespResult result = new RespResult();
        result.setCode(1000); //表示成功
        result.setMsg("查询平台信息成功");
        result.setData(baseInfo);
        return result;
    }
}1.4 测试 28
启动提供者

启动消费者

浏览器输入http://localhost:8000/api/v1/plat/info
成功没问题

看看接口文档 29
操作micr-web模块
package com.bjpowernode.front.setttings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
//配置接口文档 的头 11
@Configuration
public class SwaggerConfigruationSettings {
    //创建Docket对象
    @Bean
    public Docket docket(){
        //1创建Docket对象
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //2创建APi信息,接口文档的总体描述
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("一个金融项目")//标题
                .version("1.0")//版本
                .description("前后端分离的项目,前端Vue,后端Spring Boot+Dubbo分布式项目")//描述
                .build();
        //3.设置使用ApiInfo
        docket  = docket.apiInfo(apiInfo);
        //4. 设置哪些controller参与生成  12
        docket = docket.select()
                .apis(RequestHandlerSelectors.basePackage("com.bjpowernode.front.controller")).build();
        return docket;
    }
}启动类

浏览器输入http://localhost:8000/api/doc.html


2. 首页展示各类产品信息 30
按类型分页查询各类产品

2.1 业务接口30
操作micr-api模块 提供业务接口
2.1.1 接口ProductService
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import java.util.List;
/**
 * 分页查询产品接口   30
 */
public interface ProductService {
       /*首页的多个产品数据   31*/
    MultiProduct queryIndexPageProducts();
}2.1.2 分页查询实体类 31
MultiProduct
package com.bjpowernode.api.pojo;
import com.bjpowernode.api.model.ProductInfo;
import java.io.Serializable;
import java.util.List;
/**
 * 分页查询 多个产品数据 实体类  31
 */
public class MultiProduct implements Serializable {
    private List<ProductInfo> xinShouBao;
    private List<ProductInfo> youXuan;
    private List<ProductInfo> sanBiao;
    public List<ProductInfo> getXinShouBao() {
        return xinShouBao;
    }
    public void setXinShouBao(List<ProductInfo> xinShouBao) {
        this.xinShouBao = xinShouBao;
    }
    public List<ProductInfo> getYouXuan() {
        return youXuan;
    }
    public void setYouXuan(List<ProductInfo> youXuan) {
        this.youXuan = youXuan;
    }
    public List<ProductInfo> getSanBiao() {
        return sanBiao;
    }
    public void setSanBiao(List<ProductInfo> sanBiao) {
        this.sanBiao = sanBiao;
    }
}接下来给model中的每个实体类都序列化

2.2 业务接口实现类 30
操作micr-dataservice模块充当提供者
2.2.1 新建一个micr-common模块


在micr-dataservice中引入micr-common坐标

工具类 CommonUtil
package com.bjpowernode.common.util;
/**
 * 工具类  30
 */
public class CommonUtil {
    //以下两个是处理分页判断的工具类   30
    /*处理pageNo*/
    public static int defaultPageNo(Integer pageNo){
        int pNo = pageNo;
        if( pageNo == null || pageNo < 1 ){
            pNo = 1;
        }
        return pNo;
    }
    /*处理pageSize*/
    public static int defaultPageSize(Integer pageSize){
        int pSize = pageSize;
        if( pageSize == null || pageSize < 1 ){
            pSize = 1;
        }
        return pSize;
    }
}常量类YLBConstant
package com.bjpowernode.common.constants;
/**
 *
 * 常量类   31
 */
public class YLBConstant {
    /*****产品类型*********/
    //新手宝
    public static final  int PRODUCT_TYPE_XINSHOUBAO =  0;
    //优选
    public static final  int PRODUCT_TYPE_YOUXUAN = 1;
    //散标
    public static final  int PRODUCT_TYPE_SANBIAO = 2;
}2.2.2 接口实现类ProductServiceImpl
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.api.service.ProductService;
import com.bjpowernode.common.constants.YLBConstant;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * 分页展示产品信息实现类  30
 */
@DubboService(interfaceClass = ProductService.class,version = "1.0")
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductInfoMapper productInfoMapper;
    /*首页的多个产品数据分页查询   31*/
    @Override
    public MultiProduct queryIndexPageProducts() {
        MultiProduct result = new MultiProduct();
        //查询新手宝
        List<ProductInfo> xinShouBaoList  = productInfoMapper.selectByTypeLimit(
                                      YLBConstant.PRODUCT_TYPE_XINSHOUBAO,0,1);
        //查询优选
        List<ProductInfo> youXuanList = productInfoMapper.selectByTypeLimit(
                                      YLBConstant.PRODUCT_TYPE_YOUXUAN,0,3 );
        //散标
        List<ProductInfo> sanBiaoList = productInfoMapper.selectByTypeLimit(
                                      YLBConstant.PRODUCT_TYPE_SANBIAO,0,3 );
        result.setXinShouBao(xinShouBaoList);
        result.setYouXuan(youXuanList);
        result.setSanBiao(sanBiaoList);
        return result;
    }
}2.2.3 定义mapper中的接口方法 30
ProductInfoMapper 30
/*按产品类型分页查询   30*/
    List<ProductInfo> selectByTypeLimit(@Param("ptype") Integer ptype,
                                        @Param("offset") Integer offset,
                                        @Param("rows") Integer rows);2.2.4 编写sql 30
ProductInfoMapper.xml
<!--按产品类型分页查询   30-->
  <select id="selectByTypeLimit" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
    from b_product_info
    where product_type = #{ptype}
    order by release_time desc
    limit #{offset},#{rows}
  </select>2.3 控制层消费者controller 32
操作micr-web模块 充当消费者

2.3.1 公共BaseController 32

//产品服务   32
    @DubboReference(interfaceClass = ProductService.class,version = "1.0")
    protected ProductService productService;2.3.2 改造响应结果类RespResult 33
操作micr-common模块
枚举类RCode
package com.bjpowernode.common.enums;
/**
 * 枚举 定义相应结果码   33
 */
public enum RCode {
    UNKOWN(0,"请稍候重试"),
    SUCC(1000,"请求成功"),
    ;
    private int code;
    private String text;
    RCode(int c, String t){
        this.code = c;
        this.text = t;
    }
    /**应答码
     * 0:默认
     * 1000-2000是请求参数有误,逻辑的问题
     * 2000-3000是服务器请求错误。
     * 3000-4000是访问dubbo的应答结果
     */
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}

响应结果类RespResult 33
package com.bjpowernode.front.view;
import com.bjpowernode.common.enums.RCode;
import com.sun.javaws.jnl.RContentDesc;
import java.sql.ResultSet;
import java.util.List;
/**
 * 同一的应答结果。 controller方法的返回值都是它   28
 */
public class RespResult {
    //应答码,自定义的数字
    private int code;
    //code的文字说明,一般做提示给用户看
    private String msg;
    //单个数据
    private Object data;
    //表示成功的RespResult对象  33
    public static RespResult ok(){
        RespResult result = new RespResult();
        result.setRCode(RCode.SUCC);
        return result;
    }
    //表示失败的RespResult对象   33
    public static RespResult fail(){
        RespResult result = new RespResult();
        result.setRCode(RCode.UNKOWN);
        return result;
    }
    public void setRCode(RCode rcode){
        this.code = rcode.getCode();
        this.msg = rcode.getText();
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
}2.3.3 消费者ProductController 32
package com.bjpowernode.front.controller;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * 产品的分页查询  32
 */
@Api(tags = "理财产品功能")
@RestController
@RequestMapping("/v1")
public class ProductController extends BaseController {
    //首页三类产品列表  32
    @ApiOperation(value = "首页三类产品列表",notes = "一个新手宝,三个优选,三个散标产品")
    @GetMapping("/product/index")
    public RespResult queryProductIndex(){
        RespResult result = RespResult.ok();
        MultiProduct multiProduct = productService.queryIndexPageProducts();
        result.setData(multiProduct);
        return result;
    }
}2.4 测试 32
使用postman测试
启动提供者

启动消费者

输入http://localhost:8000/api/v1/product/index
成功

3. 展示查看更多产品页面 34
在首页我们点击查看更多产品时

跳转此页面

3.1 产品列表---分页查询一类产品 34


3.1.1 业务接口 30、34
操作micr-api模块 提供业务接口
接口ProductService
/*根据产品类型,单独查询产品,支持分页   30*/
    List<ProductInfo> queryByTypeLimit(Integer pType,Integer pageNo,Integer pageSize);
    /*某个产品类型的记录总数   34*/
    Integer queryRecordNumsByType(Integer pType);3.1.2 业务接口实现类 30、34
操作micr-dataservice模块充当提供者
接口实现类ProductServiceImpl
/*按类型单独分页查询产品   30*/
    @Override
    public List<ProductInfo> queryByTypeLimit(Integer pType, Integer pageNo, Integer pageSize) {
        List<ProductInfo> productInfos = new ArrayList<>();
        if( pType == 0 || pType == 1 || pType == 2){
            //这里对分页数据做了一个判断
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            int offset  = (pageNo - 1) * pageSize;
            productInfos = productInfoMapper.selectByTypeLimit(pType, offset, pageSize);
        }
        return productInfos;
    }定义接口mapper中的方法 34
ProductInfoMapper
//某个产品的记录总数   34
    Integer selectCountByType(@Param("ptype") Integer pType);编写sql 34
ProductInfoMapper.xml
<!--  某个产品的记录总数   34-->
  <select id="selectCountByType" resultType="java.lang.Integer">
    select count(id) ad nums from b_product_info where product_type = #{ptype}
  </select>3.1.3控制层消费者controller 34
创建一个分页数据类 35
PageInfo
package com.bjpowernode.front.view;
/**
 * 分页数据类   35
 */
public class PageInfo {
    //页号
    private Integer pageNo;
    //每页大小
    private Integer pageSize;
    //总页数
    private Integer totalPage;
    //总记录数
    private Integer totalRecord;
    public PageInfo() {
    }
    public PageInfo(Integer pageNo, Integer pageSize, Integer totalRecord) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalRecord = totalRecord;
        //计算总页数   36
        if( this.totalRecord % this.pageSize  == 0 ){
            this.totalPage = this.totalRecord / this.pageSize;
        } else {
            this.totalPage = this.totalRecord / this.pageSize + 1;
        }
    }
    public Integer getPageNo() {
        return pageNo;
    }
    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Integer getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }
    public Integer getTotalRecord() {
        return totalRecord;
    }
    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
    }
}修改应答结果类RespResult


package com.bjpowernode.front.view;
import com.bjpowernode.common.enums.RCode;
import com.sun.javaws.jnl.RContentDesc;
import java.sql.ResultSet;
import java.util.List;
/**
 * 同一的应答结果。 controller方法的返回值都是它   28
 */
public class RespResult {
    //应答码,自定义的数字
    private int code;
    //code的文字说明,一般做提示给用户看
    private String msg;
    //单个数据
    private Object data;
    //集合数据   35
    private List list;
    //分页   36
    private PageInfo page;
    //表示成功的RespResult对象  33
    public static RespResult ok(){
        RespResult result = new RespResult();
        result.setRCode(RCode.SUCC);
        return result;
    }
    //表示失败的RespResult对象  33
    public static RespResult fail(){
        RespResult result = new RespResult();
        result.setRCode(RCode.UNKOWN);
        return result;
    }
    public void setRCode(RCode rcode){
        this.code = rcode.getCode();
        this.msg = rcode.getText();
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public PageInfo getPage() {
        return page;
    }
    public void setPage(PageInfo page) {
        this.page = page;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
}在micr-common模块RCode添加枚举

消费者 ProductController 34-35
/*按产品类型分页查询   34-35*/
    @GetMapping("/product/list")
    public RespResult queryProductByType(@RequestParam("ptype") Integer pType,
                                         //required = false,defaultValue = "1"代表参数没有时就默认为1
                                         @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                         @RequestParam(value = "pageSize",required = false,defaultValue = "9") Integer pageSize){
        RespResult result = RespResult.fail();
        if(pType != null && (pType == 0 || pType == 1 || pType == 2)){
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            //记录总数
            Integer recordNums = productService.queryRecordNumsByType(pType);
            if( recordNums > 0 ){
                //产品集合 ,分页查询到数据
                List<ProductInfo> productInfos = productService.queryByTypeLimit(pType,pageNo,pageSize);
                //构建PageInfo
                PageInfo page = new PageInfo(pageNo,pageSize,recordNums);
                result = RespResult.ok();
                result.setList(productInfos);
                result.setPage(page);
            }
        } else {
            //请求参数有误   36
            result.setRCode(RCode.REQUEST_PRODUCT_TYPE_ERR);
        }
        return result;
    }3.1.4 测试 38
启动提供者

启动消费者

postman输入 http://localhost:8000/api/v1/product/list
测试成功

3.2 投资排行榜 39


3.2.1 准备redis
我放在了E:\java\tools\Redis\Redis-x64-3.2.100
启动redis

添加一些数据 add new key


查看

在添加一个


3.2.2 添加依赖 和配置redis 39
操作micr-web模块

pom.xml
<!--		redis起步依赖  39-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>配置redis

application.yml
redis: #配置redis   39
    host: localhost
    port: 63793.2.3 公共类BaseController

BaseController
//声明redis方法    39
    @Resource
    protected StringRedisTemplate stringRedisTemplate;3.2.4 投资排行榜常量类RedisKey 39
在micr-common模块中
package com.bjpowernode.common.constants;
/**
 * redis  投资排行榜常量类  39
 */
public class RedisKey {
    /*投资排行榜*/
    public static  final String KEY_INVEST_RANK = "INVEST:RANK";
}3.2.5 存储投资排行榜的数据类RankView 40
package com.bjpowernode.front.view.invest;
/**
 * 存储投资排行榜的数据实体类  40
 */
public class RankView {
    private String phone;
    private Double money;
    public RankView(String phone, Double money) {
        this.phone = phone;
        this.money = money;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public Double getMoney() {
        return money;
    }
    public void setMoney(Double money) {
        this.money = money;
    }
}3.2.6 处理手机号脱敏 40
操作mict-common模块
CommonUtil
//处理手机号脱敏  40
    public static String tuoMinPhone(String phone){
        String result = "***********";
        if (phone != null && phone.trim().length() == 11) {
            result = phone.substring(0,3) + "******" + phone.substring(9,11);
        }
        return result;
    }3.2.7 消费者InvestController 39-40
package com.bjpowernode.front.controller;
import com.bjpowernode.common.constants.RedisKey;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.front.view.RespResult;
import com.bjpowernode.front.view.invest.RankView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
 * 有关投资功能     39
 */
@Api(tags = "投资理财产品")
@RestController
public class InvestController extends BaseController {
    /*投资排行榜   39*/
    @ApiOperation(value = "投资排行榜",notes = "显式投资金额最高的3位用户信息")
    @GetMapping("/v1/invest/rank")
    public RespResult showInvestRank(){
        //从redis查询数据
        //reverseRangeWithScores(0, 2) 可以反着取数据0,1,2 三个数据,因为redis中数据时从小到大排的,我们需要从大到小排
        Set<ZSetOperations.TypedTuple<String>> sets = stringRedisTemplate
                .boundZSetOps(RedisKey.KEY_INVEST_RANK).reverseRangeWithScores(0, 2);
        List<RankView> rankList = new ArrayList<>();
        //遍历set集合   40
        sets.forEach( tuple ->{
            //tuple.getValue();//手机号
            //tuple.getScore();//投资金额
            rankList.add(new RankView(CommonUtil.tuoMinPhone(tuple.getValue()),tuple.getScore()));
        });
        RespResult  result = RespResult.ok();
        result.setList(rankList);
        return result;
    }
}测试 40
启动程序
浏览器输入http://localhost:8000/api/v1/invest/rank











