zyy
2025-07-21 506edadd3a3f3f458e32807f0b7166d16200a076
src/main/java/com/nq/service/impl/StockAiServiceImpl.java
@@ -3,7 +3,6 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.Repository.ExchangeRateRepository;
import com.nq.common.ResponseCode;
import com.nq.common.ServerResponse;
import com.nq.dao.StockAiMapper;
@@ -12,6 +11,7 @@
import com.nq.enums.*;
import com.nq.pojo.*;
import com.nq.service.*;
import com.nq.vo.stock.ai.StockAIOrderPositionVO;
import com.nq.vo.stock.ai.StockAiOrderTypeVO;
import com.nq.vo.stock.ai.StockAiOrderVO;
import com.nq.vo.stock.ai.StockAiVO;
@@ -44,6 +44,8 @@
    IUserService iUserService;
    @Autowired
    IUserAssetsServices iUserAssetsServices;
    @Autowired
    private UserAssetsServices userAssetsServices;
    /**
     * 获取上架ai产品
@@ -103,19 +105,11 @@
                }
                //获取用户账户
                UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.MX.getCode(), user.getId());
                /*BigDecimal finalBuyNum = buyNum;    //购买金额
                BigDecimal finalBuyNum = buyNum;
                //如果不是墨西哥币需要转换金额
                if (!stockAI.getStockType().equals(EStockType.MX.getCode())) {
                    EStockType stockType = EStockType.getEStockTypeByCode(stockAI.getStockType());
                    ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency(stockType.getSymbol(), EStockType.MX.getSymbol())
                            .orElse(null);
                    if (exchangeRate == null) {
                        return ServerResponse.createByErrorMsg("当前货币汇率无法转换");
                    }
                    //转换为墨西哥币
                    buyNum = iUserAssetsServices.exchangeAmountByRate(buyNum, exchangeRate.getRata());
                }*/
                    buyNum = userAssetsServices.exchangeAmountByRate(stockAI.getStockType(), buyNum);
                }
                if(buyNum.compareTo(userAssets.getAvailableBalance()) > 0){
                    return ServerResponse.createByErrorMsg("可用余额不足" + userAssets.getAvailableBalance(), request);
                }
@@ -123,13 +117,14 @@
                if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                    return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
                }
                iUserAssetsServices.aiAvailableBalanceChange(stockAI.getStockType(), user.getId(), EUserAssets.BUY_AI, buyNum);
                //已经转化 直接穿MEX类型
                iUserAssetsServices.aiAvailableBalanceChange(EStockType.MX.getCode(), user.getId(), EUserAssets.BUY_AI, buyNum);
                StockAIOrder stockAIOrder = new StockAIOrder();
                stockAIOrder.setUserId(user.getId());
                stockAIOrder.setStockAiId(id);
                stockAIOrder.setStockAiId(id.intValue());
                stockAIOrder.setBuyDate(new Date());
                stockAIOrder.setBuyAmount(buyNum);
                stockAIOrder.setRemainAmount(buyNum);
                stockAIOrder.setBuyAmount(finalBuyNum);
                stockAIOrder.setRemainAmount(finalBuyNum);
                stockAIOrder.setRealEarning(BigDecimal.valueOf(0));
                stockAIOrder.setStatus(EStockAIOrderStatus.wait.getStatus());   //等待审核
                stockAiOrderMapper.insert(stockAIOrder);
@@ -292,6 +287,7 @@
                BigDecimal earnings = model.getCoverPrice().subtract(model.getStockPrice()).multiply(BigDecimal.valueOf(model.getStockNum()));
                model.setCreatDate(new Date());
                model.setEarnings(earnings);
                model.setStatus(0);
                stockAIOrder.setRealEarning(stockAIOrder.getRealEarning().add(earnings));
                //保存
@@ -335,6 +331,7 @@
                    }
                    //结算 TODO结算建仓 计算收益
                    /*//所有建仓
                    List<StockAIOrderPosition> stockAIOrderPositionList = stockAiOrderPositionMapper
                            .selectList(new QueryWrapper<StockAIOrderPosition>()
                                    .eq("stock_ai_order_id", id));
@@ -349,6 +346,8 @@
                        amount = amount.add(earningsSUM);
                    }*/
                    StockAI stockAI = stockAiMapper.selectById(stockAIOrder.getStockAiId());
                    //结算修改为平仓状态
                    stockAiOrderPositionMapper.updateStatusByOrderId(stockAIOrder.getId(), 1);
                    //归还本金
                    iUserAssetsServices.aiAvailableBalanceChange(stockAI.getStockType(), stockAIOrder.getUserId(), EUserAssets.AI_SETTLEMENT, stockAIOrder.getBuyAmount());
                    //收益
@@ -360,7 +359,27 @@
                return ServerResponse.createBySuccess("操作成功");
            }
        } catch (Exception e) {
            log.error("StockAiService openPosition error", e);
            log.error("StockAiService orderOperation error", e);
        }
        return ServerResponse.createByError();
    }
    /**
     * 建仓列表
     * @param pageNum
     * @param pageSize
     * @param stockAiOrderId
     * @return
     */
    @Override
    public ServerResponse getPositionList(Integer pageNum, Integer pageSize, Integer stockAiOrderId) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<StockAIOrderPositionVO> positionVOList = stockAiOrderPositionMapper.getPositionListByOrderId(stockAiOrderId);
            PageInfo<StockAIOrderPositionVO> pageInfo = new PageInfo<>(positionVOList);
            return ServerResponse.createBySuccess(pageInfo);
        } catch (Exception e) {
            log.error("StockAiService getPositionList error", e);
        }
        return ServerResponse.createByError();
    }