1
zj
2025-07-31 071c1c1f0b58ad266bd1cb016f9daf94c0b6367f
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.nq.controller.backend;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.EStockType;
import com.nq.enums.EUserAssets;
import com.nq.pojo.*;
import com.nq.service.*;
import com.nq.service.impl.IntradayOrderSerivceImpl;
import com.nq.service.impl.SmartIntradaySerivceImpl;
import com.nq.service.impl.UserPositionServiceImpl;
import com.nq.utils.ConverterUtil;
import com.nq.utils.KeyUtils;
import com.nq.utils.stock.GeneratePosition;
import com.nq.vo.smart.IntradayOrderVo;
import com.nq.vo.smart.SmartIntradayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
/**
 * @program: dabaogp
 * @description: 智能日内
 * @create: 2025-07-15 10:20
 **/
@Controller
@RequestMapping({"/admin/smart/"})
public class SmartIntradayController {
 
    @Autowired
    private SmartIntradaySerivceImpl smartIntradaySerivce;
 
    @Autowired
    private IntradayOrderSerivceImpl intradayOrderSerivce;
 
    @Autowired
    private IntradayOrderMapper intradayOrderMapper;
 
    @Autowired
    private UserMapper userMapper;
 
    @Autowired
    private IUserAssetsServices iUserAssetsServices;
 
    @Autowired
    private UserAssetsMapper userAssetsMapper;
 
    @Autowired
    private StockMapper stockMapper;
 
    @Autowired
    private UserPositionMapper userPositionMapper;
    @Autowired
    IStockConfigServices iStockConfigServices;
 
