admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
package com.yeshi.fanli.job.goods;
 
import java.util.Calendar;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
 
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.goods.facade.dto.taobao.haodanku.HDKGoodsListResultDTO;
import com.yeshi.goods.facade.entity.taobao.haodanku.HDKGoodsDetail;
import com.yeshi.fanli.service.inter.goods.PullNewGoodsService;
import com.yeshi.goods.facade.service.HDKGoodsDetailService;
import com.yeshi.goods.facade.utils.taobao.HaoDanKuApiUtil;
 
@Component
public class HDKGoodsJob {
 
    @Resource
    private HDKGoodsDetailService hdkGoodsDetailService;
    
    @Resource
    private PullNewGoodsService pullNewGoodsService;
    
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
 
    /**
     * 同步所有商品
     * @Title: syncAll
     * @Description: 
     * @param param
     * @return
     * @throws Exception 
     * ReturnT<String> 返回类型
     * @throws
     */
    @XxlJob("goods-taobao-hdk-syncall")
    public ReturnT<String> syncAll(String param) throws Exception {
        Integer minId = 1;
        while (minId != null) {
            HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().listGoods(minId, null, 200);
            if (dto != null) {
                minId = dto.getMinId();
                List<HDKGoodsDetail> goodsList = dto.getGoodsList();
                if (goodsList != null)
                    for (HDKGoodsDetail goods : goodsList) {
                        try {
                            hdkGoodsDetailService.addGoods(goods);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            } else {
                minId = null;
            }
        }
 
        return ReturnT.SUCCESS;
    }
 
    /**
     * 新增
     * @Title: syncNewAdd
     * @Description: 
     * @param param
     * @return
     * @throws Exception 
     * ReturnT<String> 返回类型
     * @throws
     */
    @XxlJob("goods-taobao-hdk-newadd")
    public ReturnT<String> newAdd(String param) throws Exception {
        // 更新最近2小时的数据
        int endHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int startHour = endHour - 2;
        if (startHour < 0)
            startHour = 0;
 
        Integer minId = 1;
        while (minId != null) {
            HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().listAddGoods(minId, startHour, endHour, 200);
            if (dto != null) {
                minId = dto.getMinId();
                List<HDKGoodsDetail> goodsList = dto.getGoodsList();
                if (goodsList != null)
                    for (HDKGoodsDetail goods : goodsList) {
                        try {
                            hdkGoodsDetailService.addGoods(goods);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            } else {
                minId = null;
            }
        }
        return ReturnT.SUCCESS;
    }
 
    /**
     * 更新最新的数据
     * @Title: update
     * @Description: 
     * @param param
     * @return
     * @throws Exception 
     * ReturnT<String> 返回类型
     * @throws
     */
    @XxlJob("goods-taobao-hdk-update")
    public ReturnT<String> update(String param) throws Exception {
        Integer minId = 1;
        while (minId != null) {
            HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().listUpdateGoods(minId, 200);
            if (dto != null) {
                minId = dto.getMinId();
                List<HDKGoodsDetail> goodsList = dto.getGoodsList();
                if (goodsList != null)
                    for (HDKGoodsDetail goods : goodsList) {
                        try {
                            hdkGoodsDetailService.updateGoods(goods);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        
                        executor.execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    pullNewGoodsService.updateGoods(goods);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
            } else {
                minId = null;
            }
        }
        return ReturnT.SUCCESS;
    }
 
    /**
     * 下架失效的
     * @Title: offlineInvalid
     * @Description: 
     * @param param
     * @return
     * @throws Exception 
     * ReturnT<String> 返回类型
     * @throws
     */
    @XxlJob("goods-taobao-hdk-offline-invalid")
    public ReturnT<String> offlineInvalid(String param) throws Exception {
        int endHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int startHour = endHour - 2;
        if (startHour < 0)
            startHour = 0;
        List<Long> ids = HaoDanKuApiUtil.getInstance().listInvalidGoods(startHour, endHour);
        // 分页删除
        int pageSize = 100;
        int page = ids.size() % pageSize == 0 ? ids.size() / pageSize : ids.size() / pageSize + 1;
        for (int i = 0; i < page; i++) {
            int start = i * pageSize;
            List<Long> tempList = ids.subList(start, start + pageSize > ids.size() ? ids.size() : start + pageSize);
            hdkGoodsDetailService.deleteByItemIds(tempList);
        }
        
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    pullNewGoodsService.deleteGoods(ids);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        
        return ReturnT.SUCCESS;
    }
 
}