郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.gk.firework.Scheduls.WarningTask;
 
import com.alibaba.fastjson.JSONObject;
import com.gk.firework.Domain.*;
import com.gk.firework.Domain.Utils.HttpUtils;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Vo.WarnContentVo;
import com.gk.firework.Service.*;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
 
 
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class MessageSendTask {
    private Logger logger = LogManager.getLogger(MessageSendTask.class);
    @Autowired
    SmsLogService smsLogService;
    @Autowired
    WarnContentService warnContentService;
    @Autowired
    SaleOrderService saleOrderInfoService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    UserService userService;
    @Autowired
    RegisterService registerService;
 
    public static String key = "f89279278b739f972ed922fa708eafa0";
    public final static String URL = "http://v.juhe.cn/sms/send";
 
 
    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
    private void EmailSend() throws UnsupportedEncodingException, InterruptedException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MINUTE, -10);
        Date starttime = calendar.getTime();
        String starttimeStr = sdf.format(starttime);
        String endtimeStr = sdf.format(now);
 
        //查询10分钟内需要发短信,但又没发送过的记录
        List<WarnContentVo> warnContentInfoList = warnContentService.selectNeedMail(starttimeStr,endtimeStr);
        if (warnContentInfoList.size() > 0) {
            for (WarnContentVo warnContentInfo : warnContentInfoList){
                if (warnContentInfo.getWarntype().equals("购买超量")){
                    Long enterpriseid = saleOrderInfoService.selectByCustomer(warnContentInfo.getCustomid());
                    List<String> mobiles = selectMobilesByEnterprise(enterpriseid);
                    String content = "姓名:"+warnContentInfo.getPurchasename()+",身份证号:"+warnContentInfo.getIdcard()+" "+warnContentInfo.getWarncontent();
                    String head = "";
                    boolean issend = sendMessage(head,mobiles,content);
                    if (issend){
                        warnContentInfo.setIssend((byte)1);
                        warnContentService.updateById(warnContentInfo);
                    }
                }else if (warnContentInfo.getWarntype().equals("库存超量")){
                    List<String> mobiles = selectMobilesByEnterprise(warnContentInfo.getEnterpriseid());
                    String content = "企业名称:"+warnContentInfo.getEnterprisename()+","+warnContentInfo.getWarncontent();
                    String head = "";
                    boolean issend = sendMessage(head,mobiles,content);
                    if (issend){
                        warnContentInfo.setIssend((byte)1);
                        warnContentService.updateById(warnContentInfo);
                    }
                }
            }
        }
    }
 
    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
    private void registerEmailSend() throws UnsupportedEncodingException {
        List<RegisterInfo> registerInfos = registerService.needSendSmsList();
        if (registerInfos.size() > 0){
            for (RegisterInfo registerInfo : registerInfos){
                String tplId;
                if (registerInfo.getReviewresult().equals((byte)1)){
                    tplId = "235091";
                }else {
                    tplId = "235094";
                }
                boolean isSend = sendSms(registerInfo.getEnterprisename(),registerInfo.getLegalpersonphone(),tplId);
                if (isSend){
                    registerInfo.setSmsat(new Date());
                    registerInfo.setSmstimes(registerInfo.getSmstimes()+1);
                    registerInfo.setIssms((byte)1);
                    registerService.updateById(registerInfo);
                }
            }
        }
 
    }
 
