xiaoyong931011
2020-05-29 864b77ebac060a40bb0a454e53fa851540a4cd19
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -1,47 +1,57 @@
package com.xcong.excoin.modules.coin.service.impl;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
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;
import com.xcong.excoin.modules.coin.dao.MemberAccountFlowEntityDao;
import com.xcong.excoin.modules.coin.dao.MemberSelectSymbolsDao;
import com.xcong.excoin.modules.coin.dao.OrderCoinDao;
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.PlatformTradeSettingEntity;
import com.xcong.excoin.modules.coin.dao.OrderCoinDealDao;
import com.xcong.excoin.modules.coin.dao.OrderCoinsDao;
import com.xcong.excoin.modules.coin.entity.MemberAccountFlowEntity;
import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity;
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
import com.xcong.excoin.modules.coin.mapper.OrderWalletCoinDealMapper;
import com.xcong.excoin.modules.coin.mapper.OrderWalletCoinMapper;
import com.xcong.excoin.modules.coin.parameter.vo.FindCollectListVo;
import com.xcong.excoin.modules.coin.parameter.vo.MemberSelectSymbolsVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinDealListVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinDealVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinListVo;
import com.xcong.excoin.modules.coin.parameter.vo.OrderWalletCoinVo;
import com.xcong.excoin.modules.coin.parameter.vo.TransactionPageOfWalletCoinVo;
import com.xcong.excoin.modules.coin.service.OrderCoinService;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberSelectSymbolsEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.platform.dao.PlatformCnyUsdtExchangeDao;
import com.xcong.excoin.modules.platform.dao.TradeSettingDao;
import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
import com.xcong.excoin.utils.MessageSourceUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
@Service
public class OrderCoinServiceImpl extends ServiceImpl<OrderCoinDao, OrderCoinEntity> implements OrderCoinService{
public class OrderCoinServiceImpl extends ServiceImpl<OrderCoinsDao, OrderCoinsEntity> implements OrderCoinService{
   
