1
zj
2024-05-14 5259432d9a97529cf1ec2be5489a5b592bee78c4
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.nq.utils.task.stock;
 
import cn.hutool.core.date.DateUtil;
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.common.ServerResponse;
import com.nq.dao.StockSubscribeMapper;
import com.nq.dao.UserAssetsMapper;
import com.nq.dao.UserMapper;
import com.nq.dao.UserStockSubscribeMapper;
import com.nq.pojo.StockSubscribe;
import com.nq.pojo.User;
import com.nq.pojo.UserAssets;
import com.nq.pojo.UserStockSubscribe;
import com.nq.service.IUserAssetsServices;
import com.nq.service.IUserPositionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
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: dabao
 * @description:
 * @create: 2024-04-01 17:32
 **/
@Component
@Slf4j
public class CarryPositionTask {
 
 
    @Autowired
    UserStockSubscribeMapper userStockSubscribeMapper;
 
    @Autowired
    IUserPositionService iUserPositionService;
 
    @Autowired
    StockSubscribeMapper stockSubscribeMapper;
 
    @Autowired
    UserMapper userMapper;
 
    @Autowired
    IUserAssetsServices userAssetsServices;
 
    private final Lock ballotLock = new ReentrantLock();
 
    private final Lock subscriptionLock = new ReentrantLock();
 
    boolean ballot = true;
 
    @Scheduled(cron = "0 0/10 * * * ?")
    public void ballot() {
        if (ballot) // 定义一个布尔变量,代表新闻任务是否在处理中
            return;
        ballot = true;
        if (ballotLock.tryLock()) {
            try {
                log.info("新股上市转持仓定时任务--------->开始");
                List<StockSubscribe> stockSubscribes = stockSubscribeMapper.selectList(new LambdaQueryWrapper<StockSubscribe>()
                        .lt(StockSubscribe::getListDate, DateUtil.date()));
                if (CollectionUtils.isNotEmpty(stockSubscribes)) {
                    List<String> codeList = stockSubscribes.stream().map(StockSubscribe::getCode).collect(Collectors.toList());
                    List<UserStockSubscribe> userStockSubscribes = userStockSubscribeMapper.selectList(new LambdaQueryWrapper<UserStockSubscribe>()
                            .eq(UserStockSubscribe::getStatus, 4).in(UserStockSubscribe::getNewCode, codeList));
                    //订单转持仓
                    userStockSubscribes.forEach(f -> {
                        ServerResponse serverResponse = iUserPositionService.newStockToPosition(f.getId());//转持仓
                        if(serverResponse.isSuccess()){
                            f.setStatus(5);
                            userStockSubscribeMapper.updateById(f);
                        }else{
                            log.info("新股上市转持仓失败申购订单id:"+f.getId()+",失败原因:"+serverResponse.getMsg());
                        }
                    });
                }
                log.info("新股上市转持仓定时任务--------->结束");
            } catch (Exception e) {
                log.error("新股上市转持仓定时任务发生异常", e);
            } finally {
                ballotLock.unlock();
                ballot = false;
            }
        } else {
            log.info("新股上市转持仓定时任务--------->上次任务还未执行完成,本次任务忽略");
        }
    }
 
    boolean subscription = true;
    @Scheduled(cron = "0 0/1 * * * ?")
    public void subscription() {
        if (subscription) // 定义一个布尔变量,代表新闻任务是否在处理中
            return;
        subscription = true;
        if (subscriptionLock.tryLock()) {
            try {
                log.info("自动转已认缴--------->开始");
                List<UserStockSubscribe> userStockSubscribes = userStockSubscribeMapper.selectList(new LambdaQueryWrapper<UserStockSubscribe>()
                        .eq(UserStockSubscribe::getStatus, 3));
                userStockSubscribes.forEach(f->{
                    UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId("IN",f.getUserId());
                    if(null != userAssets && userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) == 0){
                        f.setStatus(4);
                        userStockSubscribeMapper.updateById(f);
                    }
                });
                log.info("自动转已认缴--------->结束");
            } catch (Exception e) {
                log.error("自动转已认缴定时任务发生异常", e);
            } finally {
                subscriptionLock.unlock();
                subscription = false;
            }
        } else {
            log.info("自动转已认缴定时任务--------->上次任务还未执行完成,本次任务忽略");
        }
    }
 
}