1
zj
2024-09-04 8a3f4a7f9df7d215bfa6048f01fe1d1d28e7d0dc
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,36 @@
    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);
        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 +112,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 +135,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 +241,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 +313,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 +353,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 +451,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("操作失败");
    }
}