总览:
1、service处理(根据产品id ,查询产品信息)
在api模块下service包,ProductService接口添加新方法(根据产品id ,查询产品信息queryById(Integer id)):
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.MultiProduct;
import java.util.List;
/**
* 产品接口
*/
public interface ProductService {
/*根据产品类型,查询产品,支持分页*/
List<ProductInfo> queryByTypeLimit(Integer pType,Integer pageNo,Integer pageSize);
/*某个产品类型的记录总数*/
Integer queryRecordNumsByType(Integer pType);
/*首页的多个产品数据*/
MultiProduct queryIndexPageProducts();
/** 根据产品id ,查询产品信息 */
ProductInfo queryById(Integer id);
}
2、serviceImpl处理(根据产品id ,查询产品信息)
在dataservice模块service包,实现ProductService接口,ProductServiceImpl添加实现方法(queryById(Integer id)):
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;
@DubboService(interfaceClass = ProductService.class,version = "1.0")
public class ProductServiceImpl implements ProductService {
@Resource
private ProductInfoMapper productInfoMapper;
/*按类型分页查询产品*/
@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;
}
/*某个产品类型的记录总数*/
@Override
public Integer queryRecordNumsByType(Integer pType) {
Integer counts = 0;
if( pType == 0 || pType == 1 || pType == 2){
counts = productInfoMapper.selectCountByType(pType);
}
return counts;
}
/*首页的多个产品数据*/
@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;
}
/** 根据产品id ,查询产品信息 */
@Override
public ProductInfo queryById(Integer id) {
ProductInfo productInfo = null;
if( id != null && id > 0 ){
productInfo = productInfoMapper.selectByPrimaryKey(id);
}
return productInfo;
}
}
3、pojo类处理
在api模块下pojo包,创建一个产品金额信息BidInfoProduct类:
package com.bjpowernode.api.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 产品金额信息
*/
public class BidInfoProduct implements Serializable {
private Integer id;
private String phone;
private String bidTime;
private BigDecimal bidMoney;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getBidTime() {
return bidTime;
}
public void setBidTime(String bidTime) {
this.bidTime = bidTime;
}
public BigDecimal getBidMoney() {
return bidMoney;
}
public void setBidMoney(BigDecimal bidMoney) {
this.bidMoney = bidMoney;
}
}
4、service处理(查询某个产品的投资记录)
在api模块下service包,创建一个InvestService接口:(查询某个产品的投资记录queryBidListByProductId(Integer productId, Integer pageNo, Integer pageSize);)
package com.bjpowernode.api.service;
import com.bjpowernode.api.pojo.BidInfoProduct;
import java.math.BigDecimal;
import java.util.List;
public interface InvestService {
/** 查询某个产品的投资记录 */
List<BidInfoProduct> queryBidListByProductId(Integer productId,
Integer pageNo,
Integer pageSize);
/** 投资理财产品, int 是投资的结果 , 1 投资成功*/
int investProduct(Integer uid, Integer productId, BigDecimal money);
}
5、serviceImpl处理(查询某个产品的投资记录)
在dataservice模块service包,实现InvestService接口,创建InvestServiceImpl实现类:(查询某个产品的投资记录queryBidListByProductId(Integer productId, Integer pageNo, Integer pageSize);)
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.BidInfo;
import com.bjpowernode.api.model.FinanceAccount;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.BidInfoProduct;
import com.bjpowernode.api.service.InvestService;
import com.bjpowernode.common.constants.YLBConstant;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.BidInfoMapper;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.ProductInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@DubboService(interfaceClass = InvestService.class, version = "1.0")
public class InvestServiceImpl implements InvestService {
@Resource
private BidInfoMapper bidInfoMapper;
@Resource
private FinanceAccountMapper accountMapper;
@Resource
private ProductInfoMapper productInfoMapper;
/**
* 查询某个产品的投资记录
*/
@Override
public List<BidInfoProduct> queryBidListByProductId(Integer productId,
Integer pageNo,
Integer pageSize) {
List<BidInfoProduct> bidList = new ArrayList<>();
if (productId != null && productId > 0) {
pageNo = CommonUtil.defaultPageNo(pageNo);
pageSize = CommonUtil.defaultPageSize(pageSize);
int offset = (pageNo - 1) * pageSize;
bidList = bidInfoMapper.selectByProductId(productId, offset, pageSize);
}
return bidList;
}
/*投资理财产品, int 是投资的结果 , 1 投资成功*/
@Transactional(rollbackFor = Exception.class)
@Override
public int investProduct(Integer uid, Integer productId, BigDecimal money) {
int result = 0;//默认,参数不正确
int rows = 0;
//1参数检查
if ((uid != null && uid > 0) && (productId != null && productId > 0)
&& (money != null && money.intValue() % 100 == 0 && money.intValue() >= 100)) {
//2.查询账号金额
FinanceAccount account = accountMapper.selectByUidForUpdate(uid);
if (account != null) {
if (CommonUtil.ge(account.getAvailableMoney(), money)) {
//资金满足购买要求
//3.检查产品是否可以购买
ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(productId);
if (productInfo != null
&& productInfo.getProductStatus() == YLBConstant.PRODUCT_STATUS_SELLING) {
if (CommonUtil.ge(productInfo.getLeftProductMoney(), money) &&
CommonUtil.ge(money, productInfo.getBidMinLimit()) &&
CommonUtil.ge(productInfo.getBidMaxLimit(), money)) {
//可以购买了 4. 扣除账号资金
rows = accountMapper.updateAvailableMoneyByInvest(uid, money);
if (rows < 1) {
throw new RuntimeException("投资更新账号资金失败");
}
//5.扣除产品剩余可投资金额
rows = productInfoMapper.updateLeftProductMoney(productId, money);
if (rows < 1) {
throw new RuntimeException("投资更新产品剩余金额失败");
}
//6.创建投资记录
BidInfo bidInfo = new BidInfo();
bidInfo.setBidMoney(money);
bidInfo.setBidStatus(YLBConstant.INVEST_STATUS_SUCC);
bidInfo.setBidTime(new Date());
bidInfo.setProdId(productId);
bidInfo.setUid(uid);
bidInfoMapper.insertSelective(bidInfo);
//7.判断产品是否卖完,更新产品是满标状态
ProductInfo dbProductInfo = productInfoMapper.selectByPrimaryKey(productId);
if( dbProductInfo.getLeftProductMoney().compareTo(new BigDecimal("0")) == 0 ){
rows = productInfoMapper.updateSelled(productId);
if( rows < 1 ){
throw new RuntimeException("投资更新产品满标失败");
}
}
//8.最后这是投资成功
result = 1;
}
} else {
result = 4;//理财产品不存在
}
} else {
result = 3;//资金不足
}
} else {
result = 2;//资金账号不存在
}
}
return result;
}
}
其中:
1、某个产品的投资记录:(需要在dataservice模块mapper包下的BidInfoMapper接口添加方法,并在resources/mappers/BidInfoMapper.xml编写SQL语句):
/*某个产品的投资记录*/
List<BidInfoProduct> selectByProductId(@Param("productId") Integer productId,
@Param("offset") int offset,
@Param("rows") Integer rows);
<!--某个产品的投资记录-->
<select id="selectByProductId" resultType="com.bjpowernode.api.pojo.BidInfoProduct">
SELECT
bid.id,
CONCAT( SUBSTRING( u.phone FROM 1 FOR 3 ), "******", SUBSTRING( u.phone FROM 10 FOR 2 ) ) AS phone,
DATE_FORMAT( bid.bid_time, "%Y-%m-%d %H:%i:%s" ) AS bidTime,
bid.bid_money AS bidMoney
FROM
b_bid_info bid
JOIN u_user u ON bid.uid = u.id
WHERE
bid.prod_id = #{productId}
ORDER BY
bid.bid_time DESC
LIMIT #{offset},#{rows}
</select>
6、controller处理
在web模块下controller包,公用类BaseController添加对象(投资服务):
package com.bjpowernode.front.controller;
import com.bjpowernode.api.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
/**
* 公用controller类
*/
public class BaseController {
//声明公共的方法,属性的等
@Resource
protected StringRedisTemplate stringRedisTemplate;
//平台信息服务
@DubboReference(interfaceClass = PlatBaseInfoService.class,version = "1.0")
protected PlatBaseInfoService platBaseInfoService;
//产品服务
@DubboReference(interfaceClass = ProductService.class,version = "1.0")
protected ProductService productService;
//投资服务
@DubboReference(interfaceClass = InvestService.class,version = "1.0")
protected InvestService investService;
//用户服务
@DubboReference(interfaceClass = UserService.class,version = "1.0")
protected UserService userService;
//充值服务
@DubboReference(interfaceClass = RechargeService.class,version = "1.0")
protected RechargeService rechargeService;
}
在web模块下controller包,ProductController类添加方法:(查询某个产品的详情和投资记录queryProductDetail(@RequestParam(“productId”) Integer id))
package com.bjpowernode.front.controller;
import com.bjpowernode.api.model.ProductInfo;
import com.bjpowernode.api.pojo.BidInfoProduct;
import com.bjpowernode.api.pojo.MultiProduct;
import com.bjpowernode.common.enums.RCode;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.front.view.PageInfo;
import com.bjpowernode.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "理财产品功能")
@RestController
@RequestMapping("/v1")
public class ProductController extends BaseController {
/*首页三类产品列表*/
@ApiOperation(value = "首页三类产品列表",notes = "一个新手宝,三个优选,三个散标产品")
@GetMapping("/product/index")
public RespResult queryProductIndex(){
RespResult result = RespResult.ok();
MultiProduct multiProduct = productService.queryIndexPageProducts();
result.setData(multiProduct);
return result;
}
/*按产品类型分页查询*/
@ApiOperation(value = "产品分页查询",notes = "按产品类型分页查询")
@GetMapping("/product/list")
public RespResult queryProductByType(@RequestParam("ptype") Integer pType,
@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 {
//请求参数有误
result.setRCode(RCode.REQUEST_PRODUCT_TYPE_ERR);
}
return result;
}
/*查询某个产品的详情和投资记录*/
@ApiOperation(value = "产品详情",notes = "查询某个产品的详细信息和投资5条记录")
@GetMapping("/product/info")
public RespResult queryProductDetail(@RequestParam("productId") Integer id){
RespResult result = RespResult.fail();
if( id != null && id > 0 ){
//调用产品查询
ProductInfo productInfo = productService.queryById(id);
if( productInfo != null){
//查询投资记录
List<BidInfoProduct> bidInfoList = investService.queryBidListByProductId(id,1,5);
//查询成功
result = RespResult.ok();
result.setData(productInfo);
result.setList(bidInfoList);
} else {
result.setRCode(RCode.PRODUCT_OFFLINE);
}
}
return result;
}
}