BigDecimal循环累加问题
BigDecimal total = new BigDecimal(0);
for(AccountType listResult:list){
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId", user.getId());
map.put("accountType",String.valueOf(listResult.getId()));
UserAccount userAccount = userAccountMapper.findUserAccountByUserAndType(map);
if(!ComUtil.isEmpty(userAccount)){
userBalance userBalance = userBalanceMapper.findByBalance(userAccount.getId());
if(!ComUtil.isEmpty(userBalance)){
total = total.add(userBalance.getAmount());
}
}
}
SpringBoot与axios表单提交问题
- 问题描述: axios 表单提交,springBoot 无法接受到数据,但使用swagger测试,可以。
- 原因:
1.axios的表单提交 ,content-type 默认为 application/json;charset=UTF-8
2.提交数据会附加在payload(以JSON形式)。
3.@ModelAttribute 可以接受表单的数据,但是content-type的类型需要为application/x-www-form。@RequestBody 可以接受表单数据,但是content-type类型需要为application/json。@RequestParam 从URL中接受请求参数。(不用于表单提交)。
@PostMapping("/addressBook/insertAddressBook")
@ApiOperation(value = "添加地址簿信息")
public ResponseModel insertAddressBook(@RequestBody AddressBookQuery addressBookQuery){
return null;
}
BigDecimal比较大小
@GetMapping("/withdrawMoney/drawing")
@ApiOperation(value = "提币")
public ResponseModel drawing(String number, String coinType){
val map = new HashMap<String, Object>();
BigDecimal money = new BigDecimal(number);
CoinType coinTypeResult = coinTypeService.getCoinType(coinType);
int difference = money.compareTo(coinTypeResult.getOutMin());
if(!ComUtil.isEmpty(coinTypeResult)){
if(difference==-1){
map.put("result", "金额不能小于最小提币金额");
map.put("status", false);
return ResponseHelper.buildBadResponseModel(map);
}else{
}
}
return null;
}
java大小写转换函数
- 在实际接口开发过程中,如果接口文档不明确的情况下,类型传参前段开发人员可能因为大小写原因后台解析错误,所以统一大写或小写
@GetMapping("/userRechargeCoin/getBtcAddress")
@ApiOperation(value = "获取比特币地址")
public ResponseModel getBtcAddress(String accountType) throws IOException {
val map = bitcoinAddressService.getBtcAddress(accountType.toUpperCase());
return ResponseHelper.buildResponseModel(map);
}
累加清空
- 在实际开发中,做累加操作的值放入list列表,为保证数据的有效性每次循环累加后需要吧,累加值清空,重新累加下一个对象的累加值,这个坑算是自己不严谨导致的某些问题,这边做一下记录。
for(UserBalance userBalanceT2 : userBalanceList){
if(!ComUtil.isEmpty(userBalanceT2)){
UserBalance userBalanceRs = new UserBalance();
userBalanceRs.setAmountT2(new BigDecimal(0));
userBalanceRs.setId(userBalanceT2.getId());
userBalanceService.updateUserBalance(userBalanceRs);
balanceChangeList = balanceChangeService.findBalanceChangeByBalanceId(userBalanceT2.getId());
if(!ComUtil.isEmpty(balanceChangeList)){
for(BalanceChange balanceChange : balanceChangeList){
if(balanceChange.getTypeId() == desposit && balanceChange.getStatus() == 3){
number = balanceChange.getAmount();
}else if(balanceChange.getTypeId() == extract && balanceChange.getStatus() == 3){
number = balanceChange.getAmount().negate();
}else if(balanceChange.getTypeId() == exchangeIn){
number = balanceChange.getAmount();
}else if(balanceChange.getTypeId() == exchangeOut){
number = balanceChange.getAmount().negate();
}
total = total.add(number);
}
int i = total.compareTo(new BigDecimal(0));
if(i<0 || i==0){
total = new BigDecimal(0);
}
AmountTotal amountTotal = new AmountTotal();
amountTotal.setBalanceId(userBalanceT2.getId());
amountTotal.setTotal(total);
amountTotalList.add(amountTotal);
total = new BigDecimal(0);
number = new BigDecimal(0);
}
}
}