package com.yami.trading.service.ats.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.yami.trading.bean.ats.StockAts;
|
import com.yami.trading.bean.ats.dto.StockAtsDto;
|
import com.yami.trading.bean.contract.domain.ContractApplyOrder;
|
import com.yami.trading.bean.dz.ExchangeApplyOrderDz;
|
import com.yami.trading.bean.exchange.ExchangeApplyOrder;
|
import com.yami.trading.bean.item.domain.Item;
|
import com.yami.trading.bean.model.MoneyLog;
|
import com.yami.trading.bean.model.User;
|
import com.yami.trading.bean.model.Wallet;
|
import com.yami.trading.bean.model.WalletExtend;
|
import com.yami.trading.common.constants.Constants;
|
import com.yami.trading.common.domain.Result;
|
import com.yami.trading.common.exception.YamiShopBindException;
|
import com.yami.trading.common.util.Arith;
|
import com.yami.trading.common.util.RandomUtil;
|
import com.yami.trading.dao.ats.StockAtsMapper;
|
import com.yami.trading.service.MoneyLogService;
|
import com.yami.trading.service.WalletService;
|
import com.yami.trading.service.ats.StockAtsService;
|
import com.yami.trading.service.exchange.ExchangeApplyOrderService;
|
import com.yami.trading.service.item.ItemService;
|
import com.yami.trading.service.user.UserService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.Date;
|
|
@Service
|
@Slf4j
|
public class StockAtsServiceImpl extends ServiceImpl<StockAtsMapper, StockAts> implements StockAtsService {
|
|
@Autowired
|
StockAtsMapper stockAtsMapper;
|
@Autowired
|
UserService userService;
|
@Autowired
|
WalletService walletService;
|
@Autowired
|
MoneyLogService moneyLogService;
|
@Autowired
|
ExchangeApplyOrderService exchangeApplyOrderService;
|
@Autowired
|
ItemService itemService;
|
|
@Override
|
public Result getList(int pageNum, int pageSize, String partyId) {
|
try {
|
QueryWrapper<StockAts> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("user_id", partyId);
|
|
Page<StockAts> page = new Page<>(pageNum, pageSize);
|
page = stockAtsMapper.selectPage(page, queryWrapper);
|
return Result.succeed(page);
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
}
|
return Result.failed("获取失败");
|
}
|
|
@Transactional
|
@Override
|
public Result buyAts(double price, String partyId) {
|
try {
|
if (price <= 0) {
|
throw new YamiShopBindException("请输入");
|
}
|
User party = userService.getById(partyId);
|
if (!party.isEnabled()) {
|
throw new YamiShopBindException("用户已禁用");
|
}
|
|
BigDecimal amt = BigDecimal.valueOf(price);
|
|
Wallet wallet = this.walletService.saveWalletByPartyId(partyId);
|
if (wallet.getMoney().compareTo(amt) < 0) {
|
return Result.failed("余额不足");
|
}
|
|
|
StockAts order = new StockAts();
|
order.setUserId(partyId);
|
order.setPrice(amt);
|
order.setAddTime(new Date());
|
order.setOrderNo(com.yami.trading.common.util.DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
|
stockAtsMapper.insert(order);
|
|
return Result.succeed("提交成功");
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
return Result.failed("失败");
|
}
|
}
|
|
@Override
|
public Result<Page<StockAtsDto>> getListByAdmin(int pageNum, int pageSize, String keywords) {
|
try {
|
Page<StockAtsDto> page = new Page<>(pageNum, pageSize);
|
stockAtsMapper.getListByAdmin(page, keywords);
|
return Result.succeed(page);
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
}
|
return Result.failed("获取失败");
|
}
|
|
@Transactional
|
@Override
|
public Result atsCheck(String id, Integer checkType, String stockCode, double closePrice, double price) {
|
try {
|
StockAts order = stockAtsMapper.selectById(id);
|
if (order == null) {
|
throw new YamiShopBindException("订单不存在");
|
}
|
if (!order.getState().equals(ExchangeApplyOrderDz.STATE_SUBMITTED)) {
|
throw new YamiShopBindException("订单已审核");
|
}
|
//1.通过 2.拒绝
|
if (checkType == 2) {
|
order.setState(ExchangeApplyOrderDz.STATE_FAILED);
|
stockAtsMapper.updateById(order);
|
return Result.succeed("操作成功");
|
}
|
if (price <= 0 || closePrice <= 0) {
|
throw new YamiShopBindException("输入正确金额");
|
}
|
|
|
Wallet wallet = this.walletService.saveWalletByPartyId(order.getUserId());
|
BigDecimal buyAmt = BigDecimal.valueOf(price);
|
BigDecimal closeAmt = BigDecimal.valueOf(closePrice);
|
BigDecimal orderFree = BigDecimal.ZERO;;
|
if (buyAmt.doubleValue() < 300) {
|
orderFree = BigDecimal.ONE;
|
}
|
BigDecimal orderAmt = buyAmt.add(orderFree);
|
if (wallet.getMoney().compareTo(orderAmt) < 0) {
|
throw new YamiShopBindException("订单失败,用户资金不足");
|
}
|
|
Item item = itemService.findBySymbol(stockCode);
|
if (item == null) {
|
throw new YamiShopBindException("股票代码不存在");
|
}
|
if (!item.getType().equalsIgnoreCase(Item.US_STOCKS)) {
|
throw new YamiShopBindException("请输入美股代码");
|
}
|
|
BigDecimal amountBefore = wallet.getMoney();
|
|
order.setStockCode(item.getSymbol());
|
order.setStockName(item.getName());
|
order.setStockSpell(item.getSymbolData());
|
order.setPrice(buyAmt);
|
order.setFee(orderFree.doubleValue());
|
order.setState(ExchangeApplyOrderDz.STATE_POSITION);
|
order.setBuyTime(new Date());
|
order.setClosePrice(closeAmt);
|
stockAtsMapper.updateById(order);
|
walletService.update(wallet.getUserId(), Arith.sub(0, orderAmt.doubleValue()));
|
|
//TODO 转持仓
|
intoPosition(order);
|
|
MoneyLog log = new MoneyLog();
|
log.setCategory(Constants.MONEYLOG_CATEGORY_EXCHANGE);
|
String name = Constants.MONEYLOG_MAP.get(Item.US_STOCKS);
|
String type2 = Constants.MONEYLOG_MAP_TYPE.get(Item.US_STOCKS);
|
log.setAmountBefore(amountBefore);
|
log.setAmount(orderAmt.negate());
|
log.setAmountAfter(amountBefore.subtract(orderAmt));
|
log.setLog(name + type2 + "ATS买入成交" + ",订单号[" + order.getOrderNo() + "]");
|
log.setUserId(order.getUserId());
|
log.setWalletType(Constants.WALLET);
|
log.setSymbol(order.getStockCode());
|
log.setContentType(type2 + Constants.MONEYLOG_CONTENT_OPEN);
|
log.setCreateTime(new Date());
|
log.setUpdateTime(new Date());
|
moneyLogService.save(log);
|
return Result.succeed("审核成功,订单已转持仓");
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
return Result.failed("操作失败:" + e.getMessage());
|
}
|
}
|
|
public void intoPosition(StockAts order) {
|
//到账币种数量
|
BigDecimal buyNum = order.getPrice().divide(order.getClosePrice(), 5, RoundingMode.HALF_UP);
|
|
ExchangeApplyOrder exOrder = new ExchangeApplyOrder();
|
exOrder.setPartyId(order.getUserId());
|
exOrder.setSymbol(order.getStockCode());
|
exOrder.setOffset(ExchangeApplyOrder.OFFSET_OPEN);
|
exOrder.setSymbolValue(buyNum.doubleValue());
|
exOrder.setVolume(order.getPrice().doubleValue());
|
exOrder.setPrice(order.getClosePrice().doubleValue());
|
exOrder.setOrderPriceType(ContractApplyOrder.ORDER_PRICE_TYPE_OPPONENT);
|
exOrder.setState(ExchangeApplyOrder.STATE_CREATED);
|
exOrder.setClosePrice(order.getClosePrice().doubleValue());
|
exOrder.setOrderNo(com.yami.trading.common.util.DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
|
exOrder.setFee(order.getFee());
|
exOrder.setCreateTime(new Date());
|
|
double realValue = exOrder.getSymbolValue();
|
//入账
|
WalletExtend walletExtend = walletService.saveExtendByPara(exOrder.getPartyId(), exOrder.getSymbol());
|
double amountBeforeExtend = walletExtend.getAmount();
|
this.walletService.updateExtend(walletExtend.getPartyId(), walletExtend.getWallettype(), realValue);
|
|
MoneyLog log = new MoneyLog();
|
log.setCategory(Constants.MONEYLOG_CATEGORY_EXCHANGE);
|
String name = Constants.MONEYLOG_MAP.get(Item.US_STOCKS);
|
String type2 = Constants.MONEYLOG_MAP_TYPE.get(Item.US_STOCKS);
|
log.setAmountBefore(new BigDecimal(amountBeforeExtend));
|
log.setAmount(BigDecimal.valueOf(realValue));
|
log.setAmountAfter(BigDecimal.valueOf(amountBeforeExtend + realValue));
|
log.setLog(name + type2 + "ATS买入委托单成交,订单号[" + exOrder.getOrderNo() + "]");
|
log.setUserId(exOrder.getPartyId());
|
log.setSymbol(exOrder.getSymbol());
|
log.setWalletType(exOrder.getSymbol());
|
log.setContentType(type2 + Constants.MONEYLOG_CONTENT_OPEN);
|
// 记录账变日志
|
moneyLogService.save(log);
|
|
exchangeApplyOrderService.save(exOrder);
|
}
|
|
}
|