yujian
2019-03-27 cdcbed9af813b2a02cdc01eefa24db8bec6b51a9
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
package com.yeshi.fanli.job;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import net.sf.json.JSONObject;
 
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import com.yeshi.fanli.entity.bus.lable.BoutiqueAutoRule;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.bus.lable.QualityFactory;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
/**
 * 精选商品自动入库
 * 
 * @author yj
 *
 */
// @Component
public class QualityFactoryJob implements Job {
 
    // 9.9 19.9 29.9 49.9 筛选
    public final static String IS_9K9 = "IS_9K9";
 
    // 限时抢购
    public final static String FLASH_SALE = "IS_XSQG";
 
    @Resource
    private LabelService labelService;
    @Resource
    private QualityFactoryService qualityFactoryService;
    @Resource
    private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
 
    public QualityFactoryJob() {
        System.out.println("--UpdateQualityFactoryJob 构造函数--");
    }
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        LogHelper.test("---UpdateQualityFactoryJob----execute ");
 
        // 爬取商品信息
        searchGoods(context);
    }
 
    public void searchGoods(JobExecutionContext context) {
        try {
 
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
 
            BoutiqueAutoRule autoRule = new BoutiqueAutoRule();
 
            /* 规则id */
            if (jobDataMap.get("ruleId") != null && !StringUtil.isNullOrEmpty(jobDataMap.get("ruleId").toString())) {
                Long ruleId = Long.parseLong(jobDataMap.get("ruleId").toString());
                autoRule.setId(ruleId);
            }
 
            /* 筛选条件 */
            JSONObject jsonData = null;
            if (jobDataMap.get("searchContent") != null) {
                String jsonContent = jobDataMap.get("searchContent").toString();
                jsonData = JSONObject.fromObject(jsonContent);
            }
 
            if (jsonData == null) {
                jsonData = new JSONObject();
            }
 
            /* 来源名称 */
            if (jsonData.get("sourceCalss") != null && !StringUtil.isNullOrEmpty(jsonData.get("sourceCalss").toString())) {
                String sourceCalss = jsonData.get("sourceCalss").toString();
                autoRule.setSourceCalss(sourceCalss);
            }
 
            /* 规则名称 */
            String title = "";
            if (jsonData.get("title") != null && !StringUtil.isNullOrEmpty(jsonData.get("title").toString())) {
                title = jsonData.get("title").toString();
            }
            autoRule.setTitle(title);
 
            /* 系统类目id */
            String systemCid = "";
            if (jsonData.get("systemCid") != null && !StringUtil.isNullOrEmpty(jsonData.get("systemCid").toString())) {
                systemCid = jsonData.get("systemCid").toString();
            }
            autoRule.setSystemCid(systemCid);
 
            /* 操作人: 开发账号 */
            AdminUser admin = new AdminUser(2L);
            autoRule.setAdminUser(admin);
 
            /* 标签列表 */
            List<Label> listLabs = new ArrayList<Label>();
 
            if (jsonData.get("lableNames") != null && !StringUtil.isNullOrEmpty(jsonData.get("lableNames").toString())) {
 
                // 空格隔开
                String[] arrtitles = jsonData.get("lableNames").toString().split("\\s+");
 
                /* 遍历标签名称:查询数据库中是否已存在 */
                for (String name : arrtitles) {
                    List<Label> labels = labelService.selectByTitle(name.trim());
 
                    if (labels != null && labels.size() > 0) {
                        Label label = labels.get(0);
 
                        listLabs.add(label);
                    } else {
                        // 新增
                        Label addLabel = addLabel(name, admin);
                        listLabs.add(addLabel);
                    }
 
                }
            }
 
            int source = Integer.parseInt(jobDataMap.get("source").toString());
            autoRule.setSource(QualityFactory.SOURCE_TAOBAO);
 
            if (BoutiqueAutoRule.TB_OPTIONAL == source) {
                // 来源淘宝普通接口
                autoRule.setGoodsSource(QualityFactory.SOURCE_TAOBAO);
 
                searchTaoBao(jsonData, listLabs, autoRule);
            }
 
            if (BoutiqueAutoRule.TB_MATERIAL == source) {
                autoRule.setGoodsSource(QualityFactory.SOURCE_TAOBAO_MATERIAL);
                // 淘宝推荐
                searchMaterial(jsonData, listLabs, autoRule);
            }
 
        } catch (Exception e) {
            try {
                LogHelper.test("--searchGoods--执行异常----");
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
 
    }
 
    /**
     * 淘宝api 接口数据爬取
     * 
     * @param jobDataMap
     */
    private void searchTaoBao(JSONObject jsonData, List<Label> listLabs, BoutiqueAutoRule autoRule) {
 
        try {
            Date time = new Date();
            String formatDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(time);
 
            // 页大小,默认100条
            int pageSize = 100;
 
            boolean isCrawl = true;
            // 权重起始
            int startWeight = 0;
            // 权重结束
            int endWeight = 0;
 
            // 是否加入限时抢购
            boolean isFlashSale = false;
            String title = autoRule.getTitle();
            if (title.contains(FLASH_SALE)) {
                isFlashSale = true;
            }
            autoRule.setFlashSale(isFlashSale);
 
            // 最多爬取页码
            int pageMax = 200;
            if (jsonData.get("maxPage") != null) {
                pageMax = Integer.parseInt(jsonData.get("maxPage").toString());
            }
 
            /* 参数处理 */
            SearchFilter filter = getSearchFilter(jsonData);
            // 展示条目
            filter.setPageSize(pageSize);
 
            for (int page = 1; page <= pageMax; page++) {
 
                filter.setPage(page);
 
                TaoBaoSearchResult result = TaoKeApiUtil.searchWuLiao(filter);
                LogHelper.test(title + "searchWuLiao 已爬取- " + page + "-页-");
 
                // 结果为空 时不再请求
                if (result == null) {
                    LogHelper.test(title + "searchWuLiao返回 result为空");
                    return;
                }
 
                // 结果数据处理
                List<TaoBaoGoodsBrief> listGoods = result.getTaoBaoGoodsBriefs();
                if (listGoods == null || listGoods.size() == 0) {
                    LogHelper.test(title + "searchWuLiao返回ListGoods 结果为空");
                    return;
                }
 
                /* 权重范围 */
                if (page < 3) {
                    startWeight = 10000;
                    endWeight = 12000;
                } else {
                    startWeight = 10;
                    endWeight = 3000;
                }
 
                autoRule.setStartWeight(startWeight);
                autoRule.setEndWeight(endWeight);
 
                /* 筛选商品进入精选库中 */
                if (StringUtil.isNullOrEmpty(title)) {
                    screen(listGoods, jsonData, filter, listLabs, autoRule);
                } else {
                    if (title.contains(IS_9K9)) {
                        screen9K9(filter, listGoods, jsonData, listLabs, autoRule);
                    } else {
                        screen(listGoods, jsonData, filter, listLabs, autoRule);
                    }
                }
 
                /* 销售量小值 */
                int minSales = 0;
                if (jsonData.get("startSales") != null) {
                    minSales = (int) jsonData.get("startSales");
                }
 
                /* 销售量大值 */
                int maxSales = 0;
                if (jsonData.get("endSales") != null) {
                    maxSales = (int) jsonData.get("endSales");
                }
 
                // 销量控制
                for (TaoBaoGoodsBrief goodsBrief : listGoods) {
 
                    int biz30day = goodsBrief.getBiz30day();
                    /* 验证销售量 -- 结束爬取数据 */
                    if (maxSales > 0 && minSales > 0 && (biz30day < minSales || biz30day > maxSales)) {
                        isCrawl = false;
                    } else if (maxSales > 0 && biz30day > maxSales) {
                        isCrawl = false;
                    } else if (minSales > 0 && biz30day < minSales) {
                        isCrawl = false;
                    }
                }
 
                // 总页数爬取完成
                PageEntity pageEntity = result.getPageEntity();
                int totalPage = pageEntity.getTotalPage();
                LogHelper.test(title + "总页码" + totalPage);
 
                // 最后一页数据爬取完成
                if (page == totalPage)
                    isCrawl = false;
 
                if (!isCrawl) {
                    LogHelper.test("----" + title + "--正常结束-------");
                    break;// 结束爬取数据
                }
            }
            
            // 9k9 限时抢购  不操作删除
            if (title.contains(IS_9K9) || title.contains(FLASH_SALE)) {
                return;
            }
            
            /* 更新完成后清理之前创建的数据 */
            if (jsonData.get("systemCid") != null && !StringUtil.isNullOrEmpty(jsonData.get("systemCid").toString())) {
                
                Long systemCid = Long.parseLong(jsonData.get("systemCid").toString());
            
                List<Long> list = qualityFactoryService.getAuctionIdbyClassId(systemCid, autoRule.getGoodsSource(), formatDate);
                
                if (list != null && list.size() > 0) {
                    if (list.size() <= 100) {
                        qualityFactoryService.deleteBatchByTaoBaoGoodsId(list);
                    } else {
 
                        int start = 0;
                        int end = 100;
                        int countRow = 100;
 
                        int count = list.size() / countRow;
 
                        if (list.size() % countRow > 0) {
                            count += 1;
                        }
 
                        for (int i = 0; i < count; i++) {
                            List<Long> sublist = new ArrayList<Long>();
                            if (i == count - 1) {
                                //System.out.println(list.subList(start, list.size()));
                                sublist = list.subList(start, list.size());
                                qualityFactoryService.deleteBatchByTaoBaoGoodsId(sublist);
                                break;
                            }
                            sublist = list.subList(start, end);
                            qualityFactoryService.deleteBatchByTaoBaoGoodsId(sublist);
                            start += countRow;
                            end += countRow;
                        }
 
                    }
                }
            }
 
        } catch (Exception e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
 
    }
 
    /**
     * 淘宝api 接口数据爬取
     * 
     * @param jobDataMap
     */
    private void searchMaterial(JSONObject jsonData, List<Label> listLabs, BoutiqueAutoRule autoRule) {
 
        try {
            // 默认20条
            int pageSize = 20;
 
            boolean isCrawl = true;
 
            // 权重起始
            int startWeight = 1000;
            // 权重结束
            int endWeight = 3000;
            autoRule.setStartWeight(startWeight);
            autoRule.setEndWeight(endWeight);
 
            // 是否加入限时抢购
            boolean isFlashSale = false;
            String title = autoRule.getTitle();
            if (title.contains(FLASH_SALE)) {
                isFlashSale = true;
            }
            autoRule.setFlashSale(isFlashSale);
 
            // 最多爬取页码
            int pageMax = 200;
            if (jsonData.get("maxPage") != null) {
                pageMax = Integer.parseInt(jsonData.get("maxPage").toString());
            }
 
            /* 参数处理 */
            SearchFilter filter = getSearchFilter(jsonData);
 
            for (int page = 1; page <= pageMax; page++) {
 
                int materialId = 0;
                String cateIds = filter.getCateIds();
                if (!StringUtil.isNullOrEmpty(cateIds)) {
                    materialId = Integer.parseInt(cateIds);
                }
 
                // 请求官方推荐商品库
                TaoBaoSearchResult result = TaoKeApiUtil.getMaterialByMaterialId(materialId, page, pageSize);
 
                // 结果为空 时不再请求
                if (result == null) {
                    LogHelper.test(title + "searchMaterial返回 result为空");
                    return;
                }
 
                // 结果数据处理
                List<TaoBaoGoodsBrief> listGoods = result.getTaoBaoGoodsBriefs();
                if (listGoods == null || listGoods.size() == 0) {
                    LogHelper.test(title + "searchMaterial返回ListGoods 结果为空");
                    return;
                }
 
                /* 筛选商品进入精选库中 */
                if (StringUtil.isNullOrEmpty(title)) {
                    screen(listGoods, jsonData, filter, listLabs, autoRule);
                } else {
                    if (title.contains(IS_9K9)) {
                        screen9K9(filter, listGoods, jsonData, listLabs, autoRule);
                    } else {
                        screen(listGoods, jsonData, filter, listLabs, autoRule);
                    }
                }
 
                // 总页数爬取完成
                PageEntity pageEntity = result.getPageEntity();
                long count = pageEntity.getTotalCount();
                int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
 
                // 最后一页数据爬取完成
                if (page == totalPage)
                    isCrawl = false;
 
                if (!isCrawl) {
                    LogHelper.test("----" + title + "--结束-------");
                    break;// 结束爬取数据
                }
            }
 
        } catch (Exception e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
 
    }
 
    /**
     * 参数处理加工
     * 
     * @param jobDataMap
     * @return
     */
    public SearchFilter getSearchFilter(JSONObject jsonData) {
 
        SearchFilter filter = new SearchFilter();
 
        // 排序字段 销量高到低(默认)
        filter.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
 
        if (jsonData.get("key") != null) {
            String key = jsonData.get("key").toString();
            if (!StringUtil.isNullOrEmpty(key)) {
                // 搜索关键词
                filter.setKey(key);
            }
        }
 
        if (jsonData.get("cateIds") != null) {
            // 官方推荐商品库投放ID;淘宝类目id集合
            filter.setCateIds(jsonData.get("cateIds").toString());
        }
 
        if (jsonData.get("startZkPrice") != null) {
            // 在售价范围小值
            Double startZkPrice = Double.valueOf(jsonData.get("startZkPrice").toString());
            filter.setStartPrice(new BigDecimal(startZkPrice));
        }
 
        if (jsonData.get("endZkPrice") != null) {
            // 在售价范围大值
            Double endZxPrice = Double.valueOf(jsonData.get("endZkPrice").toString());
            filter.setEndPrice(new BigDecimal(endZxPrice));
        }
 
        if (jsonData.get("startTkRate") != null) {
            // 佣金范围小值
            BigDecimal b = new BigDecimal(jsonData.get("startTkRate").toString());
            double df = b.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();
            int tkRate = (int) (df * 100);
            filter.setStartTkRate(tkRate);
        }
 
        if (jsonData.get("endTkRate") != null) {
            // 佣金范围大值
            BigDecimal b = new BigDecimal(jsonData.get("endTkRate").toString());
            double df = b.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();
            int tkRate = (int) (df * 100);
            filter.setEndTkRate(tkRate);
        }
 
        if (jsonData.get("isTmall") != null && jsonData.get("isTmall").toString().equals("1")) {
            // 是否天猫if ("1".equals(isTmall)) {
            filter.setTmall(true);
        }
 
        if (jsonData.get("hasCoupon") != null && jsonData.get("hasCoupon").toString().equals("1")) {
            // 是否有券:1 有 0 无
            filter.setQuan(1);
        }
 
        if (jsonData.get("freeShipment") != null && jsonData.get("freeShipment").toString().equals("1")) {
            // 是否包邮:1 有 0 无
            filter.setBaoYou(true);
            // ip地址
            // filter.setIp("113.251.22.10");// 重庆
            filter.setIp("218.72.111.105");// 杭州
        }
 
        if (jsonData.get("needPrepay") != null && jsonData.get("needPrepay").toString().equals("1")) {
            // 是否消费保障:1有 0 无
            filter.setNeedPrepay(true);
        }
 
        if (jsonData.get("npxLevel") != null) {
            // 牛皮癣程度,取值:1:不限,2:无,3:轻微
            filter.setNpxLevel((int) jsonData.get("npxLevel"));
        }
 
        if (jsonData.get("includePayRate30") != null && jsonData.get("includePayRate30").toString().equals("1")) {
            // 成交转化是否高于行业均值
            filter.setIncludePayRate30(true);
        }
 
        if (jsonData.get("includeGoodRate") != null && jsonData.get("includeGoodRate").toString().equals("1")) {
            // 好评率是否高于行业均值 1 有 0 无
            filter.setIncludeGoodRate(true);
        }
 
        if (jsonData.get("includeRfdRate") != null && jsonData.get("includeRfdRate").toString().equals("1")) {
            // 退款率是否低于行业均值 1 有 0 无
            filter.setIncludeRfdRate(true);
        }
 
        return filter;
    }
 
    /**
     * 验证是否满足条件
     * 
     * @return
     */
    public boolean validate(SearchFilter filter, JSONObject jsonData, TaoBaoGoodsBrief goodsBrief, int source) {
 
        /* 销售量小值 */
        int minSales = 0;
        if (jsonData.get("startSales") != null) {
            minSales = (int) jsonData.get("startSales");
        }
 
        /* 销售量大值 */
        int maxSales = 0;
        if (jsonData.get("endSales") != null) {
            maxSales = (int) jsonData.get("endSales");
        }
 
        int biz30day = goodsBrief.getBiz30day();
 
        /* 验证销售量 */
        if (maxSales > 0 && minSales > 0 && (biz30day < minSales || biz30day > maxSales)) {
            return false;
        } else if (maxSales > 0 && biz30day > maxSales) {
            return false;
        } else if (minSales > 0 && biz30day < minSales) {
            return false;
        }
 
        /* 验证比例 */
        if (BoutiqueAutoRule.TB_MATERIAL == source) {
 
            int startTkRate = filter.getStartTkRate();
            int endTkRate = filter.getEndTkRate();
 
            BigDecimal tkRate = goodsBrief.getTkRate();
 
            if (startTkRate > 0 && endTkRate > 0
                    && (tkRate.compareTo(new BigDecimal(startTkRate / 100)) < 0 || tkRate.compareTo(new BigDecimal(endTkRate / 100)) > 0)) {
                return false;
            } else if (endTkRate > 0 && tkRate.compareTo(new BigDecimal(endTkRate / 100)) > 0) {
                return false;
            } else if (startTkRate > 0 && tkRate.compareTo(new BigDecimal(startTkRate / 100)) < 0) {
                return false;
            }
        }
 
        /* 券后价范围下限 */
        BigDecimal startPrice = null;
        if (jsonData.get("startPrice") != null) {
            startPrice = new BigDecimal(jsonData.get("startPrice").toString());
        }
 
        /* 券后价范围上限 */
        BigDecimal endPrice = null;
        if (jsonData.get("endPrice") != null) {
            endPrice = new BigDecimal(jsonData.get("endPrice").toString());
        }
 
        /* 券后价--计算 */
        BigDecimal couponPrice = TaoBaoUtil.getAfterUseCouplePrice(goodsBrief);
 
        /* 验证在券后价 */
        if (startPrice != null && endPrice != null && (couponPrice.compareTo(startPrice) < 0 || couponPrice.compareTo(endPrice) > 0)) {
            return false;
        } else if (endPrice != null && couponPrice.compareTo(endPrice) > 0) {
            return false;
        } else if (startPrice != null && couponPrice.compareTo(startPrice) < 0) {
            return false;
        }
 
        /* 验证在是否有券 */
        if (filter.getQuan() > 0) {
            String couponInfo = goodsBrief.getCouponInfo();
            if (StringUtil.isNullOrEmpty(couponInfo)) {
                return false;
            }
        }
 
        return true;
    }
 
    /**
     * 9.9 19.9 29.9 39.9 商品筛选
     * 
     * @param taoBaoGoodsBriefs
     */
    public void screen9K9(SearchFilter filter, List<TaoBaoGoodsBrief> taoBaoGoodsBriefs, JSONObject jsonData, List<Label> listLabs,
            BoutiqueAutoRule autoRule) throws Exception {
 
        // 9k9数据-
        List<TaoBaoGoodsBrief> list9k9 = new ArrayList<TaoBaoGoodsBrief>();
 
        // 19k9数据-
        List<TaoBaoGoodsBrief> list19k9 = new ArrayList<TaoBaoGoodsBrief>();
 
        // 29k9数据-
        List<TaoBaoGoodsBrief> list29k9 = new ArrayList<TaoBaoGoodsBrief>();
 
        // 49k9数据-
        List<TaoBaoGoodsBrief> list49k9 = new ArrayList<TaoBaoGoodsBrief>();
 
        // 验证是否符合要求入库
        for (TaoBaoGoodsBrief goodsBrief : taoBaoGoodsBriefs) {
 
            /* 销售量小值 */
            int minSales = 0;
            if (jsonData.get("startSales") != null) {
                minSales = (int) jsonData.get("startSales");
            }
 
            /* 销售量大值 */
            int maxSales = 0;
            if (jsonData.get("endSales") != null) {
                maxSales = (int) jsonData.get("endSales");
            }
 
            int biz30day = goodsBrief.getBiz30day();
 
            /* 验证销售量 */
            if (maxSales > 0 && minSales > 0 && (biz30day < minSales || biz30day > maxSales)) {
                continue;
            } else if (maxSales > 0 && biz30day > maxSales) {
                continue;
            } else if (minSales > 0 && biz30day < minSales) {
                continue;
            }
 
            /* 验证在是否有券 */
            if (filter.getQuan() > 0) {
                String couponInfo = goodsBrief.getCouponInfo();
                if (StringUtil.isNullOrEmpty(couponInfo)) {
                    continue;
                }
            }
 
            /* 验证比例 */
            if (BoutiqueAutoRule.TB_MATERIAL == autoRule.getSource()) {
 
                int startTkRate = filter.getStartTkRate();
                int endTkRate = filter.getEndTkRate();
 
                BigDecimal tkRate = goodsBrief.getTkRate();
 
                if (startTkRate > 0 && endTkRate > 0
                        && (tkRate.compareTo(new BigDecimal(startTkRate / 100)) < 0 || tkRate.compareTo(new BigDecimal(endTkRate / 100)) > 0)) {
                    continue;
                } else if (endTkRate > 0 && tkRate.compareTo(new BigDecimal(endTkRate / 100)) > 0) {
                    continue;
                } else if (startTkRate > 0 && tkRate.compareTo(new BigDecimal(startTkRate / 100)) < 0) {
                    continue;
                }
            }
 
            /* 9k9 */
            BigDecimal price9k9 = new BigDecimal(10);
 
            /* 19k9 */
            BigDecimal price19k9 = new BigDecimal(20);
 
            /* 29.9 */
            BigDecimal price29K9 = new BigDecimal(30);
 
            /* 49.9 */
            BigDecimal price49K9 = new BigDecimal(50);
            /* 券后价--计算 */
            BigDecimal couponPrice = TaoBaoUtil.getAfterUseCouplePrice(goodsBrief);
 
            if (couponPrice.compareTo(price9k9) < 0) {
                list9k9.add(goodsBrief);
            } else if (couponPrice.compareTo(price19k9) < 0) {
                list19k9.add(goodsBrief);
            } else if (couponPrice.compareTo(price29K9) < 0) {
                list29k9.add(goodsBrief);
            } else if (couponPrice.compareTo(price49K9) < 0) {
                list49k9.add(goodsBrief);
            }
        }
 
        autoRule.setCalss9k9(true);
        AdminUser admin = autoRule.getAdminUser();
 
        if (list9k9.size() > 0) {
            List<Label> labs9K9 = new ArrayList<Label>();
            labs9K9.addAll(listLabs);
 
            List<Label> list = labelService.selectByTitleCache("labKey", "9.9");
 
            if (list == null || list.size() == 0) {
                // 新增
                Label addLabel = addLabel("9.9", admin);
                labs9K9.add(addLabel);
            } else {
                labs9K9.add(list.get(0));
            }
 
            qualityFactoryService.autoInsertOrUpadateStorage(list9k9, labs9K9, autoRule);
        }
 
        if (list19k9.size() > 0) {
            List<Label> labs19K9 = new ArrayList<Label>();
            labs19K9.addAll(listLabs);
            List<Label> list = labelService.selectByTitleCache("labKey", "19.9");
            if (list == null || list.size() == 0) {
                // 新增
                Label addLabel = addLabel("19.9", admin);
                labs19K9.add(addLabel);
            } else {
                labs19K9.add(list.get(0));
            }
            qualityFactoryService.autoInsertOrUpadateStorage(list19k9, labs19K9, autoRule);
        }
 
        if (list29k9.size() > 0) {
            List<Label> labs29K9 = new ArrayList<Label>();
            labs29K9.addAll(listLabs);
            List<Label> list = labelService.selectByTitleCache("labKey", "29.9");
            if (list == null || list.size() == 0) {
                // 新增
                Label addLabel = addLabel("29.9", admin);
                labs29K9.add(addLabel);
            } else {
                labs29K9.add(list.get(0));
            }
 
            qualityFactoryService.autoInsertOrUpadateStorage(list29k9, labs29K9, autoRule);
        }
 
        if (list49k9.size() > 0) {
            List<Label> labs49K9 = new ArrayList<Label>();
            labs49K9.addAll(listLabs);
            List<Label> list = labelService.selectByTitleCache("labKey", "49.9");
            if (list == null || list.size() == 0) {
                // 新增
                Label addLabel = addLabel("49.9", admin);
                labs49K9.add(addLabel);
            } else {
                labs49K9.add(list.get(0));
            }
 
            qualityFactoryService.autoInsertOrUpadateStorage(list49k9, labs49K9, autoRule);
        }
 
    }
 
    public void screen(List<TaoBaoGoodsBrief> taoBaoGoodsBriefs, JSONObject jsonData, SearchFilter filter, List<Label> listLabs,
            BoutiqueAutoRule autoRule) throws Exception {
 
        // 遍历返回数据-
        List<TaoBaoGoodsBrief> listGoods = new ArrayList<TaoBaoGoodsBrief>();
 
        for (TaoBaoGoodsBrief goodsBrief : taoBaoGoodsBriefs) {
            // 验证是否符合要求
            if (validate(filter, jsonData, goodsBrief, 1)) {
                listGoods.add(goodsBrief);
            }
        }
 
        if (listGoods.size() > 0) {
            autoRule.setCalss9k9(false);
            qualityFactoryService.autoInsertOrUpadateStorage(listGoods, listLabs, autoRule);
        }
    }
 
    public Label addLabel(String title, AdminUser admin) throws Exception {
        // 新增
        Label label = new Label();
        label.setTitle(title);// 名称
        label.setEntrymode(Label.MODE_SYSTEM);
        label.setCreatetime(new Date());
        label.setUpdatetime(new Date());
        label.setCreateUser(admin);
        label.setIosClick(0l);
        label.setAndroidClick(0l);
        labelService.insertSelective(label);
 
        return label;
    }
 
}