admin
2019-09-03 be19444704d40195c1355e471f1316f7419b930b
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package com.yeshi.fanli.service.impl.push;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.controller.admin.PushController;
import com.yeshi.fanli.dao.mybatis.AccountMessageMapper;
import com.yeshi.fanli.dao.mybatis.push.PushRecordMapper;
import com.yeshi.fanli.dto.push.PushTypeEnum;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.bus.user.AccountMessage;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.system.SystemZnx;
import com.yeshi.fanli.entity.xinge.PushRecord;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.push.DeviceTokenHWService;
import com.yeshi.fanli.service.inter.push.HWPushService;
import com.yeshi.fanli.service.inter.push.IOSPushService;
import com.yeshi.fanli.service.inter.push.PushRecordService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.service.inter.push.XMPushService;
import com.yeshi.fanli.service.inter.user.SystemZnxService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class PushServiceImpl implements PushService {
 
    static String IOSBundleId = "";
    static String PROJECT_NAME = "";
 
    static {
        IOSBundleId = Constant.systemCommonConfig.getIosBundleId();
        PROJECT_NAME = Constant.systemCommonConfig.getProjectChineseName();
    }
 
    @Resource
    private PushRecordService pushRecordService;
 
    @Resource
    private SystemZnxService systemZnxService;
 
    @Resource
    private DeviceTokenHWService deviceTokenHWService;
 
    @Resource
    private IOSPushService iosPushService;
 
    @Resource
    private HWPushService hwPushService;
 
    @Resource
    private XMPushService xmPushService;
 
    @Resource
    private PushRecordMapper pushRecordMapper;
 
    @Resource
    private AccountMessageMapper accountMessageMapper;
 
    @Resource
    private AppVersionService appVersionService;
 
    @Override
    public void pushGoods(Long uid, String title, String content, String url, List<String> listIOS,
            List<String> listAndroid) throws PushException {
        String gids = url.split("id=")[1];
        String gid = gids.split("&")[0];
        if (StringUtil.isNullOrEmpty(gid))
            throw new PushException(1, "商品解析失败");
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "请填写推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "请填写推送内容");
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:0
            String codes = getEffectiveVersionCodes(0, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushGoods(uid, Long.parseLong(gid), title, content, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:0
            String versions = getEffectiveVersions(0, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushGoods(uid, Long.parseLong(gid), title, content, versions);
            }
            // 华为推送
            if (listAndroid == null || listAndroid.size() > 0) {
                List<Integer> versionCodeList = null;
                if (listAndroid != null)
                    versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
                hwPushService.pushGoods(uid, Long.parseLong(gid), title, content, versionCodeList);
            }
        }
 
        PushRecord pushRecord = new PushRecord();
        pushRecord.setUid(uid);
        pushRecord.setState(1);
        pushRecord.setType(PushTypeEnum.goodsdetail.getCode()); // 都成功
        pushRecord.setUrl(url);
        pushRecord.setTitle(title);
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setAccount(1);
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
    @Override
    public void pushUrl(Long uid, String title, String content, String url, List<String> listIOS,
            List<String> listAndroid) throws PushException {
 
        if (StringUtil.isNullOrEmpty(url))
            throw new PushException(1, "无推送链接");
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "无推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "无推送内容");
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:0
            String codes = getEffectiveVersionCodes(0, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushUrl(uid, url, title, content, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:0
            String versions = getEffectiveVersions(0, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushUrl(uid, url, title, content, versions);
            }
 
            // 华为推送
            if (listAndroid == null || listAndroid.size() > 0) {
                List<Integer> versionCodeList = null;
                if (listAndroid != null)
                    versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
                hwPushService.pushUrl(uid, url, title, content, versionCodeList);
            }
        }
 
        PushRecord pushRecord = new PushRecord();
        pushRecord.setUid(uid);
        pushRecord.setState(1);
        pushRecord.setType(PushTypeEnum.url.getCode());
        pushRecord.setUrl(url);
        pushRecord.setTitle(title);
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
    @Override
    public void pushZNX(Long uId, String title, String content, List<String> listIOS, List<String> listAndroid)
            throws PushException {
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "无推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "无推送内容");
 
        SystemZnx systemZnx = null;
        AccountMessage accountMessage = null;
 
        /* 无uid , 广播 数据插入系统表 */
        if (uId == null || uId == 0) {
            // 插入数据库(系统站内信数据)
            systemZnx = new SystemZnx();
            systemZnx.setTitle(title);
            systemZnx.setContent(content);
            systemZnx.setCreateTime(System.currentTimeMillis());
            systemZnxService.save(systemZnx);
        } else {
            accountMessage = new AccountMessage();
            accountMessage.setCreateTime(System.currentTimeMillis());
            accountMessage.setIsOpen(false);
            accountMessage.setUserInfo(new UserInfo(uId));
            accountMessage.setTitle(title);
            accountMessage.setContent(content);
            accountMessageMapper.insertSelective(accountMessage);
        }
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:0
            String codes = getEffectiveVersionCodes(0, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushZNX(uId, accountMessage, systemZnx, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:0
            String versions = getEffectiveVersions(0, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushZNX(uId, accountMessage, systemZnx, versions);
            }
 
            // 华为推送
            if (listAndroid == null || listAndroid.size() > 0) {
                List<Integer> versionCodeList = null;
                if (listAndroid != null)
                    versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
                hwPushService.pushZNX(uId, title, content, versionCodeList);
            }
        }
 
        // 插入推送记录
        PushRecord pushRecord = new PushRecord();
        pushRecord.setUid(uId);
        pushRecord.setTitle(title);
        pushRecord.setState(1); // 成功
        pushRecord.setType(PushTypeEnum.ZNX.getCode()); // 类型:站内信
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
    @Override
    public void pushWEEX(Long uid, String title, String content, String weexUrl, List<String> listIOS,
            List<String> listAndroid) throws PushException {
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "无推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "无推送内容");
 
        // 适用版本 1.4.8及以后 推送weex页面
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:0
            String codes = getEffectiveVersionCodes(0, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushWEEX(uid, title, content, weexUrl, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:31
            String versions = getEffectiveVersions(0, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushWEEX(uid, title, content, weexUrl, versions);
            }
        }
 
        // 华为推送
        if (listAndroid == null || listAndroid.size() > 0) {
            List<Integer> versionCodeList = null;
            if (listAndroid != null)
                versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
            hwPushService.pushWEEX(uid, title, content, weexUrl, versionCodeList);
        }
 
        // 插入推送记录
        PushRecord pushRecord = new PushRecord();
        pushRecord.setTitle(title);
        pushRecord.setUid(uid);
        pushRecord.setState(1); // 成功
        pushRecord.setType(PushTypeEnum.weex.getCode()); // 类型:WEEX
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
    @Override
    public void pushBaiChuanUrl(Long uid, String title, String content, String url, List<String> listIOS,
            List<String> listAndroid) throws PushException {
 
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "无推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "无推送内容");
 
        // 适用版本 1.4.8及以后 推送百川网页
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:0
            String codes = getEffectiveVersionCodes(0, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushBaiChuanUrl(uid, title, content, url, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:31
            String versions = getEffectiveVersions(31, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushBaiChuanUrl(uid, title, content, url, versions);
            }
        }
 
        // 华为推送
        if (listAndroid == null || listAndroid.size() > 0) {
            List<Integer> versionCodeList = null;
            if (listAndroid != null)
                versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
            hwPushService.pushBaiChuanUrl(uid, title, content, url, versionCodeList);
        }
 
        // 插入推送记录
        PushRecord pushRecord = new PushRecord();
        pushRecord.setUrl(url);
        pushRecord.setTitle(title);
        pushRecord.setUid(uid);
        pushRecord.setState(1); // 成功
        pushRecord.setType(PushTypeEnum.baichuan.getCode()); // 类型:百川
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
    @Override
    public void pushWelfareCenter(Long uid, String title, String content, List<String> listIOS,
            List<String> listAndroid) throws PushException {
 
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "无推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "无推送内容");
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:0
            String codes = getEffectiveVersionCodes(0, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushWelfareCenter(uid, title, content, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:36
            String versions = getEffectiveVersions(36, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushWelfareCenter(uid, title, content, versions);
            }
        }
 
        // 华为推送
        if (listAndroid == null || listAndroid.size() > 0) {
            List<Integer> versionCodeList = null;
            if (listAndroid != null)
                versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
            hwPushService.pushWelfareCenter(uid, title, content, versionCodeList);
        }
 
        // 插入推送记录
        PushRecord pushRecord = new PushRecord();
        pushRecord.setTitle(title);
        pushRecord.setUid(uid);
        pushRecord.setState(1); // 成功
        pushRecord.setType(PushTypeEnum.welfare.getCode()); // 类型:百川
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
    /**
     * 查询能推送版本号
     * 
     * @param versionCode
     * @param listVersion
     * @return
     */
    private String getEffectiveVersions(int minCode, String type, List<String> listVersion) throws PushException {
 
        if (minCode == 0 && listVersion == null) {
            return null;
        }
 
        String versions = "";
        if (listVersion == null) {
            List<AppVersionInfo> list = appVersionService.listByPlatformAndMinVersionCode(type, minCode);
            if (list != null) {
                for (AppVersionInfo appVersion : list) {
                    versions += appVersion.getVersion() + ",";
                }
            }
        } else {
            List<AppVersionInfo> list = appVersionService.listByVersions(type, listVersion);
            if (list != null) {
                for (AppVersionInfo appVersion : list) {
                    Integer code = appVersion.getVersionCode();
                    if (code != null && code >= minCode) {
                        versions += appVersion.getVersion() + ",";
                    }
                }
            }
        }
 
        if (versions.endsWith(",")) {
            versions = versions.substring(0, versions.length() - 1);
        }
 
        return versions;
    }
 
    /**
     * 查询能推送版本号code
     * 
     * @param versionCode
     * @param listVersion
     * @return
     */
    private String getEffectiveVersionCodes(int minCode, String type, List<String> listVersion) throws PushException {
 
        if (minCode == 0 && listVersion == null) {
            return null;
        }
 
        String versionCodes = "";
        if (listVersion == null) {
            List<AppVersionInfo> list = appVersionService.listByPlatformAndMinVersionCode(type, minCode);
            if (list != null) {
                for (AppVersionInfo appVersion : list) {
                    versionCodes += appVersion.getVersionCode() + ",";
                }
            }
        } else {
            List<AppVersionInfo> list = appVersionService.listByVersions(type, listVersion);
            if (list != null) {
                for (AppVersionInfo appVersion : list) {
                    Integer code = appVersion.getVersionCode();
                    if (code != null && code >= minCode) {
                        versionCodes += appVersion.getVersionCode() + ",";
                    }
                }
            }
        }
 
        if (versionCodes.endsWith(",")) {
            versionCodes = versionCodes.substring(0, versionCodes.length() - 1);
        }
 
        return versionCodes;
    }
 
    @Override
    public void pushUserSignInNotification(Long uid, String title, String content, List<String> listIOS,
            List<String> listAndroid) throws PushException {
        if (StringUtil.isNullOrEmpty(title))
            throw new PushException(1, "无推送标题");
        if (StringUtil.isNullOrEmpty(content))
            throw new PushException(1, "无推送内容");
 
        /* IOS端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listIOS == null || listIOS.size() > 0) {
            // 限制推送版本号:1.6.5以后
            String codes = getEffectiveVersionCodes(60, AppVersionInfo.PLATFORM_IOS, listIOS);
            if (listIOS == null || (listIOS.size() > 0 && codes != null && codes.trim().length() > 0)) {
                iosPushService.pushUserSignInNotification(uid, title, content, codes);
            }
        }
 
        /* Android端推送 (注明:list等于 'null' 时全推, size等于0 不做推送) */
        if (listAndroid == null || listAndroid.size() > 0) {
            // 限制推送版本号:1.6.5以后
            String versions = getEffectiveVersions(47, AppVersionInfo.PLATFORM_ANDROID, listAndroid);
            if (listAndroid == null || (listAndroid.size() > 0 && versions != null && versions.trim().length() > 0)) {
                xmPushService.pushUserSignInNotification(uid, title, content, versions);
            }
        }
 
        // 华为推送
        if (listAndroid == null || listAndroid.size() > 0) {
            List<Integer> versionCodeList = null;
            if (listAndroid != null)
                versionCodeList = appVersionService.listVersionCodeByVersions("android", listAndroid);
            hwPushService.pushUserSignInNotification(uid, title, content, versionCodeList);
        }
 
        // 插入推送记录
        PushRecord pushRecord = new PushRecord();
        pushRecord.setTitle(title);
        pushRecord.setUid(uid);
        pushRecord.setState(1); // 成功
        pushRecord.setType(PushTypeEnum.signin.getCode()); // 类型:百川
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
        pushRecord.setCreatetime(java.lang.System.currentTimeMillis());
        pushRecordMapper.insertSelective(pushRecord);
    }
 
}