yujian
2019-07-26 6f3ee199558c79d840137c1a77efe462aca63178
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
package com.yeshi.fanli.service.impl.goods;
 
import java.util.HashMap;
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.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};
        return getJingFenGoods(page, pageSize, "9k9-", arrayId);
    }
    
    private JDSearchResult getJDPeiSongContent(String cid, int page, int pageSize) {
        int[] arrayId = {15, 18};
        return getJingFenGoods(page, pageSize, "peisong-", arrayId);
    }
    
    private JDSearchResult getJiaDianContent(String cid, int page, int pageSize) {
        int[] arrayId = {5, 2};
        return getJingFenGoods(page, pageSize, "jiadian-", arrayId);
    }
    
    private JDSearchResult getBaiHuoContent(String cid, int page, int pageSize) {
        int[] arrayId = {6, 3};
        return getJingFenGoods(page, pageSize, "baihuo-", arrayId);
    }
    
    private JDSearchResult getJuJiaContent(String cid, int page, int pageSize) {
        int[] arrayId = {7, 4};
        return getJingFenGoods(page, pageSize, "jujia-", arrayId);
    }
    
    
    /**
     * 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;
    }
 
}