| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.nq.common.RedisLockService; |
| | | import com.nq.common.ServerResponse; |
| | | import com.nq.dao.StockSubscribeMapper; |
| | | import com.nq.dao.UserAssetsMapper; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.dao.DuplicateKeyException; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 新股申购 |
| | |
| | | @Autowired |
| | | UserAssetsMapper userAssetsMapper; |
| | | |
| | | @Autowired |
| | | RedisLockService redisLockService; |
| | | |
| | | /** |
| | | * 用户新股申购 |
| | | * @param model |
| | |
| | | return ServerResponse.createByErrorMsg("参数错误",request); |
| | | } |
| | | User user = iUserService.getCurrentRefreshUser(request); |
| | | synchronized (user.getId()){ |
| | | |
| | | // 使用分布式锁 |
| | | String lockKey = "subscribe:lock:" + user.getId(); |
| | | String lockValue = UUID.randomUUID().toString(); // 生成唯一值 |
| | | |
| | | // 尝试获取锁,设置5秒过期时间 |
| | | boolean locked = redisLockService.tryLock(lockKey, lockValue, 5); |
| | | if (!locked) { |
| | | return ServerResponse.createByErrorMsg("请求处理中,请勿重复提交", request); |
| | | } |
| | | try { |
| | | UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN",user.getId()); |
| | | if (model.getNewCode() != null) { |
| | | StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<StockSubscribe>() |
| | |
| | | return ServerResponse.createByErrorMsg("已结束",request); |
| | | } |
| | | |
| | | // //重复申购限制 |
| | | //重复申购限制 |
| | | Long count = userStockSubscribeMapper.selectCount(new QueryWrapper<UserStockSubscribe>().eq( |
| | | "new_code", model.getNewCode()).eq("user_id", user.getId()).eq("type", model.getType())); |
| | | if (count > 0) { |
| | |
| | | }else{ |
| | | bound = new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice()); |
| | | } |
| | | |
| | | try{ |
| | | if(stockSubscribe.getType() == 1){ |
| | | model.setUserId(user.getId()); |
| | | model.setNewName(stockSubscribe.getName()); |
| | |
| | | } |
| | | }else{ |
| | | |
| | | // BigDecimal bound; |
| | | // if(stockSubscribe.getMinPrice() != null || stockSubscribe.getMinPrice().compareTo(BigDecimal.ZERO)<0){ |
| | | // bound = new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getMinPrice()); |
| | | // }else{ |
| | | // bound = new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice()); |
| | | // } |
| | | // |
| | | BigDecimal useEnaAmount = iUserAssetsServices.getAvailableBalance(EStockType.IN.getCode(), user.getId()); |
| | | if(useEnaAmount.compareTo(bound)<0){ |
| | | return ServerResponse.createByErrorMsg("余额不足,配售失败",request); |
| | |
| | | return ServerResponse.createByErrorMsg("配售失败",request); |
| | | } |
| | | } |
| | | } catch (DuplicateKeyException e) { |
| | | return ServerResponse.createByErrorMsg("请勿重复提交", request); |
| | | } |
| | | } |
| | | } finally { |
| | | // 释放锁 |
| | | redisLockService.unlock(lockKey, lockValue); |
| | | } |
| | | return ServerResponse.createByErrorMsg("申购失败",request); |
| | | } |
| | | @Override |