| | |
| | | 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; |
| | |
| | | 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; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | IUserService iUserService; |
| | | @Autowired |
| | | IUserAssetsServices iUserAssetsServices; |
| | | @Autowired |
| | | private UserAssetsServices userAssetsServices; |
| | | |
| | | /** |
| | | * 获取上架ai产品 |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ServerResponse getStockAiList(Integer pageNum, Integer pageSize) { |
| | | public ServerResponse getStockAiList(Integer pageNum, Integer pageSize, String stockName) { |
| | | try { |
| | | PageHelper.startPage(pageNum, pageSize); |
| | | List<StockAI> stockAiList = stockAiMapper.selectList(new QueryWrapper<StockAI>() |
| | | .eq("status", EStockAIStatus.online.getValue())); |
| | | .eq("status", EStockAIStatus.online.getValue()) |
| | | .eq(StringUtils.isNotBlank(stockName),"stock_name", stockName)); |
| | | // 获取分页信息 |
| | | PageInfo<StockAI> pageInfo = new PageInfo<>(stockAiList); |
| | | return ServerResponse.createBySuccess(pageInfo); |
| | |
| | | return ServerResponse.createByErrorMsg("最低购买数量" + stockAI.getMinPrice(), request); |
| | | } |
| | | //获取用户账户 |
| | | UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.MX.getCode(), user.getId()); |
| | | /*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()); |
| | | UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stockAI.getStockType(), user.getId()); |
| | | /*BigDecimal finalBuyNum = buyNum; |
| | | //如果不是默认货币需要转换金额 |
| | | if (!stockAI.getStockType().equals(EStockType.getDefault().getCode())) { |
| | | buyNum = userAssetsServices.exchangeAmountByRate(stockAI.getStockType(), buyNum); |
| | | }*/ |
| | | if(buyNum.compareTo(userAssets.getAvailableBalance()) > 0){ |
| | | return ServerResponse.createByErrorMsg("可用余额不足" + userAssets.getAvailableBalance(), request); |
| | |
| | | iUserAssetsServices.aiAvailableBalanceChange(stockAI.getStockType(), 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); |
| | |
| | | if (model == null) { |
| | | return ServerResponse.createByErrorMsg("editStockAi model is null"); |
| | | } |
| | | if (model.getStockType().isEmpty() || model.getStockName().isEmpty() || model.getMinPrice() == null |
| | | || model.getSuccessRate() == null || model.getExpectedEarning() == null || model.getStatus().isEmpty()) { |
| | | if (StringUtils.isBlank(model.getStockType()) || StringUtils.isBlank(model.getStockName()) || model.getMinPrice() == null |
| | | || model.getSuccessRate() == null || model.getExpectedEarning() == null || StringUtils.isBlank(model.getStatus())) { |
| | | return ServerResponse.createByErrorMsg("请完善必填信息"); |
| | | } |
| | | if (!model.getStockType().equals(EStockType.US.getCode()) && !model.getStockType().equals(EStockType.MX.getCode())) { |
| | | return ServerResponse.createByErrorMsg("只能选择美股或墨西哥股"); |
| | | if (!EStockType.isExistByCode(model.getStockType())) { |
| | | return ServerResponse.createByErrorMsg("股票市场不存在"); |
| | | } |
| | | //新增 |
| | | if (model.getId() == null) { |
| | |
| | | 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)); |
| | | |
| | | //保存 |
| | |
| | | } |
| | | //结算 TODO结算建仓 计算收益 |
| | | /*//所有建仓 |
| | | |
| | | List<StockAIOrderPosition> stockAIOrderPositionList = stockAiOrderPositionMapper |
| | | .selectList(new QueryWrapper<StockAIOrderPosition>() |
| | | .eq("stock_ai_order_id", id)); |
| | |
| | | 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()); |
| | | //收益 |
| | |
| | | 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(); |
| | | } |