   @Resource
   TradeSettingDao platformTradeSettingDao;
@@ -50,7 +60,38 @@
   @Resource
   MemberSelectSymbolsDao memberSelectSymbolsDao;
   @Resource
   CnyUsdtExchangeDao cnyUsdtExchangeDao;
    PlatformCnyUsdtExchangeDao cnyUsdtExchangeDao;
   @Resource
   OrderCoinsDao orderCoinsDao;
   @Resource
   OrderCoinDealDao orderCoinDealDao;
   @Resource
   MemberAccountFlowEntityDao memberAccountFlowEntityDao;
   @Override
   public String generateSimpleSerialno(String userId) {
      StringBuilder sb = new StringBuilder();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
      Date now = new Date();
      sb.append(sd.format(now));
      Calendar calendar = new GregorianCalendar();
      calendar.setTime(now);
      calendar.add(calendar.DATE, 1);
      Date nextDate = calendar.getTime();
      if (StrUtil.isNotEmpty(userId)) {
         sb.append(userId);
      }
      sb.append(RandomUtil.randomInt(2));
      long count = orderCoinsDao.getOrderCountByToday(sdf.format(now), sdf.format(nextDate));
      count++;
      int size = 4;
      for (int i = 0; i < size - String.valueOf(count).length(); i++) {
         sb.append("0");
      }
      sb.append(count);
      return sb.toString();
   }
   @Override
   public Result enterTransactionPageOfWalletCoin(String symbol, String type) {
@@ -63,7 +104,7 @@
      MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
      
      PlatformTradeSettingEntity tradeSetting = platformTradeSettingDao.findTradeSetting();
      if (tradeSetting == null) {
      if (ObjectUtil.isEmpty(tradeSetting)) {
         return Result.fail(MessageSourceUtils.getString("order_service_0003"));
      }
      //获取USDT的币币账户信息
@@ -77,60 +118,58 @@
      BigDecimal closePrice = new BigDecimal("100.0000");
      
      List<MemberSelectSymbolsEntity> memSymbols = memberSelectSymbolsDao.selectSymbolByMemIdAndSymbol(memberId, symbol);
      CnyUsdtExchange cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne();
      PlatformCnyUsdtExchangeEntity cnyUsdtExchange = cnyUsdtExchangeDao.getCNYAndUSDTOne();
      BigDecimal cnyUsdt = cnyUsdtExchange.getValue();
      Map<String, Object> map = new HashMap<String, Object>();
      TransactionPageOfWalletCoinVo transactionPageOfWalletCoinVo = new TransactionPageOfWalletCoinVo();
      //是否自选
      if(CollUtil.isEmpty(memSymbols)) {
         map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_ISCOLLECT_NO.getName(),
               MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_ISCOLLECT_NO.getValue());//是否已经自选该币种
         transactionPageOfWalletCoinVo.setIsCollect(TransactionPageOfWalletCoinVo.ISCOLLECT_NO);
      }else {
         map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_ISCOLLECT_YES.getName(),
               MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_ISCOLLECT_YES.getValue());//是否已经自选该币种
         transactionPageOfWalletCoinVo.setIsCollect(TransactionPageOfWalletCoinVo.ISCOLLECT_YES);
      }
      if (ObjectUtil.isEmpty(walletCoin))
         return Result.fail(MessageSourceUtils.getString("order_service_0003"));
      map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_SPREAD.getValue(), tradeSetting.getSpread());// 点差
      map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_CLOSINGRATIO.getValue(), tradeSetting.getFeeRatio());// 手续费用率
      // 点差
      transactionPageOfWalletCoinVo.setSpread(tradeSetting.getSpread().setScale(4, BigDecimal.ROUND_DOWN));
      // 手续费用率
      transactionPageOfWalletCoinVo.setFeeRatio(tradeSetting.getFeeRatio().setScale(4, BigDecimal.ROUND_DOWN));
      // 用户可用金额
      if(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_BUY.getValue().equals(type)) {//买入
         map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_MEMBERMONEY.getValue(), walletCoinUsdt.getAvailableBalance());// 用户可用金额
         transactionPageOfWalletCoinVo.setAvailableBalance(walletCoinUsdt.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
      }else {
         map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_MEMBERMONEY.getValue(), walletCoin.getAvailableBalance());// 用户可用金额
         transactionPageOfWalletCoinVo.setAvailableBalance(walletCoin.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
      }
      //当前价
      transactionPageOfWalletCoinVo.setCurrentPrice(closePrice.setScale(4, BigDecimal.ROUND_DOWN));
      //比例
      transactionPageOfWalletCoinVo.setCnyUsdt(cnyUsdt.setScale(4, BigDecimal.ROUND_DOWN));
      //换算成人民币的币种价格
      transactionPageOfWalletCoinVo.setCurrentPriceCny(cnyUsdt.multiply(closePrice).setScale(4, BigDecimal.ROUND_DOWN));
      
      map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_CURRENTPRICE.getValue(), closePrice);//当前价
      map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_CNYUSDT.getValue(), cnyUsdt);//比例
      map.put(MemberWalletCoinEnum.ENTERTRANSACTIONPAGEOFWALLETCOIN_CURRENTPRICECNY.getValue(), cnyUsdt.multiply(closePrice));//换算成人民币的币种价格
      return Result.ok(map);
}
      transactionPageOfWalletCoinVo.setSymbol(symbol);
      transactionPageOfWalletCoinVo.setType(type);
      return Result.ok(transactionPageOfWalletCoinVo);
   }
   @Override
   @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");
         
         BigDecimal nowPrice = new BigDecimal("10.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) ) {
         MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
         if (ObjectUtil.isEmpty(walletCoin)) {
            return Result.fail(MessageSourceUtils.getString("order_service_0003"));
         }
         // 查询交易设置
@@ -138,150 +177,311 @@
         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 closingPrice = price.multiply(amount).multiply(new BigDecimal(MemberWalletCoinEnum.SUBMITSALESWALLETCOINORDER_SERVICERATE.getValue()));
         //总费用 = 成交价*数量+手续费
         BigDecimal totalPayPrice = price.multiply(amount).add(closingPrice).setScale(4, BigDecimal.ROUND_DOWN);
         if(MemberWalletCoinEnum.SUBMITSALESWALLETCOINORDER_TYPE.getValue().equals(type.toString())) {
         BigDecimal totalPayPrice = price.multiply(amount).add(closingPrice);
         String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
         MemberWalletCoinEntity walletCoinUsdt = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId,walletCode);
         if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
            //买入,所需总费用跟用户USDT金额进行比较
            if (totalPayPrice.compareTo(walletCoinUsdt.getAvailableBalance()) > 0) {
            BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance();
            if (totalPayPrice.compareTo(availableBalance) > 0) {
               return Result.fail(MessageSourceUtils.getString("order_service_0010"));
            }
         }else {
            //卖出,所需总费用跟用户所对应的币种金额进行比较
            if (amount.compareTo(walletCoin.getAvailableBalance()) > 0) {
            BigDecimal availableBalance = walletCoin.getAvailableBalance();
            if (amount.compareTo(availableBalance) > 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() + ""));
         OrderCoinsEntity order = new OrderCoinsEntity();
         if (OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType)) {
            // 如果是限价交易直接插入主表数据
            order.setMemberId(memberId);
            order.setOrderNo(generateSimpleSerialno(memberId.toString()));
            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.setSymbol(symbol);
            order.setMarkPrice(nowPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealAmount(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DODING);
            order.setTradeType(tradeType);
            order.setVersion(1);
            order.setClosingPrice(closingPrice);
            orderMapper.addCoinMainOrder(order);
            order.setFeeAmount(closingPrice.setScale(4, BigDecimal.ROUND_DOWN));
            orderCoinsDao.insert(order);
            
            //更新用户钱包信息
            //冻结相应的资产
            if(type == 1) {//如果是买入,所对应的币种增加,USDT账户减少金额
               // 更新用户的可用金额,冻结金额
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance()-totalPayPrice.doubleValue());
               walletCoinUsdt.setFrozenBalance(walletCoinUsdt.getFrozenBalance()+totalPayPrice.doubleValue());
               walletCoinDao.updateByModel(walletCoinUsdt);
            if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
               //如果是买入,所对应的币种增加,USDT账户减少金额
               BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice);
               BigDecimal frozenBalance = walletCoinUsdt.getFrozenBalance().add(totalPayPrice);
               walletCoinUsdt.setAvailableBalance(availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
               walletCoinUsdt.setFrozenBalance(frozenBalance.setScale(4, BigDecimal.ROUND_DOWN));
               memberWalletCoinDao.updateById(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);
               BigDecimal availableBalance = walletCoin.getAvailableBalance().subtract(amount);
               BigDecimal frozenBalance = walletCoin.getFrozenBalance().add(amount);
               walletCoin.setAvailableBalance(availableBalance.setScale(4, BigDecimal.ROUND_DOWN));
               walletCoin.setFrozenBalance(frozenBalance.setScale(4, BigDecimal.ROUND_DOWN));
               memberWalletCoinDao.updateById(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.setMemberId(memberId);
            order.setOrderNo(generateSimpleSerialno(memberId.toString()));
            order.setOrderType(type);
            order.setRewardRatio(nowPrice
                  .multiply(amount).doubleValue());
//            order.setStatus(3);
            order.setSysbol(symbol);
            order.setSysbolNumber(amount);
            order.setSysbolFlatNumber(amount);
            order.setSymbol(symbol);
            order.setMarkPrice(nowPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setEntrustPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            order.setDealAmount(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
            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);
            order.setFeeAmount(closingPrice.setScale(4, BigDecimal.ROUND_DOWN));
            orderCoinsDao.insert(order);
            
            if(type == 1) {//如果是买入,所对应的币种增加,USDT账户减少金额
            OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
            detail.setMemberId(memberId);
            detail.setOrderId(order.getId());
            detail.setOrderNo(order.getOrderNo());
            detail.setOrderType(type);
            detail.setTradeType(tradeType);
            detail.setSymbol(symbol);
            detail.setSymbolCnt(amount.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setEntrustPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setDealPrice(price.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setDealAmount(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            detail.setFeeAmount(closingPrice.setScale(4, BigDecimal.ROUND_DOWN));
            orderCoinDealDao.insert(detail);
            if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
               //如果是买入,所对应的币种增加,USDT账户减少金额
               // 更新用户的可用金额
               BigDecimal availableBalance = new BigDecimal(walletCoin.getAvailableBalance()).add(amount).setScale(8, BigDecimal.ROUND_HALF_UP);
               walletCoin.setAvailableBalance(availableBalance.doubleValue());
               walletCoinDao.updateByModel(walletCoin);
               walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(amount).setScale(4, BigDecimal.ROUND_DOWN));
               memberWalletCoinDao.updateById(walletCoin);
               
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance()-totalPayPrice.doubleValue());
               walletCoinDao.updateByModel(walletCoinUsdt);
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice).setScale(4, BigDecimal.ROUND_DOWN));
               memberWalletCoinDao.updateById(walletCoinUsdt);
            }else {
               //如果是卖出,币种减少,USDT增加
               BigDecimal availableBalance = new BigDecimal(walletCoin.getAvailableBalance()).subtract(amount).setScale(8, BigDecimal.ROUND_HALF_UP);
               walletCoin.setAvailableBalance(availableBalance.doubleValue());
               walletCoinDao.updateByModel(walletCoin);
               walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().subtract(amount).setScale(4, BigDecimal.ROUND_DOWN));
               memberWalletCoinDao.updateById(walletCoin);
               
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance()+totalPayPrice.doubleValue());
               walletCoinDao.updateByModel(walletCoinUsdt);
               walletCoinUsdt.setAvailableBalance(walletCoinUsdt.getAvailableBalance().add(totalPayPrice).setScale(4, BigDecimal.ROUND_DOWN));
               memberWalletCoinDao.updateById(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());
         MemberAccountFlowEntity record = new MemberAccountFlowEntity();
         record.setMemberId(memberId);
         if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
            record.setPrice(totalPayPrice.setScale(4, BigDecimal.ROUND_DOWN));
            record.setSource(MemberAccountFlowEntity.SOURCE_BUY+symbol);
            record.setRemark(MemberAccountFlowEntity.REMARK_BUY+symbol+":"+amount.setScale(4, BigDecimal.ROUND_DOWN));
         } else {
            record.setDirectionSource("币币卖出"+symbol);
            record.setRemark("卖出"+symbol+":"+amount);
            record.setPrice(-totalPayPrice.doubleValue());
            record.setPrice(totalPayPrice.negate().setScale(4, BigDecimal.ROUND_DOWN));
            record.setSource(MemberAccountFlowEntity.SOURCE_SALE+symbol);
            record.setRemark(MemberAccountFlowEntity.REMARK_SALE+symbol+":"+amount.setScale(4, BigDecimal.ROUND_DOWN));
         }
         record.setMemberBalance(walletCoinUsdt.getAvailableBalance());
         record.setMemberid(member.getmId());
         record.setSymbolName(symbol);
         record.setMemberName(member.getRealName());
         record.setMemberPhone(member.getPhone());
         record.setSymbol(symbol);
         record.setBalance(walletCoinUsdt.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
         
         memberMapper.addFlowRecord(record);
      } catch (Exception e) {
         e.printStackTrace();
         memberAccountFlowEntityDao.insert(record);
      return Result.ok(MessageSourceUtils.getString("order_service_0011"));
   }
   @Override
   public Result getEntrustWalletCoinOrder(String symbol, Integer status) {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      OrderWalletCoinListVo orderWalletCoinListVo = new OrderWalletCoinListVo();
      List<OrderWalletCoinVo> arrayList = new ArrayList<>();
      List<OrderCoinsEntity> findCoinOrderListByMemberIdAndSysmbol = orderCoinsDao.findCoinOrderListByMemberIdAndSysmbol(memberId, symbol, status);
      if(CollUtil.isNotEmpty(findCoinOrderListByMemberIdAndSysmbol)) {
         for(OrderCoinsEntity orderCoinsEntity : findCoinOrderListByMemberIdAndSysmbol) {
            OrderWalletCoinVo entityToVo = OrderWalletCoinMapper.INSTANCE.entityToVo(orderCoinsEntity);
            arrayList.add(entityToVo);
         }
      }
      return Result.success(MessageSourceUtils.getString("order_service_0011"));
      orderWalletCoinListVo.setOrderWalletCoinVo(arrayList);
      return Result.ok(arrayList);
   }
   @Override
   @Transactional
   public Result cancelEntrustWalletCoinOrder(String orderId) {
         //获取用户ID
         Long memberId = LoginUserUtils.getAppLoginUser().getId();
         OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
            if(ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId() == memberId) {
               if(orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL){
                  return Result.fail(MessageSourceUtils.getString("order_service_0012"));
               }
               orderCoinsEntity.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
               orderCoinsDao.updateById(orderCoinsEntity);
               String symbol = orderCoinsEntity.getSymbol();
               if(orderCoinsEntity.getOrderType() == OrderCoinsEntity.TRADETYPE_FIXEDPRICE) {
                  //如果是限价买入,撤单将USDT账户冻结金额返回
                  String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
                  MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
                  if (ObjectUtil.isNotEmpty(walletCoin)) {
                     //手续费 = 开仓价*数量*手续费率
                     //返还金额=开仓价*未成交数量+手续费
                     BigDecimal returnBalance = orderCoinsEntity.getDealAmount().add(orderCoinsEntity.getFeeAmount());
                     walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     memberWalletCoinDao.updateById(walletCoin);
                     // 流水记录
                     MemberAccountFlowEntity record = new MemberAccountFlowEntity();
                     record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
                     record.setRemark(MemberAccountFlowEntity.REMARK_CANCEL+symbol+MemberAccountFlowEntity.REMARK_RETURNBALANCE+returnBalance);
                     record.setBalance(walletCoin.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
                     record.setMemberId(memberId);
                     record.setSymbol(symbol);
                     record.setPrice(returnBalance);
                     memberAccountFlowEntityDao.insert(record);
                     return Result.ok(MessageSourceUtils.getString("order_service_0013"));
                  }
               }else {
                  //如果是限价卖出,撤单将对应的钱包冻结金额返回
                  MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
                  if (ObjectUtil.isNotEmpty(walletCoin)) {
                     //返还金额=开仓价*未成交数量
                     BigDecimal returnBalance = walletCoin.getAvailableBalance().add(walletCoin.getFrozenBalance());
                     walletCoin.setAvailableBalance(returnBalance.setScale(4, BigDecimal.ROUND_DOWN));
                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance).setScale(4, BigDecimal.ROUND_DOWN));
                     memberWalletCoinDao.updateById(walletCoin);
                     // 流水记录
                     MemberAccountFlowEntity record = new MemberAccountFlowEntity();
                     record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
                     record.setRemark(MemberAccountFlowEntity.REMARK_CANCEL+symbol+MemberAccountFlowEntity.REMARK_RETURNBALANCE+returnBalance);
                     record.setBalance(walletCoin.getAvailableBalance().setScale(4, BigDecimal.ROUND_DOWN));
                     record.setMemberId(memberId);
                     record.setSymbol(symbol);
                     record.setPrice(walletCoin.getFrozenBalance().setScale(4, BigDecimal.ROUND_DOWN));
                     memberAccountFlowEntityDao.insert(record);
                     return Result.ok(MessageSourceUtils.getString("order_service_0013"));
                  }
               }
               OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
               detail.setMemberId(memberId);
               detail.setOrderId(orderCoinsEntity.getId());
               detail.setOrderNo(generateSimpleSerialno(memberId.toString()));
               detail.setOrderType(orderCoinsEntity.getOrderType());
               detail.setTradeType(orderCoinsEntity.getTradeType());
               detail.setSymbol(symbol);
               detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_CANCEL);
               detail.setSymbolCnt(orderCoinsEntity.getEntrustCnt());
               detail.setEntrustPrice(orderCoinsEntity.getEntrustPrice().setScale(4, BigDecimal.ROUND_DOWN));
               detail.setDealPrice(orderCoinsEntity.getDealPrice().setScale(4, BigDecimal.ROUND_DOWN));
               detail.setDealAmount(orderCoinsEntity.getDealAmount().setScale(4, BigDecimal.ROUND_DOWN));
               detail.setFeeAmount(orderCoinsEntity.getFeeAmount().setScale(4, BigDecimal.ROUND_DOWN));
               orderCoinDealDao.insert(detail);
            }
      return Result.fail(MessageSourceUtils.getString("order_service_0043"));
   }
   @Override
   public Result findAllWalletCoinOrder() {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      OrderWalletCoinDealListVo orderWalletCoinDealListVo = new OrderWalletCoinDealListVo();
      List<OrderWalletCoinDealVo> arrayList = new ArrayList<OrderWalletCoinDealVo>();
      List<OrderCoinsDealEntity> selectAllWalletCoinOrder = orderCoinDealDao.selectAllWalletCoinOrder(memberId);
      if(CollUtil.isNotEmpty(selectAllWalletCoinOrder)) {
         for(OrderCoinsDealEntity orderCoinsDealEntity: selectAllWalletCoinOrder) {
            OrderWalletCoinDealVo entityToVo = OrderWalletCoinDealMapper.INSTANCE.entityToVoOrder(orderCoinsDealEntity);
            arrayList.add(entityToVo);
         }
      }
      orderWalletCoinDealListVo.setOrderWalletCoinDealVo(arrayList);
      return Result.ok(orderWalletCoinDealListVo);
   }
   @Override
   public Result findWalletCoinOrder(Long orderId) {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      OrderCoinsDealEntity selectWalletCoinOrder = orderCoinDealDao.selectWalletCoinOrder(orderId,memberId);
      OrderWalletCoinDealVo entityToVo = OrderWalletCoinDealMapper.INSTANCE.entityToVoOrder(selectWalletCoinOrder);
      return Result.ok(entityToVo);
   }
   @Override
   public Result findCollect(String symbol, Integer type) {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      if(type==1) {
         //添加自选
         MemberSelectSymbolsEntity symbols = new MemberSelectSymbolsEntity();
         symbols.setMemberId(memberId);
         symbols.setSymbol(symbol);
         memberSelectSymbolsDao.insert(symbols);
         return Result.ok(MessageSourceUtils.getString("order_service_0015"));
      }else {
         Map<String, Object> columnMap = new HashMap<>();
         columnMap.put("symbol", symbol);
         columnMap.put("member_id", memberId);
         memberSelectSymbolsDao.deleteByMap(columnMap);;
         return Result.ok(MessageSourceUtils.getString("order_service_0016"));
      }
   }
   @Override
   public Result checkIsCollect(String symbol) {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      MemberSelectSymbolsVo memberSelectSymbolsVo = new MemberSelectSymbolsVo();
      List<MemberSelectSymbolsEntity> selectSymbolByMemIdAndSymbol = memberSelectSymbolsDao.selectSymbolByMemIdAndSymbol(memberId, symbol);
      if(CollUtil.isNotEmpty(selectSymbolByMemIdAndSymbol)) {
         memberSelectSymbolsVo.setIsCollect(MemberSelectSymbolsVo.ISCOLLECT_YES);
      }else {
         memberSelectSymbolsVo.setIsCollect(MemberSelectSymbolsVo.ISCOLLECT_NO);
      }
      memberSelectSymbolsVo.setSymbol(symbol);
      return Result.ok(memberSelectSymbolsVo);
   }
   @Override
   public Result findCollectList() {
      //获取用户ID
      Long memberId = LoginUserUtils.getAppLoginUser().getId();
      Map<String, Object> columnMap = new HashMap<>();
      columnMap.put("member_id", memberId);
      List<MemberSelectSymbolsEntity> selectByMap = memberSelectSymbolsDao.selectByMap(columnMap);
      FindCollectListVo findCollectListVo = new FindCollectListVo();
      List<MemberSelectSymbolsVo> arrayList = new ArrayList<>();
      if(CollUtil.isNotEmpty(selectByMap)) {
         for(MemberSelectSymbolsEntity memberSelectSymbolsEntity : selectByMap) {
            MemberSelectSymbolsVo memberSelectSymbolsVo = new MemberSelectSymbolsVo();
            memberSelectSymbolsVo.setSymbol(memberSelectSymbolsEntity.getSymbol());
            arrayList.add(memberSelectSymbolsVo);
         }
      }
      findCollectListVo.setMemberSelectSymbolsVo(arrayList);
      return Result.ok(findCollectListVo);
   }
}