zyy
2025-07-22 031ea962cfd1790726e96620cad65209315f27c7
src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
@@ -5,13 +5,9 @@
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.StockSubscribeMapper;
import com.nq.dao.UserAssetsMapper;
import com.nq.dao.UserMapper;
import com.nq.dao.UserStockSubscribeMapper;
import com.nq.dao.*;
import com.nq.enums.EStockType;
import com.nq.enums.EUserAssets;
import com.nq.pojo.*;
@@ -60,10 +56,6 @@
    ISiteProductService iSiteProductService;
    @Autowired
    IUserService iUserService;
    @Autowired
    ISiteSettingService iSiteSettingService;
    @Autowired
    TradingHourServiceImpl tradingHourService;
    @Resource
    IUserAssetsServices iUserAssetsServices;
@@ -72,7 +64,7 @@
    UserAssetsMapper userAssetsMapper;
    @Autowired
    ExchangeRateRepository exchangeRateRepository;
    MoneyLogMapper moneyLogMapper;
    /**
     * 用户新股申购
@@ -92,6 +84,9 @@
            if (model.getNewCode() != null) {
                StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<StockSubscribe>()
                        .eq("newlist_id", model.getNewlistId()));
                if (stockSubscribe == null) {
                    return ServerResponse.createByErrorMsg("新的股票代码不存在",request);
                }
                //判断购买新股需不需要密钥
                if(StringUtils.isNotEmpty(stockSubscribe.getPassword()) && !stockSubscribe.getPassword().equals(model.getPassword())){
                    return ServerResponse.createByErrorMsg("密钥输入错误",request);
@@ -117,10 +112,10 @@
                    return ServerResponse.createByErrorMsg("最多可申购:" + stockSubscribe.getOrderNumber(), request);
                }
                BigDecimal sumPrice = stockSubscribe.getPrice().multiply(new BigDecimal(model.getApplyNums()));
                /*BigDecimal sumPrice = stockSubscribe.getPrice().multiply(new BigDecimal(model.getApplyNums()));
                if(userAssets.getAvailableBalance().compareTo(sumPrice) < 0){
                    return ServerResponse.createByErrorMsg("账户余额不足", request);
                }
                }*/
                Date currentDate = new Date();
                if(currentDate.before(stockSubscribe.getSubscriptionTime())){
@@ -139,9 +134,6 @@
                if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
                    return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定",request);
                }
                if (stockSubscribe == null) {
                    return ServerResponse.createByErrorMsg("新的股票代码不存在",request);
                }
                model.setApplyNums((int) (model.getApplyNums()*stockSubscribe.getLever()));
@@ -152,6 +144,15 @@
                }else{
                    bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice());
                }
                //如果不是墨西哥币需要转换金额
                if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
                    bound = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), bound);
                }
                if(userAssets.getAvailableBalance().compareTo(bound) < 0){
                    return ServerResponse.createByErrorMsg("账户余额不足", request);
                }
                String before = userAssets.getAvailableBalance().toString();
                if(stockSubscribe.getType() == 1){
                    model.setUserId(user.getId());
                    model.setNewName(stockSubscribe.getName());
@@ -168,10 +169,6 @@
                    userStockSubscribe.setNewStockId(stockSubscribe.getNewlistId());
                    ret = userStockSubscribeMapper.insert(userStockSubscribe);
                    //如果不是墨西哥币需要转换金额
                    if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
                        bound = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), bound);
                    }
                    BigDecimal subtract = userAssets.getAvailableBalance().subtract(bound);
                    if(subtract.compareTo(BigDecimal.ZERO) >= 0){
                        userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(bound));
@@ -182,7 +179,19 @@
                        userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().add(subtract.abs()));
                    }
                    userAssetsMapper.updateById(userAssets);
                    //交易记录MEX
                    MoneyLog moneyLog = new MoneyLog();
                    EUserAssets eUserAssets = EUserAssets.BUY;
                    moneyLog.setDescs(eUserAssets.getDesc());
                    moneyLog.setBeFore(before);
                    moneyLog.setAfter(userAssets.getAvailableBalance().toString());
                    moneyLog.setAmount(bound.negate().toString());
                    moneyLog.setAccectType(EStockType.MX.getCode());
                    moneyLog.setType(eUserAssets.getCode());
                    moneyLog.setUserId(user.getId()+"");
                    moneyLog.setSymbol(EStockType.MX.getSymbol());
                    moneyLog.setCreateTime(new Date());
                    moneyLogMapper.insert(moneyLog);
                    if (ret > 0) {
                        return ServerResponse.createBySuccessMsg("申购成功",request);
                    } else {
@@ -272,10 +281,12 @@
                            return ServerResponse.createByErrorMsg("客户资金账户不存在");
                        }
                        int applyNumber = userStockSubscribe.getApplyNums() - model.getApplyNumber();
                        BigDecimal refundPrice = BigDecimal.ZERO;
                        String before = userAssets.getAvailableBalance().toString();
                        if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) <= 0){
                            if(applyNumber > 0){
                                //需要退回的资金
                                BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
                                refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
                                //如果不是墨西哥币需要转换金额
                                if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
                                    refundPrice = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), refundPrice);
@@ -285,7 +296,7 @@
                            }
                        }else{
                            if(applyNumber > 0){
                                BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
                                refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
                                //如果不是墨西哥币需要转换金额
                                if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
                                    refundPrice = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), refundPrice);
@@ -300,6 +311,19 @@
                                }
                            }
                        }
                        //退回资金 交易记录MEX
                        MoneyLog moneyLog = new MoneyLog();
                        EUserAssets eUserAssets = EUserAssets.BUY_RT;
                        moneyLog.setDescs(eUserAssets.getDesc());
                        moneyLog.setBeFore(before);
                        moneyLog.setAfter(userAssets.getAvailableBalance().toString());
                        moneyLog.setAmount(refundPrice.toString());
                        moneyLog.setAccectType(EStockType.MX.getCode());
                        moneyLog.setType(eUserAssets.getCode());
                        moneyLog.setUserId(userAssets.getUserId()+"");
                        moneyLog.setSymbol(EStockType.MX.getSymbol());
                        moneyLog.setCreateTime(new Date());
                        moneyLogMapper.insert(moneyLog);
                        ret = userStockSubscribeMapper.update1(model);
                        userAssetsMapper.updateById(userAssets);
                        iUserPositionService.newStockToPosition(model.getId(),userAssets.getAmountToBeCovered());//转持仓