From 5f97f550b3fb60ac2142d7ca3c78d6bd04f18b80 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Fri, 11 Oct 2024 17:08:57 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java |  153 +++++++++++++++++++++++++++++++++++----------------
 1 files changed, 105 insertions(+), 48 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java b/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
index f5637cb..347b70d 100644
--- a/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
@@ -1,10 +1,12 @@
 package com.nq.service.impl;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 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;
@@ -12,6 +14,7 @@
 import com.github.pagehelper.PageInfo;
 import com.nq.common.ServerResponse;
 import com.nq.dao.*;
+import com.nq.enums.EStockType;
 import com.nq.pojo.*;
 import com.nq.service.*;
 import com.nq.utils.timeutil.DateTimeUtil;
@@ -65,28 +68,44 @@
     private ISiteSettingService iSiteSettingService;
     @Autowired
     private UserPositionMapper userPositionMapper;
+    @Autowired
+    UserAssetsMapper userAssetsMapper;
 
     @Override
     public ServerResponse addOrder(String stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, BigDecimal targetPrice, HttpServletRequest request) {
         User user = this.iUserService.getCurrentRefreshUser(request);
 
         if (user == null) {
-            return ServerResponse.createByErrorMsg("Please log in first");
+            return ServerResponse.createByErrorMsg("Please log in first",request);
         }
         SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
         if (buyNum.intValue() < siteSetting.getBuyMinNum().intValue()) {
             return ServerResponse.createByErrorMsg("The pending order failed, and the purchased quantity was less than" + siteSetting
-                    .getBuyMinNum() + "stocks");
+                    .getBuyMinNum() + "stocks",request);
         }
         if (buyNum.intValue() > siteSetting.getBuyMaxNum().intValue()) {
             return ServerResponse.createByErrorMsg("The pending order failed because the purchased quantity was greater than" + siteSetting
-                    .getBuyMaxNum() + "stocks");
-        }
-        UserPendingorder userPendingorder = userPendingorderMapper.selectOne(new QueryWrapper<UserPendingorder>().eq("user_id", user.getId()).eq("stock_id", stockId).eq("status", 0));
-        if (userPendingorder != null) {
-            return ServerResponse.createByErrorMsg("Please do not repeat the order");
+                    .getBuyMaxNum() + "stocks",request);
         }
 
+        UserAssets userAssets = userAssetsMapper.selectOne(new LambdaQueryWrapper<UserAssets>()
+                .eq(UserAssets::getUserId, user.getId())
+                .eq(UserAssets::getAccectType, "JP")
+        );
+
+        BigDecimal amount = new BigDecimal(buyNum).multiply(targetPrice).setScale(5, RoundingMode.DOWN);
+        if (userAssets.getAvailableBalance().compareTo(amount) < 0) {
+            return ServerResponse.createByErrorMsg("订单失败,余额不足", request);
+        }
+        if(buyNum<100){
+            return ServerResponse.createByErrorMsg("最低购买数量"+siteSetting.getBuyMinNum(), request);
+        }
+
+        userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount.negate()));
+        userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount));
+        userAssetsMapper.updateById(userAssets);
+
+        UserPendingorder userPendingorder = new UserPendingorder();
         userPendingorder = new UserPendingorder();
         userPendingorder.setUserId(user.getId());
         userPendingorder.setStockId(stockId);
@@ -101,9 +120,15 @@
         userPendingorder.setStatus(0);
         int ret = userPendingorderMapper.insert(userPendingorder);
         if (ret > 0) {
-            return ServerResponse.createBySuccessMsg("If the pending order is successfully added, the order will be automatically placed if the order conditions are met");
+            String lang = request.getHeader("lang");
+            if(lang.equals("ja")){
+                String msg = "注文が成功しました。取引時間内に自動的に約定されます。";
+                return ServerResponse.createBySuccessMsg(msg);
+            }else{
+                return ServerResponse.createBySuccessMsg("If the pending order is successfully added, the order will be automatically placed if the order conditions are met",request);
+            }
         }
-        return ServerResponse.createByErrorMsg("Add failure");
+        return ServerResponse.createByErrorMsg("Add failure",request);
 
     }
 
@@ -118,7 +143,7 @@
             User user = (User) JsonUtil.string2Obj(userJson, User.class);
 //            log.info("user:{}",user);
             if (user != null) {
-                List<UserPendingorder> userPendingorders = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("user_id", user.getId()));
+                List<UserPendingorder> userPendingorders = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("user_id", user.getId()).ne("status",1));
 
                 List UserPendingorderList = new ArrayList();
 
