937af82e81a46d74018250f44ecba7a5ca42b723..5ffa4ccd62c2f32b5808e0a5f6c9a65142c201ff
2021-11-25 xiaoyong931011
Merge branch 'fish' of http://120.27.238.55:7000/r/exchange into fish
5ffa4c diff | tree
2021-11-25 xiaoyong931011
20211125 fish
54ef34 diff | tree
4 files modified
114 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/fish/controller/MemberCannonController.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/fish/dto/CoinGoldExchangeDto.java 2 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/fish/MemberAccountGoldDao.xml 2 ●●● patch | view | raw | blame | history
src/test/java/com/xcong/excoin/HuobiTest.java 101 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/fish/controller/MemberCannonController.java
@@ -17,6 +17,7 @@
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
@Slf4j
@Api(value = "MemberCannonController", tags = "炮台接口类")
@@ -52,7 +53,7 @@
     */
    @ApiOperation(value = "代币金币互转")
    @PostMapping(value = "/coinGoldExchange")
    public Result coinGoldExchange(@RequestBody CoinGoldExchangeDto coinGoldExchangeDto) {
    public Result coinGoldExchange(@RequestBody @Valid CoinGoldExchangeDto coinGoldExchangeDto) {
        return memberCannonService.coinGoldExchange(coinGoldExchangeDto);
    }
@@ -71,7 +72,7 @@
    @ApiOperation(value = "获取大炮列表")
    @ApiResponses({@ApiResponse( code = 200, message = "success", response = CannonSettingVo.class)})
    @PostMapping(value = "/getCannons")
    public Result getCannons(@RequestBody GetCannonsDto getCannonsDto) {
    public Result getCannons(@RequestBody @Valid GetCannonsDto getCannonsDto) {
        return memberCannonService.getCannons(getCannonsDto);
    }
@@ -80,7 +81,7 @@
     */
    @ApiOperation(value = "兑换大炮")
    @PostMapping(value = "/cannonExchange")
    public Result cannonExchange(@RequestBody CannonExchangeDto cannonExchangeDto) {
    public Result cannonExchange(@RequestBody @Valid CannonExchangeDto cannonExchangeDto) {
        return memberCannonService.cannonExchange(cannonExchangeDto);
    }
@@ -99,7 +100,7 @@
     */
    @ApiOperation(value = "捕鱼")
    @PostMapping(value = "/fishing")
    public Result fishing(@RequestBody FishingDto fishingDto) {
    public Result fishing(@RequestBody @Valid FishingDto fishingDto) {
        return memberCannonService.fishing(fishingDto);
    }
src/main/java/com/xcong/excoin/modules/fish/dto/CoinGoldExchangeDto.java
@@ -4,6 +4,7 @@
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
@@ -12,6 +13,7 @@
public class CoinGoldExchangeDto {
    @NotNull(message = "兑换金额不能为空")
    @DecimalMin(value = "1",message = "兑换数量最小为1")
    @ApiModelProperty(value = "兑换金额", example = "100")
    private BigDecimal balance;
src/main/resources/mapper/fish/MemberAccountGoldDao.xml
@@ -11,7 +11,7 @@
        <set>
            <if test="availableBalance != null">
                available_balance = (
                case when  IFNULL(available_balance, 0) + #{available_balance}>0 then  IFNULL(available_balance, 0) + #{availableBalance} else 0 end
                case when  IFNULL(available_balance, 0) + #{availableBalance}>0 then  IFNULL(available_balance, 0) + #{availableBalance} else 0 end
                ),
            </if>
            <if test="totalBalance != null">
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() {