admin
2020-02-12 df7554c138d0366514968eba9a32670876510df7
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package com.yeshi.fanli.controller.wxmp.v1;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
 
import org.springframework.core.task.TaskExecutor;
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.dto.ConfigParamsDTO;
import com.yeshi.fanli.dto.WXMPAcceptData;
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.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.dto.pdd.PDDGoodsResult;
import com.yeshi.fanli.dto.pdd.PDDSearchFilter;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.search.HotSearch;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.brand.BrandInfoService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.HotSearchService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.manger.goods.jd.JDGoodsLinkParseManager;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
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.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil;
import com.yeshi.fanli.util.taobao.SearchFilterUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Controller("WXMPSearchController")
@RequestMapping("/wxmp/api/v1/search")
public class SearchController {
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
 
    @Resource
    private BrandInfoService brandInfoService;
 
    @Resource
    private JDGoodsLinkParseManager jdGoodsLinkParseManager;
 
    @Resource
    private HotSearchService hotSearchService;
 
    /**
     * 粘贴板信息推荐
     * 
     * @param acceptData
     * @param url
     *            商品链接
     * @param out
     */
    @RequestMapping(value = "getRecommendInfo", method = RequestMethod.POST)
    public void getRecommendInfo(WXMPAcceptData acceptData, String text, Long uid, PrintWriter out) {
        if (StringUtil.isNullOrEmpty(text)) {
            out.print(JsonUtil.loadFalseResult("值为空"));
            return;
        }
 
        // 去除前后空格
        int type = 1;
        text = text.trim();
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(text);
        if (!matcher.find()) {// 不包含链接
            if (text.length() > 256) {
                out.print(JsonUtil.loadFalseResult("值过长"));
                return;
            }
            String pattern = "^[A-Za-z0-9-]+$";
            if (Pattern.matches(pattern, text.replace(" ", ""))) {// 删除空格
                out.println(JsonUtil.loadFalseResult("不支持推荐"));
                return;
            }
 
            LogHelper.test("根据粘贴板推荐:" + text);
            if (text.length() > 80) {
                out.print(JsonUtil.loadFalseResult("值过长"));
                return;
            }
 
            if (!StringUtil.isNullOrEmpty(TaoBaoUtil.parseSystemTaoToken(text))) {
                out.print(JsonUtil.loadFalseResult("不支持推荐"));
                return;
            }
            JSONObject data = new JSONObject();
            data.put("type", type);
            data.put("title", text);
            out.print(JsonUtil.loadTrueResult(data));
            return;
        }
 
        type = 2;
        CommonGoods commonGoods = null;
        text = matcher.group();
        String jdId = JDUtil.getJDGoodsId(text);
        if (StringUtil.isNullOrEmpty(jdId)) {
            jdId = JDUtil.getJDGoodsIdByWeiXin(text); // 微信链接
            if (StringUtil.isNullOrEmpty(jdId) && text.contains("u.jd.com")) {
                jdId = jdGoodsLinkParseManager.parseGoodsIdByJDShortUrl(text); // 领券短连接
            }
        }
        if (!StringUtil.isNullOrEmpty(jdId)) {
            JDGoods goods = JDApiUtil.getGoodsDetail(Long.parseLong(jdId));
            if (goods != null) {
                // 高级接口 -- 信息更完整
                JDGoods jdGoods = JDApiUtil.queryGoodsDetail(Long.parseLong(jdId));
                if (jdGoods != null) {
                    commonGoods = CommonGoodsFactory.create(jdGoods);
                } else {
                    commonGoods = CommonGoodsFactory.create(goods);
                }
            } else {
                type = 3;
                goods = JDUtil.getSimpleGoodsInfo(jdId);
                if (goods != null) {
                    commonGoods = new CommonGoods();
                    commonGoods.setTitle(goods.getSkuName());
                    commonGoods.setPicture(goods.getPicUrl());
                }
            }
        } else {
            String pddId = PinDuoDuoUtil.getPDDGoodsId(text);
            if (!StringUtil.isNullOrEmpty(pddId)) {
                PDDGoodsDetail goods = PinDuoDuoApiUtil.getGoodsDetail(Long.parseLong(pddId));
                if (goods != null) {
                    commonGoods = CommonGoodsFactory.create(goods);
                } else {
                    type = 3;
                    goods = PinDuoDuoUtil.getPDDGoodsInfo(pddId);
                    if (goods != null) {
                        commonGoods = new CommonGoods();
                        commonGoods.setTitle(goods.getGoodsName());
                        commonGoods.setPicture(goods.getGoodsThumbnailUrl());
                    }
                }
            }
        }
 
        if (commonGoods == null) {
            out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
            return;
        }
 
        if (type == 3) {
            JSONObject data = new JSONObject();
            JSONObject goodsJSON = new JSONObject();
            goodsJSON.put("title", commonGoods.getTitle());
            goodsJSON.put("pictUrl", commonGoods.getPicture());
            data.put("type", type);
            data.put("desc", "该商品无推广信息");
            data.put("goods", goodsJSON);
            out.print(JsonUtil.loadTrueResult(data));
            return;
        }
 
        JSONObject data = new JSONObject();
        Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
        data.put("type", type);
        data.put("goods", gson.toJson(GoodsDetailVOFactory.convertCommonGoods(commonGoods,
                hongBaoManageService.getShowComputeRate(acceptData.getPlatform(), acceptData.getVersion()))));
        out.print(JsonUtil.loadTrueResult(data));
        return;
    }
 