@@ -224,7 +249,7 @@
                     int ret = userPendingorder.getBuyType().intValue() == 0 ? userPendingorder.getTargetPrice().compareTo(new BigDecimal(nowPrice)) : new BigDecimal(nowPrice).compareTo(userPendingorder.getTargetPrice());
                     //当前时间String
                     String buyTime = DateTimeUtil.dateToStr(new Date());
-                    if (ret <= 0) {
+                    if (ret == 0) {
                         if (code != null && !"".equals(code)) {
                             try {
                                 this.iUserPositionService.create(userPendingorder.getUserId(), code, nowPrice, buyTime, userPendingorder.getBuyNum(), userPendingorder.getBuyType(), userPendingorder.getLever(), userPendingorder.getProfitTarget(), userPendingorder.getStopTarget());
@@ -296,26 +321,35 @@
             User user = (User) JsonUtil.string2Obj(userJson, User.class);
             UserPendingorder userPendingorder = this.userPendingorderMapper.selectById(id);
             if (userPendingorder == null) {
-                return ServerResponse.createByErrorMsg("The pending order does not exist");
+                return ServerResponse.createByErrorMsg("The pending order does not exist",request);
             }
             if (user.getId().intValue() != userPendingorder.getUserId().intValue()) {
-                return ServerResponse.createByErrorMsg("The pending order does not belong to you");
+                return ServerResponse.createByErrorMsg("The pending order does not belong to you",request);
             }
             int delCount = this.userPendingorderMapper.deleteById(id);
             if (delCount > 0) {
-                return ServerResponse.createByErrorMsg("Successfully deleted");
+                UserAssets userAssets = userAssetsMapper.selectOne(new LambdaQueryWrapper<UserAssets>()
+                        .eq(UserAssets::getUserId, user.getId())
+                        .eq(UserAssets::getAccectType, "JP")
+                );
+                BigDecimal amount = new BigDecimal(userPendingorder.getBuyNum()).multiply(userPendingorder.getTargetPrice()).setScale(5, RoundingMode.DOWN);
+                userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount));
+                userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount.negate()));
+                userAssetsMapper.updateById(userAssets);
+                return ServerResponse.createByErrorMsg("Successfully deleted",request);
             }
-            return ServerResponse.createByErrorMsg("Deletion failure");
+            return ServerResponse.createByErrorMsg("Deletion failure",request);
         }
 
-        return ServerResponse.createByErrorMsg("Please log in");
+        return ServerResponse.createByErrorMsg("Please log in",request);
     }
 
 
     @Override
