package com.yami.trading.api.controller.exchange;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.exchange.ExchangeApplyOrder;
import com.yami.trading.bean.exchange.dto.ExchangeSymbolDto;
import com.yami.trading.bean.exchange.dto.SumEtfDto;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.*;
import com.yami.trading.bean.purchasing.dto.ExchangeLock;
import com.yami.trading.bean.syspara.domain.Syspara;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.*;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.CapitaltWalletService;
import com.yami.trading.service.RealNameAuthRecordService;
import com.yami.trading.service.SessionTokenService;
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.ItemService;
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.user.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
/**
* 兑换
* 币币交易 低买高卖
*/
@RestController
@CrossOrigin
@Slf4j
@Api(tags = " 币币交易 -api")
public class ApiExchangeApplyOrderController {
private final String action = "/api/exchangeapplyorder!";
@Autowired
private SessionTokenService sessionTokenService;
@Autowired
UserService userService;
@Autowired
private SysparaService sysparaService;
@Autowired
private DataService dataService;
@Autowired
private ExchangeApplyOrderService exchangeApplyOrderService;
@Autowired
WalletService walletService;
@Autowired
RealNameAuthRecordService realNameAuthRecordService;
@Autowired
ItemService itemService;
@Autowired
CapitaltWalletService capitaltWalletService;
/**
* 兑换币 如果是使用usdt兑换其他币种,则直接执行正常买币open流程 如果是其他币种--》usdt 则是直接卖币流程
* 如果是其他币种到另一个币种,则需要先卖出,然后再买入
*
* 兑换接口
*/
@RequestMapping(action + "buy_and_sell.action")
public Object buy_and_sell(HttpServletRequest request) {
String volume_temp = request.getParameter("volume");
if (StringUtils.isNullOrEmpty(volume_temp)
|| !StringUtils.isDouble(volume_temp) || Double.valueOf(volume_temp) <= 0) {
throw new YamiShopBindException("请输入正确的货币数量");
}
double volume = Double.valueOf(volume_temp);
String symbol = request.getParameter("symbol");
String symbol_to = request.getParameter("symbol_to");
if (symbol.equals(symbol_to)) {
throw new YamiShopBindException("请选择正确的币种");
}
String session_token = request.getParameter("session_token");
String partyId = SecurityUtils.getUser().getUserId();
Object object = this.sessionTokenService.cacheGet(session_token);
this.sessionTokenService.del(session_token);
if ((object == null) || (!partyId.equals((String) object))) {
log.info("sessionToken{}", object);
System.out.println("sessionToken " + object);
throw new YamiShopBindException("请稍后再试");
}
User party = userService.getById(partyId);
if (!party.isEnabled()) {
throw new YamiShopBindException("用户已禁用!");
}
symbol = symbol.toLowerCase();
symbol_to = symbol_to.toLowerCase();
String relation_order_no = UUID.randomUUID().toString();
// 如果是使用usdt兑换其他币种,则直接执行正常买币open流程
if ("usdt".equals(symbol) || "usdt".equals(symbol_to)) {
ExchangeApplyOrder order = new ExchangeApplyOrder();
order.setPartyId(partyId);
order.setVolume(volume);
order.setOrderPriceType("opponent");
order.setRelationOrderNo(relation_order_no);
if ("usdt".equals(symbol)) {
order.setSymbol(symbol_to);
double openPrice = getRealtimePrice(symbol_to);
order.setPrice(openPrice);
order.setOffset(ExchangeApplyOrder.OFFSET_OPEN);
} else if ("usdt".equals(symbol_to)) {
order.setSymbol(symbol);
double closePrice = getRealtimePrice(symbol);
order.setPrice(closePrice);
order.setOffset(ExchangeApplyOrder.OFFSET_CLOSE);
}
exchangeApplyOrderService.saveCreate(order);
} else {
// 非usdt则需要进行一次卖出
ExchangeApplyOrder order_sell = new ExchangeApplyOrder();
order_sell.setPartyId(partyId);
order_sell.setSymbol(symbol);
order_sell.setOffset(ExchangeApplyOrder.OFFSET_CLOSE);
order_sell.setVolume(volume);
order_sell.setOrderPriceType("opponent");
order_sell.setRelationOrderNo(relation_order_no);
double sellClose = getRealtimePrice(symbol);
order_sell.setPrice(sellClose);
this.exchangeApplyOrderService.saveCreate(order_sell);
double close = getRealtimePrice(symbol);
double sub = Arith.sub(volume,
Arith.mul(volume, sysparaService.find("exchange_apply_order_sell_fee").getDouble()));
double amount = Arith.mul(sub, close);
// 再买入币种
ExchangeApplyOrder order_buy = new ExchangeApplyOrder();
order_buy.setPartyId(partyId);
order_buy.setSymbol(symbol_to);
order_buy.setOffset(ExchangeApplyOrder.OFFSET_OPEN);
order_buy.setVolume(amount);
order_buy.setRelationOrderNo(relation_order_no);
order_buy.setOrderPriceType("opponent");
double buyClose = getRealtimePrice(symbol_to);
order_buy.setPrice(buyClose);
exchangeApplyOrderService.saveCreate(order_buy);
}
return Result.succeed();
}
private double getRealtimePrice(String symbol) {
List realtimes = dataService.realtime(symbol);
double close = 1;
if (realtimes != null && realtimes.size() > 0) {
close = realtimes.get(0).getClose().doubleValue();
} else {
throw new YamiShopBindException("参数错误");
}
return close;
}
/**
* 首次进入页面,传递session_token
*/
@RequestMapping(action + "view.action")
public Result view() {
String partyId = SecurityUtils.getUser().getUserId();
Map session = new HashMap();
String session_token = sessionTokenService.savePut(partyId);
session.put("session_token", session_token);
return Result.succeed(session);
}
/**
* 兑换汇率
*/
@RequestMapping(action + "buy_and_sell_fee.action")
public Object buy_and_sell_fee(HttpServletRequest request) {
// 需兑换币种
String symbol = request.getParameter("symbol");
// 兑换后的币种
String symbol_to = request.getParameter("symbol_to");
if (symbol.equals(symbol_to)) {
throw new YamiShopBindException("请选择正确的币种");
}
// 委托数量
String volume_temp = request.getParameter("volume");
if (StringUtils.isNullOrEmpty(volume_temp)
|| !StringUtils.isDouble(volume_temp) || Double.valueOf(volume_temp) < 0) {
throw new YamiShopBindException("请输入正确的兑换数量");
}
Map data = new HashMap();
symbol = symbol.toLowerCase();
symbol_to = symbol_to.toLowerCase();
double buy_fee = Double.valueOf(sysparaService.find("exchange_apply_order_buy_fee").getSvalue());
double sell_fee = Double.valueOf(sysparaService.find("exchange_apply_order_sell_fee").getSvalue());
double volume = Double.valueOf(volume_temp);
if ("usdt".equals(symbol) || "usdt".equals(symbol_to)) {
if ("usdt".equals(symbol)) {
// 如果是使用Usdt 则计算收益
List realtime_list = this.dataService.realtime(symbol_to);
Realtime realtime = null;
if (realtime_list.size() > 0) {
realtime = realtime_list.get(0);
} else {
throw new YamiShopBindException("系统错误,请稍后重试");
}
double symbol_to_price = realtime.getClose().doubleValue();
// usdt除以的数量
data.put("get_rate", Arith.div(1, symbol_to_price, 5));
// 实际兑换数量= 兑换数量-手续费数量
double fact_volume = Arith.sub(volume, Arith.mul(volume, buy_fee));
// 实际价值 = 实际兑换数量 * 被兑换品种价格
double fact_price = Arith.mul(fact_volume, 1);
// 实际获取数量 = 实际价值 / 将要兑换的品种的价值
data.put("get_volume", Arith.div(fact_price, symbol_to_price, 5));
}
if ("usdt".equals(symbol_to)) {
/**
* 如果是转成Usdt 则计算收益
*/
List realtime_list = this.dataService.realtime(symbol);
Realtime realtime = null;
if (realtime_list.size() > 0) {
realtime = realtime_list.get(0);
} else {
throw new YamiShopBindException("系统错误,请稍后重试");
}
double symbol_price = realtime.getClose().doubleValue();
// 对应usdt数量
data.put("get_rate", Arith.div(symbol_price, 1, 5));
// 实际兑换数量= 兑换数量-手续费数量
double fact_volume = Arith.sub(volume, Arith.mul(volume, sell_fee));
// 实际价值 = 实际兑换数量 * 被兑换品种价格
double fact_price = Arith.mul(Arith.div(symbol_price, 1, 5), fact_volume);
// 实际获取数量 = 实际价值 / 将要兑换的品种的价值
data.put("get_volume", Arith.div(fact_price, 1, 5));
}
} else {
double symbol_price = 0;
double symbol_to_price = 0;
// 获取币种最新价格
List realtime_list = this.dataService.realtime(symbol);
Realtime realtime = null;
if (realtime_list.size() > 0) {
realtime = realtime_list.get(0);
symbol_price = realtime.getClose().doubleValue();
} else {
throw new YamiShopBindException("系统错误,请稍后重试");
}
// 获取币种最新价格
List realtime_list_to = this.dataService.realtime(symbol_to);
Realtime realtime_to = null;
if (realtime_list_to.size() > 0) {
realtime_to = realtime_list_to.get(0);
symbol_to_price = realtime_to.getClose().doubleValue();
} else {
throw new YamiShopBindException("系统错误,请稍后重试");
}
if (symbol_to_price == 0) {
symbol_to_price = 1;
}
if (symbol_price == 0) {
symbol_price = 1;
}
data.put("get_rate", Arith.div(symbol_price, symbol_to_price, 5));
// 总手续费比例
double all_fee = Arith.add(buy_fee, sell_fee);
// 实际兑换数量= 兑换数量-手续费数量
double fact_volume = Arith.sub(volume, Arith.mul(volume, all_fee));
// 实际价值 = 实际兑换数量 * 被兑换品种价格
double fact_price = Arith.mul(fact_volume, symbol_price);
// 实际获取数量 = 实际价值 / 将要兑换的品种的价值
data.put("get_volume", Arith.div(fact_price, symbol_to_price, 5));
}
return Result.succeed(data);
}
/**
* 兑换记录
* 委托单记录
*/
@RequestMapping(action + "list.action")
public Result list(HttpServletRequest request) {
List