总览:
在api模块下的service包,创建一个充值接口RechargeService,并创建一个(根据userID查询它的充值记录)方法:
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.RechargeRecord;
import java.util.List;
/**
* 充值接口
*/
public interface RechargeService {
/*根据userID查询它的充值记录*/
List<RechargeRecord> queryByUid(Integer uid,Integer pageNo, Integer pageSize);
int addRechargeRecord(RechargeRecord record);
/*处理后续充值*/
int handleKQNotify(String orderId, String payAmount, String payResult);
}
实现这个接口方法,在dataservice模块service包下,创建相应的实现类RechargeServiceImpl:(根据userID查询它的充值记录)
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.RechargeRecord;
import com.bjpowernode.api.service.RechargeService;
import com.bjpowernode.common.constants.YLBConstant;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.RechargeRecordMapper;
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.List;
import java.util.concurrent.locks.ReentrantLock;
@DubboService(interfaceClass = RechargeService.class,version = "1.0")
public class RechargeServiceImpl implements RechargeService {
@Resource
private RechargeRecordMapper rechargeMapper;
@Resource
private FinanceAccountMapper accountMapper;
/*根据userID查询它的充值记录*/
@Override
public List<RechargeRecord> queryByUid(Integer uid, Integer pageNo, Integer pageSize) {
List<RechargeRecord> records = new ArrayList<>();
if( uid != null && uid > 0 ){
pageNo = CommonUtil.defaultPageNo(pageNo);
pageSize = CommonUtil.defaultPageSize(pageSize);
int offset = (pageNo -1 ) * pageSize;
records = rechargeMapper.selectByUid(uid, offset, pageSize);
}
return records;
}
@Override
public int addRechargeRecord(RechargeRecord record) {
return rechargeMapper.insertSelective(record);
}
/*处理后续充值*/
@Transactional(rollbackFor = Exception.class)
@Override
public synchronized int handleKQNotify(String orderId, String payAmount, String payResult) {
int result = 0;//订单不存在
int rows = 0;
//1.查询订单
RechargeRecord record = rechargeMapper.selectByRechargeNo(orderId);
if(record != null ){
if( record.getRechargeStatus() == YLBConstant.RECHARGE_STATUS_PROCESSING){
//2.判断金额是否一致
String fen = record.getRechargeMoney().multiply(new BigDecimal("100"))
.stripTrailingZeros().toPlainString();
if( fen.equals(payAmount)){
//金额一致
if("10".equals(payResult)){
//成功
rows = accountMapper.updateAvailableMoneyByRecharge(record.getUid(),record.getRechargeMoney());
if(rows < 1 ){
throw new RuntimeException("充值更新资金账号失败");
}
//更新充值记录的状态
rows = rechargeMapper.updateStatus(record.getId(),YLBConstant.RECHARGE_STATUS_SUCCESS);
if( rows < 1) {
throw new RuntimeException("充值更新充值记录状态失败");
}
result = 1;//成功
} else {
//充值失败
//更新充值记录的状态
rows = rechargeMapper.updateStatus(record.getId(),YLBConstant.RECHARGE_STATUS_FAIL);
if( rows < 1) {
throw new RuntimeException("充值更新充值记录状态失败");
}
result = 2;//充值结果是失败的
}
} else {
result = 4;//金额不一样
}
} else {
result = 3;//订单已经处理过了
}
}
return result;
}
}
其中:
1、查询充值记录RechargeRecordMapper:
(需要在dataservice模块mapper包下的UserMapper接口添加方法,并在resources/mappers/UserMapper.xml编写SQL语句):
/*按userId查询充值记录*/
List<RechargeRecord> selectByUid(@Param("uid") Integer uid,
@Param("offset") int offset,
@Param("rows") Integer rows);
<!--按userId查询充值记录-->
<select id="selectByUid" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from b_recharge_record
where uid= #{uid}
order by recharge_time desc
limit #{offset},#{rows}
</select>
在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;
/**
*/
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包下,创建RechargeController(查询充值流水):
package com.bjpowernode.front.controller;
import com.bjpowernode.api.model.RechargeRecord;
import com.bjpowernode.front.view.RespResult;
import com.bjpowernode.front.view.recharge.ResultView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@Api(tags = "充值业务")
@RestController
public class RechargeController extends BaseController {
/*查询充值流水*/
@ApiOperation(value = "查询某个用户的充值记录")
@GetMapping("/v1/recharge/records")
public RespResult queryRechargePage(@RequestHeader("uid") Integer uid,
@RequestParam(required = false,defaultValue = "1") Integer pageNo,
@RequestParam(required = false,defaultValue = "6") Integer pageSize){
RespResult result = RespResult.fail();
if( uid != null && uid > 0 ){
List<RechargeRecord> records = rechargeService.queryByUid(uid, pageNo, pageSize);
result = RespResult.ok();
result.setList( toView(records));
//没有做分页。
}
return result;
}
private List<ResultView> toView(List<RechargeRecord> src){
List<ResultView> target = new ArrayList<>();
src.forEach( record -> {
target.add( new ResultView(record));
});
return target;
}
}
在web模块view.recharge包下,创建ResultView(查询数据记录转换):
package com.bjpowernode.front.view.recharge;
import com.bjpowernode.api.model.RechargeRecord;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.math.BigDecimal;
/**
* 查询数据记录转换
*/
public class ResultView {
private Integer id;
private String result = "未知";
private String rechargeDate = "-";
private BigDecimal rechargeMoney;
public ResultView(RechargeRecord record) {
this.id = record.getId();
this.rechargeMoney = record.getRechargeMoney();
if (record.getRechargeTime() != null) {
rechargeDate = DateFormatUtils.format(record.getRechargeTime(), "yyyy-MM-dd");
}
switch (record.getRechargeStatus()) {
case 0:
result = "充值中";
break;
case 1:
result = "成功";
break;
case 2:
result = "失败";
}
}
public Integer getId() {
return id;
}
public String getResult() {
return result;
}
public String getRechargeDate() {
return rechargeDate;
}
public BigDecimal getRechargeMoney() {
return rechargeMoney;
}
}
在web模块的controller包下,RechargeController添加查询数据记录转换toView:
package com.bjpowernode.front.controller;
import com.bjpowernode.api.model.RechargeRecord;
import com.bjpowernode.front.view.RespResult;
import com.bjpowernode.front.view.recharge.ResultView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@Api(tags = "充值业务")
@RestController
public class RechargeController extends BaseController {
/*查询充值流水*/
@ApiOperation(value = "查询某个用户的充值记录")
@GetMapping("/v1/recharge/records")
public RespResult queryRechargePage(@RequestHeader("uid") Integer uid,
@RequestParam(required = false,defaultValue = "1") Integer pageNo,
@RequestParam(required = false,defaultValue = "6") Integer pageSize){
RespResult result = RespResult.fail();
if( uid != null && uid > 0 ){
List<RechargeRecord> records = rechargeService.queryByUid(uid, pageNo, pageSize);
result = RespResult.ok();
result.setList( toView(records));
//没有做分页。
}
return result;
}
private List<ResultView> toView(List<RechargeRecord> src){
List<ResultView> target = new ArrayList<>();
src.forEach( record -> {
target.add( new ResultView(record));
});
return target;
}
}