| | |
| | | package com.yami.trading.service.exchange.impl; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.common.util.UTCDateUtils; |
| | | import com.yami.trading.dao.exchange.ExchangeApplyOrderMapper; |
| | | import com.yami.trading.service.MarketOpenChecker; |
| | | import com.yami.trading.service.MoneyLogService; |
| | | import com.yami.trading.service.WalletService; |
| | | import com.yami.trading.service.data.DataService; |
| | | import com.yami.trading.service.exchange.ExchangeApplyOrderService; |
| | | import com.yami.trading.service.item.ItemPreMarketService; |
| | | import com.yami.trading.service.item.ItemService; |
| | | import com.yami.trading.service.rate.ExchangeRateService; |
| | | import com.yami.trading.service.syspara.SysparaService; |
| | | import com.yami.trading.service.user.UserDataService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | @Autowired |
| | | ItemService itemService; |
| | | @Autowired |
| | | ItemPreMarketService itemPreMarketService; |
| | | @Autowired |
| | | DataService dataService; |
| | | @Autowired |
| | | WalletService walletService; |
| | |
| | | ExchangeApplyOrderMapper exchangeApplyOrderMapper; |
| | | @Autowired |
| | | private ExchangeRateService exchangeRateService; |
| | | @Autowired |
| | | @Lazy |
| | | private ExchangeApplyOrderService self; |
| | | |
| | | @Override |
| | | public List<ExchangeApplyOrder> findSubmitted() { |
| | |
| | | * 现货交易买入 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public void saveSpotTradOpen(ExchangeApplyOrder order) { |
| | | order.setOrderNo(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8)); |
| | | double fee = Arith.mul(order.getVolume(), sysparaService.find("exchange_apply_order_buy_fee").getDouble()); |
| | | Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId()); |
| | | String symbol = order.getSymbol(); |
| | | Item item = itemService.findBySymbol(symbol); |
| | | if (item != null && StrUtil.isNotBlank(item.getSymbol())) { |
| | | symbol = item.getSymbol(); |
| | | order.setSymbol(symbol); |
| | | } |
| | | double amountBefore = wallet.getMoney().doubleValue(); |
| | | double realAmount = order.getVolume() + fee; |
| | | String type = item.getType(); |
| | |
| | | throw new YamiShopBindException("余额不足"); |
| | | } |
| | | // 休市期间,下市价订单, 叫 "竞价单",竞价单订单号长度为21,其他订单号长度为20位 |
| | | boolean preMarketActive = itemPreMarketService != null |
| | | && itemPreMarketService.isPreMarketTradingActive(symbol); |
| | | if (ExchangeApplyOrder.ORDER_PRICE_TYPE_OPPONENT.equals(order.getOrderPriceType())) { |
| | | boolean isOpen = MarketOpenChecker.isMarketOpenByItemCloseType(item.getOpenCloseType()); |
| | | boolean isOpen = preMarketActive || itemService.isOpen(symbol); |
| | | if (!isOpen) { |
| | | order.setOrderNo(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(9)); |
| | | } |
| | |
| | | order.setCreateTime(new Date()); |
| | | save(order); |
| | | userDataService.saveBuy(order); |
| | | tryImmediatePreMarketFill(order, symbol, preMarketActive); |
| | | } |
| | | |
| | | /** |
| | |
| | | order.setCreateTime(new Date()); |
| | | order.setFee(Arith.mul(order.getVolume(), sysparaService.find("exchange_apply_order_sell_fee").getDouble())); |
| | | Item item = itemService.findBySymbol(order.getSymbol()); |
| | | // 休市期间,不让下市价卖出单 |
| | | String symbol = order.getSymbol(); |
| | | if (item != null && StrUtil.isNotBlank(item.getSymbol())) { |
| | | symbol = item.getSymbol(); |
| | | order.setSymbol(symbol); |
| | | } |
| | | boolean preMarketActive = itemPreMarketService != null |
| | | && itemPreMarketService.isPreMarketTradingActive(symbol); |
| | | // 休市期间,不让下市价卖出单(盘前除外) |
| | | if (ExchangeApplyOrder.ORDER_PRICE_TYPE_OPPONENT.equals(order.getOrderPriceType())) { |
| | | boolean isOpen = MarketOpenChecker.isMarketOpenByItemCloseType(item.getOpenCloseType()); |
| | | if (!isOpen) { |
| | | if (!preMarketActive && !itemService.isOpen(symbol)) { |
| | | throw new YamiShopBindException("The current stock market is closed"); |
| | | } |
| | | } |
| | | WalletExtend walletExtend = walletService.saveExtendByPara(order.getPartyId(), order.getSymbol()); |
| | | WalletExtend walletExtend = walletService.saveExtendByPara(order.getPartyId(), symbol); |
| | | double amount_before = walletExtend.getAmount(); |
| | | walletService.updateExtend(walletExtend.getPartyId(), walletExtend.getWallettype(), Arith.sub(0, order.getVolume())); |
| | | String type = item.getType(); |
| | |
| | | moneylog.setAmountAfter(BigDecimal.valueOf(Arith.sub(amount_before, order.getVolume()))); |
| | | moneylog.setLog(name + type2 + "现货交易卖出,订单号[" + order.getOrderNo() + "]"); |
| | | moneylog.setUserId(order.getPartyId()); |
| | | moneylog.setWalletType(order.getSymbol()); |
| | | moneylog.setSymbol(order.getSymbol()); |
| | | moneylog.setWalletType(symbol); |
| | | moneylog.setSymbol(symbol); |
| | | moneylog.setContentType(type2 + Constants.MONEYLOG_CONTENT_CLOSE); |
| | | moneylog.setCreateTime(new Date()); |
| | | moneylog.setUpdateTime(new Date()); |
| | | moneyLogService.save(moneylog); |
| | | save(order); |
| | | userDataService.saveSell(order); |
| | | tryImmediatePreMarketFill(order, symbol, preMarketActive); |
| | | } |
| | | |
| | | /** 盘前时段:限价/市价均立即按盘前价成交,避免进入委托队列 */ |
| | | private void tryImmediatePreMarketFill(ExchangeApplyOrder order, String symbol, boolean preMarketActive) { |
| | | if (!preMarketActive || itemPreMarketService == null) { |
| | | return; |
| | | } |
| | | Double prePrice = itemPreMarketService.getActivePreMarketPrice(symbol); |
| | | if (prePrice == null) { |
| | | log.warn("盘前已激活但未取到盘前价 symbol={} orderNo={}", symbol, order.getOrderNo()); |
| | | return; |
| | | } |
| | | order.setPrice(prePrice); |
| | | if (ExchangeApplyOrder.OFFSET_OPEN.equals(order.getOffset())) { |
| | | self.saveSpotTradOpenCreated(order, prePrice); |
| | | } else if (ExchangeApplyOrder.OFFSET_CLOSE.equals(order.getOffset())) { |
| | | self.saveSpotTradCloseCreated(order, prePrice); |
| | | } |
| | | } |
| | | //================================================闪兑============================================================== |
| | | |