admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package com.yeshi.fanli.service.impl.goods;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dto.common.JDCommonContentTypeEnum;
import com.yeshi.fanli.dto.jd.JDSearchResult;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.service.inter.goods.JDCommonTemplateContentService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.jd.JDApiUtil;
 
@Service
public class JDCommonTemplateContentServiceImpl implements JDCommonTemplateContentService {
 
    private Map<String, Long> countMap = new HashMap<String, Long>();
 
    @Cacheable(value = "jdCommonContentCache", key = "#type+'-'+#cid+'-'+#page+'-'+#pageSize")
    @Override
    public JDSearchResult getContentList(JDCommonContentTypeEnum type, String cid, int page, int pageSize) {
        if (type == JDCommonContentTypeEnum._9k9) {
            return get9K9Content(cid, page, pageSize);
        } else if (type == JDCommonContentTypeEnum.jdPeiSong) {
            return getJDPeiSongContent(cid, page, pageSize);
        } else if (type == JDCommonContentTypeEnum.jiaDian) {
            return getJiaDianContent(cid, page, pageSize);
        } else if (type == JDCommonContentTypeEnum.baiHuo) {
            return getBaiHuoContent(cid, page, pageSize);
        } else if (type == JDCommonContentTypeEnum.juJia) {
            return getJuJiaContent(cid, page, pageSize);
        }
        return null;
    }
 
    private JDSearchResult get9K9Content(String cid, int page, int pageSize) {
        int[] arrayId = { 10, 17 };
        JDSearchResult result = getJingFenGoods(page, pageSize, "9k9-", arrayId);
 
        return filterGoods(result);
    }
 
    private JDSearchResult filterGoods(JDSearchResult result) {
        List<Long> skuIds = new ArrayList<>();
        if (result != null && result.getGoodsList() != null && result.getGoodsList().size() > 0) {
            for (JDGoods goods : result.getGoodsList()) {
                skuIds.add(goods.getSkuId());
            }
            List<JDGoods> goodsList = JDApiUtil.listGoodsDetail(skuIds);
            result.setGoodsList(goodsList);
        }
        return result;
    }
 
    private JDSearchResult getJDPeiSongContent(String cid, int page, int pageSize) {
        int[] arrayId = { 15, 18 };
        JDSearchResult result = getJingFenGoods(page, pageSize, "peisong-", arrayId);
        return filterGoods(result);
    }
 
    private JDSearchResult getJiaDianContent(String cid, int page, int pageSize) {
        int[] arrayId = { 5, 2 };
        JDSearchResult result = getJingFenGoods(page, pageSize, "jiadian-", arrayId);
        return filterGoods(result);
    }
 
    private JDSearchResult getBaiHuoContent(String cid, int page, int pageSize) {
        int[] arrayId = { 6, 3 };
        JDSearchResult result = getJingFenGoods(page, pageSize, "baihuo-", arrayId);
        return filterGoods(result);
    }
 
    private JDSearchResult getJuJiaContent(String cid, int page, int pageSize) {
        int[] arrayId = { 7, 4 };
        JDSearchResult result = getJingFenGoods(page, pageSize, "jujia-", arrayId);
        return filterGoods(result);
    }
 
    /**
     * 9快9包邮
     * 
     * @param cid
     * @param page
     * @param pageSize
     * @return
     */
    private JDSearchResult getJingFenGoods(int page, int pageSize, String mapKey, int[] arrayId) {
        int pageTemp = 0;
        long addCount = 0;
        Integer categoryId = null;
        long totalCount = (long) (page * Constant.PAGE_SIZE);
        for (int i = 0; i < arrayId.length; i++) {
            Long count = countMap.get(mapKey + arrayId[i]);
            if (count != null) {
                addCount += count;
            }
 
            if (count != null && totalCount > addCount) {
                pageTemp = (int) (addCount / Constant.PAGE_SIZE);
                continue; // 当前分类商品不足
            } else {
                page = page - pageTemp;
                categoryId = arrayId[i];
                break;
            }
        }
 
        if (categoryId == null) {
            return null;
        }
 
        JDSearchResult result = JDApiUtil.getJingFenGoods(page, categoryId);
        long count = 0;
        if (result != null) {
            PageEntity pageEntity = result.getPageEntity();
            if (pageEntity != null) {
                count = pageEntity.getTotalCount();
            }
        }
 
        long mcount = (count / Constant.PAGE_SIZE) * Constant.PAGE_SIZE;
        if (mcount == 0) {
            mcount = 20;
        }
        countMap.put(mapKey + categoryId, mcount);
        return result;
    }
 
}