From b64e973372447a50059163016e3ce3fe56e19153 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 25 Nov 2021 16:01:24 +0800
Subject: [PATCH] Merge branch 'fish' of http://120.27.238.55:7000/r/exchange into fish
---
src/test/java/com/xcong/excoin/HuobiTest.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 101 insertions(+), 0 deletions(-)
diff --git a/src/test/java/com/xcong/excoin/HuobiTest.java b/src/test/java/com/xcong/excoin/HuobiTest.java
index 1491015..0710303 100644
--- a/src/test/java/com/xcong/excoin/HuobiTest.java
+++ b/src/test/java/com/xcong/excoin/HuobiTest.java
@@ -1,13 +1,31 @@
package com.xcong.excoin;
+import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.huobi.client.SubscriptionClient;
import com.huobi.client.SubscriptionOptions;
import com.huobi.client.model.Candlestick;
import com.huobi.client.model.enums.CandlestickInterval;
+import com.xcong.excoin.common.LoginUserUtils;
+import com.xcong.excoin.common.enumerates.CoinTypeEnum;
+import com.xcong.excoin.common.response.Result;
+import com.xcong.excoin.modules.coin.service.CoinService;
+import com.xcong.excoin.modules.fish.dao.*;
+import com.xcong.excoin.modules.fish.dto.CoinGoldExchangeDto;
+import com.xcong.excoin.modules.fish.entity.CannonAccountMoneyChange;
+import com.xcong.excoin.modules.fish.entity.CannonExchangeRatio;
+import com.xcong.excoin.modules.fish.entity.MemberAccountGold;
+import com.xcong.excoin.modules.fish.service.MemberCannonService;
+import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
+import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
+import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.List;
/**
* @author wzy
@@ -16,6 +34,89 @@
@Slf4j
@SpringBootTest
public class HuobiTest {
+ @Resource
+ MemberAccountGoldDao memberAccountGoldDao;
+ @Resource
+ MemberWalletCoinDao memberWalletCoinDao;
+ @Resource
+ private MemberCannonService memberCannonService;
+ @Resource
+ CannonAccountMoneyChangeDao cannonAccountMoneyChangeDao;
+ @Resource
+ CannonOwnRecordDao cannonOwnRecordDao;
+ @Resource
+ CannonSettingDao cannonSettingDao;
+ @Resource
+ CannonGameRecordDao cannonGameRecordDao;
+ @Resource
+ RedisUtils redisUtils;
+ @Resource
+ private CoinService coinService;
+ @Test
+ public void exchange(){
+
+ CoinGoldExchangeDto coinGoldExchangeDto = new CoinGoldExchangeDto();
+ coinGoldExchangeDto.setBalance(new BigDecimal(1));
+ coinGoldExchangeDto.setType(2);
+ BigDecimal balance = coinGoldExchangeDto.getBalance();
+ if (balance.compareTo(BigDecimal.ZERO) <= 0) {
+// return Result.fail("兑换金额不能小于或等于0");
+ }
+ Long memberId = 3L;
+ //获取兑换比例
+ List<CannonExchangeRatio> cannonExchangeRatios = cannonSettingDao.selectCannonExchangeRatio();
+ if(CollUtil.isEmpty(cannonExchangeRatios)){
+// return Result.fail("系统繁忙请稍候重试");
+ }
+ CannonExchangeRatio cannonExchangeRatio = cannonExchangeRatios.get(0);
+ Integer type = coinGoldExchangeDto.getType();
+ //1:金币兑换1代币 2:1代币兑换金币
+ if(type == 1){
+ MemberAccountGold memberAccountGold = memberAccountGoldDao.selectAccountGoldByMemberId(memberId);
+ MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.XCT.name());
+ if(balance.compareTo(memberAccountGold.getAvailableBalance()) > 0){
+// return Result.fail("金币不足");
+ }
+ BigDecimal coinRatio = cannonExchangeRatio.getCoinRatio();
+ BigDecimal divide = balance.divide(coinRatio).setScale(8,BigDecimal.ROUND_DOWN);
+ //金币账户减少
+ memberCannonService.updateTotalBalanceAndAvailableBalance(memberAccountGold.getId(),balance.negate(),balance.negate(),null);
+ //代币账户增加
+ coinService.updateWalletBalance(memberWalletCoinEntity.getId(),divide,divide,null);
+
+ CannonAccountMoneyChange cannonAccountMoneyChange = new CannonAccountMoneyChange();
+ cannonAccountMoneyChange.setMemberId(memberId);
+ cannonAccountMoneyChange.setAmount(balance.negate());
+ cannonAccountMoneyChange.setType(2);
+ cannonAccountMoneyChange.setContent("金币兑换");
+ cannonAccountMoneyChange.setChangeBalance(balance);
+ cannonAccountMoneyChange.setChangeBefore(memberAccountGold.getAvailableBalance());
+ cannonAccountMoneyChange.setChangeAfter(memberAccountGold.getAvailableBalance().subtract(balance));
+ cannonAccountMoneyChangeDao.insert(cannonAccountMoneyChange);
+ }else if(type == 2){
+ MemberAccountGold memberAccountGold = memberAccountGoldDao.selectAccountGoldByMemberId(memberId);
+ MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.XCT.name());
+ if(balance.compareTo(memberWalletCoinEntity.getAvailableBalance()) > 0){
+// return Result.fail("代币不足");
+ }
+ BigDecimal coinRatio = cannonExchangeRatio.getCoinRatio();
+ BigDecimal multiply = balance.multiply(coinRatio);
+ //金币账户增加
+ memberCannonService.updateTotalBalanceAndAvailableBalance(memberAccountGold.getId(),multiply,multiply,null);
+ //代币账户减少
+ coinService.updateWalletBalance(memberWalletCoinEntity.getId(),balance.negate(),balance.negate(),null);
+
+ CannonAccountMoneyChange cannonAccountMoneyChange = new CannonAccountMoneyChange();
+ cannonAccountMoneyChange.setMemberId(memberId);
+ cannonAccountMoneyChange.setAmount(balance);
+ cannonAccountMoneyChange.setType(3);
+ cannonAccountMoneyChange.setContent("兑换金币");
+ cannonAccountMoneyChange.setChangeBalance(balance);
+ cannonAccountMoneyChange.setChangeBefore(memberWalletCoinEntity.getAvailableBalance());
+ cannonAccountMoneyChange.setChangeAfter(memberWalletCoinEntity.getAvailableBalance().add(balance));
+ cannonAccountMoneyChangeDao.insert(cannonAccountMoneyChange);
+ }
+ }
@Test
public void websocketTest() {
--
Gitblit v1.9.1