zj
2024-06-03 81a29edf665881828e4ca1f0e444bfcbc6ab6f24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
    }
}