package com.nq.service.impl;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageInfo;
|
import com.nq.common.ServerResponse;
|
import com.nq.dao.*;
|
import com.nq.enums.EConfigKey;
|
import com.nq.enums.EUserAssets;
|
import com.nq.pojo.*;
|
import com.nq.service.*;
|
import com.nq.utils.KeyUtils;
|
import com.nq.utils.stock.GeneratePosition;
|
import com.nq.utils.timeutil.DateTimeUtil;
|
import com.nq.utils.PropertiesUtil;
|
import com.nq.utils.redis.JsonUtil;
|
import com.nq.utils.redis.RedisShardedPoolUtils;
|
import com.nq.utils.stock.sina.StockApi;
|
import com.nq.vo.foreigncurrency.ExchangeVO;
|
import com.nq.vo.position.UserPendingorderVO;
|
import com.nq.vo.stock.MarketVO;
|
import com.nq.vo.stock.StockListVO;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
/**
|
* @author Administrator
|
* @description 针对表【user_pendingorder】的数据库操作Service实现
|
* @createDate 2022-11-10 06:10:40
|
*/
|
@Service
|
@Slf4j
|
public class UserPendingorderServiceImpl extends ServiceImpl<UserPendingorderMapper, UserPendingorder>
|
implements UserPendingorderService {
|
@Autowired
|
private UserPendingorderMapper userPendingorderMapper;
|
@Autowired
|
private StockMapper stockMapper;
|
@Autowired
|
private IUserService iUserService;
|
@Autowired
|
private IUserAssetsServices iUserAssetsServices;
|
@Autowired
|
ISiteProductService iSiteProductService;
|
@Autowired
|
IStockConfigServices iStockConfigServices;
|
@Autowired
|
IPriceServices priceServices;
|
@Autowired
|
ITradingHourService tradingHourService;
|
@Override
|
public ServerResponse addOrder(Integer stockId, Integer buyNum, Integer buyType,
|
Integer lever, BigDecimal profitTarget,
|
BigDecimal stopTarget,String targetPrice, HttpServletRequest request) {
|
SiteProduct siteProduct = iSiteProductService.getProductSetting();
|
|
User user = this.iUserService.getCurrentRefreshUser(request);
|
synchronized (user.getId()){
|
if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
|
return ServerResponse.createByErrorMsg("订单失败,请先实名认证", request);
|
}
|
// 手续费率
|
BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
|
|
if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
|
return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定", request);
|
}
|
|
Stock stock = stockMapper.selectByPrimaryKey(stockId);
|
if (stock == null) {
|
return ServerResponse.createByErrorMsg("订单失败,股票代码不存在", request);
|
}
|
//判断股票是否在可交易时间段
|
Boolean b = tradingHourService.timeCheck(stock.getStockCode());
|
if (!b) {
|
return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
|
}
|
|
|
StockConfig mainBuyConfig = iStockConfigServices.queryByKey(EConfigKey.MIN_BUY.getCode());
|
|
if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
|
return ServerResponse.createByErrorMsg("最低购买数量"+mainBuyConfig.getCValue(), request);
|
}
|
|
UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
|
StockConfig maxBuyConfig = iStockConfigServices.queryByKey(EConfigKey.MAX_BUY.getCode());
|
if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
|
return ServerResponse.createByErrorMsg("最高购买数量"+maxBuyConfig.getCValue(), request);
|
}
|
if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
|
return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
|
}
|
if (stock.getIsLock() != 0) {
|
return ServerResponse.createByErrorMsg("订单失败,股票被锁定", request);
|
}
|
|
if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
|
return ServerResponse.createByErrorMsg("暂无配额", request);
|
}
|
|
//股票类型 现价 数据源的处理
|
BigDecimal nowPrice = new BigDecimal(targetPrice);
|
|
if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
|
return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
|
}
|
|
BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
|
BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
|
|
BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
|
BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
|
if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
|
return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
|
}
|
UserPendingorder userPendingorder = new UserPendingorder();
|
if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
|
userPendingorder.setProfitTargetPrice(profitTarget);
|
}
|
if (stopTarget != null && stopTarget.compareTo(new BigDecimal("0")) > 0) {
|
userPendingorder.setStopTargetPrice(stopTarget);
|
}
|
userPendingorder.setPositionType(1);
|
userPendingorder.setHangingOrderType(1);
|
userPendingorder.setStockType(stock.getStockType());
|
userPendingorder.setPositionSn(KeyUtils.getUniqueKey());
|
userPendingorder.setUserId(user.getId());
|
userPendingorder.setNickName(user.getRealName());
|
userPendingorder.setAgentId(user.getAgentId());
|
userPendingorder.setStockCode(stock.getStockCode());
|
userPendingorder.setStockName(stock.getStockName());
|
userPendingorder.setStockGid(stock.getStockType());
|
userPendingorder.setStockSpell(stock.getStockSpell());
|
userPendingorder.setBuyOrderId(GeneratePosition.getPositionId());
|
userPendingorder.setBuyOrderTime(new Date());
|
userPendingorder.setBuyOrderPrice(nowPrice);
|
userPendingorder.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
|
userPendingorder.setOrderNum(buyNum);
|
if (stock.getStockPlate() != null) {
|
userPendingorder.setStockPlate(stock.getStockPlate());
|
}
|
userPendingorder.setIsLock(Integer.valueOf(0));
|
userPendingorder.setOrderLever(lever);
|
userPendingorder.setOrderTotalPrice(buyAmt);
|
// 手续费
|
|
userPendingorder.setOrderFee(orderFree);
|
userPendingorder.setOrderSpread(BigDecimal.ZERO);
|
userPendingorder.setSpreadRatePrice(BigDecimal.ZERO);
|
BigDecimal profit_and_lose = new BigDecimal("0");
|
userPendingorder.setProfitAndLose(profit_and_lose);
|
userPendingorder.setAllProfitAndLose(profit_and_lose.add(orderFree));
|
userPendingorder.setOrderStayDays(Integer.valueOf(0));
|
userPendingorder.setOrderStayFee(BigDecimal.ZERO);
|
userPendingorderMapper.insert(userPendingorder);
|
iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
|
return ServerResponse.createBySuccessMsg("挂单成功", request);
|
}
|
}
|
|
@Override
|
public ServerResponse hjyyAddOrder(String name, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopLoss, String targetPrice, HttpServletRequest request) {
|
SiteProduct siteProduct = iSiteProductService.getProductSetting();
|
|
User user = this.iUserService.getCurrentRefreshUser(request);
|
synchronized (user.getId()){
|
if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
|
return ServerResponse.createByErrorMsg("订单失败,请先实名认证", request);
|
}
|
// 手续费率
|
BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
|
|
if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
|
return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定", request);
|
}
|
|
BigDecimal price = new BigDecimal(targetPrice);
|
|
UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("USD", user.getId());
|
|
if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
|
return ServerResponse.createByErrorMsg("挂单失败,请先缴清待补资金", request);
|
}
|
|
if (price.compareTo(new BigDecimal("0")) == 0) {
|
return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
|
}
|
|
BigDecimal buyAmt = price.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
|
BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
|
|
BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
|
BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
|
if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
|
return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
|
}
|
UserPendingorder userPendingorder = new UserPendingorder();
|
userPendingorder.setPositionType(1);
|
userPendingorder.setHangingOrderType(1);
|
userPendingorder.setStockType("HJYY");
|
userPendingorder.setPositionSn(KeyUtils.getUniqueKey());
|
userPendingorder.setUserId(user.getId());
|
userPendingorder.setNickName(user.getRealName());
|
userPendingorder.setAgentId(user.getAgentId());
|
userPendingorder.setStockCode("HJYY");
|
userPendingorder.setStockName(name);
|
userPendingorder.setStockGid("HJYY");
|
userPendingorder.setStockSpell(name);
|
userPendingorder.setBuyOrderId(GeneratePosition.getPositionId());
|
userPendingorder.setBuyOrderTime(new Date());
|
userPendingorder.setBuyOrderPrice(price);
|
userPendingorder.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
|
userPendingorder.setOrderNum(buyNum);
|
userPendingorder.setIsLock(Integer.valueOf(0));
|
userPendingorder.setOrderLever(lever);
|
userPendingorder.setOrderTotalPrice(buyAmt);
|
// 手续费
|
userPendingorder.setOrderFee(orderFree);
|
userPendingorder.setOrderSpread(BigDecimal.ZERO);
|
userPendingorder.setSpreadRatePrice(BigDecimal.ZERO);
|
BigDecimal profit_and_lose = new BigDecimal("0");
|
userPendingorder.setProfitAndLose(profit_and_lose);
|
userPendingorder.setAllProfitAndLose(profit_and_lose.add(orderFree));
|
userPendingorder.setOrderStayDays(Integer.valueOf(0));
|
userPendingorder.setOrderStayFee(BigDecimal.ZERO);
|
userPendingorderMapper.insert(userPendingorder);
|
iUserAssetsServices.availablebalanceChange("USD", user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
|
return ServerResponse.createBySuccessMsg("下单成功", request);
|
}
|
}
|
|
}
|