LiveApplyJob.java
10.1 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
package com.cnlive.shenhe.job;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyEmail;
import com.cnlive.shenhe.entity.ShyLiveapply;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.mapper.ShyLiveapplyMapper;
import com.cnlive.shenhe.service.EmailService;
import com.cnlive.shenhe.service.LiveApplyService;
import com.cnlive.shenhe.service.SitesService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.HttpUtil;
import com.cnlive.shenhe.utils.OpenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Administrator
*/
@Component
@Async
public class LiveApplyJob {
private static Logger logger = LoggerFactory.getLogger(LiveApplyJob.class);
/**
* //开发者对应的id,与app_key相对应
*/
@Value("${email_app_id}")
private String email_app_id;
/**
* //开发者帐号对应的key. 请开发者谨慎保管此值。
*/
@Value("${email_app_key}")
private String email_app_key;
/**
* //调用邮件发送服务器的地址
*/
@Value("${email_send_URL}")
private String email_send_URL;
@Value("${mobile_send_URL}")
private String mobile_send_URL;
@Autowired
SitesService sitesService;
@Autowired
LiveApplyService liveApplyService;
@Autowired
ShyLiveapplyMapper shyLiveApplyMapper;
@Autowired
EmailService emailService;
@Scheduled(fixedRate = 60 * 1000)
public void updateLiveApply() {
//遍历免审的spid去更改状态值
ShyLiveapply shyLiveApply = new ShyLiveapply();
// 审核类型为免审
shyLiveApply.setShenhe_type(CommonConst.AUDIT_TYPE_FREE);
shyLiveApply.setShenhe_state(CommonConst.AUDIT_INTT);
List<ShyLiveapply> liveApplyList = liveApplyService.findShyLiveApply(shyLiveApply);
if (liveApplyList.size() > 0) {
for (ShyLiveapply liveApply : liveApplyList) {
liveApplyService.updateLiveApply(liveApply.getId(), "", "白名单", CommonConst.AUDIT_SUCCESS, "白名单通过");
}
}
}
/**
* 直播申请邮件通知 五分钟一次
* 检测未通知的邮箱进行通知
*/
@Scheduled(fixedRate = 5 * 60 * 1000)
public void autoLiveApplyEmailNotice() {
Example example = new Example(ShyLiveapply.class);
Criteria criteria = example.createCriteria();
//审核通过的
criteria.andEqualTo("shenhe_state", CommonConst.AUDIT_SUCCESS);
//状态等于未通知
criteria.andEqualTo("email_notice_state", CommonConst.NOTICE_INTT);
List<ShyLiveapply> shyLiveApplyList = shyLiveApplyMapper.selectByExample(example);
if (shyLiveApplyList.size() > 0) {
//开发者对应的id,与app_key相对应
String appId = email_app_id;
//开发者帐号对应的key. 请开发者谨慎保管此值。
String appKey = email_app_key;
//调用邮件发送服务器的地址
String url = email_send_URL;
List<ShyEmail> shyEmailList = emailService.getEmailSendList(CommonConst.EMAIL_LIVEAPPLY);
logger.info("直播申请邮件通知开始,共" + shyLiveApplyList.size() + "个申请,每个申请通知" + shyEmailList.size() + "个邮箱");
for (ShyLiveapply liveApply : shyLiveApplyList) {
logger.info("当前发送邮件的直播申请id为:" + liveApply.getId());
//初始化通知状态
Integer noticeState = liveApply.getEmail_notice_state();
//初始化通知消息
StringBuilder noticeMsg = new StringBuilder(liveApply.getEmail_notice_msg());
//显示spid简称
ShySites shySites = sitesService.findspidDescByspid(liveApply.getSp_id());
if (CommonUtils.isNotNull(shySites)) {
liveApply.setSpid_desc(shySites.getName());
}
StringBuilder content = new StringBuilder();
//循环需要发送邮件的邮箱
for (ShyEmail shyEmail : shyEmailList) {
String startTime = CommonUtils.formatDate(liveApply.getPre_start_time(), "yyyy-MM-dd HH:mm:ss");
String endTime = CommonUtils.isNotNull(liveApply.getPre_end_time()) ? CommonUtils.formatDate(liveApply.getPre_end_time(), "yyyy-MM-dd HH:mm:ss") : "待定";
content.append(liveApply.getSender()).append("__").append(liveApply.getMobile()).append("__").append(liveApply.getOrganization()).append("__").append(liveApply.getTheme()).append("__").append(startTime).append("__").append(endTime).append("__").append(liveApply.getLive_place()).append("__").append(liveApply.getPush_url()).append("__").append(liveApply.getPull_source()).append(",").append(liveApply.getPull_url());
//获取一个格式化后的当前时间
String dateTime = CommonUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss");
String emailAddr = shyEmail.getEmail();
String title = liveApply.getSpid_desc() + "新直播开播申请";
String templateId = "4017";
Map<String, String> emailparams = new HashMap<>(16);
emailparams.put("appId", appId);
emailparams.put("emailAddr", emailAddr);
emailparams.put("title", title);
emailparams.put("content", content.toString());
emailparams.put("templateId", templateId);
emailparams.put("sign", OpenUtil.sign(emailparams, appKey));
HttpUtil httpUtil = HttpUtil.init();
httpUtil.setParamMap(emailparams);
try {
Map<String, String> post = httpUtil.post(url);
logger.info("当前发送邮件用户为:" + emailAddr + ",发送邮件的直播申请审核云id为:" + liveApply.getId() + "结果为:" + post.get("result"));
JSONObject result = JSONObject.parseObject(post.get("result"));
if (result == null) {
noticeState = CommonConst.NOTICE_FAIL;
noticeMsg.append("\n").append(dateTime).append(" 邮件服务器调用失败,result为空");
break;
}
Integer code = result.getInteger("errorCode");
if (code != 0) {
noticeState = CommonConst.NOTICE_FAIL;
noticeMsg.append("\n").append(dateTime).append(" 邮件服务器调用失败,result返回消息为:").append(result.getString("errorMessage"));
break;
} else {
noticeState = CommonConst.NOTICE_SUCCESS;
noticeMsg.append("\n").append(dateTime).append(" 邮件").append(emailAddr).append("(").append(shyEmail.getRemark()).append(")发送成功");
}
} catch (Exception e) {
logger.info("异常结果为:{}", e.getMessage());
noticeState = CommonConst.NOTICE_FAIL;
noticeMsg.append("\n").append(dateTime).append(" 邮件服务器调用失败,服务器报错了");
break;
}
}
liveApply.setEmail_notice_state(noticeState);
liveApply.setEmail_notice_msg(noticeMsg.toString());
liveApplyService.updateShyLiveApply(liveApply);
}
}
}
/**
* 每个小时执行一次,定时修改直播申请库中的邮箱通知状态
* 检测邮箱通知失败状态的邮箱状态改为未通知状态
*/
@Scheduled(fixedRate = 60 * 60 * 1000)
public void autoLiveApplyEmailNoticeStateModify() {
Example example = new Example(ShyLiveapply.class);
Criteria criteria = example.createCriteria();
//审核通过的
criteria.andEqualTo("shenhe_state", CommonConst.AUDIT_SUCCESS);
//状态等于通知失败的
criteria.andEqualTo("email_notice_state", CommonConst.NOTICE_FAIL);
List<ShyLiveapply> shyLiveApplyList = shyLiveApplyMapper.selectByExample(example);
logger.info("邮箱通知失败的修改数量:{}", shyLiveApplyList.size());
for (ShyLiveapply shyLiveapply : shyLiveApplyList) {
if (CommonUtils.isNotNull(shyLiveapply)) {
//修改为未通知 继续发送
shyLiveapply.setEmail_notice_state(CommonConst.NOTICE_INTT);
liveApplyService.updateShyLiveApply(shyLiveapply);
}
}
}
/**
* 自动退领
* 每天0点执行
*/
@Scheduled(cron = "0 0 0 * * ? ")
public void autoRetReceive() {
logger.info("直播申请自动退领的进入.....");
ShyLiveapply liveApply = new ShyLiveapply();
//审核状态:未审核
liveApply.setShenhe_state(CommonConst.AUDIT_INTT);
//已领取
liveApply.setReceive(CommonConst.RECEIVE_AFTER);
List<ShyLiveapply> shyLiveapplyList = liveApplyService.findShyLiveApply(liveApply);
if (shyLiveapplyList.size() > 0) {
for (ShyLiveapply liveapply : shyLiveapplyList) {
logger.info("自动退领的liveApply_id:" + liveapply.getId());
//未领取
liveapply.setReceive(CommonConst.RECEIVE_BEFORE);
liveapply.setAuditor(null);
liveApplyService.updateShyLiveApply(liveApply);
}
}
}
}