yujian
2019-07-25 0cc3b905e5945d1b4b02ee5f91a615bcfd8f08d6
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
package com.yeshi.fanli.service.impl.jd;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.dto.jd.JDFilter;
import com.yeshi.fanli.dto.jd.JDSearchFilter;
import com.yeshi.fanli.dto.jd.JDSearchResult;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.exception.jd.JDGoodsException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.jd.JDGoodsService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.jd.JDUtil;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Service
public class JDGoodsServiceImpl implements JDGoodsService {
    
    @Resource
    private ConfigService configService;
    
    @Resource
    private HongBaoManageService hongBaoManageService;
    
    
    private static final Map<Long, String> cidMap = new HashMap<Long, String>();
    private static final Map<String, Long> countMap = new HashMap<String, Long>();
 
    static {
        cidMap.put(1L, "nzjh");// 精选
        cidMap.put(2L, "737");// 电器: 家用电器
        cidMap.put(3L, "652,670");// 数码: 数码/电脑、办公
        cidMap.put(4L, "9987");// 手机:手机通讯 
        cidMap.put(5L, "12218");// 生鲜
        cidMap.put(6L, "1315");// 服饰: 服饰内衣 
        cidMap.put(7L, "1319,6233");// 母婴: 母婴/玩具乐器 
        cidMap.put(8L, "1713");// 图书
        cidMap.put(9L, "6196");// 厨具
        cidMap.put(10L, "1320");// 食品: 食品饮料
        cidMap.put(11L, "1316,16750");// 美妆: 美妆护肤/个人护理
        cidMap.put(12L, "15901,1620,1672,12259,9192");// 百货:家庭清洁、纸品/居家日用/礼品/酒类/医药保健
        cidMap.put(13L, "11729");// 鞋靴
        cidMap.put(14L, "1318");// 运动:运动户外
        cidMap.put(15L, "17329");// 箱包: 箱包皮具
        cidMap.put(16L, "6994");// 宠物: 宠物生活
        cidMap.put(17L, "9855,9847,15248");// 家居: 家装建材/家具/家纺
        cidMap.put(18L, "6728");// 汽车:汽车用品
    }
 
    
    @Override
    public List<GoodsClass> getSpecialClass() {
        List<GoodsClass> list = new ArrayList<GoodsClass>();
        list.add(new GoodsClass(1L, "精选"));
        list.add(new GoodsClass(2L, "电器"));
        list.add(new GoodsClass(3L, "数码"));
        list.add(new GoodsClass(4L, "手机"));
        list.add(new GoodsClass(5L, "生鲜"));
        list.add(new GoodsClass(6L, "服饰"));
        list.add(new GoodsClass(7L, "母婴"));
        list.add(new GoodsClass(8L, "图书"));
        list.add(new GoodsClass(9L, "厨具"));
        list.add(new GoodsClass(10L, "食品"));
        list.add(new GoodsClass(11L, "美妆"));
        list.add(new GoodsClass(12L, "百货"));
        list.add(new GoodsClass(13L, "鞋靴"));
        list.add(new GoodsClass(14L, "运动"));
        list.add(new GoodsClass(15L, "箱包"));
        list.add(new GoodsClass(16L, "宠物"));
        list.add(new GoodsClass(17L, "家居"));
        list.add(new GoodsClass(18L, "汽车"));
        return list; 
    }
    
    @Override
    @Transactional
    public JDSearchResult specialSearch(Integer page, Long cid) throws JDGoodsException{
        if (cid == null) {
            throw new JDGoodsException(1, "分类id为空");
        }
        
        String jdcid = cidMap.get(cid);
        if (StringUtil.isNullOrEmpty(jdcid)) {
            throw new JDGoodsException(1, "分类id不存在");
        }
        
        Long jdCid1 = null;
        Integer categoryId = null;
        long totalCount = (long) (page * Constant.PAGE_SIZE);
        
        
        if (jdcid.contains(",")) {
            String[] arrayId = jdcid.split(",");
            for (int i = 0; i < arrayId.length; i++) {
                Long count = countMap.get(cid + "" + arrayId[i]);
                if (count != null && totalCount > count) {
                    continue; // 当前分类商品不足
                } else {
                    jdCid1 =  Long.parseLong(jdcid);
                    categoryId = Integer.parseInt(jdcid);
                }
            }
        } else {
            jdCid1 =  Long.parseLong(jdcid);
            categoryId = Integer.parseInt(jdcid);
        }
        
        
        // 搜索
        JDSearchResult result = null;
        String way = configService.get("jd_api_search_key");
        if ("1".equals(way)) { // API搜索
            JDFilter filterAPI = new JDFilter();
            filterAPI.setPageIndex(page);
            filterAPI.setPageSize(Constant.PAGE_SIZE);
            filterAPI.setCid1(jdCid1);
            result = JDApiUtil.queryByKey(filterAPI);
        } else { // 网页爬取
            JDSearchFilter jdfilter = new JDSearchFilter();
            jdfilter.setPageNo(page);
            jdfilter.setPageSize(Constant.PAGE_SIZE);
            jdfilter.setCategoryId(categoryId);
            result = JDUtil.searchByKey(jdfilter);
        }
        
        long count = 0;
        if (result != null) {
            PageEntity pageEntity = result.getPageEntity();
            if (pageEntity != null) {
                count = pageEntity.getTotalCount();
            }
        }
        countMap.put(cid + "" + jdCid1, count);
        
        return result;
    }
 
}