OtcEntrustInterval.java
8.87 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.cjs.cms.biz.trade;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cjs.cms.dao.trade.OtcEntrustDao;
import com.cjs.cms.model.entrust.EcEntrustInfo;
import com.cjs.cms.model.pub.ConstInfo;
import com.cjs.cms.model.trade.OtcEntrustInfo;
import com.cjs.cms.util.lang.JsonUtil;
import com.cjs.cms.util.lang.PageUtils;
import com.cjs.cms.util.redis.JedisTemplate;
import com.cjs.cms.util.t2.T2Result;
/**
* 定时获取委托信息
*
* @author tongyufu
*
*/
@Service
public class OtcEntrustInterval {
@Autowired
private OtcEntrustDao otcEntrustDao;
private DateFormat timeParse = new SimpleDateFormat("HHmmss");
private DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
@Autowired
private TradeBiz tradeBiz;
@Autowired
private JedisTemplate jedisTemplate;
Logger log = LogManager.getLogger();
/**
* 首页最新发布、最新成交
*/
public void lastEntrustInfo() {
//非交易时间不更新行情
if (!this.isWorkTime()) {
return;
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("rows", 15);
PageUtils.processOralcePage(params);
//最新发布
String beforeLastEntrust = jedisTemplate.get(ConstInfo.Entrust.ENTRUST_LAST);
final String lastEntrust = JsonUtil.toJson(otcEntrustDao.queryLastEntrust(params));
//log.debug("最新发布:" + lastEntrust);
if (!StringUtils.equals(lastEntrust, beforeLastEntrust)) {
jedisTemplate.set(ConstInfo.Entrust.ENTRUST_LAST, lastEntrust);
jedisTemplate.publish(ConstInfo.Entrust.ENTRUST_LAST_TOPIC, lastEntrust);
}
//最新成交
String beforeLastBargin = jedisTemplate.get(ConstInfo.Entrust.BARGIN_LAST);
List<OtcEntrustInfo> bargins = otcEntrustDao.queryLastBargin(params);
for (OtcEntrustInfo bargin : bargins) {
try {
String time = bargin.getCurrTime();
if (time.length() == 8) {
time = "0" + time;
}
time = time.substring(0, 6);
time = timeFormat.format(timeParse.parse(time));
bargin.setCurrTime(time);
} catch (ParseException e) {
log.error(e);
}
}
final String lastBargin = JsonUtil.toJson(bargins);
//log.debug("最新成交:" + lastBargin);
if (!StringUtils.equals(lastBargin, beforeLastBargin)) {
jedisTemplate.set(ConstInfo.Entrust.BARGIN_LAST, lastBargin);
jedisTemplate.publish(ConstInfo.Entrust.ENTRUST_BARGIN_TOPIC, lastBargin);
}
}
/**官网,买卖的全部商品查询*/
public void entrustingInfo() {
//非交易时间不更新行情
if (!this.isWorkTime()) {
return;
}
List<EcEntrustInfo> entrusts = otcEntrustDao.queryEntrustingInfo();
final String lastEntrust = JsonUtil.toJson(entrusts);
String beforeLastEntrust = jedisTemplate.get(ConstInfo.Entrust.ENTRUST_ENTRUSTING);
//log.debug("买卖的全部商品:" + lastEntrust);
if (StringUtils.equals(lastEntrust, beforeLastEntrust)) {
return;
}
jedisTemplate.set(ConstInfo.Entrust.ENTRUST_ENTRUSTING, lastEntrust);
jedisTemplate.publish(ConstInfo.Entrust.ENTRUST_ENTRUSTING_TOPIC, lastEntrust);
//this.entrustDetail(entrusts);
}
/**藏品买卖五档*/
@SuppressWarnings("unchecked")
public void entrustDetailInfo() {
//非交易时间不更新行情
if (!this.isWorkTime()) {
return;
}
T2Result tradeData = tradeBiz.requestTradeData();
if (tradeData.getCode() != 0) {
log.error("获取行情失败:" + tradeData.toString());
return;
}
List<Map<String, Object>> trades = tradeData.getData();
List<Map<String, Object>> buyList = otcEntrustDao.queryBuyRank(5);
List<Map<String, Object>> sellList = otcEntrustDao.querySellRank(5);
String redisKey = ConstInfo.Entrust.ENTRUST_DETAIL + "2";
String preEntrust = jedisTemplate.get(redisKey);
Map<String, Object> preEntrusts = null;
Map<String, Map<String, Object>> preEntrustMap = null;
Map<String, Map<String, Object>> tradeMap = new HashMap<String, Map<String, Object>>();
if (buyList.isEmpty() && sellList.isEmpty()) {
log.debug("藏品买卖五档为空");
return;
}
if (StringUtils.isNotBlank(preEntrust)) {
preEntrustMap = JsonUtil.fromJson(preEntrust, HashMap.class);
}
//转换行情格式
for (Map<String, Object> trade : trades) {
String otcCode = trade.get("mch_code").toString();
tradeMap.put(otcCode, trade);
}
//买入五档
for (Map<String, Object> entrust : buyList) {
String otcCode = entrust.get("OTC_CODE").toString();
Map<String, Object> trade = tradeMap.get(otcCode);
List<Map<String, Object>> entrustList = (List<Map<String, Object>>) trade
.get("buyList");
if (entrustList == null) {
entrustList = new ArrayList<Map<String, Object>>();
}
entrustList.add(entrust);
trade.put("buyList", entrustList);
tradeMap.put(otcCode, trade);
}
//卖出五档
for (Map<String, Object> entrust : sellList) {
String otcCode = entrust.get("OTC_CODE").toString();
Map<String, Object> trade = tradeMap.get(otcCode);
List<Map<String, Object>> entrustList = (List<Map<String, Object>>) trade
.get("sellList");
if (entrustList == null) {
entrustList = new ArrayList<Map<String, Object>>();
}
entrustList.add(entrust);
trade.put("sellList", entrustList);
tradeMap.put(otcCode, trade);
}
String entrust = JsonUtil.toJson(tradeMap);
if (StringUtils.equals(entrust, preEntrust)) {
return;
}
jedisTemplate.set(redisKey, entrust);
//推送
for (String otcCode : tradeMap.keySet()) {
Map<String, Object> trade = tradeMap.get(otcCode);
entrust = JsonUtil.toJson(trade);
//如果与前一次相同,不推送
if (preEntrustMap != null) {
preEntrusts = preEntrustMap.get(otcCode);
if (preEntrusts != null) {
preEntrust = JsonUtil.toJson(preEntrusts);
if (StringUtils.equals(entrust, preEntrust)) {
continue;
}
}
}
String channel = ConstInfo.Entrust.ENTRUST_DETAIL_TOPIC + otcCode;
jedisTemplate.publish(channel, entrust);
}
}
private boolean isWorkTime() {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (day == Calendar.SATURDAY || day == Calendar.SUNDAY || hour < 9 || hour > 15) {
return false;
}
return true;
}
/**
* 将全部商品列表存储到redis的Hash中,并将有变化的商品信息推送到redis发布通道
*
* @param entrusts
*/
private void entrustDetail(List<EcEntrustInfo> entrusts) {
List<String> otcCodes = new ArrayList<String>();
Map<String, String> entrustMap = new HashMap<String, String>();
final Map<String, String> publishMap = new HashMap<String, String>();
List<String> otcs;
for (EcEntrustInfo entrust : entrusts) {
entrustMap.put(entrust.getOtcCode(), JsonUtil.toJson(entrust));
otcCodes.add(entrust.getOtcCode());
}
otcs = jedisTemplate.hmget(ConstInfo.Entrust.ENTRUST_DETAIL,
otcCodes.toArray(new String[otcCodes.size()]));
for (EcEntrustInfo entrust : entrusts) {
String otc = JsonUtil.toJson(entrust);
if (!otcs.contains(otc)) {
publishMap.put(entrust.getOtcCode(), otc);
}
}
jedisTemplate.hmset(ConstInfo.Entrust.ENTRUST_DETAIL, entrustMap);
for (String otcCode : publishMap.keySet()) {
String channel = ConstInfo.Entrust.ENTRUST_DETAIL_TOPIC + otcCode;
jedisTemplate.publish(channel, publishMap.get(otcCode));
}
}
}