BatchEntrustTask.java
7.15 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
package com.cjs.cms.biz.trade;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cjs.cms.biz.pub.CommonBiz;
import com.cjs.cms.dao.pub.OtcExchangeDateDao;
import com.cjs.cms.dao.trade.HisOtcEntrustDao;
import com.cjs.cms.dao.trade.OtcEntrustBatchDao;
import com.cjs.cms.model.trade.OtcEntrustBatchInfo;
import com.cjs.cms.util.lang.DateEnum;
import com.cjs.cms.util.lang.DateUtil;
import com.cjs.cms.util.lang.PageUtils;
import com.cjs.cms.util.lang.SpringContextUtil;
import com.cjs.cms.util.redis.JedisTemplate;
/**
* 自动委托挂单
*
* @author tongyufu
*
*/
@Service
public class BatchEntrustTask {
final Logger log = LogManager.getLogger(BatchEntrustTask.class);
@Autowired
private CommonBiz commonBiz;
@Autowired
private HisOtcEntrustDao hisOtcEntrustDao;
@Autowired
private JedisTemplate jedisTemplate;
@Autowired
private OtcEntrustBatchDao otcEntrustBatchDao;
@Autowired
private OtcExchangeDateDao otcExchangeDateDao;
public void entrust() {
String entrustTask = "entrust:task:" + DateUtil.getNow(DateEnum.UNSIGNED_DATE);
log.info("批量委托开始:" + DateUtil.getNow());
if (jedisTemplate.get(entrustTask) != null) {
log.info("批量委托终止:当天任务已执行");
return;
}
jedisTemplate.set(entrustTask, "1", 60 * 60 * 24);
if (!commonBiz.checkTradeDay()) {
log.info("批量委托终止:非交易日");
return;
}
/**/
int checkTime = 0;
while (commonBiz.checkMarketStatus() != 2) {
try {
log.info("批量委托重试:当前非可交易状态");
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
checkTime += 10;
if (checkTime >= 600) {
log.info("批量委托终止:非可交易状态");
return;
}
}
Integer lastTradeDate = hisOtcEntrustDao.queryLastEntrustDate();
if (lastTradeDate == null) {
log.info("批量委托终止:最后交易日期为空");
return;
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("date", lastTradeDate);
int entrustTotal = hisOtcEntrustDao.queryEntrustTotal(params);
int fetchCount = new BigDecimal(entrustTotal / 200.0).setScale(0, RoundingMode.CEILING)
.intValue();
if (entrustTotal == 0) {
log.info("批量委托终止:委托列表为空");
return;
}
ExecutorService exec = Executors.newFixedThreadPool(10);
final BatchEntrustBiz batchEntrustBiz = SpringContextUtil.getBean(BatchEntrustBiz.class);
batchEntrustBiz.setEntrustNo(new AtomicInteger(0));
final CountDownLatch latch = new CountDownLatch(entrustTotal);
try {
for (int i = 0; i < fetchCount; i++) {
params.put("rows", 200);
params.put("page", i + 1);
PageUtils.processOralcePage(params);
List<OtcEntrustBatchInfo> entrusts = hisOtcEntrustDao.queryEntrust(params);
Map<String, String> currDateMap = this.getCurrentDate(entrusts);
for (int j = 0; j < entrusts.size(); j++) {
final OtcEntrustBatchInfo entrust = entrusts.get(j);
entrust.setCurrDate(DateUtil.getNow(DateEnum.UNSIGNED_DATE));
entrust.setCurrTime(DateUtil.getNow(DateEnum.UNSIGNED_TIME2));
//if (!this.compareCurrDate(currDateMap.get(entrust.getOpStation()), entrust.getCurrDate())) {
if (currDateMap.get(entrust.getOpStation()) != null) {
entrust.setSuccess("0");
entrust.setT2Result("委托已连续超过7天");
otcEntrustBatchDao.save(entrust);
continue;
}
exec.submit(new Runnable() {
@Override
public void run() {
batchEntrustBiz.entrust(entrust, latch);
}
});
}
}
latch.await(10, TimeUnit.MINUTES);
} catch (Exception e) {
log.error("批量委托出错", e);
} finally {
exec.shutdown();
}
log.info("批量委托结束:" + DateUtil.getNow());
this.backups();
}
@SuppressWarnings("unused")
private boolean compareCurrDate(String startDate, String endDate) {
if (StringUtils.isBlank(startDate)) {
return true;
}
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(DateUtil.parseDate(startDate, DateEnum.UNSIGNED_DATE));
end.setTime(DateUtil.parseDate(endDate, DateEnum.UNSIGNED_DATE));
start.add(Calendar.DAY_OF_MONTH, 7);
return start.after(end);
}
private Map<String, String> getCurrentDate(List<OtcEntrustBatchInfo> entrusts) {
List<String> opStations = new ArrayList<String>();
Map<String, String> currDateMap = new HashMap<String, String>();
for (OtcEntrustBatchInfo entrust : entrusts) {
String station = entrust.getOpStation();
if (StringUtils.isNotBlank(station) && station.length() > 20) {
opStations.add(station);
}
}
if (opStations.isEmpty()) {
return currDateMap;
}
//当天前第七个交易日
Integer sevenDay = otcExchangeDateDao.queryBeforeDay(7);
Map<String, Object> params = new HashMap<String, Object>();
params.put("opStations", opStations);
params.put("initDate", sevenDay);
List<Map<String, Object>> currDates = otcEntrustBatchDao.queryCurrDate(params);
for (Map<String, Object> currDate : currDates) {
currDateMap.put(currDate.get("OP_STATION").toString(),
currDate.get("CURR_DATE").toString());
}
return currDateMap;
}
/**
* 批量委托结束后,将之前的委托记录转移到历史表中。
* 为了防止当天多次执行或者委托有效期变更,备份10个交易日前的数据
*/
private void backups() {
Integer workDay = otcExchangeDateDao.queryBeforeDay(10);
log.info("批量委托备份数据:" + workDay);
otcEntrustBatchDao.backupsEntrust(workDay);
otcEntrustBatchDao.deleteToBackups(workDay);
}
}