package com.nq.utils.task.stock;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
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.IUserStockSubscribeService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.collections.CollectionUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Optional;
|
|
@Component
|
@Slf4j
|
public class AdminStockSubscribeTask {
|
|
boolean enableRun = false;
|
@Autowired
|
StockSubscribeMapper stockSubscribeMapper;
|
@Autowired
|
IUserStockSubscribeService iUserStockSubscribeService;
|
@Autowired
|
UserMapper userMapper;
|
@Resource
|
private UserStockSubscribeMapper userStockSubscribeMapper;
|
|
@Scheduled(cron = "0 0/1 * * * *")
|
public void run() {
|
if (enableRun) return;
|
|
try {
|
log.info("【新股申购中签】::定时任务::开始执行...");
|
enableRun = true;
|
int limitNo = 0;
|
while (true) {
|
// 主查询
|
List<UserStockSubscribe> list = findList(limitNo++);
|
if (CollectionUtils.isEmpty(list)) return;
|
for (UserStockSubscribe subscribe : list) {
|
try {
|
apply(subscribe);
|
} catch (Exception e) {
|
log.error("【新股申购中签】::执行错误::异常信息::{}", e.getMessage());
|
}
|
}
|
}
|
|
} catch (Exception e) {
|
log.error("【新股申购中签】::执行错误::终止执行...");
|
} finally {
|
log.info("【新股申购中签】::执行完成::结束任务...");
|
enableRun = false;
|
}
|
}
|
|
public List<UserStockSubscribe> findList(int limitNo) {
|
int limit = 10;
|
return userStockSubscribeMapper.selectList(new LambdaQueryWrapper<UserStockSubscribe>()
|
.ne(UserStockSubscribe::getStatus, 3)
|
.isNotNull(UserStockSubscribe::getApplyLastNumber)
|
.last(" limit " + limitNo * limit + "," + limit + " "));
|
}
|
|
private void apply(UserStockSubscribe model) {
|
// 查询
|
User user = userMapper.selectByPrimaryKey(model.getUserId());
|
StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new QueryWrapper<>(new StockSubscribe()).eq("code", model.getNewCode()));
|
if (model.getApplyLastNumber() == null || model.getApplyLastNumber() == 0) return;
|
|
Calendar c = Calendar.getInstance();
|
if (model.getEndTime() != null) {
|
c.setTime(model.getEndTime());
|
//增加一天
|
c.add(Calendar.DAY_OF_MONTH, 1);
|
}
|
|
Date date = Optional.ofNullable(model.getPushTime()).orElse(
|
Optional.ofNullable(stockSubscribe.getPushTime()).orElse(model.getEndTime() == null ? null : c.getTime())
|
);
|
|
if (date != null && date.before(new Date()))
|
// 更新
|
iUserStockSubscribeService.edit(model, stockSubscribe, user);
|
}
|
}
|