zyy
2025-07-18 dc11990acb945329cdcb28dae7a0aa353a3c18c7
src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
@@ -5,6 +5,8 @@
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;
@@ -17,11 +19,8 @@
import com.nq.utils.timeutil.DateTimeUtil;
import com.nq.utils.KeyUtils;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.redis.JsonUtil;
import com.nq.utils.redis.RedisShardedPoolUtils;
import com.nq.utils.stock.BuyAndSellUtils;
import com.nq.vo.stock.UserStockSubscribeAddIn;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.nq.vo.subscribe.UserStockSubscribeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -72,13 +71,16 @@
    @Autowired
    UserAssetsMapper userAssetsMapper;
    @Autowired
    ExchangeRateRepository exchangeRateRepository;
    /**
     * 用户新股申购
     * @param model
     * @return
     */
    @Override
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse insert(UserStockSubscribeAddIn model, HttpServletRequest request) throws Exception {
        int ret = 0;
        if (model == null) {
@@ -150,7 +152,6 @@
                }else{
                    bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice());
                }
                if(stockSubscribe.getType() == 1){
                    model.setUserId(user.getId());
                    model.setNewName(stockSubscribe.getName());
@@ -167,6 +168,10 @@
                    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));
@@ -211,7 +216,7 @@
                    UserStockSubscribe userStockSubscribe = Convert.convert(UserStockSubscribe.class, model);
                    userStockSubscribe.setNewStockId(stockSubscribe.getNewlistId());
                    ret = userStockSubscribeMapper.insert(userStockSubscribe);
                    iUserAssetsServices.availablebalanceChange(EStockType.US.getCode(), user.getId(),EUserAssets.BUY,bound.negate(),"","");
                    iUserAssetsServices.availablebalanceChange(stockSubscribe.getStockType(), user.getId(),EUserAssets.BUY,bound.negate(),"","");
                    if (ret > 0) {
                        return ServerResponse.createBySuccessMsg("配售成功",request);
                    } else {
@@ -232,7 +237,7 @@
     * admin 新股申购-添加和修改
     */
    @Override
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse save(UserStockSubscribe model, HttpServletRequest request) throws Exception {
        int ret = 0;
        if( model.getId() != null  ){
@@ -270,12 +275,20 @@
                        if(applyNumber > 0){
                            //需要退回的资金
                            BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
                            //如果不是墨西哥币需要转换金额
                            if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
                                refundPrice = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), refundPrice);
                            }
                            userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(refundPrice));
                            userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(refundPrice));
                        }
                    }else{
                        if(applyNumber > 0){
                            BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
                            //如果不是墨西哥币需要转换金额
                            if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
                                refundPrice = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), refundPrice);
                            }
                            BigDecimal subtract = refundPrice.subtract(userAssets.getAmountToBeCovered());
                            if(subtract.compareTo(BigDecimal.ZERO) <= 0){//如果退回的资金不足补足待补则直接减
                                userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().subtract(refundPrice));
