yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package com.yeshi.fanli.controller.xcx;
 
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.search.HistorySearch;
import com.yeshi.fanli.entity.bus.search.HotSearch;
import com.yeshi.fanli.entity.bus.su.search.SuperHotSearch;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBriefExtra;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchNav;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.SuperHotSearchService;
import com.yeshi.fanli.service.inter.goods.GoodsClassService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.user.HistorySearchService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.DaTaoKeUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("api/xcx/v1/XcxSearch")
public class XcxSearchController {
 
    @Resource
    private XcxMiniControllerUtil xcxMiniControllerUtil;
 
    @Resource
    private SuperHotSearchService superHotSearchSerivce;
 
    @Resource
    private HistorySearchService historySearchService;
 
    @Resource
    private HongBaoManageService manageService;
 
    @Resource
    private GoodsClassService goodsClassService;
 
    @Resource
    private ConfigService configService;
 
    /**
     * 
     * 方法说明: 热门搜索
     * 
     * @author mawurui createTime 2018年4月25日 下午3:03:12
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "gethotsearch", method = RequestMethod.POST)
    public void getHotSearch(AcceptData acceptData, PrintWriter out) {
        /*
         * System system = xcxMiniControllerUtil.getSystem(acceptData); if
         * (system == null) {
         * out.print(JsonUtil.loadFalseResult(XcxMiniControllerUtil.NOSYSTEM));
         * return; }
         */
        List<SuperHotSearch> superHotSearchList = superHotSearchSerivce.getSuperHotSearchBySystemId(4); // 返利券app
        if (superHotSearchList == null || superHotSearchList.size() == 0) {
            out.print(JsonUtil.loadFalseResult(XcxMiniControllerUtil.NODATA));
            return;
        }
        List<HotSearch> hotSearchList = new ArrayList<HotSearch>();
        for (SuperHotSearch superHotSearch : superHotSearchList) {
            HotSearch hotSearch = superHotSearch.getHotSearch();
            hotSearchList.add(hotSearch);
        }
 
