package com.nq.utils.task.stock; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.nq.dao.StockSubscribeMapper; import com.nq.dao.UserMapper; import com.nq.dao.UserStockSubscribeMapper; import com.nq.pojo.StockSubscribe; import com.nq.pojo.User; import com.nq.pojo.UserStockSubscribe; import com.nq.service.IUserPositionService; import com.nq.utils.DateUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; /** * @program: huihuangserver * @description: 中签定时任务 * @create: 2024-03-25 15:21 **/ @Component @Slf4j public class BallotTask { @Autowired UserStockSubscribeMapper userStockSubscribeMapper; @Autowired IUserPositionService iUserPositionService; @Autowired StockSubscribeMapper stockSubscribeMapper; @Autowired UserMapper userMapper; private final Lock ballotLock = new ReentrantLock(); private final Lock transferFundsLock = new ReentrantLock(); @Scheduled(cron = "0 0 0/1 * * ? ") // @Scheduled(cron = "0 0/1 * * * *") public void ballot() { if (ballotLock.tryLock()) { try { log.info("新股申购转持仓定时任务--------->开始"); List stockSubscribes = stockSubscribeMapper.selectList(new LambdaQueryWrapper().lt(StockSubscribe::getListingTime, DateUtils.getDate())); if (CollectionUtils.isNotEmpty(stockSubscribes)) { List codeList = stockSubscribes.stream().map(StockSubscribe::getCode).collect(Collectors.toList()); List userStockSubscribes = userStockSubscribeMapper.selectList(new LambdaQueryWrapper() .eq(UserStockSubscribe::getStatus, 4).in(UserStockSubscribe::getNewCode, codeList)); //订单转持仓 userStockSubscribes.forEach(f -> { iUserPositionService.newStockToPosition(f.getId());//转持仓 f.setStatus(5); userStockSubscribeMapper.updateById(f); }); } log.info("新股申购转持仓定时任务--------->结束"); } catch (Exception e) { log.error("新股申购转持仓定时任务发生异常", e); } finally { ballotLock.unlock(); } } else { log.info("新股申购转持仓定时任务--------->上次任务还未执行完成,本次任务忽略"); } } @Scheduled(cron = "0/10 * * * * ? ") public void transferFunds() { if (transferFundsLock.tryLock()) { log.info("锁定账户待补资金扣钱定时任务--------->开始"); try { //查询锁定的账户 List users = userMapper.selectList(new LambdaQueryWrapper().gt(User::getFundsReplenished, 0)); for (int i = 0; i < users.size(); i++) { User user = users.get(i); if (user.getEnableAmt().compareTo(BigDecimal.ZERO) > 0) { if (user.getEnableAmt().compareTo(user.getFundsReplenished()) >= 0) { user.setEnableAmt(user.getEnableAmt().subtract(user.getFundsReplenished())); user.setFundsReplenished(BigDecimal.ZERO); // user.setIsLock(0); } else { user.setFundsReplenished(user.getFundsReplenished().subtract(user.getEnableAmt())); user.setEnableAmt(BigDecimal.ZERO); // user.setIsLock(1); } userMapper.updateById(user); } } } catch (Exception e) { log.error("锁定账户待补资金扣钱定时任务发生异常", e); } finally { transferFundsLock.unlock(); log.info("锁定账户待补资金扣钱定时任务--------->结束"); } } else { log.info("锁定账户待补资金扣钱定时任务--------->上次任务还未执行完成,本次任务忽略"); } } }