@@ -296,7 +309,7 @@
                    }
                    BigDecimal cCount = new BigDecimal(model.getApplyNums()-model.getApplyNumber());
                    BigDecimal tMoney = ((stockSubscribe.getMinPrice() != null ? stockSubscribe.getMinPrice() : stockSubscribe.getPrice())).multiply(cCount);
                    iUserAssetsServices.availablebalanceChange(EStockType.US.getCode(),userStockSubscribe.getUserId(),
                    iUserAssetsServices.availablebalanceChange(stockSubscribe.getStockType(),userStockSubscribe.getUserId(),
                            EUserAssets.TOP_UP,tMoney,"","");
                    model.setBond((stockSubscribe.getMinPrice() != null ? stockSubscribe.getMinPrice() : stockSubscribe.getPrice()).multiply(BigDecimal.valueOf(model.getApplyNumber())));
                    model.setDbMoney(BigDecimal.ZERO);
@@ -457,28 +470,16 @@
        String header = request.getHeader(property);
        if (header != null) {
             User user = iUserService.getCurrentRefreshUser(request);
            List<UserStockSubscribe> userStockSubscribe = null;
            QueryWrapper<UserStockSubscribe> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", user.getId());
            if (type != null && !type.isEmpty()){
                queryWrapper.eq("type", type);
            }
            if (status != null){
                queryWrapper.eq("status", status);
            }
            queryWrapper.orderByDesc("add_time");
            userStockSubscribe = this.userStockSubscribeMapper.selectList(queryWrapper);
            List<UserStockSubscribeVo> userStockSubscribe = this.userStockSubscribeMapper.getListByParam(user.getId(), type, status);
            /*if (type==null||type.equals("")){
                userStockSubscribe = this.userStockSubscribeMapper.selectList(new QueryWrapper<>(new UserStockSubscribe()).eq("user_id", user.getId()).orderByDesc("add_time"));
            }else{
                userStockSubscribe = this.userStockSubscribeMapper.selectList(new QueryWrapper<>(new UserStockSubscribe()).eq("user_id", user.getId()).eq("type", type).orderByDesc("add_time"));
            }*/
            List<UserStockSubscribe> list = new ArrayList<>();
            for (UserStockSubscribe userStockSubscribe1 : userStockSubscribe) {
            List<UserStockSubscribeVo> list = new ArrayList<>();
            for (UserStockSubscribeVo userStockSubscribe1 : userStockSubscribe) {
                StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<>(new StockSubscribe())
                        .eq("newlist_id", userStockSubscribe1.getNewStockId()));
                if (stockSubscribe != null) {
@@ -487,51 +488,60 @@
            }
            return ServerResponse.createBySuccess(list);
        }
        return ServerResponse.createByErrorCodeMsg(401,"请先登录");
        return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录");
    }
    /**
     * 新股申购-用户提交金额
     */
    @Override
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public ServerResponse userSubmit(Integer id,HttpServletRequest request) {
        int ret = 0;
        String property = PropertiesUtil.getProperty("user.cookie.name");
        String header = request.getHeader(property);
        if (header != null) {
            User user = iUserService.getCurrentRefreshUser(request);
            if(id == null){
                return ServerResponse.createByErrorMsg("参数错误",request);
            }
            UserStockSubscribe userStockSubscribe = userStockSubscribeMapper.load(id);
            if (userStockSubscribe != null && userStockSubscribe.getUserId().equals(user.getId())) {
                if(userStockSubscribe.getType()== 2 ){
                    return ServerResponse.createByErrorMsg("未中签,无需支付",request);
        try {
            int ret = 0;
            String property = PropertiesUtil.getProperty("user.cookie.name");
            String header = request.getHeader(property);
            if (header != null) {
                User user = iUserService.getCurrentRefreshUser(request);
                if(id == null){
                    return ServerResponse.createByErrorMsg("参数错误",request);
                }
                if (userStockSubscribe.getStatus() == 3) {
                    userStockSubscribe.setSubmitTime(DateTimeUtil.getCurrentDate());
                    userStockSubscribe.setStatus(4);
                    BigDecimal bigDecimal =  iUserAssetsServices.
                            getAvailableBalance(EStockType.US.getCode(),
                                    userStockSubscribe.getUserId());
                    BigDecimal multiply = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(userStockSubscribe.getApplyNumber()));
                    if(bigDecimal.compareTo(multiply) <= 0){
                        return ServerResponse.createByErrorMsg("余额不足",request);
                UserStockSubscribe userStockSubscribe = userStockSubscribeMapper.load(id);
                if (userStockSubscribe != null && userStockSubscribe.getUserId().equals(user.getId())) {
                    if(userStockSubscribe.getType()== 2 ){
                        return ServerResponse.createByErrorMsg("未中签,无需支付",request);
                    }
                    iUserAssetsServices.availablebalanceChange(EStockType.US.getCode(),userStockSubscribe.getUserId(),
                            EUserAssets.BUY,multiply.negate(),"","");
                    userStockSubscribe.setDbMoney(BigDecimal.ZERO);
                    userStockSubscribeMapper.update1(userStockSubscribe);
                    return  ServerResponse.createBySuccess();
                    if (userStockSubscribe.getStatus() == 3) {
                        StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<>(new StockSubscribe())
                                .eq("newlist_id", userStockSubscribe.getNewStockId()));
                        if (stockSubscribe == null) {
                            return ServerResponse.createByErrorMsg("新股不存在",request);
                        }
                        userStockSubscribe.setSubmitTime(DateTimeUtil.getCurrentDate());
                        userStockSubscribe.setStatus(4);
                        BigDecimal bigDecimal =  iUserAssetsServices.
                                getAvailableBalance(EStockType.US.getCode(),
                                        userStockSubscribe.getUserId());
                        BigDecimal multiply = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(userStockSubscribe.getApplyNumber()));
                        if(bigDecimal.compareTo(multiply) <= 0){
                            return ServerResponse.createByErrorMsg("余额不足",request);
                        }
                        iUserAssetsServices.availablebalanceChange(stockSubscribe.getStockType(), userStockSubscribe.getUserId(),
                                EUserAssets.BUY,multiply.negate(),"","");
                        userStockSubscribe.setDbMoney(BigDecimal.ZERO);
                        userStockSubscribeMapper.update1(userStockSubscribe);
                        return  ServerResponse.createBySuccess();
                    } else {
                        return ServerResponse.createByErrorMsg("不成功的申请者不收取任何费用",request);
                    }
                } else {
                    return ServerResponse.createByErrorMsg("不成功的申请者不收取任何费用",request);
                    return ServerResponse.createByErrorMsg("新股认购订单不存在!",request);
                }
            } else {
                return ServerResponse.createByErrorMsg("新股认购订单不存在!",request);
            }
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录");
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg(e.getMessage());
        }
        return ServerResponse.createByErrorCodeMsg(401,"请先登录");
    }
    /**
     * 新股申购-删除