        JSONObject data = new JSONObject();
        data.put("count", hotSearchList.size());
        data.put("hotSearchList", hotSearchList);
        String result = JsonUtil.loadTrueResult(data);
        out.print(result);
        return;
    }
 
    /**
     * 
     * 方法说明: 搜索(动作)
     * 
     * @author mawurui createTime 2018年4月25日 下午3:18:34
     * @param acceptData
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @param fastFilter
     * @param out
     */
    @RequestMapping(value = "search")
    public void search(AcceptData acceptData, String appid, String kw, int page, String filter, String order,
            String startprice, String endprice, String fastFilter, PrintWriter out) {
 
        Integer version = null;
        if (!StringUtil.isNullOrEmpty(acceptData.getVersion()))
            version = new Integer(acceptData.getVersion());
        String bid = getHistorySearchBid(acceptData);
        historySearchService.addHistorySearch(kw, bid);
        /*
         * System system = xcxMiniControllerUtil.getSystem(acceptData);
         * if(system == null){
         * out.print(JsonUtil.loadFalseResult(XcxMiniControllerUtil.NOSYSTEM));
         * return; }
         */
 
        if (!StringUtil.isNullOrEmpty(fastFilter)) {
            if (!StringUtil.isNullOrEmpty(filter)) {
                JSONArray filterArray = JSONArray.fromObject(filter);
                JSONArray fastFilterArray = JSONArray.fromObject(fastFilter);
                filterArray.addAll(fastFilterArray);
                filter = filterArray.toString();
            } else {
                filter = fastFilter;
            }
        }
 
        JSONObject data = null;
        filter = "[{\"type\": \"quantype\"}]";
        if (!configService.xcxShow(appid, version)) {
            data = searchTest(kw);
            out.print(JsonUtil.loadTrueResult(data));
            return;
        } else
            // JSONObject data = searchTest(kw);
            data = search(kw, page, filter, order, startprice, endprice);
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 
     * 方法说明: 通过商品类型查询商品
     * 
     * @author mawurui createTime 2018年4月26日 下午3:22:11
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @param out
     */
    @RequestMapping(value = "searchGoodsByType", method = RequestMethod.POST)
    public void searchGoodsByType(String appid, Integer version, Long id, int page, int pagesize, String filter,
            String order, String startprice, String endprice, PrintWriter out) {
        if (id.equals("") || "0".equals(id)) {
            out.print(JsonUtil.loadFalseResult("Id有误"));
        }
 
        // filter = "[{\"type\": \"quantype\"}]";
        if (!configService.xcxShow(appid, version)) {
            JSONObject data = searchTest(Integer.parseInt(id + ""));
            out.print(JsonUtil.loadTrueResult(data));
            return;
        }
 
        String kw = goodsClassService.getKwById(id);
        filter = "[{\"type\": \"quantype\"}]";
        JSONObject data = search(kw, pagesize, filter, order, startprice, endprice);
        // if (StringUtil.isNullOrEmpty(kw)) {
        // out.print(JsonUtil.loadFalseResult("不存在与该id对应的key"));
        // } else {
        // filter = "[{\"type\": \"quantype\"}]";
        // JSONObject data = searchTest(Integer.parseInt(id + ""));
        // out.print(JsonUtil.loadTrueResult(data));
        // }
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 
     * 方法说明: 商品缓存
     * 
     * @author mawurui createTime 2018年4月25日 下午3:03:48
     * @param acceptData
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @param md5
     * @return
     */
    @Cacheable(value = "goodsCache", key = "#md5")
    public JSONObject searchClass(AcceptData acceptData, String kw, int page, String filter, String order,
            String startprice, String endprice, String md5) {
        JSONObject data = search(kw, page, filter, order, startprice, endprice);
        return data;
    }
 
    /**
     * 上线测试
     * 
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @return
     */
    private JSONObject searchTest(int type) {
        TaoBaoSearchResult result = DaTaoKeUtil.getGoodsByType(type);
        List<TaoBaoSearchNav> navList = TaoBaoUtil.getBaseNav();
        if (result.getNavList() != null)
            navList.addAll(result.getNavList());
        Gson gson = new GsonBuilder().create();
        JSONObject data = new JSONObject();
        data.put("nav", gson.toJson(navList));
        List<TaoBaoGoodsBriefExtra> re = new ArrayList<TaoBaoGoodsBriefExtra>();
        List<TaoBaoGoodsBrief> taoBaoGoodsBriefs = result.getTaoBaoGoodsBriefs();
 
        BigDecimal proportion =manageService.getFanLiRate();
 
        TaoBaoGoodsBriefExtra taoBaoGoodsBriefExtra;
        for (TaoBaoGoodsBrief taoBaoGoodsBrief : taoBaoGoodsBriefs) {
            taoBaoGoodsBriefExtra = TaoBaoUtil.getTaoBaoGoodsBriefExtra(taoBaoGoodsBrief, proportion.toString(), "");
            re.add(taoBaoGoodsBriefExtra);
        }
 
        Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        data.put("result", gson2.toJson(re));
        data.put("count", result.getTaoBaoHead().getDocsfound());
        return data;
    }
 
    /**
     * 上线测试
     * 
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @return
     */
    private JSONObject searchTest(String kw) {
        TaoBaoSearchResult result = DaTaoKeUtil.searchByKey(kw);
        List<TaoBaoSearchNav> navList = TaoBaoUtil.getBaseNav();
        if (result.getNavList() != null)
            navList.addAll(result.getNavList());
        Gson gson = new GsonBuilder().create();
        JSONObject data = new JSONObject();
        data.put("nav", gson.toJson(navList));
        List<TaoBaoGoodsBriefExtra> re = new ArrayList<TaoBaoGoodsBriefExtra>();
        List<TaoBaoGoodsBrief> taoBaoGoodsBriefs = result.getTaoBaoGoodsBriefs();
 
        Map<String, String> map = manageService.convertMap();
        BigDecimal proportion =manageService.getFanLiRate();
 
        TaoBaoGoodsBriefExtra taoBaoGoodsBriefExtra;
        for (TaoBaoGoodsBrief taoBaoGoodsBrief : taoBaoGoodsBriefs) {
            taoBaoGoodsBriefExtra = TaoBaoUtil.getTaoBaoGoodsBriefExtra(taoBaoGoodsBrief, proportion.toString(), "");
            re.add(taoBaoGoodsBriefExtra);
        }
 
        Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        data.put("result", gson2.toJson(re));
        data.put("count", result.getTaoBaoHead().getDocsfound());
        return data;
    }
 
    private JSONObject search(String kw, int page, String filter, String order, String startprice, String endprice) {
 
        SearchFilter sf = new SearchFilter();
        sf.setKey(kw);
        sf.setPage(page);
        sf.setSort(0);
 
        if (!com.yeshi.fanli.util.StringUtil.isNullOrEmpty(startprice)) {
            sf.setStartPrice(new BigDecimal(startprice));
        }
        if (!com.yeshi.fanli.util.StringUtil.isNullOrEmpty(endprice)) {
            sf.setEndPrice(new BigDecimal(endprice));
        }
        if (!com.yeshi.fanli.util.StringUtil.isNullOrEmpty(order)) {
            if (Integer.parseInt(order) == 1) {
                sf.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
            } else if (Integer.parseInt(order) == 2) {
                sf.setSort(TaoBaoUtil.SORT_PRICE_HIGH_TO_LOW);
            } else if (Integer.parseInt(order) == 3) {
                sf.setSort(TaoBaoUtil.SORT_PRICE_LOW_TO_HIGH);
            }
        } else {
            // 默认销量从高到低排序
            sf.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
        }
 
        if (!com.yeshi.fanli.util.StringUtil.isNullOrEmpty(filter)) {
            // 查询淘宝天猫
            JSONArray array = JSONArray.fromObject(filter);
            Set<String> params = new HashSet<String>();
            for (int i = 0; i < array.size(); i++) {
                String ty = array.optJSONObject(i).optString("type");
                if ("shoptype".equalsIgnoreCase(ty)) {
                    if (array.optJSONObject(i).optInt("id") == 21) {
                        sf.setTmFlagship(false); // 不筛�?
                    } else {
                        sf.setTmFlagship(true);
                        sf.setTmall(true);
                        sf.setShopTag("b2c");
                    }
                } else if ("category".equalsIgnoreCase(ty)) {
                    sf.setCateIds(array.optJSONObject(i).optString("id"));
                } else if ("userType".equalsIgnoreCase(ty)) {
                    // if(array.optJSONObject(i).optInt("id") == 51)
                    sf.setTmall(true);
                    // else
                    // sf.setTmall(false);
                } else if ("baoyou".equalsIgnoreCase(ty)) {
                    // if(array.optJSONObject(i).optInt("id") == 61)
                    sf.setBaoYou(true);
                    // else
                    // sf.setBaoYou(false);
                } else if ("quantype".equalsIgnoreCase(ty)) {
                    // if (array.optJSONObject(i).optInt("id") == 31){
                    sf.setQuan(1);
                    if (sf.isTmFlagship()) {
                        sf.setShopTag(sf.getShopTag() + ",dpyhq");
                    } else {
                        sf.setShopTag("dpyhq");
                    }
                    // }
                    // else
                    // sf.setQuan(2);
                } else if ("hongbaotype".equalsIgnoreCase(ty)) {
                    if (array.optJSONObject(i).optInt("id") == 41)// 有红�?
                        sf.setHongbao(1);
                    else// 无红�?
                        sf.setHongbao(2);
                } else if ("property".equalsIgnoreCase(ty)) {
                    String pid = (String) array.optJSONObject(i).opt("pid");
                    String id = (String) array.optJSONObject(i).opt("id");
                    boolean flag = false;
                    String str = null;
                    for (String string : params) {
                        flag = string.contains(pid + ":");
                        if (flag) {
                            str = string;
                            break;
                        }
                    }
                    if (flag) {
                        params.add(str + "," + id);
                        params.remove(str);
                    } else {
                        params.add(pid + ":" + id);
                    }
                }
            }
            sf.setParams(params);
        }
        TaoBaoSearchResult result = TaoBaoUtil.search(sf);
        List<TaoBaoSearchNav> navList = TaoBaoUtil.getBaseNav();
        if (result.getNavList() != null)
            navList.addAll(result.getNavList());
        Gson gson = new GsonBuilder().create();
        JSONObject data = new JSONObject();
        data.put("nav", gson.toJson(navList));
        List<TaoBaoGoodsBriefExtra> re = new ArrayList<TaoBaoGoodsBriefExtra>();
        List<TaoBaoGoodsBrief> taoBaoGoodsBriefs = result.getTaoBaoGoodsBriefs();
        int fq = sf.getQuan();
        int fh = sf.getHongbao();
        boolean ft = sf.isTmall();
        Map<String, String> map = manageService.convertMap();
        BigDecimal proportion =manageService.getFanLiRate();
        if (fq == 0 && fh == 0 && !ft) {
            for (TaoBaoGoodsBrief bf : result.getTaoBaoGoodsBriefs()) {
                re.add(TaoBaoUtil.getTaoBaoGoodsBriefExtra(bf, proportion.toString(), ""));
            }
        } else {
            TaoBaoGoodsBriefExtra taoBaoGoodsBriefExtra;
            for (TaoBaoGoodsBrief taoBaoGoodsBrief : taoBaoGoodsBriefs) {
                taoBaoGoodsBriefExtra = TaoBaoUtil.getTaoBaoGoodsBriefExtra(taoBaoGoodsBrief, proportion.toString(), "");
                re.add(taoBaoGoodsBriefExtra);
            }
        }
        Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        data.put("result", gson2.toJson(re));
        data.put("count", result.getTaoBaoHead().getDocsfound());
        return data;
    }
 
    /**
     * 
     * 方法说明: 历史搜索
     * 
     * @author mawurui createTime 2018年4月25日 下午3:04:06
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "gethistorysearch", method = RequestMethod.POST)
    public void getHistorySearch(AcceptData acceptData, PrintWriter out) {
        String bid = getHistorySearchBid(acceptData);
        List<HistorySearch> historySearchList = historySearchService.getHistorySearch(bid);
        if (historySearchList == null || historySearchList.size() == 0) {
            out.print(JsonUtil.loadFalseResult(XcxMiniControllerUtil.NODATA));
            return;
        }
        JSONObject data = new JSONObject();
        data.put("count", historySearchList.size());
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        data.put("historySearchList", gson.toJson(historySearchList));
        out.print(JsonUtil.loadTrueResult(data));
        return;
    }
 
    /**
     * 
     * 方法说明: 清空历史搜索
     * 
     * @author mawurui createTime 2018年4月25日 下午3:04:23
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "clearhistorysearch", method = RequestMethod.POST)
    public void clearHistorySearch(AcceptData acceptData, PrintWriter out) {
        String bid = getHistorySearchBid(acceptData);
        long count = historySearchService.clearHistorySearch(bid);
        JSONObject data = new JSONObject();
        data.put("count", count);
        out.print(JsonUtil.loadTrueResult(data));
        return;
    }
 
    /**
     * 
     * 方法说明: 关键字提示建议搜索
     * 
     * @author mawurui createTime 2018年4月25日 下午3:04:52
     * @param acceptData
     * @param kw
     * @param out
     */
    @RequestMapping(value = "suggestsearch", method = RequestMethod.POST)
    public void getSugguestSearch(AcceptData acceptData, String kw, PrintWriter out) {
        List<String> list = TaoBaoUtil.getSuguestSearch(kw);
        if (list != null && list.size() > 0) {
            JSONArray array = new JSONArray();
            for (String st : list)
                array.add(st);
            out.print(JsonUtil.loadTrueResult(array));
        } else {
            out.print(JsonUtil.loadFalseResult("暂无建议内容"));
        }
        return;
    }
 
    private String getHistorySearchBid(AcceptData acceptData) {
        StringBuffer sb = new StringBuffer();
        String link = "#$$$#";
        String platform = acceptData.getPlatform();
        String packages = acceptData.getPackages();
        String device = acceptData.getDevice();
        sb.append(platform).append(link).append(packages).append(link).append(device);
        return StringUtil.Md5(sb.toString());
    }
 
}