xiaoyong931011
2020-05-25 6f44fa20505ec0b1c95b460043f25b9050511aa0
20200525   代码提交
7 files modified
226 ■■■■ changed files
src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java 8 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/dao/OrderCoinDao.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/dao/OrderCoinDealDao.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/service/OrderCoinService.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java 16 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java 186 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/coin/controller/CoinController.java
@@ -5,6 +5,7 @@
import javax.annotation.Resource;
import javax.validation.Valid;
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinInfoVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberWalletCoinVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
@@ -48,9 +49,10 @@
     * @return
     */
    @ApiOperation(value="获取币币账户某个币种信息", notes="获取币币账户某个币种信息")
    @GetMapping(value = "/getWalletCoinById")
    public Result getWalletCoinById(@RequestParam("id") Long id) {
        return coinService.getWalletCoinById(id);
    @ApiResponses({@ApiResponse( code = 200, message = "success", response = MemberWalletCoinInfoVo.class)})
    @GetMapping(value = "/getWalletCoinBySymbol")
    public Result getWalletCoinBySymbol(@ApiParam(name="symbol",value="币种",required=true)String symbol) {
        return coinService.getWalletCoinBySymbol(symbol);
    }
    
    /**
src/main/java/com/xcong/excoin/modules/coin/dao/OrderCoinDao.java
@@ -1,8 +1,8 @@
package com.xcong.excoin.modules.coin.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xcong.excoin.modules.coin.entity.OrderCoinEntity;
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
public interface OrderCoinDao extends BaseMapper<OrderCoinEntity>{
public interface OrderCoinDao extends BaseMapper<OrderCoinsEntity>{
}
src/main/java/com/xcong/excoin/modules/coin/dao/OrderCoinDealDao.java
@@ -1,8 +1,8 @@
package com.xcong.excoin.modules.coin.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xcong.excoin.modules.coin.entity.OrderCoinDealEntity;
import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity;
public interface OrderCoinDealDao  extends BaseMapper<OrderCoinDealEntity>{
public interface OrderCoinDealDao  extends BaseMapper<OrderCoinsDealEntity>{
}
src/main/java/com/xcong/excoin/modules/coin/service/CoinService.java
@@ -10,8 +10,6 @@
    public Result getWalletCoin();
    public Result getWalletCoinById(Long id);
    public Result getWalletContractById();
    public Result coinWalletTransferToContract(BigDecimal balance, String symbol);
@@ -32,4 +30,6 @@
    public Result findWalletAgentBySymbol();
    public Result getWalletCoinBySymbol(String symbol);
}
src/main/java/com/xcong/excoin/modules/coin/service/OrderCoinService.java
@@ -4,9 +4,9 @@
import com.baomidou.mybatisplus.extension.service.IService;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.modules.coin.entity.OrderCoinEntity;
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
public interface OrderCoinService extends IService<OrderCoinEntity>{
public interface OrderCoinService extends IService<OrderCoinsEntity>{
    Result enterTransactionPageOfWalletCoin(String symbol, String type);
src/main/java/com/xcong/excoin/modules/coin/service/impl/CoinServiceImpl.java
@@ -132,27 +132,31 @@
    }
    @Override
    public Result getWalletCoinById(Long id) {
    public Result getWalletCoinBySymbol(String symbol) {
        try {
            //获取用户ID
            Long memberId = LoginUserUtils.getAppLoginUser().getId();
            MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectById(id);
            MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId,symbol);
            MemberWalletCoinInfoVo memberWalletCoinInfoVo = new MemberWalletCoinInfoVo();
            memberWalletCoinInfoVo.setFrozenBalance(walletCoin.getFrozenBalance());
            memberWalletCoinInfoVo.setAvailableBalance(walletCoin.getAvailableBalance());
            memberWalletCoinInfoVo.setMemberId(memberId);
            memberWalletCoinInfoVo.setWalletCode(symbol);
            if(!StrUtil.isEmpty(memberId.toString())) {
                CnyUsdtExchange cnyUsdtExchange =cnyUsdtExchangeDao.getCNYAndUSDTOne();
                BigDecimal cnyUsdt = cnyUsdtExchange.getValue();
                BigDecimal total = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
                
                if(MemberWalletCoinEnum.WALLETCOINCODE.getValue().equals(walletCoin.getWalletCode())) {
                    walletCoin.setTotalBalance(total.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
                    memberWalletCoinInfoVo.setTotalBalance(total.multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
                    
                }else {
                    BigDecimal closePrice = new BigDecimal("10.0000");
                    //Double closePrice = symbolsService.getCloseSymbolsBySymbolsName(wallet.getCode()+"/USDT");
                    walletCoin.setTotalBalance(total.multiply(closePrice).multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
                    memberWalletCoinInfoVo.setTotalBalance(total.multiply(closePrice).multiply(cnyUsdt).setScale(4, BigDecimal.ROUND_DOWN));
                }
            }
            return Result.ok(walletCoin);
            return Result.ok(memberWalletCoinInfoVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageSourceUtils.getString("member_service_0003"));
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -8,18 +8,10 @@
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kebex.app.entity.member.Member;
import com.kebex.app.entity.member.MemberAccountFlowRecord;
import com.kebex.app.entity.member.WalletCoin;
import com.kebex.app.entity.order.CoinOrderMain;
import com.kebex.app.entity.order.CoinsOrderDetail;
import com.kebex.app.entity.trade.TradeSetting;
import com.kebex.common.huobiapi.CoinTypeConConvert;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.enumerates.MemberWalletCoinEnum;
import com.xcong.excoin.common.response.Result;
@@ -28,7 +20,7 @@
import com.xcong.excoin.modules.coin.dao.platform.CnyUsdtExchangeDao;
import com.xcong.excoin.modules.coin.dao.platform.TradeSettingDao;
import com.xcong.excoin.modules.coin.entity.CnyUsdtExchange;
import com.xcong.excoin.modules.coin.entity.OrderCoinEntity;
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
import com.xcong.excoin.modules.coin.entity.PlatformTradeSettingEntity;
import com.xcong.excoin.modules.coin.service.OrderCoinService;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
@@ -41,7 +33,7 @@
import cn.hutool.core.util.StrUtil;
@Service
public class OrderCoinServiceImpl extends ServiceImpl<OrderCoinDao, OrderCoinEntity> implements OrderCoinService{
public class OrderCoinServiceImpl extends ServiceImpl<OrderCoinDao, OrderCoinsEntity> implements OrderCoinService{
    
    @Resource
    TradeSettingDao platformTradeSettingDao;
@@ -110,178 +102,8 @@
    @Transactional
    public Result submitSalesWalletCoinOrder(String symbol, Integer type, Integer tradeType, BigDecimal price,
            BigDecimal amount) {
        try {
            //获取用户ID
            Long memberId = LoginUserUtils.getAppLoginUser().getId();
            String[] symbols = symbol.split("/");
            if (symbols.length <= 0) {
                return Result.fail(MessageSourceUtils.getString("order_service_0008"));
            }
            /**
             * todo
             */
            //查询当前价
            //BigDecimal nowPrice = new BigDecimal(redisUtil.getString(CoinTypeConConvert.convertToKey(symbol+"/USDT")));
            BigDecimal nowPrice = new BigDecimal("100.0000");
            // 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
            symbol = symbol.toUpperCase();
            MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbols[0]);
            MemberWalletCoinEntity walletCoinUsdt = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId,
                                                                        MemberWalletCoinEnum.WALLETCOINCODE.getValue());
            if (ObjectUtil.isEmpty(walletCoin) || ObjectUtil.isEmpty(walletCoinUsdt) ) {
                return Result.fail(MessageSourceUtils.getString("order_service_0003"));
            }
            // 查询交易设置
            PlatformTradeSettingEntity tradeSetting = platformTradeSettingDao.findTradeSetting();
            if(ObjectUtil.isEmpty(tradeSetting)) {
                return Result.fail(MessageSourceUtils.getString("order_service_0009"));
            }
            //手续费率
            BigDecimal ServiceRate = new BigDecimal(MemberWalletCoinEnum.SUBMITSALESWALLETCOINORDER_SERVICERATE.getValue());
            // 手续费用(手续费=建仓价X数量X手续费率)
            BigDecimal closingPrice = price.multiply(amount).multiply(ServiceRate).setScale(4, BigDecimal.ROUND_DOWN);
            //总费用 = 成交价*数量+手续费
            BigDecimal totalPayPrice = price.multiply(amount).add(closingPrice).setScale(4, BigDecimal.ROUND_DOWN);
            if(MemberWalletCoinEnum.SUBMITSALESWALLETCOINORDER_TYPE.getValue().equals(type.toString())) {
                //买入,所需总费用跟用户USDT金额进行比较
                if (totalPayPrice.compareTo(walletCoinUsdt.getAvailableBalance()) > 0) {
                    return Result.fail(MessageSourceUtils.getString("order_service_0010"));
                }
            }else {
                //卖出,所需总费用跟用户所对应的币种金额进行比较
                if (amount.compareTo(walletCoin.getAvailableBalance()) > 0) {
                    return Result.fail(MessageSourceUtils.getString("order_service_0010"));
                }
            }
            CoinOrderMain order = new CoinOrderMain();
            // 创建订单
            if (tradeType == 2) {// 如果是限价交易直接插入主表数据
                order.setStatus(1);
                order.setMemId(member.getmId());
                order.setOpenPrice(price.doubleValue());
                order.setOpenTime(new Date());
                order.setOrderNo(generateSimpleSerialno(member.getmId() + ""));
                order.setOrderType(type);
                order.setRewardRatio(price
                        .multiply(amount).doubleValue());
                order.setForceSetPrice(nowPrice.doubleValue());
//                order.setStatus(3);
                order.setSysbol(symbol);
                order.setSysbolNumber(amount);
                order.setSysbolFlatNumber(amount);
                order.setTradeType(tradeType);
                order.setVersion(1);
                order.setClosingPrice(closingPrice);
                orderMapper.addCoinMainOrder(order);
                //更新用户钱包信息
                //冻结相应的资产
                if(type == 1) {//如果是买入,所对应的币种增加,USDT账户减少金额
                    // 更新用户的可用金额,冻结金额
                    walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance()-totalPayPrice.doubleValue());
                    walletCoinUsdt.setFrozenBalance(walletCoinUsdt.getFrozenBalance()+totalPayPrice.doubleValue());
                    walletCoinDao.updateByModel(walletCoinUsdt);
                }else {
                    //如果是卖出,币种减少,USDT增加
                    BigDecimal availableBalance = new BigDecimal(walletCoin.getAvailableBalance()).subtract(amount).setScale(8, BigDecimal.ROUND_HALF_UP);
                    walletCoin.setAvailableBalance(availableBalance.doubleValue());
                    walletCoin.setFrozenBalance(walletCoin.getFrozenBalance()+amount.doubleValue());
                    walletCoinDao.updateByModel(walletCoin);
                }
            } else {
                //如果是市价交易,主表和附表都需要插入数据
                order.setStatus(3);
                order.setMemId(member.getmId());
                order.setForceSetPrice(nowPrice.doubleValue());
                order.setOpenPrice(nowPrice.doubleValue());
                order.setOpenTime(new Date());
                order.setOrderNo(generateSimpleSerialno(member.getmId() + ""));
                order.setOrderType(type);
                order.setRewardRatio(nowPrice
                        .multiply(amount).doubleValue());
//                order.setStatus(3);
                order.setSysbol(symbol);
                order.setSysbolNumber(amount);
                order.setSysbolFlatNumber(amount);
                order.setTradeType(tradeType);
                order.setVersion(1);
                order.setClosingPrice(closingPrice);
                orderMapper.addCoinMainOrder(order);
                //
                CoinsOrderDetail detail = new CoinsOrderDetail();
                detail.setClosingPrice(closingPrice.doubleValue());
                detail.setCreateTime(new Date());
                detail.setEntrustStatus(1);
                detail.setEntrustType(type);
                detail.setExitPrice(price.doubleValue());
                detail.setExitType(1);
                detail.setMainId(order.getId());
                detail.setMemberId(member.getmId());
                detail.setMemberPhone(member.getPhone());
                detail.setPrePrice(price
                        .multiply(amount).doubleValue());
                detail.setRewardRatio(0.0);
                detail.setSerialno(generateSimpleSerialno(member.getmId() + ""));
                detail.setSymbol(symbol);
                detail.setSymbolSku(amount);
                detail.setSymbolSkuNumber(amount);
                detail.setTradePrice(price.doubleValue());
                detail.setTradeType(tradeType);
                detail.setVersion(1);
                coinOrderDetailMapper.insert(detail);
                if(type == 1) {//如果是买入,所对应的币种增加,USDT账户减少金额
                    // 更新用户的可用金额
                    BigDecimal availableBalance = new BigDecimal(walletCoin.getAvailableBalance()).add(amount).setScale(8, BigDecimal.ROUND_HALF_UP);
                    walletCoin.setAvailableBalance(availableBalance.doubleValue());
                    walletCoinDao.updateByModel(walletCoin);
                    walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance()-totalPayPrice.doubleValue());
                    walletCoinDao.updateByModel(walletCoinUsdt);
                }else {
                    //如果是卖出,币种减少,USDT增加
                    BigDecimal availableBalance = new BigDecimal(walletCoin.getAvailableBalance()).subtract(amount).setScale(8, BigDecimal.ROUND_HALF_UP);
                    walletCoin.setAvailableBalance(availableBalance.doubleValue());
                    walletCoinDao.updateByModel(walletCoin);
                    walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance()+totalPayPrice.doubleValue());
                    walletCoinDao.updateByModel(walletCoinUsdt);
                }
                //返佣
//                calYj(member.getmId(), closingPrice, order);
//                String reference = member.getReference();
                //返佣
//                agentReturn(reference,closingPrice.doubleValue(),detail);
            }
            // 流水记录
            MemberAccountFlowRecord record = new MemberAccountFlowRecord();
            record.setCreateTime(new Date());
            if (type == 1) {
                record.setDirectionSource("币币买入"+symbol);
                record.setRemark("买入"+symbol+":"+amount);
                record.setPrice(totalPayPrice.doubleValue());
            } else {
                record.setDirectionSource("币币卖出"+symbol);
                record.setRemark("卖出"+symbol+":"+amount);
                record.setPrice(-totalPayPrice.doubleValue());
            }
            record.setMemberBalance(walletCoinUsdt.getAvailableBalance());
            record.setMemberid(member.getmId());
            record.setSymbolName(symbol);
            record.setMemberName(member.getRealName());
            record.setMemberPhone(member.getPhone());
            memberMapper.addFlowRecord(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(MessageSourceUtils.getString("order_service_0011"));
        return Result.ok(MessageSourceUtils.getString("order_service_0011"));
    }
}