EventSubmitTask.java
6.62 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
package com.xiniunet.service.railway.util.thread;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.xiniunet.basic.enumeration.SmsTemplateProviderEnum;
import com.xiniunet.framework.security.Passport;
import com.xiniunet.railway.domain.Event;
import com.xiniunet.railway.request.EventExtendCreateRequest;
import com.xiniunet.railway.request.EventGetRequest;
import com.xiniunet.service.railway.biz.EventExtendManager;
import com.xiniunet.service.railway.biz.EventManager;
import com.xiniunet.service.railway.dal.NoticePersonMapper;
import com.xiniunet.service.railway.po.NoticePersonPO;
import com.xiniunet.service.railway.util.SendSmsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static com.xiniunet.service.railway.Constant.RAILWAY_TENANT_ID;
/**
* @author xiang
*/
public class EventSubmitTask implements Runnable {
private final Logger log = LoggerFactory.getLogger(EventSubmitTask.class);
private final NoticePersonMapper noticePersonMapper;
private final EventExtendManager eventExtendManager;
private final EventManager eventManager;
/**事件id*/
private final Long eventId;
private final Passport passport;
public EventSubmitTask(Long eventId,
EventManager eventManager,
EventExtendManager eventExtendManager,
NoticePersonMapper noticePersonMapper,
Passport passport) {
this.eventId = eventId;
this.eventManager = eventManager;
this.eventExtendManager = eventExtendManager;
this.noticePersonMapper = noticePersonMapper;
this.passport = passport;
}
@Override
public void run() {
EventExtendCreateRequest extendCreateRequest = new EventExtendCreateRequest();
boolean isNoticed = true;
log.warn("=========event_submit---{}", eventId);
try {
if (eventId == null) {
isNoticed = false;
throw new RuntimeException("事件为空,事件id也为空");
}
Event event;
int waitCount = 0;
do {
EventGetRequest getEventRequest = new EventGetRequest();
getEventRequest.setId(eventId);
event = eventManager.get(getEventRequest, passport).getEvent();
if (event == null) {
waitCount++;
try {
// 等待10ms,mysql事务提交
TimeUnit.MILLISECONDS.sleep(20);
log.warn("=========event_submit_eventId={}_waitCount---{}", eventId, waitCount);
} catch (InterruptedException e) {
log.warn(e.getMessage(), e);
}
}
} while (event == null && waitCount < 16);
if (event == null) {
log.error("事件为空:event=null ---- eventId={}", eventId);
isNoticed = false;
return;
}
String description = event.getDescription();
Map<String, String> params = new HashMap<>(6);
params.put("station_name", event.getStation());
params.put("event_time", event.getHappenTime());
params.put("event_number", event.getAppEventId() != null ? event.getAppEventId() : "");
params.put("line_carriage", (event.getLineName() + event.getCarriageNumber()).replaceAll("null", ""));
params.put("event_titile", event.getTitle());
params.put("event_description", description == null ? "" : description.length() > 20 ? description.substring(0, 20) + "... " : description);
List<SmsSendRequest> smsList = new ArrayList<>();
if (!StringUtils.isBlank(event.getStationId()) && !StringUtils.isBlank(event.getLineId())) {
log.warn("=========event_submit_by_station---{}", eventId);
// 按车站发消息
List<NoticePersonPO> persons = noticePersonMapper.queryPersonByStation("line", event.getLineId(), "station", event.getStationId());
if (persons.isEmpty()) {
isNoticed = false;
return;
}
for (NoticePersonPO person : persons) {
smsList.add(getSmsSendRequest(person, JSON.toJSONString(params), "shmetro.station.event.deal"));
}
} else if (StringUtils.isBlank(event.getStationId()) && !StringUtils.isBlank(event.getLineId())) {
log.warn("=========event_submit_by_line---{}", eventId);
// 按线路发消息
List<NoticePersonPO> persons = noticePersonMapper.queryPersonByLine("line", event.getLineId());
if (persons.isEmpty()) {
isNoticed = false;
return;
}
for (NoticePersonPO person : persons) {
smsList.add(getSmsSendRequest(person, JSON.toJSONString(params), "shmetro.carriage.event.deal"));
}
}
SmsSendResponse smsSendResponse = SendSmsUtils.send(smsList);
isNoticed = !smsSendResponse.hasError();
log.warn("eventId-----------------------{}==smsSendResponse===={}", eventId, JSON.toJSONString(smsSendResponse));
} catch (Exception e) {
log.error("=========event_submit_exception---{}", eventId);
log.error("=========event_submit_exception---" + e.getMessage(), e);
isNoticed = false;
} finally {
log.error("=========event_submit_finally---{}--isNoticed={}", eventId, isNoticed);
extendCreateRequest.setId(eventId);
extendCreateRequest.setIsNoticed(isNoticed);
extendCreateRequest.setNoticeTime(isNoticed ? new Date() : null);
extendCreateRequest.setIsHurried(false);
eventExtendManager.create(extendCreateRequest, passport);
}
}
private SmsSendRequest getSmsSendRequest(NoticePersonPO person, String params, String function) {
SmsSendRequest smsSendRequest = new SmsSendRequest();
smsSendRequest.setTenantId(RAILWAY_TENANT_ID);
smsSendRequest.setFunction(function);
smsSendRequest.setParam(params);
smsSendRequest.setMobile(person.getUserMobile());
smsSendRequest.setProvider(SmsTemplateProviderEnum.ALIYUN.name());
smsSendRequest.setBusinessId(eventId);
smsSendRequest.setBusinessType(person.getType());
smsSendRequest.setSign("平安地铁");
return smsSendRequest;
}
}