    /**
     * 热门搜索
     * 
     * @param acceptData
     * @param goodsType
     * @param out
     */
    @RequestMapping(value = "gethotsearch", method = RequestMethod.POST)
    public void getHotSearch(AcceptData acceptData, Integer goodsType, PrintWriter out) {
        // 默认淘宝
        if (goodsType == null) {
            goodsType = Constant.SOURCE_TYPE_TAOBAO;
        }
 
        List<HotSearch> list = hotSearchService.getHotSearchCache(goodsType);
        if (list == null || list.size() == 0) {
            out.print(JsonUtil.loadFalseResult("没有数据"));
            return;
        }
        List<String> hotSearchList = new ArrayList<String>();
        if(list!=null)
        for (HotSearch hot : list)
            hotSearchList.add(hot.getName());
 
        JSONObject data = new JSONObject();
        data.put("count", list.size());
        data.put("hotSearchList", hotSearchList);
        data.put("helpLink", configService.get(ConfigKeyEnum.newerGonglue.getKey()));
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 搜索候选词
     * 
     * @param acceptData
     * @param kw
     * @param out
     */
    @RequestMapping(value = "suggestSearch", method = RequestMethod.POST)
    public void getSugguestSearch(WXMPAcceptData acceptData, String kw, Integer goodsType, PrintWriter out) {
        if (goodsType == null) {
            out.print(JsonUtil.loadFalseResult("平台类型不能为空"));
            return;
        }
 
        List<String> list = null;
        if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
            list = TaoBaoUtil.getSuguestSearch(kw);
        } else if (goodsType == Constant.SOURCE_TYPE_JD) {
            list = JDUtil.suggestSearch(kw);
        } else if (goodsType == Constant.SOURCE_TYPE_PDD) {
            list = PinDuoDuoUtil.suggestSearch(kw);
        }
 
        if (list == null || list.size() == 0) {
            out.print(JsonUtil.loadFalseResult("暂无建议内容"));
            return;
        }
 
        JSONArray array = new JSONArray();
        for (String words : list) {
            array.add(words);
        }
        out.print(JsonUtil.loadTrueResult(array));
    }
 
