1
zj
2025-07-10 37670b2ff5379e8603d3b0eec6d493daf2d6cfcb
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.nq.utils.task.stock;
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.gson.Gson;
import com.nq.dao.StockMapper;
import com.nq.dao.UserPositionMapper;
import com.nq.enums.EStockType;
import com.nq.pojo.DataStockBean;
import com.nq.pojo.ReponseBase;
import com.nq.pojo.Stock;
import com.nq.pojo.UserPosition;
import com.nq.service.IMandatoryLiquidationService;
import com.nq.service.IStockService;
import com.nq.service.IUserPositionService;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.redis.RedisKeyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
 
@Component
public class StockTask {
    @Autowired
    IStockService stockService;
    @Autowired
    StockMapper stockMapper;
 
    @Autowired
    IUserPositionService userPositionService;
 
    @Autowired
    UserPositionMapper userPositionMapper;
 
    private final Lock stockConstraintLock = new ReentrantLock();
 
 
    @Autowired
    IMandatoryLiquidationService mandatoryLiquidationService;
 
 
    private static final Logger log = LoggerFactory.getLogger(StockTask.class);
 
 
 
    private final AtomicBoolean syncINStockData = new AtomicBoolean(false);
 
    private final Lock syncINStockDataLock = new ReentrantLock();
 
    /**
     * 同步系统所需要的股票
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void syncINStockData() {
        if (syncINStockData.get()) { // 判断任务是否在处理中
            return;
        }
        if (syncINStockDataLock.tryLock()) {
            try {
                syncINStockData.set(true); // 设置处理中标识为true
                loadAllStock(EStockType.JP);
            } finally {
                syncINStockDataLock.unlock();
                syncINStockData.set(false); // 设置处理中标识为false
            }
        }
    }
 
 
    /**
     * 同步美国股票
     */
//    @Scheduled(cron = "0 0/30 * * * ?")
    public void loadStockCompanies() {
        loadAllCompanies();
    }
 
 
    /**
     * 加载公司信息
     */
    public void loadAllCompanies() {
        List<Stock> list = stockMapper.findStockList();
        for (int i = 0; i < list.size(); i++) {
            Stock stock = list.get(i);
            EStockType eStockType = EStockType.getEStockTypeByCode(stock.getStockType());
            String result = HttpClientRequest.doGet(eStockType.stockUrl + "companies?pid=+" + stock.getStockCode() + "+country_id=" + eStockType.getContryId() + "&size=1&page=1&key=" + eStockType.stockKey);
            try {
                JSONObject jsonObject = JSONObject.parseObject(result);
                JSONObject companiesInfo = jsonObject.getJSONArray("data").getJSONObject(0);
                RedisKeyUtil.setCacheCompanies(stock, new Gson().toJson(companiesInfo));
            } catch (Exception e) {
                log.info("");
 
            }
        }
 
    }
 
    /**
     * 加载所有股票数据
     */
    public void loadAllStock(EStockType eStockType) {
        log.info("同步股票 数据 {}", eStockType.getCode());
        List<DataStockBean> list = new ArrayList<>();
        int totleStock = 1;
        int page = 0;
        try {
            while (totleStock > list.size()) {
                try {
                    String result = HttpClientRequest.doGet(eStockType.stockUrl + "list?country_id=" + eStockType.getContryId() + "&size=1000&page=" + page + "&key=" + eStockType.stockKey);
                    ReponseBase reponseBase = new Gson().fromJson(result, ReponseBase.class);
                    list.addAll(reponseBase.getData());
                    page++;
                    totleStock = reponseBase.getTotal();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
            for (DataStockBean o : list) {
                Stock stock = stockMapper.findStockByCode(o.getId());
                if (stock == null) {
                    stock = new Stock();
                    stock.setStockCode(o.getId());
                    stock.setStockName(o.getName());
                    stock.setStockType(eStockType.getCode());
                    if (o.getType() == null) {
                        stock.setStockGid(eStockType.getCode());
                    } else {
                        stock.setStockGid(o.getType());
                    }
                    stock.setStockSpell(o.getSymbol());
                    stock.setIsLock(0);
                    stock.setIsShow(0);
                    stock.setDataBase(0);
                    stock.setAddTime(new Date());
                    stockMapper.insert1(stock);
                } else {
                    stock.setStockCode(o.getId());
                    stock.setStockName(o.getName());
                    stock.setStockType(eStockType.getCode());
                    if (o.getType() == null) {
                        stock.setStockGid(eStockType.getCode());
                    } else {
                        stock.setStockGid(o.getType());
                    }
                    stock.setStockSpell(o.getSymbol());
                    stock.setIsLock(0);
                    stock.setIsShow(0);
                    stock.setDataBase(0);
                    stock.setAddTime(new Date());
                    stockMapper.updateById(stock);
                }
                RedisKeyUtil.setCaCheKeyBaseStock(eStockType, o);
            }
            log.info("同步股票 数据 成功 {}  总共同步数据 {}", eStockType.getCode(), list.size());
        } catch (
                Exception e) {
            log.error("同步出错", e);
        }
    }
 
    private final AtomicBoolean stockConstraint = new AtomicBoolean(false);
 
    /**
     * 强制平仓
     */
//    @Scheduled(cron = "0/1 * * * * ?")
    public void stockConstraint() {
        if (stockConstraint.get()) { // 判断任务是否在处理中
            return;
        }
        if (stockConstraintLock.tryLock()) {
            try {
                stockConstraint.set(true); // 设置处理中标识为true
                List<UserPosition> userPositions = userPositionMapper.selectList(new LambdaQueryWrapper<UserPosition>().isNull(UserPosition::getSellOrderId));
                if (CollectionUtils.isNotEmpty(userPositions)) {
                    userPositionService.stockConstraint(userPositions);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("强制平仓任务错误:" + e.getMessage());
            } finally {
                stockConstraintLock.unlock();
                stockConstraint.set(false); // 设置处理中标识为false
            }
        } else {
            log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略");
        }
    }
}