admin
2024-10-15 4995469ae28ce99f5e682895c0708d15f4dc63cd
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
package com.yeshi.fanli.job.order.suning;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.yeshi.fanli.dto.mq.order.body.CommonOrderMQMsg;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.mq.cmq.order.OrdersCMQManager;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqManager;
import org.springframework.stereotype.Component;
 
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.fanli.dto.suning.SuningOrderQueryResultDTO;
import com.yeshi.fanli.entity.suning.SuningOrderInfo;
import com.yeshi.fanli.exception.suning.SuningOrderException;
import com.yeshi.fanli.service.inter.order.suning.SuningOrderService;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.mq.cmq.order.SuningOrderCMQManager;
import com.yeshi.fanli.util.suning.SuningApiUtil;
 
//从淘宝爬去订单更新
@Component
public class UpdateSuningOrderJob {
 
    @Resource
    private SuningOrderService suningOrderService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private OrdersCMQManager ordersCMQManager;
 
 
 
    /**
     * 保存订单
     * 
     * @param SuningOrderList
     */
    public void saveSuningOrders(List<SuningOrderInfo> suningOrderList) {
 
        //5分钟不更新就报警
        try {
            redisManager.cacheCommonString(RedisKeyEnum.monitor.getKey() + Constant.SOURCE_TYPE_SUNING, "1", 60 * 5);
        } catch (Exception e) {
        }
        for (SuningOrderInfo order : suningOrderList) {
            try {
                suningOrderService.addOrder(order);
                /**
                 * 做频率限制
                 */
                String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SuningOrder, order.getOrderCode() + "");
                String result = redisManager.getCommonString(key);
                // 判断
                if (StringUtil.isNullOrEmpty(result)) {
                    ordersCMQManager.addOrder(new CommonOrderMQMsg(order.getOrderCode() + "",Constant.SOURCE_TYPE_SUNING));
                    redisManager.cacheCommonString(key, "1", 60 * 60 * 2);// 2小时内不再更新
                }
            } catch (SuningOrderException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 快速订单更新(爬取本小时内的单,每分钟一次)
     */
    @XxlJob("updateSuningOrderHandler")
    public ReturnT<String> updateSuningSoonOrder(String param) throws Exception {
        long endTime = System.currentTimeMillis();
        if ("1hour".equalsIgnoreCase(param)) {// 更新1小时内的订单
            updateByOrderTime(endTime - 1000 * 60 * 60 * 1L, endTime,2);
            updateByOrderTime(endTime - 1000 * 60 * 60 * 1L, endTime,3);
            updateByOrderTime(endTime - 1000 * 60 * 60 * 1L, endTime,4);
            updateByOrderTime(endTime - 1000 * 60 * 60 * 1L, endTime,5);
        } else if ("1day".equalsIgnoreCase(param)) {// 更新最近1天更新的数据
            updateByOrderTime(endTime - 1000 * 60 * 60 * 24L, endTime,2);
            updateByOrderTime(endTime - 1000 * 60 * 60 * 24L, endTime,3);
            updateByOrderTime(endTime - 1000 * 60 * 60 * 24L, endTime,4);
            updateByOrderTime(endTime - 1000 * 60 * 60 * 24L, endTime,5);
        }
        return ReturnT.SUCCESS;
    }
 
    /**
     * 按下单时间更新
     * @Title: updateByOrderTime
     * @Description: 
     * @param startTime
     * @param endTime 
     * void 返回类型
     * @throws
     */
 
    public void updateByOrderTime(long startTime, long endTime,int status) {
        List<SuningOrderInfo> suningOrderList = new ArrayList<>();
        int page = 1;
 
        SuningOrderQueryResultDTO result = SuningApiUtil.getOrderList(new Date(startTime), new Date(endTime), page, status);
 
        while (result != null && result.getOrderList().size() > 0) {
            page++;
            suningOrderList.addAll(result.getOrderList());
            result = SuningApiUtil.getOrderList(new Date(startTime), new Date(endTime), page, status);
        }
 
        saveSuningOrders(suningOrderList);
    }
}