yujian
2020-05-17 facf0c3bcbbb6cc993d87caa12ac34bede690f38
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package com.yeshi.fanli.service.impl.push;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.push.PushGoodsMapper;
import com.yeshi.fanli.entity.dynamic.GoodsEvaluate;
import com.yeshi.fanli.entity.dynamic.GoodsEvaluate.EvaluateEnum;
import com.yeshi.fanli.entity.dynamic.ImgInfo;
import com.yeshi.fanli.entity.dynamic.ImgInfo.ImgEnum;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.push.PushGoods;
import com.yeshi.fanli.entity.push.PushGoodsGroup;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.exception.push.PushGoodsException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.push.PushGoodsGroupService;
import com.yeshi.fanli.service.inter.push.PushGoodsService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class PushGoodsServiceImpl implements PushGoodsService {
 
    @Resource
    private PushService pushService;
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private PushGoodsMapper pushGoodsMapper;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Resource
    private PushGoodsGroupService pushGoodsGroupService;
    
    @Resource
    private GoodsEvaluateService goodsEvaluateService;
    
    
 
    @Override
    public int deleteByPrimaryKey(Long id) throws PushGoodsException {
        return pushGoodsMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(PushGoods record) throws PushGoodsException {
        return pushGoodsMapper.insert(record);
    }
 
    @Override
    public int insertSelective(PushGoods record) throws PushGoodsException {
        return pushGoodsMapper.insertSelective(record);
    }
 
    @Override
    public PushGoods selectByPrimaryKey(Long id) {
        return pushGoodsMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(PushGoods record) throws PushGoodsException {
        return pushGoodsMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(PushGoods record) throws PushGoodsException {
        return pushGoodsMapper.updateByPrimaryKey(record);
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void save(PushGoods record, List<Long> goodsIds) throws PushGoodsException, Exception {
        // 定时时间
        if (StringUtil.isNullOrEmpty(record.getControlTime_str())) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " ")));
        }
 
        record.setIsPush(PushGoods.STATE_INIT);
        record.setType(PushGoods.TYPE_GOODS);
        
        boolean isAdd = false;
        // 推送id
        Long pushId = record.getId();
        if (pushId == null) {
            // 新增
            isAdd = true;
 
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            pushGoodsMapper.insert(record);
 
            pushId = record.getId();
 
        } else {
            // 修改
            PushGoods current = pushGoodsMapper.selectByPrimaryKey(pushId);
            if (current == null) {
                throw new PushGoodsException(1, "该记录已不存在");
            }
            
            record.setCreateTime(current.getCreateTime());
            record.setUpdateTime(new Date());
            pushGoodsMapper.updateByPrimaryKey(record);
        }
 
        // 处理商品信息
        saveGoodsInfo(isAdd, pushId, goodsIds);
    }
 
    @Transactional(rollbackFor=Exception.class)
    public void saveGoodsInfo(boolean isAdd, Long pushId, List<Long> goodsIds) throws Exception {
        if (goodsIds == null || goodsIds.size() == 0) {
            if (!isAdd) {
                pushGoodsGroupService.deleteByPushId(pushId);
            }
            return;
        }
 
        // 商品信息获取与保存
        List<Long> listCommonId = new ArrayList<Long>();
        for (Long auctionId : goodsIds) {
            // 获取商品详情
            try {
                TaoBaoGoodsBrief goodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
                // 转换简版商品信息
                CommonGoods commonGoods = CommonGoodsFactory.create(goodsBrief);
                if (commonGoods != null) {
                    commonGoodsService.addOrUpdateCommonGoods(commonGoods);
                    Long cid = commonGoods.getId();
                    if (cid != null) {
                        listCommonId.add(cid);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        /* 修改时删除 部分 */
        if (!isAdd) {
            List<Long> listdelete = new ArrayList<Long>();
            List<PushGoodsGroup> listGroup = pushGoodsGroupService.selectByPushId(pushId);
            if (listGroup != null && listGroup.size() > 0) {
                for (PushGoodsGroup group : listGroup) {
                    Long groupId = group.getId();
 
                    CommonGoods commonGoods = group.getCommonGoods();
                    if (commonGoods != null) {
                        Long commonId = commonGoods.getId();
                        if (commonId != null && listCommonId.contains(groupId)) {
                            // 已经存在
                            listCommonId.remove(commonId);
                        } else {
                            // 已经被删除
                            listdelete.add(groupId);
                        }
                    }
                }
            }
 
            // 删除已被前端清除商品
            if (listdelete.size() > 0) {
                pushGoodsGroupService.deleteBatchByPrimaryKey(listdelete);
            }
        }
 
        // 插入商品列表
        if (listCommonId.size() > 0 && !listCommonId.isEmpty()) {
            List<PushGoodsGroup> listAdd = new ArrayList<PushGoodsGroup>();
            for (Long cid : listCommonId) {
                PushGoodsGroup pushGoodsGroup = new PushGoodsGroup();
                pushGoodsGroup.setPushId(pushId);
                pushGoodsGroup.setCommonGoods(new CommonGoods(cid));
                listAdd.add(pushGoodsGroup);
            }
            pushGoodsGroupService.insertBatch(listAdd);
        }
    }
    
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveInfo(PushGoods record) throws PushGoodsException, Exception {
        if (StringUtil.isNullOrEmpty(record.getOtherId())) {
            throw new PushGoodsException(1, "相关内容ID不能为空");
        }
        
        if (StringUtil.isNullOrEmpty(record.getTitle())) {
            throw new PushGoodsException(1, "标题不能为空");
        }
        
        if (StringUtil.isNullOrEmpty(record.getContent())) {
            throw new PushGoodsException(1, "内容不能为空");
        }
        
        if (record.getType() == null) {
            throw new PushGoodsException(1, "推送类型不能为空");
        }
        
        if (record.getType() == PushGoods.TYPE_ACTIVITY) {
            GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(record.getOtherId());
            if (goodsEvaluate == null) {
                throw new PushGoodsException(1, "发圈活动信息不存在");
            }
            
            if (goodsEvaluate.getType() != EvaluateEnum.activity) {
                throw new PushGoodsException(1, "该内容非发圈活动信息");
            }
            
            String picUrl = null;
            List<ImgInfo> imgList = goodsEvaluate.getImgList();
            if (imgList != null && imgList.size() > 0) {
                for (ImgInfo imgInfo: imgList) {
                    if (imgInfo.getType() == ImgEnum.img && !StringUtil.isNullOrEmpty(imgInfo.getUrl())) {
                        picUrl = imgInfo.getUrl();
                    }
                }
            }
            if (StringUtil.isNullOrEmpty(picUrl)) {
                throw new PushGoodsException(1, "该非发圈活动无图片信息");
            }
            record.setPicture(picUrl);
        }
         
        
        // 定时时间
        if (StringUtil.isNullOrEmpty(record.getControlTime_str())) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " ")));
        }
        // 浏览数量随机
        if (record.getScanNum() == null || record.getScanNum()  <= 0)
            record.setScanNum((int) (Math.random() * 5000) + 1000);
        
        record.setIsPush(PushGoods.STATE_INIT);
        record.setAlertTitle(record.getTitle());
        record.setAlertContent(record.getContent());
        
        Long id = record.getId();
        if (id == null) {
            record.setCreateTime(new Date());
            pushGoodsMapper.insert(record);
        } else {
            PushGoods current = pushGoodsMapper.selectByPrimaryKey(id);
            if (current == null) {
                throw new PushGoodsException(1, "该记录已不存在");
            }
            record.setCreateTime(current.getCreateTime());
            record.setScanNum(current.getScanNum());
            record.setUpdateTime(new Date());
            pushGoodsMapper.updateByPrimaryKey(record);
        }
    }
 
    
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int deleteBatchByPrimaryKey(List<Long> list) throws Exception {
 
        /*List<PushGoods> lisState = pushGoodsMapper.listByPushState(list);
        if (lisState != null && lisState.size() > 0) {
            for (PushGoods pushGoods : lisState) {
                // 已推送的消息不能删除
                list.remove(pushGoods.getId());
            }
        }*/
 
        // 删除对应商品
        pushGoodsGroupService.deleteBatchByPushId(list);
 
        return pushGoodsMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public List<PushGoods> listQuery(long start, int count, String key, Integer state) {
        return pushGoodsMapper.listQuery(start, count, key, state);
    }
 
    @Override
    public long countQuery(String key, Integer state) {
        return pushGoodsMapper.countQuery(key, state);
    }
 
    @Override
    public List<PushGoods> listHistoryByPushTime(long start, int count, Long uid, Date pushTime) {
        return pushGoodsMapper.listHistoryByPushTime(start, count, uid, pushTime);
    }
 
    @Override
    public long countHistoryByPushTime(Long uid, Date pushTime) {
        return pushGoodsMapper.countHistoryByPushTime(uid, pushTime);
    }
 
    
    @Override
    public List<PushGoods> listTask() {
        return pushGoodsMapper.listTask();
    }
    
    
    @Override
    public void taskPush(PushGoods record) {
        String msg = null;
        int state = PushGoods.STATE_FAIL;
        try {
            // 执行推送
            executePush(record);
            state = PushGoods.STATE_SUCCESS;
        } catch (PushGoodsException e) {
            msg = e.getMsg();
        } catch (PushException e) {
            msg = e.getMsg();
        } catch (Exception e) {
            msg = "系统推送失败";
        }
        record.setIsPush(state);
        record.setPushTime(new Date());
        record.setRemark(msg);
        record.setUpdateTime(new Date());
        pushGoodsMapper.updateByPrimaryKey(record);
    }
    
    
    @Override
    public void handPush(Long id) throws Exception, PushGoodsException, PushException {
        PushGoods record = selectByPrimaryKey(id);
        if (record == null) {
            throw new PushGoodsException(1, "推送信息已不存在");
        }
        // 执行推送
        executePush(record);
        
        record.setIsPush(PushGoods.STATE_SUCCESS);
        record.setPushTime(new Date());
        record.setUpdateTime(new Date());
        pushGoodsMapper.updateByPrimaryKey(record);
    }
 
    
    
    @Override
    public void executePush(PushGoods pushGoods) throws Exception, PushGoodsException, PushException {
        if (StringUtil.isNullOrEmpty(pushGoods.getTitle()) || StringUtil.isNullOrEmpty(pushGoods.getContent())) {
            throw new PushGoodsException(1, "推送标题及内容不能为空");
        }
        
        if (StringUtil.isNullOrEmpty(pushGoods.getVersions())) {
            throw new PushGoodsException(1, "推送版本不能为空");
        }
        
        if (pushGoods.getType() == null || pushGoods.getType() == PushGoods.TYPE_GOODS) {
            executePushGoods(pushGoods); // 推送商品
        } else if (pushGoods.getType() == PushGoods.TYPE_ACTIVITY) {
            executePushActivity(pushGoods); // 推送活动
        }
    }
    
    
    /**
     * 推送活动
     * @param pushGoods
     * @throws Exception
     * @throws PushGoodsException
     * @throws PushException
     */
    private void executePushActivity(PushGoods pushGoods) throws Exception, PushGoodsException, PushException {
        String otherId = pushGoods.getOtherId();
        if (StringUtil.isNullOrEmpty(otherId)) {
            throw new PushGoodsException(1, "活动id不能为空");
        }
        
        GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(otherId);
        if (goodsEvaluate == null) {
            throw new PushGoodsException(1, "发圈活动信息不存在");
        }
        
        if (goodsEvaluate.getType() != EvaluateEnum.activity) {
            throw new PushGoodsException(1, "该内容非发圈活动信息");
        }
        
        String picUrl = null;
        List<ImgInfo> imgList = goodsEvaluate.getImgList();
        if (imgList != null && imgList.size() > 0) {
            for (ImgInfo imgInfo: imgList) {
                if (imgInfo.getType() == ImgEnum.img && !StringUtil.isNullOrEmpty(imgInfo.getUrl())) {
                    picUrl = imgInfo.getUrl();
                }
            }
        }
        
        if (StringUtil.isNullOrEmpty(picUrl)) {
            throw new PushGoodsException(1, "该非发圈活动无图片信息");
        }
        
        
        String versions = pushGoods.getVersions();
        JSONObject json = JSONObject.fromObject(versions);
        
        String versionsIOS = json.getString("IOS");
        List<String> listIOS = new ArrayList<String>();
        if (versionsIOS != null && versionsIOS.trim().length() > 0) {
            if (versionsIOS.contains("全推")) {
                listIOS = null;
            } else {
                listIOS = Arrays.asList(versionsIOS.split(","));
            }
        }
        
        List<String> listAndroid = new ArrayList<String>();
        String versionsAndroid  = json.getString("Android");
        if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
            if (versionsAndroid.contains("全推")) {
                listAndroid = null;
            } else {
                listAndroid = Arrays.asList(versionsAndroid.split(","));
            }
        }
        
        String url = configService.get(ConfigKeyEnum.pushActivityLink.getKey());
        if (StringUtil.isNullOrEmpty(url)) {
            throw new PushGoodsException(1, "推送页面链接不存在");
        }
        url = url + "?id=" + pushGoods.getId();
        // h活动全推
        pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid);
    }
    
    
    /**
     * 推送商品
     * @param pushGoods
     * @throws Exception
     * @throws PushGoodsException
     * @throws PushException
     */
    private void executePushGoods(PushGoods pushGoods) throws Exception, PushGoodsException, PushException {
        Long id = pushGoods.getId();
        List<PushGoodsGroup> goodsList = pushGoodsGroupService.getAllInfoByPushId(id);
        if (goodsList == null || goodsList.size() == 0) {
            throw new PushGoodsException(1, "推送无商品,请完善数据");
        }
        
        String versions = pushGoods.getVersions();
        if (versions == null || versions.trim().length() == 0) {
            throw new PushGoodsException(1, "推送版本不能为空");
        }
        
        List<String> listIOS = new ArrayList<String>();
        JSONObject json = JSONObject.fromObject(versions);
        String versionsIOS = json.getString("IOS");
        if (versionsIOS != null && versionsIOS.trim().length() > 0) {
            if (versionsIOS.contains("全推")) {
                listIOS = null;
            } else {
                listIOS = Arrays.asList(versionsIOS.split(","));
            }
        }
        
        List<String> listAndroid = new ArrayList<String>();
        String versionsAndroid  = json.getString("Android");
        if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
            if (versionsAndroid.contains("全推")) {
                listAndroid = null;
            } else {
                listAndroid = Arrays.asList(versionsAndroid.split(","));
            }
        }
        
        if (goodsList.size() == 1) {
            /* 单个商品推送: 直接处理为商品信息  */
            PushGoodsGroup pushGoodsGroup = goodsList.get(0);
            CommonGoods commonGoods = pushGoodsGroup.getCommonGoods();
            if (commonGoods == null) {
                throw new PushGoodsException(1, "商品详细信息已不存在");
            }
 
            Long goodsId = commonGoods.getGoodsId();
            String url = "https://item.taobao.com/item.htm?id=" + goodsId;
            pushService.pushGoods(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url,listIOS, listAndroid);
        } else {
            /*   多个商品推送    */
            String url = configService.get(ConfigKeyEnum.pushGoodsDetails.getKey());
            if (StringUtil.isNullOrEmpty(url)) {
                throw new PushGoodsException(1, "推送页面链接不存在");
            }
            // 生成链接
            url = url + "?id=" + id;
            pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid);
        }
    }
    
    
}