1
zj
2025-04-30 4bb0b890438349a7cfd7ab2dc30999346a5acf58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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);
        }
    }
 
}