admin
2020-07-25 95fe9ea9f77bf5e2a3f36761908337432bf573ff
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
package com.blks.goods.job;
 
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.goods.facade.service.HDKGoodsDetailService;
import com.yeshi.goods.facade.utils.taobao.HaoDanKuApiUtil;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
 
@Component
public class HDKGoodsJob {
 
    @Resource
    private HDKGoodsDetailService hdkGoodsDetailService;
 
    private void addGoods(List<HDKGoodsDetail> goodsList) {
        if (goodsList != null)
            for (HDKGoodsDetail goods : goodsList) {
                try {
                    hdkGoodsDetailService.addGoods(goods);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }
 
    /**
     * 同步所有商品
     *
     * @param param
     * @return
     * @throws Exception ReturnT<String> 返回类型
     * @throws
     * @Title: syncAll
     * @Description:
     */
    @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();
                addGoods(goodsList);
            } else {
                minId = null;
            }
        }
 
        return ReturnT.SUCCESS;
    }
 
    /**
     * 新增
     *
     * @param param
     * @return
     * @throws Exception ReturnT<String> 返回类型
     * @throws
     * @Title: syncNewAdd
     * @Description:
     */
    @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();
                addGoods(goodsList);
            } else {
                minId = null;
            }
        }
        return ReturnT.SUCCESS;
    }
 
    /**
     * 更新最新的数据
     *
     * @param param
     * @return
     * @throws Exception ReturnT<String> 返回类型
     * @throws
     * @Title: update
     * @Description:
     */
    @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();
                        }
                    }
            } else {
                minId = null;
            }
        }
        return ReturnT.SUCCESS;
    }
 
    /**
     * 下架失效的
     *
     * @param param
     * @return
     * @throws Exception ReturnT<String> 返回类型
     * @throws
     * @Title: offlineInvalid
     * @Description:
     */
    @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);
        }
        return ReturnT.SUCCESS;
    }
 
}