    /**
     * 搜索-新版
     * 
     * @param acceptData
     * @param kw
     * @param page
     * @param filter
     * @param order
     *            销量由高到低:1 、 价格从高到低:2 、 价格从低到高:3 、 推广量高到低:4(综合默认)、返利比高到低:5
     *            、返利比低到高:6 、推荐20
     * @param startprice
     * @param endprice
     * @param fastFilter
     * @param out
     */
    @RequestMapping(value = "searchGoods")
    public void searchGoods(WXMPAcceptData acceptData, Integer goodsType, String key, Integer page, String filter,
            Integer order, Long uid, HttpSession session, PrintWriter out) {
        if (goodsType == null || goodsType < 2 || goodsType > 3) {
            out.print(JsonUtil.loadFalseResult(1, "请传递正确平台参数"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(key)) {
            out.print(JsonUtil.loadFalseResult(1, "请输入搜索内容"));
            return;
        }
 
        if (page == null || page < 1) {
            page = 1;
        }
 
        final String searchkey = key.trim();
        if (searchkey.startsWith("http://") || searchkey.startsWith("https://")) {
            JSONObject data = new JSONObject();
            data.put("count", 0);
            data.put("list", new JSONArray());
            out.print(JsonUtil.loadTrueResult(data));
            return;
        }
 
        /*--------- 京东商品  -------*/
        if (goodsType.intValue() == Constant.SOURCE_TYPE_JD) {
            searchJDGoods(acceptData, searchkey, page, filter, order, out);
            return;
        }
 
        /*-------- 拼多多商品  -------*/
        if (goodsType.intValue() == Constant.SOURCE_TYPE_PDD) {
            searchPDDGoods(acceptData, searchkey, page, filter, order, out);
            return;
        }
    }
 
    /**
     * 京东
     * 
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @return
     */
    private void searchJDGoods(WXMPAcceptData acceptData, String key, Integer page, String filter, Integer order,
            PrintWriter out) {
 
        JDSearchResult result = null;
        boolean hasCoupon = false;
        String way = configService.get(ConfigKeyEnum.jdApiSearchKey.getKey());
        if ("1".equals(way)) {
            JDFilter filterAPI = new JDFilter();
            filterAPI.setKeyword(SearchFilterUtil.filterSearchContent(key));
            filterAPI.setPageIndex(page);
            filterAPI.setPageSize(Constant.PAGE_SIZE);
 
            if (order != null) {
                int sort = order.intValue();
                switch (sort) {
                case 1: // 销量 desc
                    filterAPI.setSort(JDFilter.SORT_DESC);
                    filterAPI.setSortName(JDFilter.SORTNAME_ORDER_COUNT_30DAYS);
                    break;
                case 2: // 价格—desc
                    filterAPI.setSort(JDFilter.SORT_DESC);
                    filterAPI.setSortName(JDFilter.SORTNAME_PRICE);
                    break;
                case 3: // 价格—asc
                    filterAPI.setSort(JDFilter.SORT_ASC);
                    filterAPI.setSortName(JDFilter.SORTNAME_PRICE);
                    break;
                case 4: // 返利比—DESC
                    filterAPI.setSort(JDFilter.SORT_DESC);
                    filterAPI.setSortName(JDFilter.SORTNAME_COMMISSION_SHARE);
                    break;
                default:
                    break;
                }
            }
 
            if (!StringUtil.isNullOrEmpty(filter)) {
                JSONObject jsonfilter = JSONObject.fromObject(filter);
                Boolean coupon = jsonfilter.optBoolean("coupon");
                if (coupon != null && coupon) {
                    hasCoupon = true;
                    filterAPI.setIsCoupon(1); // 有券
                }
 
                Boolean zy = jsonfilter.optBoolean("zy");
                if (zy != null && zy) {
                    filterAPI.setOwner("g"); // 自营
                }
 
                String minPrice = jsonfilter.optString("minPrice");
                if (!StringUtil.isNullOrEmpty(minPrice)) {
                    filterAPI.setPricefrom(Double.parseDouble(minPrice));
                }
 
                String maxPrice = jsonfilter.optString("maxPrice");
                if (!StringUtil.isNullOrEmpty(maxPrice)) {
                    filterAPI.setPriceto(Double.parseDouble(maxPrice));
                }
            }
 
            result = JDApiUtil.queryByKey(filterAPI);
        } else {
            // 网页爬取
            JDSearchFilter jdfilter = new JDSearchFilter();
            jdfilter.setKey(SearchFilterUtil.filterSearchContent(key));
            jdfilter.setPageNo(page);
            jdfilter.setPageSize(Constant.PAGE_SIZE);
 
            if (order != null) {
                int sort = order.intValue();
                switch (sort) {
                case 1: // 销量 desc
                    jdfilter.setSort(JDSearchFilter.SORT_DESC);
                    jdfilter.setSortName(JDSearchFilter.SORTNAME_ORDER_COUNT_30DAYS);
                    break;
                case 2: // 价格—desc
                    jdfilter.setSort(JDSearchFilter.SORT_DESC);
                    jdfilter.setSortName(JDSearchFilter.SORTNAME_PRICE);
                    break;
                case 3: // 价格—asc
                    jdfilter.setSort(JDSearchFilter.SORT_ASC);
                    jdfilter.setSortName(JDSearchFilter.SORTNAME_PRICE);
                    break;
                case 4: // 返利比—DESC
                    jdfilter.setSort(JDSearchFilter.SORT_DESC);
                    jdfilter.setSortName(JDSearchFilter.SORTNAME_COMMISSION_SHARE);
                    break;
                default:
                    break;
                }
            }
 
            if (!StringUtil.isNullOrEmpty(filter)) {
                JSONObject jsonfilter = JSONObject.fromObject(filter);
                Boolean coupon = jsonfilter.optBoolean("coupon");
                if (coupon != null && coupon) {
                    hasCoupon = true;
                    jdfilter.setHasCoupon(1); // 有券
                }
 
                Boolean zy = jsonfilter.optBoolean("zy");
                if (zy != null && zy) {
                    jdfilter.setIsZY(1); // 自营
                }
 
                Boolean delivery = jsonfilter.optBoolean("delivery");
                if (delivery != null && delivery) {
                    jdfilter.setDeliveryType(1); // 京东配送
                }
 
                String minPrice = jsonfilter.optString("minPrice");
                if (!StringUtil.isNullOrEmpty(minPrice)) {
                    if (minPrice.contains(".")) {
                        minPrice = minPrice.replace(".", "-");
                        minPrice = minPrice.split("-")[0];
                    }
                    jdfilter.setFromPrice(Integer.parseInt(minPrice));
                }
 
                String maxPrice = jsonfilter.optString("maxPrice");
                if (!StringUtil.isNullOrEmpty(maxPrice)) {
                    if (maxPrice.contains(".")) {
                        maxPrice = maxPrice.replace(".", "-");
                        maxPrice = maxPrice.split("-")[0];
                    }
                    jdfilter.setToPrice(Integer.parseInt(maxPrice));
                }
            }
            result = JDUtil.searchByKey(jdfilter);
 
        }
 
        long count = 0;
        JSONObject data = new JSONObject();
        JSONArray array = new JSONArray();
        if (result != null) {
            PageEntity pageEntity = result.getPageEntity();
            if (pageEntity != null) {
                count = pageEntity.getTotalCount();
            }
 
            List<JDGoods> goodsList = result.getGoodsList();
            if (goodsList != null && goodsList.size() > 0) {
                Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                        .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
                ConfigParamsDTO paramsDTO = hongBaoManageService.getShowComputeRate(acceptData.getPlatform(),
                        acceptData.getVersion());
                for (JDGoods goods : goodsList) {
                    GoodsDetailVO goodsDetailVO = GoodsDetailVOFactory.convertJDGoods(goods, paramsDTO);
                    if (hasCoupon) {
                        if (goodsDetailVO.isHasCoupon()) {
                            array.add(gson.toJson(goodsDetailVO));
                        }
                    } else {
                        array.add(gson.toJson(goodsDetailVO));
                    }
                }
            }
        }
 
        data.put("count", count);
        data.put("list", array);
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 拼多多
     * 
     * @param kw
     * @param page
     * @param filter
     * @param order
     * @param startprice
     * @param endprice
     * @return
     */
    private void searchPDDGoods(WXMPAcceptData acceptData, String key, Integer page, String filter, Integer order,
            PrintWriter out) {
        PDDSearchFilter pddfilter = new PDDSearchFilter();
        pddfilter.setKw(SearchFilterUtil.filterSearchContent(key));
        pddfilter.setPage(page);
        pddfilter.setPageSize(Constant.PAGE_SIZE);
 
        if (order != null) {
            int sort = order.intValue();
            switch (sort) {
            case 1: // 销量 desc
                pddfilter.setSortType(6);
                break;
            case 2: // 价格—desc
                pddfilter.setSortType(4);
                break;
            case 3: // 价格—asc
                pddfilter.setSortType(3);
                break;
            case 4: // 返利比—desc
                pddfilter.setSortType(2);
                break;
            default: // 综合排序
                pddfilter.setSortType(0);
                break;
            }
        }
 
        if (!StringUtil.isNullOrEmpty(filter)) {
            JSONObject jsonfilter = JSONObject.fromObject(filter);
            Boolean coupon = jsonfilter.optBoolean("coupon");
            if (coupon != null && coupon) {
                pddfilter.setHasCoupon(true); // 有券
            }
 
            Boolean brand = jsonfilter.optBoolean("brand");
            if (brand != null && brand) {
                pddfilter.setIsBrand(true); // 是否是品牌
            }
        }
 
        int count = 0;
        JSONObject data = new JSONObject();
        JSONArray array = new JSONArray();
 
        PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
        if (result != null) {
            count = result.getTotalCount();
            Gson gson = JsonUtil.getApiCommonGson();
            List<PDDGoodsDetail> goodsList = result.getGoodsList();
            if (goodsList != null && goodsList.size() > 0) {
                ConfigParamsDTO paramsDTO = hongBaoManageService.getShowComputeRate(acceptData.getPlatform(),
                        acceptData.getVersion());
 
                for (PDDGoodsDetail goods : goodsList) {
                    GoodsDetailVO goodsDetailVO = GoodsDetailVOFactory.convertPDDGoods(goods, paramsDTO);
                    array.add(gson.toJson(goodsDetailVO));
                }
            }
        }
 
        data.put("count", count);
        data.put("list", array);
        out.print(JsonUtil.loadTrueResult(data));
    }
}