admin
2019-02-14 70cf74ae648db9ec89a9d1243729c0c7b0dce81b
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
package com.yeshi.fanli.job;
 
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
import com.yeshi.fanli.util.CMQManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
/**
 * 淘宝渠道订单更新
 * 
 * @author hexiaohui
 *
 */
@Component
public class UpdateRelationAndSpecialOrderJob {
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
 
    @Resource
    private RedisManager redisManager;
 
    /**
     * 更新渠道订单(最多20分钟)
     * 
     * @param startTime
     */
    private void updateRelationOrder(long startTime) {
        String startTimeStr = TimeUtil.getGernalTime(startTime, "yyyy-MM-dd HH:mm:ss");
        List<TaoBaoOrder> orderList = TaoKeApiUtil.getTaoBaoRelationOrder(startTimeStr, 1, Constant.TAOBAO_AUTH_APPKEY,
                Constant.TAOBAO_AUTH_APPSECRET);
        addRelationAndSpecialOrder(orderList);
    }
 
    /**
     * 更新渠道订单
     * 
     * @param fromPage
     *            开始页码
     * @param toPage
     *            结束页码(每页100条数据)
     */
    private void updateRelationOrder(int fromPage, int toPage) {
        // 最近90天的订单
        String startTimeStr = TimeUtil.getGernalTime(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 90L,
                "yyyy-MM-dd HH:mm:ss");
        for (int p = fromPage; p < toPage; p++) {
            List<TaoBaoOrder> orderList = TaoKeApiUtil.getTaoBaoRelationOrder(startTimeStr, p,
                    Constant.TAOBAO_AUTH_APPKEY, Constant.TAOBAO_AUTH_APPSECRET);
            addRelationAndSpecialOrder(orderList);
        }
    }
 
    /**
     * 按天更新
     * 
     * @param day
     */
    private void updateRelationOrderByDay(String day) {
        long timestamp = TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd");
        for (int i = 0; i < 72; i++) {
            updateRelationOrder(timestamp + 1000 * 60 * 20L * i);
        }
    }
 
    /**
     * 更新会员订单(最多20分钟)
     * 
     * @param startTime
     */
    private void updateSpecialOrder(long startTime) {
        String startTimeStr = TimeUtil.getGernalTime(startTime, "yyyy-MM-dd HH:mm:ss");
        List<TaoBaoOrder> orderList = TaoKeApiUtil.getTaoBaoSpecialOrder(startTimeStr, 1, Constant.TAOBAO_AUTH_APPKEY,
                Constant.TAOBAO_AUTH_APPSECRET);
        addRelationAndSpecialOrder(orderList);
    }
 
    /**
     * 更新会员订单()
     * 
     * @param fromPage-开始页码
     * @param toPage-结束页码(每页20条数据)
     */
    private void updateSpecialOrder(int fromPage, int toPage) {
        String startTimeStr = TimeUtil.getGernalTime(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 90L,
                "yyyy-MM-dd HH:mm:ss");
        for (int p = fromPage; p < toPage; p++) {
            List<TaoBaoOrder> orderList = TaoKeApiUtil.getTaoBaoSpecialOrder(startTimeStr, p,
                    Constant.TAOBAO_AUTH_APPKEY, Constant.TAOBAO_AUTH_APPSECRET);
            addRelationAndSpecialOrder(orderList);
        }
    }
 
    /**
     * 按天更新
     * 
     * @param day
     */
    private void updateSpecialOrderByDay(String day) {
        long timestamp = TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd");
        for (int i = 0; i < 72; i++) {
            updateSpecialOrder(timestamp + 1000 * 60 * 20L * i);
        }
    }
 
    private void addRelationAndSpecialOrder(List<TaoBaoOrder> orderList) {
        try {
            taoBaoOrderService.addTaoBaoOrderList(orderList);
        } catch (Exception e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
 
        try {
            Map<String, List<TaoBaoOrder>> map = TaoBaoOrderUtil.classifyTaoBaoOrderByOrderId(orderList);
            if (map != null) {
                Iterator<String> its = map.keySet().iterator();
                while (its.hasNext()) {
                    String key = its.next();
                    List<TaoBaoOrder> orders = map.get(key);
                    String redisKey = "addorderqueue-" + key;
                    // redis做频率限制
                    try {
                        if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(redisKey))) {
                            continue;
                        }
 
                    } catch (Exception e) {
 
                    }
 
                    CMQManager.getInstance().addTaoBaoOrderMsg(key, orders);
 
                    try {
                        // 20分钟内不再处理
                        redisManager.cacheCommonString(redisKey, "1", 60 * 20);
                    } catch (Exception e) {
 
                    }
                }
            }
        } catch (Exception e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
 
    // 每30s爬取一次
    @Scheduled(cron = "0/30 * * * * ? ")
    public void doJob1() {
        if (!Constant.IS_TASK)
            return;
        // 爬取最近3页数据
        updateRelationOrder(1, 3);
        updateSpecialOrder(1, 3);
    }
 
    // 每5分钟更新(3-10页数据)
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void doJob2() {
        if (!Constant.IS_TASK)
            return;
        updateRelationOrder(3, 11);
        updateSpecialOrder(3, 11);
    }
 
    // 每个小时更新10-1000页数据
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void doJob3() {
        if (!Constant.IS_TASK)
            return;
        Calendar calendar = Calendar.getInstance();
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        // 每个小时更新100页数据
        int fromPage = h * 100;
        if (fromPage <= 0)
            fromPage = 11;
        int toPage = h * 100 + 100;
        updateRelationOrder(fromPage, toPage);
        updateSpecialOrder(fromPage, toPage);
    }
 
}