    /**
     * 添加智能日内
     * @param vo
     * @return
     */
    @RequestMapping(value = {"addSmart.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse addSmart(@Valid SmartIntradayVo vo) {
        long count = smartIntradaySerivce.count(new LambdaQueryWrapper<>(SmartIntraday.class)
                .eq(SmartIntraday::getDepositName, vo.getDepositName())
                .eq(SmartIntraday::getAccountType, vo.getAccountType())
                .eq(SmartIntraday::getStockType, vo.getStockType())
        );
        if(count > 0){
            return ServerResponse.createByErrorMsg("已存在同名产品");
        }
        SmartIntraday smartIntraday = ConverterUtil.convert(vo, SmartIntraday.class);
 
        smartIntraday.setStockType(EStockType.JP.getCode());
        smartIntraday.setAccountType(EStockType.JP.getCode());
        smartIntradaySerivce.save(smartIntraday);
        return ServerResponse.createBySuccessMsg("添加成功");
    }
 
    /**
     * 修改智能日内
     * @param vo
     * @return
     */
    @RequestMapping(value = {"updateSmart.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse updateSmart(@Valid SmartIntradayVo vo) {
        long count = smartIntradaySerivce.count(new LambdaQueryWrapper<>(SmartIntraday.class)
                .eq(SmartIntraday::getDepositName, vo.getDepositName())
                .eq(SmartIntraday::getAccountType, vo.getAccountType())
                .eq(SmartIntraday::getStockType, vo.getStockType())
                .ne(SmartIntraday::getId,vo.getId())
        );
        if(count > 0){
            return ServerResponse.createByErrorMsg("已存在同名产品");
        }
        SmartIntraday smartIntraday = ConverterUtil.convert(vo, SmartIntraday.class);
        smartIntradaySerivce.updateById(smartIntraday);
        return ServerResponse.createBySuccessMsg("修改成功");
    }
 
    /**
     * 智能日内列表
     */
    @RequestMapping(value = {"listSmart.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse listSmart(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "15") int pageSize,
                                    @RequestParam(value = "status" , required = false) Integer status) {
        // 创建分页对象
       Page<SmartIntraday> page = Page.of(pageNum, pageSize);
        // 创建查询条件
        LambdaQueryWrapper<SmartIntraday> wrapper = new LambdaQueryWrapper<>();
 
        // 状态查询
        if (status != null) {
            wrapper.eq(SmartIntraday::getStatus, status);
        }
        // 按创建时间倒序
        wrapper.orderByDesc(SmartIntraday::getCreatedAt);
        // 执行分页查询
        Page<SmartIntraday> intradayPage = smartIntradaySerivce.page(page, wrapper);
        return ServerResponse.createBySuccess(intradayPage);
    }
 
    /**
     * 删除智能日内
     * @return
     */
    @RequestMapping(value = {"deleteSmart.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse deleteSmart( @RequestParam("id") Integer id) {
        smartIntradaySerivce.removeById(id);
        return ServerResponse.createBySuccessMsg("删除成功");
    }
 
 
 
    /**
     * 审核日内订单
     * @param vo
     * @return
     */
    @RequestMapping(value = {"updateIntraday.do"}, method = {RequestMethod.POST})
    @ResponseBody
    @Transactional(rollbackFor = Exception.class) // 异常时回滚
    public ServerResponse updateIntraday(@Valid IntradayOrderVo vo) {
        IntradayOrder intradayOrder = intradayOrderSerivce.getById(vo.getId());
        if(intradayOrder.getAuditStatus() != 0){
            return ServerResponse.createByErrorMsg("订单已操作");
        }
        Stock stock = stockMapper.findStockByCode(vo.getStockCode());
        if(ObjectUtil.isEmpty(stock)){
            return ServerResponse.createByErrorMsg("选择的股票不存在!");
        }
 
 
        intradayOrder.setStockType(vo.getStockType());
        intradayOrder.setStockCode(vo.getStockCode());
        intradayOrder.setStockName(stock.getStockName());
        intradayOrder.setStockGid(stock.getStockGid());
        intradayOrder.setStockQuantity(vo.getStockQuantity());
        intradayOrder.setPriceType(vo.getPriceType());
        intradayOrder.setStockPrice(new BigDecimal(vo.getStockPrice()));
        intradayOrder.setAuditStatus(vo.getAuditStatus());
        intradayOrder.setOrderTime(DateUtil.parse(vo.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
        intradayOrder.setIsLocked(vo.getIsLocked());
        intradayOrder.setStockId(stock.getId());
 
        // 手续费率
        BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
 
        if(vo.getAuditStatus() == 2){
            UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.JP.getCode(), intradayOrder.getUserId());
            BigDecimal orderAmount = vo.getOrderAmount();
            userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(orderAmount));
            userAssetsMapper.updateById(userAssets);
            return ServerResponse.createBySuccessMsg("操作成功");
        }
        BigDecimal stockQuantity = new BigDecimal(intradayOrder.getStockQuantity());//股票数量
        BigDecimal orderTotalPrice = intradayOrder.getStockPrice().multiply(stockQuantity);//订单总价
 
        //手续费
        BigDecimal orderFree = siteSettingBuyFee.multiply(orderTotalPrice);
        BigDecimal totalPrice = orderFree.add(orderTotalPrice);
 
        if(intradayOrder.getOrderAmount().compareTo(totalPrice) < 0){
            return ServerResponse.createByErrorMsg("股票总价超过订单金额!");
        }
 
 
        User user = userMapper.selectById(intradayOrder.getUserId());
 
        //结余资金返还账户
        BigDecimal surplusAmount = intradayOrder.getOrderAmount().subtract(totalPrice);//结余
        UserPosition userPosition = new UserPosition();
        userPosition.setProfitTargetPrice(BigDecimal.ZERO);
        userPosition.setStopTargetPrice(BigDecimal.ZERO);
        userPosition.setPositionType(user.getAccountType());
        userPosition.setPositionSn(KeyUtils.getUniqueKey());
        userPosition.setUserId(intradayOrder.getUserId());
        userPosition.setNickName(user.getRealName());
        userPosition.setAgentId(user.getAgentId());
        userPosition.setStockCode(stock.getStockCode());
        userPosition.setStockName(stock.getStockName());
        userPosition.setStockGid(stock.getStockType());
        userPosition.setStockSpell(stock.getStockSpell());
        userPosition.setBuyOrderId(GeneratePosition.getPositionId());
        userPosition.setBuyOrderTime(new Date());
        userPosition.setBuyOrderPrice(intradayOrder.getStockPrice());
        userPosition.setOrderDirection((intradayOrder.getPriceType() == 1) ? "买涨" : "买跌");
        userPosition.setOrderNum(intradayOrder.getStockQuantity());
        if (stock.getStockPlate() != null) {
            userPosition.setStockPlate(stock.getStockPlate());
        }
        userPosition.setIsLock(intradayOrder.getIsLocked());
        userPosition.setOrderLever(1);
        userPosition.setOrderTotalPrice(orderTotalPrice);
        // 手续费
        userPosition.setOrderFee(orderFree);
        userPosition.setOrderSpread(BigDecimal.ZERO);
        userPosition.setSpreadRatePrice(BigDecimal.ZERO);
        BigDecimal profit_and_lose = new BigDecimal("0");
        userPosition.setProfitAndLose(profit_and_lose);
        userPosition.setAllProfitAndLose(profit_and_lose.add(BigDecimal.ZERO));
        userPosition.setOrderStayDays(Integer.valueOf(0));
        userPosition.setOrderStayFee(BigDecimal.ZERO);
        userPositionMapper.insert(userPosition);
 
        UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.JP.getCode(), intradayOrder.getUserId());
        userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(surplusAmount));
        userAssets.setFreezeMoney(userPosition.getOrderTotalPrice());
        userAssetsMapper.updateById(userAssets);
        intradayOrder.setPositionOrder(userPosition.getId());
        intradayOrder.setPurchaseAmount(totalPrice);
        intradayOrderMapper.updateById(intradayOrder);
        return ServerResponse.createBySuccessMsg("操作成功");
    }
 
    /**
     * 智能日内订单列表
     */
    @RequestMapping(value = {"listIntraday.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse listIntraday(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "15") int pageSize,
                                       @RequestParam(value = "userId" , required = false) Integer userId,
                                       @RequestParam(value = "phone" , required = false) String phone,
                                       @RequestParam(value = "stockName" , required = false) String stockName,
                                       @RequestParam(value = "stockGid" , required = false) String stockGid,
                                       @RequestParam(value = "status" , required = false) Integer status
 
    ) {
        // 执行分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<IntradayOrder> intradayOrderList = intradayOrderMapper.conditionQuery(userId,phone,stockName,stockGid,status);
        PageInfo<IntradayOrder> pageInfo = new PageInfo<>(intradayOrderList);
        return ServerResponse.createBySuccess(pageInfo);
    }
 
    /**
     * 删除日内订单
     * @return
     */
    @RequestMapping(value = {"deleteIntraday.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse deleteIntraday( @RequestParam("id") Integer id) {
        intradayOrderSerivce.removeById(id);
        return ServerResponse.createBySuccessMsg("删除成功");
    }
 
    /**
     * 根据条件查询所有股票
     */
    @RequestMapping(value = {"getStockByKeyWords.do"}, method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse updateIntraday(@RequestParam("keyWords") String keyWords) {
        Stock stock = stockMapper.selectOne(new LambdaQueryWrapper<>(Stock.class)
                .eq(Stock::getStockCode, keyWords).last(" limit 1"));
        if(ObjectUtil.isEmpty(stock)){
            return ServerResponse.createBySuccessMsg("股票不存在");
        }
        return ServerResponse.createBySuccess(stock);
    }
 
}