//    private boolean issendTest = false;
//    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
//    private void EmailSendTest() throws UnsupportedEncodingException, InterruptedException {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date now = new Date();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(now);
//        calendar.add(Calendar.MINUTE, -10);
//        Date starttime = calendar.getTime();
//
//        List<String> mobiles = new ArrayList<>();
//        mobiles.add("15651251027");
//        String content = "测试短信";
//        String head = "";
//        if (!issendTest){
//            issendTest = sendMessage(head,mobiles,content);
//        }
//    }
 
 
 
    private List<String> selectMobilesByEnterprise(Long enterpriseid) {
        List<String> mobiles = new ArrayList<> ();
        //先查直属监管部门,若不存在则再查上一级,直到查到对应监管部门用户
        Enterprise enterprise = enterpriseService.getById(enterpriseid);
        UserInfo userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),enterprise.getDistrict(),enterprise.getStreet(),enterprise.getCommittee());
        if (userInfo == null){
            userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),enterprise.getDistrict(),enterprise.getStreet(),null);
            if (userInfo == null){
                userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),enterprise.getDistrict(),null,null);
                if (userInfo == null){
                    userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),null,null,null);
                    if (userInfo == null){
                        userInfo = userService.selectSupervise(enterprise.getProvince(),null,null,null,null);
                        if (userInfo == null){
                            userInfo = userService.selectSupervise(null,null,null,null,null);
                        }
                    }
                }
            }
        }
        if (userInfo != null && StringUtils.isNotBlank(userInfo.getMobile())){
            mobiles.add(userInfo.getMobile());
        }
        return mobiles;
    }
 
 
    public Boolean sendMessage(String head,List<String> mobiles,String content) throws UnsupportedEncodingException {
        String url ="http://v.juhe.cn/sms/send";//请求接口地址
        String tpl_id = "231913";
        String dtype = "json";
        String tpl_value = "#name#=" + head + "&#code#=" + content;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Date now = new Date();
        String starttime  = sdfstart.format(now);
        String endtime = sdfend.format(now);
 
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        Date onehour = calendar.getTime();
        String onehourstart = sdf.format(onehour);
        String onehourend = sdf.format(now);
 
        boolean issend = false;
        for (String mobile : mobiles) {
            /**运营商限制同1个号码同1个签名的内容1分钟内只能接收1条,10分钟3条,1小时内4条,一天20条,否则可能会被运营商屏蔽**/
            SmsLogInfo smsLogInfo = smsLogService.selectByTel(mobile,starttime,endtime,(byte)2);
            if (null != smsLogInfo && smsLogInfo.getTimes()>=20){
                logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",当天已发出20条,无法继续发送");
                continue;
            }
            SmsLogInfo smsLogInfoHour = smsLogService.selectByTel(mobile,onehourstart,onehourend,(byte)1);
            if (null != smsLogInfoHour && smsLogInfoHour.getTimes()>=4){
                logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",本小时已发出4条,无法继续发送");
                continue;
            }
            String respnse = null;
            Map params = new HashMap();//请求参数
            params.put("mobile", mobile);//接收短信的手机号码
            params.put("tpl_id", tpl_id);//短信模板ID,请参考个人中心短信模板设置
            params.put("tpl_value", tpl_value);
            params.put("key", key);//应用APPKEY(应用详细页查询)
            params.put("dtype", dtype);//返回数据的格式,xml或json,默认json
            try {
                respnse = HttpUtils.net(url, params, "GET");
                JSONObject jsonResult = JSONObject.parseObject(respnse);
                String errorcode = jsonResult.getString("error_code");
                String reason = jsonResult.getString("reason");
                if (errorcode.equals("0")) {
                    //增加发送次数
                    JSONObject result = jsonResult.getJSONObject("result");
                    Integer count = result.getInteger("count");
                    if (null !=  smsLogInfo){
                        smsLogInfo.setTimes(smsLogInfo.getTimes()+count);
                        smsLogService.updateById(smsLogInfo);
                    }else {
                        SmsLogInfo smsLogInfo1 = new SmsLogInfo();
                        smsLogInfo1.setMobile(mobile);
                        smsLogInfo1.setSendtime(now);
                        smsLogInfo1.setTimes(count);
                        smsLogInfo1.setType((byte)2);
                        smsLogService.save(smsLogInfo1);
                    }
                    issend = true;
                    if (null !=  smsLogInfoHour){
                        smsLogInfoHour.setTimes(smsLogInfoHour.getTimes()+count);
                        smsLogService.updateById(smsLogInfoHour);
                    }else {
                        SmsLogInfo smsLogInfo1 = new SmsLogInfo();
                        smsLogInfo1.setMobile(mobile);
                        smsLogInfo1.setSendtime(now);
                        smsLogInfo1.setTimes(count);
                        smsLogInfo1.setType((byte)1);
                        smsLogService.save(smsLogInfo1);
                    }
                } else {
                    logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + "发送短信失败,原因为:" + reason);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return issend;
    }
 
    public Boolean sendSms(String head, String mobile, String tplId) throws UnsupportedEncodingException {
        String dtype = "json";
        String tplValue = "#name#=" + head;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Date now = new Date();
        String starttime  = sdfstart.format(now);
        String endtime = sdfend.format(now);
 
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        Date onehour = calendar.getTime();
        String onehourstart = sdf.format(onehour);
        String onehourend = sdf.format(now);
 
        boolean isSend = false;
 
        /** 运营商限制同1个号码同1个签名的内容1分钟内只能接收1条,10分钟3条,1小时内4条,一天20条,否则可能会被运营商屏蔽 **/
        SmsLogInfo smsLogInfo = smsLogService.selectByTel(mobile,starttime,endtime,(byte)2);
        if (null != smsLogInfo && smsLogInfo.getTimes()>= 20){
            logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",当天已发出20条,无法继续发送");
            return false;
        }
        SmsLogInfo smsLogInfoHour = smsLogService.selectByTel(mobile,onehourstart,onehourend,(byte)1);
        if (null != smsLogInfoHour && smsLogInfoHour.getTimes()>= 4){
            logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",本小时已发出4条,无法继续发送");
            return false;
        }
        String response;
        Map params = new HashMap();//请求参数
        params.put("mobile", mobile);//接收短信的手机号码
        params.put("tpl_id", tplId);//短信模板ID,请参考个人中心短信模板设置
        params.put("tpl_value", tplValue);
        params.put("key", key);//应用APPKEY(应用详细页查询)
        params.put("dtype", dtype);//返回数据的格式,xml或json,默认json
        try {
            response = HttpUtils.net(URL, params, "GET");
            JSONObject jsonResult = JSONObject.parseObject(response);
            String errorcode = jsonResult.getString("error_code");
            String reason = jsonResult.getString("reason");
            if (errorcode.equals("0")) {
                //增加发送次数
                JSONObject result = jsonResult.getJSONObject("result");
                Integer count = result.getInteger("count");
                if (null !=  smsLogInfo){
                    smsLogInfo.setTimes(smsLogInfo.getTimes()+count);
                    smsLogService.updateById(smsLogInfo);
                }else {
                    SmsLogInfo smsLogInfo1 = new SmsLogInfo();
                    smsLogInfo1.setMobile(mobile);
                    smsLogInfo1.setSendtime(now);
                    smsLogInfo1.setTimes(count);
                    smsLogInfo1.setType((byte)2);
                    smsLogService.save(smsLogInfo1);
                }
                isSend = true;
                if (null !=  smsLogInfoHour){
                    smsLogInfoHour.setTimes(smsLogInfoHour.getTimes()+count);
                    smsLogService.updateById(smsLogInfoHour);
                }else {
                    SmsLogInfo smsLogInfo1 = new SmsLogInfo();
                    smsLogInfo1.setMobile(mobile);
                    smsLogInfo1.setSendtime(now);
                    smsLogInfo1.setTimes(count);
                    smsLogInfo1.setType((byte)1);
                    smsLogService.save(smsLogInfo1);
                }
            } else {
                logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + "发送短信失败,原因为:" + reason);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSend;
    }
 
 
 
 
}