-    public ServerResponse orderListByAdmin(int pageNum, int pageSize, String keywords, String status, HttpServletRequest request) {
+    public ServerResponse orderListByAdmin(int pageNum, int pageSize, String keywords, Integer status, HttpServletRequest request) {
         PageHelper.startPage(pageNum, pageSize);
         QueryWrapper<UserPendingorder> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("buy_type",0);
         if (keywords != null && !keywords.equals("")) {
             queryWrapper.like("stock_id", keywords).or().like("user_id", keywords);
         }
@@ -327,42 +361,37 @@
         List UserPendingorderList = new ArrayList();
         for (UserPendingorder userPendingorder : stockSubscribeList) {
             UserPendingorderVO userPendingorderVO = new UserPendingorderVO();
-            //挂单-指数
-            if (userPendingorder.getStockId().contains("sh") || userPendingorder.getStockId().contains("sz") || userPendingorder.getStockId().contains("hk") || userPendingorder.getStockId().contains("us")) {
-                StockIndex model = stockIndexMapper.selectIndexByCode(userPendingorder.getStockId().replace("sh", "").replace("sz", "").replace("hk", "").replace("us", ""));
-
-                MarketVO marketVO = this.iStockIndexService.querySingleIndex(model.getIndexGid());
-                userPendingorderVO.setNowPrice(new BigDecimal(marketVO.getNowPrice()));
-                userPendingorderVO.setStockName(model.getIndexName());
-                userPendingorderVO.setStockId(model.getIndexGid());
-
+            //挂单-股票
+            Stock stock = stockMapper.findStockByCode(userPendingorder.getStockId());
+            StockListVO stockListVO = new StockListVO();
+            if (stock.getStockType().equals("hk")) {
+                String hk = RedisShardedPoolUtils.get(stock.getStockGid(), 1);
+                stockListVO = StockApi.otherStockListVO(hk);
+            } else if (stock.getStockType().equals("us")) {
+                String us = RedisShardedPoolUtils.get(stock.getStockGid(), 2);
+                stockListVO = StockApi.otherStockListVO(us);
             } else {
-                //挂单-股票
-                Stock stock = stockMapper.findStockByCode(userPendingorder.getStockId());
-                StockListVO stockListVO = new StockListVO();
-                if (stock.getStockType().equals("hk")) {
-                    String hk = RedisShardedPoolUtils.get(stock.getStockGid(), 1);
-                    stockListVO = StockApi.otherStockListVO(hk);
-                } else if (stock.getStockType().equals("us")) {
-                    String us = RedisShardedPoolUtils.get(stock.getStockGid(), 2);
-                    stockListVO = StockApi.otherStockListVO(us);
-                } else {
-                    stockListVO = StockApi.getStockRealTime(
-                            stock);
-                }
-                String nowPrice = stockListVO.getNowPrice();
-                if (nowPrice == null) {
-                    nowPrice = String.valueOf(0);
-                }
-                userPendingorderVO.setNowPrice(new BigDecimal(nowPrice));
-                userPendingorderVO.setStockName(stock.getStockName());
-                userPendingorderVO.setStockId(stock.getStockCode());
+                stockListVO = StockApi.getStockRealTime(
+                        stock);
             }
+            String nowPrice = stockListVO.getNowPrice();
+            if (nowPrice == null) {
+                nowPrice = String.valueOf(0);
+            }
+            userPendingorderVO.setUserId(userPendingorder.getUserId());
+            userPendingorderVO.setNowPrice(new BigDecimal(nowPrice));
+            userPendingorderVO.setStockName(stock.getStockName());
+            userPendingorderVO.setStockId(stock.getStockCode());
             userPendingorderVO.setBuyNum(userPendingorder.getBuyNum());
             userPendingorderVO.setBuyType(userPendingorder.getBuyType());
             userPendingorderVO.setLever(userPendingorder.getLever());
-            userPendingorderVO.setProfitTarget(userPendingorder.getProfitTarget());
-            userPendingorderVO.setStopTarget(userPendingorder.getStopTarget());
+            if(null != userPendingorder.getProfitTarget()){
+                userPendingorderVO.setProfitTarget(userPendingorder.getProfitTarget());
+            }
+            if(null != userPendingorder.getStopTarget()){
+                userPendingorderVO.setStopTarget(userPendingorder.getStopTarget());
+            }
+
             userPendingorderVO.setTargetPrice(userPendingorder.getTargetPrice());
             userPendingorderVO.setAddTime(userPendingorder.getAddTime());
             userPendingorderVO.setStatus(userPendingorder.getStatus());
@@ -430,6 +459,34 @@
         }
         return ServerResponse.createByErrorMsg("删除失败");
     }
+
+    @Override
+    public ServerResponse examine(Integer id) {
+
+        try{
+            UserPendingorder userPendingorder = getById(id);
+            if(null != userPendingorder && userPendingorder.getStatus() != 0){
+                return ServerResponse.createByErrorMsg("当前状态无法操作");
+            }
+            userPendingorder.setStatus(1);
+            SiteTaskLog siteTaskLog = new SiteTaskLog();
+            siteTaskLog.setTaskType("股票挂单转持仓");
+            String tasktarget = "此次挂单买入id:" + userPendingorder.getId();
+            siteTaskLog.setTaskTarget(tasktarget);
+            siteTaskLog.setAddTime(new Date());
+            siteTaskLog.setIsSuccess(0);
+            siteTaskLog.setErrorMsg("");
+            this.userPendingorderMapper.updateById(userPendingorder);
+            this.siteTaskLogMapper.insert(siteTaskLog);
+            //当前时间String
+            String buyTime = DateTimeUtil.dateToStr(new Date());
+            this.iUserPositionService.create(userPendingorder.getUserId(), userPendingorder.getStockId(), String.valueOf(userPendingorder.getTargetPrice()), buyTime, userPendingorder.getBuyNum(), userPendingorder.getBuyType(), userPendingorder.getLever(), userPendingorder.getProfitTarget(), userPendingorder.getStopTarget());
+            return ServerResponse.createBySuccessMsg("审核成功,挂单已转持仓");
+        }catch (Exception e){
+            log.error("挂单失败");
+        }
+        return ServerResponse.createByErrorMsg("操作失败");
+    }
 }
 
 

--
Gitblit v1.9.3