|  |  | 
 |  |  | import javax.annotation.Resource;
 | 
 |  |  | 
 | 
 |  |  | import com.alibaba.fastjson.JSONObject;
 | 
 |  |  | import com.xcong.excoin.common.contants.AppContants;
 | 
 |  |  | import com.xcong.excoin.common.enumerates.CoinTypeEnum;
 | 
 |  |  | import com.xcong.excoin.modules.blackchain.service.RocService;
 | 
 |  |  | import com.xcong.excoin.modules.coin.mapper.OrderCoinsDealMapper;
 | 
 |  |  | 
 |  |  |         Long memberId = LoginUserUtils.getAppLoginUser().getId();
 | 
 |  |  |         // 需要实名
 | 
 |  |  |         MemberEntity memberEntity = memberDao.selectById(memberId);
 | 
 |  |  |         if(!MemberEntity.CERTIFY_STATUS_Y.equals(memberEntity.getCertifyStatus())){
 | 
 |  |  |         if (!MemberEntity.CERTIFY_STATUS_Y.equals(memberEntity.getCertifyStatus())) {
 | 
 |  |  |             return Result.fail(MessageSourceUtils.getString("member_controller_0001"));
 | 
 |  |  |         }
 | 
 |  |  |         // 需要先
 | 
 |  |  |         String phone = memberEntity.getPhone();
 | 
 |  |  |         if(!"13632989240".equals(phone) && !"15158130575".equals(phone)){
 | 
 |  |  |             if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type)){
 | 
 |  |  |         if (!"13632989240".equals(phone) && !"15158130575".equals(phone)) {
 | 
 |  |  |             if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
 | 
 |  |  |                 // 不能超过800个
 | 
 |  |  | 
 | 
 |  |  |                 if(amount!=null && amount.compareTo(new BigDecimal("800"))>0){
 | 
 |  |  |                     return Result.fail("买入额度受限");
 | 
 |  |  |                 }
 | 
 |  |  |                 BigDecimal bigDecimal = orderCoinDealDao.sumTodayBuyAmount(memberId, symbol);
 | 
 |  |  |                 if(bigDecimal==null){
 | 
 |  |  |                     bigDecimal= BigDecimal.ZERO;
 | 
 |  |  |                 }
 | 
 |  |  |                 amount= amount==null?BigDecimal.ZERO:amount;
 | 
 |  |  |                 bigDecimal = bigDecimal.add(amount);
 | 
 |  |  |                 if(bigDecimal!=null && bigDecimal.compareTo(new BigDecimal("800"))>0){
 | 
 |  |  |                     return Result.fail("买入额度受限");
 | 
 |  |  |                 }
 | 
 |  |  |                 // 挂单不能超过800
 | 
 |  |  |                 BigDecimal bigDecimal1 = orderCoinDealDao.sumTodayEntrustCntBuyAmount(memberId, symbol);
 | 
 |  |  |                 if(bigDecimal1==null){
 | 
 |  |  |                     bigDecimal1=BigDecimal.ZERO;
 | 
 |  |  |                 }
 | 
 |  |  |                 bigDecimal1 = bigDecimal1.add(amount);
 | 
 |  |  |                 if(bigDecimal1!=null && bigDecimal1.compareTo(new BigDecimal("800"))>0){
 | 
 |  |  |                     return Result.fail("买入额度受限");
 | 
 |  |  |                 }
 | 
 |  |  |             }else{
 | 
 |  |  |                 return Result.fail("卖出受限");
 | 
 |  |  | //                if (amount != null && amount.compareTo(new BigDecimal("800")) > 0) {
 | 
 |  |  | //                    return Result.fail("买入额度受限");
 | 
 |  |  | //                }
 | 
 |  |  | //                BigDecimal bigDecimal = orderCoinDealDao.sumTodayBuyAmount(memberId, symbol);
 | 
 |  |  | //                if (bigDecimal == null) {
 | 
 |  |  | //                    bigDecimal = BigDecimal.ZERO;
 | 
 |  |  | //                }
 | 
 |  |  | //                amount = amount == null ? BigDecimal.ZERO : amount;
 | 
 |  |  | //                bigDecimal = bigDecimal.add(amount);
 | 
 |  |  | //                if (bigDecimal != null && bigDecimal.compareTo(new BigDecimal("800")) > 0) {
 | 
 |  |  | //                    return Result.fail("买入额度受限");
 | 
 |  |  | //                }
 | 
 |  |  | //                // 挂单不能超过800
 | 
 |  |  | //                BigDecimal bigDecimal1 = orderCoinDealDao.sumTodayEntrustCntBuyAmount(memberId, symbol);
 | 
 |  |  | //                if (bigDecimal1 == null) {
 | 
 |  |  | //                    bigDecimal1 = BigDecimal.ZERO;
 | 
 |  |  | //                }
 | 
 |  |  | //                bigDecimal1 = bigDecimal1.add(amount);
 | 
 |  |  | //                if (bigDecimal1 != null && bigDecimal1.compareTo(new BigDecimal("800")) > 0) {
 | 
 |  |  | //                    return Result.fail("买入额度受限");
 | 
 |  |  | //                }
 | 
 |  |  |             } else {
 | 
 |  |  |                return Result.fail("卖出受限");
 | 
 |  |  |             }
 | 
 |  |  |         }
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |             entrustAmount = price.multiply(amount);
 | 
 |  |  |         } else {
 | 
 |  |  |             // 市价
 | 
 |  |  |             if(OrderCoinsEntity.ORDERTYPE_BUY==type){
 | 
 |  |  |             if (OrderCoinsEntity.ORDERTYPE_BUY == type) {
 | 
 |  |  |                 closingPrice = entrustAmount.multiply(tradeSetting.getCoinFeeRatio());
 | 
 |  |  |                 totalPayPrice = entrustAmount.add(closingPrice);
 | 
 |  |  |             }
 | 
 |  |  |         }
 | 
 |  |  |         // BigDecimal totalPayPricCoin = nowPrice.multiply(amount).add(closingPrice);
 | 
 |  |  | 
 | 
 |  |  |         String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
 | 
 |  |  |         MemberWalletCoinEntity walletCoinUsdt = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, walletCode);
 | 
 |  |  |         if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
 | 
 |  |  | 
 |  |  |         //冻结相应的资产
 | 
 |  |  |         if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
 | 
 |  |  |             //如果是买入,所对应的币种增加,USDT账户减少金额
 | 
 |  |  | //            BigDecimal availableBalance = walletCoinUsdt.getAvailableBalance().subtract(totalPayPrice);
 | 
 |  |  | //            BigDecimal frozenBalance = walletCoinUsdt.getFrozenBalance().add(totalPayPrice);
 | 
 |  |  | //            walletCoinUsdt.setAvailableBalance(availableBalance);
 | 
 |  |  | //            walletCoinUsdt.setFrozenBalance(frozenBalance);
 | 
 |  |  | //            memberWalletCoinDao.updateById(walletCoinUsdt);
 | 
 |  |  |             memberWalletCoinDao.updateWalletBalance(walletCoinUsdt.getId(),totalPayPrice.negate(),totalPayPrice.negate(),entrustAmount);
 | 
 |  |  |             memberWalletCoinDao.updateWalletBalance(walletCoinUsdt.getId(), totalPayPrice.negate(), totalPayPrice.negate(), entrustAmount);
 | 
 |  |  |         } else {
 | 
 |  |  |             //如果是卖出,币种减少,USDT增加
 | 
 |  |  | //            BigDecimal availableBalance = walletCoin.getAvailableBalance().subtract(amount);
 | 
 |  |  | //            BigDecimal frozenBalance = walletCoin.getFrozenBalance().add(amount);
 | 
 |  |  | //            walletCoin.setAvailableBalance(availableBalance);
 | 
 |  |  | //            walletCoin.setFrozenBalance(frozenBalance);
 | 
 |  |  | //            memberWalletCoinDao.updateById(walletCoin);
 | 
 |  |  |             memberWalletCoinDao.updateWalletBalance(walletCoin.getId(),amount.negate(),amount.negate(),amount);
 | 
 |  |  |             memberWalletCoinDao.updateWalletBalance(walletCoin.getId(), amount.negate(), amount.negate(), amount);
 | 
 |  |  |         }
 | 
 |  |  |         // 加入到撮合 TODO  通过消息队列发送到交易撮合
 | 
 |  |  |         //CoinTrader trader = factory.getTrader(symbol);
 | 
 |  |  |         //trader.trade(order);
 | 
 |  |  |         // 加入到撮合
 | 
 |  |  |         order.setSymbol(symbol);
 | 
 |  |  |         orderSubmitProducer.sendMsg(JSONObject.toJSONString(order));
 | 
 |  |  |         return Result.ok(MessageSourceUtils.getString("order_service_0011"));
 | 
 |  |  | 
 |  |  |     @Override
 | 
 |  |  |     @Transactional
 | 
 |  |  |     public Result cancelEntrustWalletCoinOrder(String orderId) {
 | 
 |  |  |         // 将这个取消放入redis
 | 
 |  |  |         boolean b = redisUtils.setNotExist(AppContants.ORDER_CANCEL_KEY + orderId, orderId, 10);
 | 
 |  |  |         if (!b) {
 | 
 |  |  |             return Result.ok(MessageSourceUtils.getString("order_service_0012"));
 | 
 |  |  |         }
 | 
 |  |  |         //获取用户ID
 | 
 |  |  |         Long memberId = LoginUserUtils.getAppLoginUser().getId();
 | 
 |  |  |         OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
 | 
 |  |  |         if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId().equals(memberId) ) {
 | 
 |  |  |         if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId().equals(memberId)) {
 | 
 |  |  |             // 如果是撮合交易单
 | 
 |  |  |             if (SymbolsConstats.EXCHANGE_SYMBOLS.contains(orderCoinsEntity.getSymbol())) {
 | 
 |  |  |                 // 这里先更新状态 判断状态 防止消息发送过程中的二次提交
 | 
 |  |  |                 if (!orderCoinsEntity.getOrderStatus().equals(OrderCoinsEntity.ORDERSTATUS_DODING)) {
 | 
 |  |  |                     // 不是持仓中 返回
 | 
 |  |  |                     return Result.ok(MessageSourceUtils.getString("order_service_0013"));
 | 
 |  |  |                 }
 | 
 |  |  | 
 | 
 |  |  |                 // 更新为已取消(可能在这个过程中  这个单已经成交)
 | 
 |  |  |                 orderSubmitProducer.sendCancelMsg(orderId);
 | 
 |  |  |                // return this.cancelEntrustWalletCoinOrderForMatch(orderId);
 | 
 |  |  |                 return Result.ok(MessageSourceUtils.getString("order_service_0013"));
 | 
 |  |  |             }
 | 
 |  |  |             if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
 | 
 |  |  |             if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_DONE) {
 | 
 |  |  |                 return Result.fail(MessageSourceUtils.getString("order_service_0012"));
 | 
 |  |  |             }
 | 
 |  |  |             orderCoinsEntity.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
 | 
 |  |  | 
 |  |  |     @Override
 | 
 |  |  |     @Transactional
 | 
 |  |  |     public Result cancelEntrustWalletCoinOrderForMatch(String orderId) {
 | 
 |  |  |         //获取用户ID
 | 
 |  |  |         //如果redis中没有这个单 则不再往下走
 | 
 |  |  |         OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
 | 
 |  |  |         if(orderCoinsEntity==null){
 | 
 |  |  |         if (orderCoinsEntity == null) {
 | 
 |  |  |             return Result.ok("");
 | 
 |  |  |         }
 | 
 |  |  |         Long memberId = orderCoinsEntity.getMemberId();
 | 
 |  |  |         // 取消撮合订单的单
 | 
 |  |  |         CoinTrader trader = factory.getTrader(orderCoinsEntity.getSymbol());
 | 
 |  |  |         trader.cancelOrder(orderCoinsEntity);
 | 
 |  |  |         if (ObjectUtil.isNotEmpty(orderCoinsEntity) ) {
 | 
 |  |  |             if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
 | 
 |  |  |         // 从撮合交易系统得到的已成交的数据
 | 
 |  |  |         OrderCoinsEntity coinsEntityCancel = trader.cancelOrder(orderCoinsEntity);
 | 
 |  |  |         if (coinsEntityCancel == null) {
 | 
 |  |  |             // 此时说明撮合系统已经没这个单了 不需要继续处理
 | 
 |  |  |             return null;
 | 
 |  |  |         }
 | 
 |  |  | 
 | 
 |  |  |         if (ObjectUtil.isNotEmpty(orderCoinsEntity)) {
 | 
 |  |  |             if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_DONE) {
 | 
 |  |  |                 // 已完成的直接返回
 | 
 |  |  |                 return Result.fail(MessageSourceUtils.getString("order_service_0012"));
 | 
 |  |  |             }
 | 
 |  |  |             orderCoinsEntity.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
 | 
 |  |  |             orderCoinsDao.updateById(orderCoinsEntity);
 | 
 |  |  |             OrderCoinsEntity update = new OrderCoinsEntity();
 | 
 |  |  |             update.setId(Long.valueOf(orderId));
 | 
 |  |  |             update.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
 | 
 |  |  |             //orderCoinsEntity.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_CANCEL);
 | 
 |  |  |             orderCoinsDao.updateById(update);
 | 
 |  |  | 
 | 
 |  |  |             String symbol = orderCoinsEntity.getSymbol();
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |             detail.setTradeType(orderCoinsEntity.getTradeType());
 | 
 |  |  |             detail.setSymbol(symbol);
 | 
 |  |  |             detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_CANCEL);
 | 
 |  |  |             detail.setSymbolCnt(orderCoinsEntity.getEntrustCnt());
 | 
 |  |  |             detail.setSymbolCnt(BigDecimal.ZERO);
 | 
 |  |  |             detail.setEntrustPrice(orderCoinsEntity.getEntrustPrice());
 | 
 |  |  |             detail.setDealPrice(orderCoinsEntity.getDealPrice());
 | 
 |  |  |             detail.setDealAmount(orderCoinsEntity.getDealAmount());
 | 
 |  |  |             detail.setDealPrice(BigDecimal.ZERO);
 | 
 |  |  |             detail.setDealAmount(BigDecimal.ZERO);
 | 
 |  |  |             detail.setFeeAmount(orderCoinsEntity.getFeeAmount());
 | 
 |  |  |             if (OrderCoinsEntity.ORDERTYPE_BUY.equals(orderCoinsEntity.getOrderType())) {
 | 
 |  |  |                 //如果是限价买入,撤单将USDT账户冻结金额返回
 | 
 |  |  | 
 |  |  |                     //手续费 = 开仓价*数量*手续费率
 | 
 |  |  |                     //返还金额=开仓价*未成交数量+手续费
 | 
 |  |  |                     // 这里根据成交的单计算
 | 
 |  |  |                     List<OrderCoinsDealEntity> orderCoinsDealEntities = orderCoinDealDao.selectCoinOrderDealByOrderId(Long.valueOf(orderId));
 | 
 |  |  |                     BigDecimal dealAmount = BigDecimal.ZERO;
 | 
 |  |  |                     if(CollectionUtils.isNotEmpty(orderCoinsDealEntities)){
 | 
 |  |  |                         for (OrderCoinsDealEntity orderCoinsDealEntity : orderCoinsDealEntities) {
 | 
 |  |  |                             dealAmount = dealAmount.add(orderCoinsDealEntity.getDealAmount());
 | 
 |  |  |                         }
 | 
 |  |  |                     }
 | 
 |  |  |                     BigDecimal dealAmount = coinsEntityCancel.getDealAmount();
 | 
 |  |  |                     // 市价的按成交额退款
 | 
 |  |  |                     BigDecimal returnBalance = orderCoinsEntity.getEntrustAmount().subtract(dealAmount);
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |                         returnFee = orderCoinsEntity.getFeeAmount().subtract(needFee);
 | 
 |  |  |                     }
 | 
 |  |  |                     BigDecimal avi = returnBalance.add(returnFee);
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(walletCoin.getId(),avi,null,returnBalance.negate());
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(walletCoin.getId(), avi, null, returnBalance.negate());
 | 
 |  |  |                     walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).add(returnFee));
 | 
 |  |  |                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
 | 
 |  |  |                     //memberWalletCoinDao.updateById(walletCoin);
 | 
 |  |  | 
 |  |  |                 MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
 | 
 |  |  |                 if (ObjectUtil.isNotEmpty(walletCoin)) {
 | 
 |  |  |                     // 卖出按卖出的数量计算手续费
 | 
 |  |  |                     BigDecimal returnBalance = orderCoinsEntity.getEntrustCnt().subtract(orderCoinsEntity.getDealCnt());
 | 
 |  |  |                     BigDecimal returnBalance = orderCoinsEntity.getEntrustCnt().subtract(coinsEntityCancel.getDealCnt());
 | 
 |  |  |                     walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance));
 | 
 |  |  |                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
 | 
 |  |  |                     memberWalletCoinDao.updateById(walletCoin);
 | 
 |  |  |                     //memberWalletCoinDao.updateById(walletCoin);
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(walletCoin.getId(), returnBalance, null, returnBalance.negate());
 | 
 |  |  |                     // 流水记录
 | 
 |  |  |                     MemberAccountFlowEntity record = new MemberAccountFlowEntity();
 | 
 |  |  |                     record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
 | 
 |  |  | 
 |  |  |             BigDecimal price = exchangeTrade.getPrice();
 | 
 |  |  |             // 卖单
 | 
 |  |  |             Long sellOrderId = exchangeTrade.getSellOrderId();
 | 
 |  |  | 
 | 
 |  |  |             // 买卖单都需要处理
 | 
 |  |  |             // 买单
 | 
 |  |  |             OrderCoinsEntity buyOrderCoinsEntity = orderCoinsDao.selectById(buyOrderId);
 | 
 |  |  |             if(buyOrderCoinsEntity==null){
 | 
 |  |  |                 return;
 | 
 |  |  |             }
 | 
 |  |  |             BigDecimal buyEntrustCnt = buyOrderCoinsEntity.getEntrustCnt();
 | 
 |  |  |             if(buyEntrustCnt==null){
 | 
 |  |  |                 buyEntrustCnt = BigDecimal.ZERO;
 | 
 |  |  |             }
 | 
 |  |  |             Long memberId = buyOrderCoinsEntity.getMemberId();
 | 
 |  |  |             if (buyOrderCoinsEntity != null) {
 | 
 |  |  |                 List<OrderCoinsDealEntity> orderCoinsDealEntities = orderCoinDealDao.selectCoinOrderDealByOrderId(buyOrderId);
 | 
 |  |  |                 // 比较剩余的量
 | 
 |  |  |                 BigDecimal dealAmount = BigDecimal.ZERO;
 | 
 |  |  |                 BigDecimal dealCnt = BigDecimal.ZERO;
 | 
 |  |  |                 if(CollectionUtils.isNotEmpty(orderCoinsDealEntities)){
 | 
 |  |  |                     for (OrderCoinsDealEntity orderCoinsDealEntity : orderCoinsDealEntities) {
 | 
 |  |  |                         dealAmount=dealAmount.add(orderCoinsDealEntity.getDealAmount());
 | 
 |  |  |                         dealCnt = dealCnt.add(orderCoinsDealEntity.getSymbolCnt());
 | 
 |  |  |                     }
 | 
 |  |  |                 }
 | 
 |  |  | 
 | 
 |  |  |                 // 单的总金额
 | 
 |  |  |                 BigDecimal entrustAmount = buyOrderCoinsEntity.getEntrustAmount();
 | 
 |  |  |                 BigDecimal add = dealAmount.add(buyTurnover);
 | 
 |  |  |                 BigDecimal closingPrice = buyTurnover.multiply(new BigDecimal("0.002"));
 | 
 |  |  | 
 | 
 |  |  |                 //成交总量
 | 
 |  |  |                  dealCnt = dealCnt.add(amount);
 | 
 |  |  |                 // 创建一个完成的单
 | 
 |  |  |                 OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
 | 
 |  |  |                 detail.setMemberId(buyOrderCoinsEntity.getMemberId());
 | 
 |  |  | 
 |  |  |                 detail.setDealAmount(buyTurnover);
 | 
 |  |  |                 detail.setFeeAmount(closingPrice);
 | 
 |  |  |                 detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
 | 
 |  |  |                 // 如果这个单在取消状态 则不执行
 | 
 |  |  |                 orderCoinDealDao.insert(detail);
 | 
 |  |  |                 // 如果这个单成交完  更改状态
 | 
 |  |  |                 if (add.compareTo(entrustAmount) >= 0 ||(buyEntrustCnt.compareTo(BigDecimal.ZERO)>0 &&dealCnt.compareTo(buyEntrustCnt)>=0) ) {
 | 
 |  |  |                     OrderCoinsEntity update = new OrderCoinsEntity();
 | 
 |  |  |                     update.setId(buyOrderId);
 | 
 |  |  |                     update.setDealAmount(entrustAmount);
 | 
 |  |  |                     update.setDealCnt(buyOrderCoinsEntity.getDealCnt().add(amount));
 | 
 |  |  |                     update.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
 | 
 |  |  |                     update.setUpdateTime(new Date());
 | 
 |  |  |                     orderCoinsDao.updateById(update);
 | 
 |  |  |                     // 限价买入时,如果成交价比设置的价格低,需要退还多余的冻结
 | 
 |  |  |                     OrderCoinsEntity coinsEntity = orderCoinsDao.selectById(buyOrderId);
 | 
 |  |  |                     BigDecimal subtract = coinsEntity.getEntrustAmount().subtract(coinsEntity.getDealAmount());
 | 
 |  |  |                     if(subtract.compareTo(BigDecimal.ZERO)>=0){
 | 
 |  |  |                         // 下单扣的比较多
 | 
 |  |  |                         memberWalletCoinDao.updateWalletBalance(coinsEntity.getId(),subtract,subtract,subtract.negate());
 | 
 |  |  |                     }
 | 
 |  |  |                 } else {
 | 
 |  |  |                     // 更新买单
 | 
 |  |  |                     orderCoinsDao.updateDeal(buyOrderId,amount,buyTurnover);
 | 
 |  |  |                 // 更新买单
 | 
 |  |  |                 //orderCoinsDao.updateDeal(buyOrderId, amount, buyTurnover);
 | 
 |  |  |                 // 买币扣除冻结usdt 增加币种的可用
 | 
 |  |  |                 MemberWalletCoinEntity usdtWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
 | 
 |  |  |                 if (usdtWallet != null) {
 | 
 |  |  |                     // 减少usdt冻结
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(usdtWallet.getId(), null, null, buyTurnover.negate());
 | 
 |  |  |                 }
 | 
 |  |  |                 // 增加买的币
 | 
 |  |  |                 MemberWalletCoinEntity buySymbolWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 if (buySymbolWallet != null) {
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(buySymbolWallet.getId(), amount, amount, null);
 | 
 |  |  |                 }
 | 
 |  |  |                 // 流水记录
 | 
 |  |  |                 MemberAccountFlowEntity record = new MemberAccountFlowEntity();
 | 
 |  |  |                 record.setMemberId(buyOrderCoinsEntity.getMemberId());
 | 
 |  |  |                 record.setPrice(buyTurnover.setScale(4, BigDecimal.ROUND_DOWN).negate());
 | 
 |  |  |                 record.setSource(MemberAccountFlowEntity.SOURCE_BUY + buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 record.setRemark(MemberAccountFlowEntity.REMARK_BUY + buyOrderCoinsEntity.getSymbol() + ":" + amount);
 | 
 |  |  |                 record.setSymbol(buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 record.setBalance(usdtWallet.getAvailableBalance().subtract(buyTurnover));
 | 
 |  |  |                 memberAccountFlowEntityDao.insert(record);
 | 
 |  |  |             }
 | 
 |  |  | 
 | 
 |  |  |             // 卖单
 | 
 |  |  |             OrderCoinsEntity sellOrderCoinsEntity = orderCoinsDao.selectById(sellOrderId);
 | 
 |  |  |             if (sellOrderCoinsEntity != null) {
 | 
 |  |  |                 // 比较剩余的量
 | 
 |  |  |                 BigDecimal dealAmount = sellOrderCoinsEntity.getDealCnt();
 | 
 |  |  |                 // 单的总数量
 | 
 |  |  |                 BigDecimal entrustCnt = sellOrderCoinsEntity.getEntrustCnt();
 | 
 |  |  |                 BigDecimal add = dealAmount.add(amount);
 | 
 |  |  |                 // 创建一个完成的单
 | 
 |  |  |                 OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
 | 
 |  |  |                 detail.setMemberId(sellOrderCoinsEntity.getMemberId());
 | 
 |  |  | 
 |  |  |                 detail.setOrderStatus(OrderCoinsDealEntity.ORDERSTATUS_DONE);
 | 
 |  |  |                 orderCoinDealDao.insert(detail);
 | 
 |  |  |                 // 如果这个单成交完  更改状态
 | 
 |  |  |                 if (add.compareTo(entrustCnt) >= 0) {
 | 
 |  |  |                     OrderCoinsEntity update = new OrderCoinsEntity();
 | 
 |  |  |                     update.setId(sellOrderId);
 | 
 |  |  |                     // 总成交额
 | 
 |  |  |                     update.setDealAmount(buyTurnover.add(sellOrderCoinsEntity.getDealAmount()));
 | 
 |  |  |                     update.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
 | 
 |  |  |                     update.setDealCnt(entrustCnt);
 | 
 |  |  |                     update.setUpdateTime(new Date());
 | 
 |  |  |                     orderCoinsDao.updateById(update);
 | 
 |  |  |                 } else {
 | 
 |  |  |                     // 未完成
 | 
 |  |  |                     OrderCoinsEntity update = new OrderCoinsEntity();
 | 
 |  |  |                     update.setId(sellOrderId);
 | 
 |  |  |                     // 总成交额
 | 
 |  |  |                     update.setDealAmount(buyTurnover.add(sellOrderCoinsEntity.getDealAmount()));
 | 
 |  |  |                     update.setDealCnt(sellOrderCoinsEntity.getDealCnt().add(amount));
 | 
 |  |  |                     update.setUpdateTime(new Date());
 | 
 |  |  |                     orderCoinsDao.updateById(update);
 | 
 |  |  |                 }
 | 
 |  |  |                 // 买币扣除冻结usdt 增加币种的可用
 | 
 |  |  |                 MemberWalletCoinEntity usdtWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
 | 
 |  |  |                 if (usdtWallet != null) {
 | 
 |  |  |                     // 减少usdt冻结
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(usdtWallet.getId(), null, null, buyTurnover.negate());
 | 
 |  |  |                 }
 | 
 |  |  | 
 | 
 |  |  |                 // 增加买的币
 | 
 |  |  |                 MemberWalletCoinEntity buySymbolWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 if (buySymbolWallet != null) {
 | 
 |  |  |                     memberWalletCoinDao.updateWalletBalance(buySymbolWallet.getId(), amount, amount, null);
 | 
 |  |  |                 }
 | 
 |  |  |                 // 流水记录
 | 
 |  |  |                 MemberAccountFlowEntity record = new MemberAccountFlowEntity();
 | 
 |  |  |                 record.setMemberId(buyOrderCoinsEntity.getMemberId());
 | 
 |  |  |                 record.setPrice(buyTurnover.setScale(4, BigDecimal.ROUND_DOWN).negate());
 | 
 |  |  |                 record.setSource(MemberAccountFlowEntity.SOURCE_BUY + buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 record.setRemark(MemberAccountFlowEntity.REMARK_BUY + buyOrderCoinsEntity.getSymbol() + ":" + amount);
 | 
 |  |  |                 record.setSymbol(buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 record.setBalance(usdtWallet.getAvailableBalance().subtract(buyTurnover));
 | 
 |  |  |                 memberAccountFlowEntityDao.insert(record);
 | 
 |  |  |                 //orderCoinsDao.updateDeal(sellOrderId, amount, buyTurnover);
 | 
 |  |  |                 // 卖家需要减少冻结的币种  增加usdt
 | 
 |  |  |                 MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(sellOrderCoinsEntity.getMemberId(), sellOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 if (memberWalletCoinEntity != null) {
 | 
 |  |  | 
 |  |  |                 MemberAccountFlowEntity recordSell = new MemberAccountFlowEntity();
 | 
 |  |  |                 recordSell.setMemberId(sellOrderCoinsEntity.getMemberId());
 | 
 |  |  |                 recordSell.setPrice(buyTurnover.setScale(4, BigDecimal.ROUND_DOWN));
 | 
 |  |  |                 recordSell.setSource(MemberAccountFlowEntity.SOURCE_SALE + buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 recordSell.setRemark(MemberAccountFlowEntity.REMARK_SALE + buyOrderCoinsEntity.getSymbol() + ":" + amount.toPlainString());
 | 
 |  |  |                 recordSell.setSymbol(buyOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 recordSell.setSource(MemberAccountFlowEntity.SOURCE_SALE + sellOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 recordSell.setRemark(MemberAccountFlowEntity.REMARK_SALE + sellOrderCoinsEntity.getSymbol() + ":" + amount.toPlainString());
 | 
 |  |  |                 recordSell.setSymbol(sellOrderCoinsEntity.getSymbol());
 | 
 |  |  |                 recordSell.setBalance(sellWalletCoinEntity.getAvailableBalance().add(buyTurnover));
 | 
 |  |  |                 memberAccountFlowEntityDao.insert(recordSell);
 | 
 |  |  |             }
 | 
 |  |  | 
 |  |  |     }
 | 
 |  |  | 
 | 
 |  |  |     @Override
 | 
 |  |  |     @Transactional
 | 
 |  |  |     public void completeOrder(List<OrderCoinsEntity> trades) {
 | 
 |  |  |         // 订单完成 更新他们的状态
 | 
 |  |  |         List<Long> ids = new ArrayList<>();
 | 
 |  |  |         if (CollectionUtils.isNotEmpty(trades)) {
 | 
 |  |  |             for (OrderCoinsEntity trade : trades) {
 | 
 |  |  |                 if (trade != null) {
 | 
 |  |  |                     orderCoinsDao.updateStatus(trade.getId(),OrderCoinsEntity.ORDERSTATUS_DONE);
 | 
 |  |  |                     ids.add(trade.getId());
 | 
 |  |  |                     // 买单 实际成交金额小于委托的 这一部分从冻结扣除
 | 
 |  |  |                     if(OrderCoinsEntity.ORDERTYPE_BUY==trade.getOrderType()){
 | 
 |  |  |                         if(trade.getEntrustAmount().compareTo(trade.getDealAmount())>0){
 | 
 |  |  |                             // 此时退回这部分的差额
 | 
 |  |  |                             BigDecimal subtract = trade.getEntrustAmount().subtract(trade.getDealAmount());
 | 
 |  |  |                             System.out.println(subtract);
 | 
 |  |  |                             MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(trade.getMemberId(), CoinTypeEnum.USDT.name());
 | 
 |  |  |                             if(memberWalletCoinEntity!=null){
 | 
 |  |  |                                 memberWalletCoinDao.updateWalletBalance(memberWalletCoinEntity.getId(),subtract,null,subtract.negate());
 | 
 |  |  |                             }
 | 
 |  |  |                         }
 | 
 |  |  |                     }
 | 
 |  |  |                 }
 | 
 |  |  |             }
 | 
 |  |  |         }
 | 
 |  |  |         if (CollectionUtils.isNotEmpty(ids)) {
 | 
 |  |  |            // orderCoinsDao.batchUpdateStatus(ids, OrderCoinsEntity.ORDERSTATUS_DONE);
 | 
 |  |  |         }
 | 
 |  |  |     }
 | 
 |  |  | 
 | 
 |  |  |     @Override
 | 
 |  |  |     public void initOrders(String symbol, Integer type, Integer tradeType, BigDecimal price,
 | 
 |  |  |                            BigDecimal amount, BigDecimal entrustAmount) {
 | 
 |  |  | 
 |