admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
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
package com.yeshi.buwan.service.imp;
 
import com.yeshi.buwan.dao.*;
import com.yeshi.buwan.dao.system.DetailSystemDao;
import com.yeshi.buwan.domain.*;
import com.yeshi.buwan.domain.solr.SolrAlbumVideo;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.video.InternetSearchVideo;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.domain.web.HotSearchAdmin;
import com.yeshi.buwan.dto.search.SolrResultDTO;
import com.yeshi.buwan.dto.search.SolrVideoSearchFilter;
import com.yeshi.buwan.videos.pptv.PPTVUtil;
import com.yeshi.buwan.service.inter.video.VideoInfoExtraService;
import com.yeshi.buwan.service.manager.search.SolrAlbumVideoDataManager;
import com.yeshi.buwan.service.manager.search.SolrInternetSearchVideoDataManager;
import com.yeshi.buwan.util.*;
import com.yeshi.buwan.util.factory.VideoInfoFactory;
import com.yeshi.buwan.util.video.VideoConstant;
import com.yeshi.buwan.vo.video.VideoListResultVO;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.*;
 
@Service
public class SearchService {
    private Logger logger = LoggerFactory.getLogger(SearchService.class);
 
    private final String ALBUM_SEARCH_FUZZY_KEYS = "《,》,。";
 
 
    @Resource
    private SearchDao searchDao;
    @Resource
    private ClassService classService;
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private HotSearchDao hotSearchDao;
    @Resource
    private SuperHotSearchDao superHotSearchDao;
    @Resource
    private VideoBanQuanVideoDao videoBanQuanVideoDao;
    @Resource
    private DetailSystemDao detailSystemDao;
    @Resource
    private SolrAlbumVideoDataManager solrAlbumDataManager;
 
    @Resource
    private SolrInternetSearchVideoDataManager solrInternetSearchVideoDataManager;
 
    @Resource
    private VideoInfoExtraService videoInfoExtraService;
 
    @SuppressWarnings("rawtypes")
    @Cacheable(value = "userCache", key = "'suggestSearch'+'-'+#key+'-'+#system")
    public List<String> suggestSearch(String key, String system) {
        if (StringUtil.isNullOrEmpty(key) || key.startsWith("%"))
            return new ArrayList<>();
 
        List<String> list;
        Session session = null;
        if (key != null && key.length() > 10)
            key = key.substring(0, 10);
        String sql = "";
        if (system.equalsIgnoreCase("1")) {
            sql = "select name as result from wk_video_video where name like ?";
        }
 
        list = new ArrayList<>();
 
        List li = null;
        try {
            li = videoInfoDao.sqlList(sql,
                    new String[]{(new StringBuilder(String.valueOf(key))).append("%").toString()});
        } catch (Exception e) {
 
        }
        if (li != null) {
            for (int i = 0; i < li.size(); i++) {
                Object objs = (Object) li.get(i);
                list.add(objs.toString());
            }
 
        }
 
        Set<String> set = new TreeSet<String>();
        if (list != null) {
            String st;
            for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); set.add(st))
                st = (String) iterator.next();
 
        }
 
        Iterator<String> it = set.iterator();
        list = new ArrayList<>();
        while (it.hasNext()) {
            list.add(it.next());
            if (list.size() > 10)
                break;
        }
 
        return list;
    }
 
    @SuppressWarnings("unchecked")
    public List<Serializable> search(String ip, String uid, String key, int page, int type, int videoType,
                                     String system) {
 
        SearchHistory sh = new SearchHistory();
        sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString());
        sh.setIp(ip);
        sh.setKey(key);
        UserInfo user = new UserInfo();
        user.setId(uid);
        sh.setUser(user);
        searchDao.create(sh);
        // 查询数据库
        String videoTypeList[] = classService.getChildrenType(videoType);
        String typewhere = "";
        if (system.equalsIgnoreCase("2")) {
            typewhere = " v.videoType.id=105 or v.videoType.id=106 or v.videoType.id=107 or v.videoType.id=108 ";
        } else if (videoType > 0) {
            for (int i = 0; i < videoTypeList.length; i++)
                typewhere = (new StringBuilder(String.valueOf(typewhere))).append(" or v.videoType.id=")
                        .append(videoTypeList[i]).toString();
 
            if (typewhere.startsWith(" or"))
                typewhere = typewhere.substring(3, typewhere.length());
        }
 
        String sql;
        if (!StringUtil.isNullOrEmpty(typewhere))// 改为sql
            sql = (new StringBuilder("from VideoInfo v where v.show='1' and (v.name like ?) and (")).append(typewhere)
                    .append(") order by REPLACE(v.name,?,'') ,v.watchCount desc").toString();
        else
            sql = "from VideoInfo v where v.show='1' and (v.name like ?) order by REPLACE(v.name,?,''),v.watchCount desc";// 改为sql
        List<Serializable> list = new ArrayList<>();
        BaiduVideoUtil util = new BaiduVideoUtil();
        List<VideoInfo> localList = videoInfoDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount,
                new String[]{(new StringBuilder("")).append(key).append("%").toString(), key});
        if (localList != null && localList.size() > 0) {
            for (VideoInfo info : localList) {
                list.add(info);
            }
        }
 
        // 查询数据库结束
 
        if (system.equalsIgnoreCase("1"))
            if (page == 1)
                try {
                    List<VideoInfo> fromNet = util.searchVideo(key);
                    if (fromNet != null && fromNet.size() > 0) {
                        for (int i = 0; i < fromNet.size(); i++)
                            if (videoType == 0)
                                list.add((VideoInfo) fromNet.get(i));
                            else if (((VideoInfo) fromNet.get(i)).getVideoType().getId() == (long) videoType)
                                list.add((VideoInfo) fromNet.get(i));
 
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
 
        final List<Serializable> newList = list;
        list = (List<Serializable>) searchDao.excute(new HibernateCallback<List<Serializable>>() {
            public List<Serializable> doInHibernate(Session session) throws HibernateException {
                return BanQuanUtil.filterShowVideo(newList, session);
            }
        });
        return list;
    }
 
    @Cacheable(value = "userCache", key = "'search'+'-'+#detailSystem+'-'+#key+'-'+#page+'-'+#contentType+'-'+#system+'-'+#cacheMD5")
    public List<VideoInfo> search(String detailSystem, String ip, String uid, String key, int page, int contentType,
                                  String system, List<Long> resourceList, String cacheMD5) {
        String resourceWhere = "";
        for (Long re : resourceList) {
            resourceWhere += " rv.resourceid=" + re + " or";
        }
 
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
 
        SearchHistory sh = new SearchHistory();
        sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString());
        sh.setIp(ip);
        if (!StringUtil.isNullOrEmpty(key) && key.length() > 30)
            key = key.substring(0, 30);
        sh.setKey(key);
        UserInfo user = new UserInfo();
        user.setId(uid);
        sh.setUser(user);
 
        searchDao.create(sh);
        // 查询数据库
 
        List<VideoInfo> list = new ArrayList<>();
        List<VideoInfo> localList = null;
        try {
            long startt = System.currentTimeMillis();
            localList = null;
            if (contentType == 0)
                localList = SolrUtil.search(key, page);
            else if (contentType == 1)
                localList = SolrUtil.searchZhengPian(key, page);
            else
                localList = SolrUtil.searchZhuanJi(key, page);
            System.out.println("搜索耗时:" + (System.currentTimeMillis() - startt));
 
            String sql = "";
            startt = System.currentTimeMillis();
            for (int i = 0; i < localList.size(); i++) {
                sql += " select count(*) from wk_resource_video rv left join wk_video_video v on rv.videoid=v.id where v.id is not null and v.show=1 and rv.videoid="
                        + localList.get(i).getId() + " and (" + resourceWhere + ") union all";
            }
 
            if (sql.endsWith("union all"))
                sql = sql.substring(0, sql.length() - 9);
 
            List rlist = videoInfoDao.sqlList(sql);
            List<Integer> pList = new ArrayList<>();
            for (int i = 0; i < localList.size(); i++) {
                if (Integer.parseInt(rlist.get(i) + "") < 1) {
                    pList.add(i);
                    // localList.remove(i);
                    // i--;
                }
            }
 
            Collections.sort(pList);
            for (int i = pList.size() - 1; i >= 0; i--) {
                localList.remove((int) pList.get(i));
            }
 
            System.out.println("排除耗时:" + (System.currentTimeMillis() - startt));
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        if (localList != null && localList.size() > 0) {
            for (VideoInfo info : localList) {
                list.add(info);
            }
        }
        return list;
    }
 
 
    private VideoInfo loadAlbumData(VideoInfo video) {
        video.setShowType(1);
        if (!StringUtil.isNullOrEmpty(video.getMainActor())) {
            video.setMainActor("主演:" + video.getMainActor());
        }
 
        //设置tag
        String tag = "";
        if (!StringUtil.isNullOrEmpty(video.getYear())) {
            tag += video.getYear() + "/";
        }
 
        if (video.getVideoType() != null) {
            String vt = VideoConstant.getMainCategoryName(video.getVideoType().getId());
            if (!StringUtil.isNullOrEmpty(vt)) {
                tag += vt + "/";
            }
        }
 
        if (!StringUtil.isNullOrEmpty(video.getArea())) {
            tag += video.getArea() + "/";
        }
 
        if (tag.endsWith("/"))
            tag = tag.substring(0, tag.length() - 1);
        video.setTag(tag);
 
        return video;
 
    }
 
 
    public SearchResult searchAlbum(int videoType, String key, List<Long> resourceIds, int page, int pageSize, boolean fuzzy) {
        List<VideoInfo> localList = new ArrayList<>();
        //专辑视频集合
        Set<String> solrAlbumVids = new HashSet<>();
 
        SolrVideoSearchFilter filter = new SolrVideoSearchFilter();
        filter.setFuzzy(fuzzy);
        filter.setKey(key);
        if (videoType == Constant.SEARCH_RESULT_TYPE_HIGH_DEFINITION) {
            filter.setResourceIds(Arrays.asList(new String[]{PPTVUtil.RESOURCE_ID + ""}));
            logger.info("高清搜索# key:{}", key);
        } else {
            List<String> rids = new ArrayList<>();
            for (Long rid : resourceIds) {
                rids.add(rid + "");
            }
            filter.setResourceIds(rids);
            filter.setVideoType(videoType == 0 ? null : videoType);
        }
        filter.setContentType(1);
 
        SolrResultDTO solrResultDTO = solrAlbumDataManager.find(filter, page, pageSize);
        Set<String> albumSet = new HashSet<>();
        int albumCount = 0;
        if (solrResultDTO != null) {
            albumCount = solrResultDTO.getTotalCount();
            for (SolrAlbumVideo sv : (List<SolrAlbumVideo>) solrResultDTO.getVideoList()) {
                VideoInfo video = VideoInfoFactory.create(sv);
                albumSet.add(video.getName() + "#" + video.getVideoType().getId() + "#" + video.getYear());
                video = loadAlbumData(video);
                localList.add(video);
                solrAlbumVids.add(video.getId());
            }
        }
 
        //第一页,不为高清时搜索
        if (page == 1 && videoType != Constant.SEARCH_RESULT_TYPE_HIGH_DEFINITION) {
            //page为1时 全网搜
            SolrResultDTO internetSearchResultDTO = solrInternetSearchVideoDataManager.find(filter, 1, 20);
            if (internetSearchResultDTO != null && internetSearchResultDTO.getVideoList().size() > 0) {
                List<InternetSearchVideo> iList = internetSearchResultDTO.getVideoList();
                List<InternetSearchVideo> isvList = new ArrayList<>();
                if (iList != null && iList.size() > 0)
                    isvList.addAll(iList);
                //删除前面有的数据,根据rootType与名称筛选
                for (int i = 0; i < isvList.size(); i++) {
                    String uniqueId = isvList.get(i).getName() + "#" + isvList.get(i).getRootType() + "#" + isvList.get(i).getYear();
                    if (albumSet.contains(uniqueId)) {
                        isvList.remove(i);
                        i--;
                    }
                }
 
                //精确匹配,不分词
                if (!fuzzy) {
                    for (InternetSearchVideo sv : isvList) {
                        VideoInfo video = VideoInfoFactory.create(sv);
                        video = loadAlbumData(video);
                        //第一个专辑数量
                        if (albumCount < 3) {
                            localList.add(video);
                        } else {
                            if (video.getName().equalsIgnoreCase(key))
                                localList.add(video);
                        }
                    }
                } else {
                    for (InternetSearchVideo sv : isvList) {
                        VideoInfo video = VideoInfoFactory.create(sv);
                        video = loadAlbumData(video);
                        localList.add(video);
                    }
                }
            }
            //按关键词的匹配度排序
            Comparator<VideoInfo> cm = (VideoInfo o1, VideoInfo o2) -> {
                if (o1.getDefinition() - o2.getDefinition() == 0) {
                    float s1 = CompareStrSimUtil.getSimilarityRatio(o1.getName(), key, true);
                    float s2 = CompareStrSimUtil.getSimilarityRatio(o2.getName(), key, true);
//                        if (s1 != s2)//相似度越高越靠前
                    try {
                        return s1 != s2 ? (s2 > s1 ? 1 : -1) : (Integer.parseInt(o2.getYear()) - Integer.parseInt(o1.getYear()));
                    } catch (Exception e) {
                        return s2 > s1 ? 1 : -1;
                    }
//                        else
//                            return o1.getDefinition() - o2.getDefinition();//高清排在前
                } else {
                    return o2.getDefinition() - o1.getDefinition();//高清排在前
                }
 
            };
 
            Collections.sort(localList, cm);
        }
        //未搜索到内容
        if (localList == null || localList.size() == 0) {
            logger.warn("专辑搜索无结果#{}", key);
        }
        return new SearchResult(albumCount, localList);
    }
 
    private void filterSearchResult(List<VideoInfo> localList, List<Long> resourceList) {
        String sql = "";
        String resourceWhere = "";
        for (Long re : resourceList) {
            resourceWhere += " rv.resourceid=" + re + " or";
        }
 
        if (resourceWhere.endsWith("or"))
            resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2);
 
 
        List<String> filterVideoIdList = new ArrayList<>();
 
        for (int i = 0; i < localList.size(); i++) {
            if (NumberUtil.isNumeric(localList.get(i).getId())) {
                filterVideoIdList.add(localList.get(i).getId());
                sql += " select count(*) from wk_resource_video rv left join wk_video_video v on rv.videoid=v.id where v.id is not null and v.show=1 and rv.videoid="
                        + localList.get(i).getId() + " and (" + resourceWhere + ") union all";
            }
        }
 
        if (sql.endsWith("union all"))
            sql = sql.substring(0, sql.length() - 9);
 
        List rlist = StringUtil.isNullOrEmpty(sql) ? new ArrayList() : videoInfoDao.sqlList(sql);
        Set<String> deleteVids = new HashSet<>();
        for (int i = 0; i < filterVideoIdList.size(); i++) {
            if (Integer.parseInt(rlist.get(i) + "") < 1) {
                deleteVids.add(filterVideoIdList.get(i));
            }
        }
 
        for (int i = 0; i < localList.size(); i++) {
            if (deleteVids.contains(localList.get(i).getId())) {
                localList.remove(i);
                i--;
            }
        }
    }
 
 
    @Cacheable(value = "userCache", key = "'searchNew'+'-'+#detailSystem+'-'+#key+'-'+#page+'-'+#videoType+'-'+#system+'-'+#cacheMD5")
    public VideoListResultVO searchNew(String detailSystem, String ip, String uid, String key, int page, int videoType,
                                       String system, List<Long> resourceList, String cacheMD5) {
        SearchHistory sh = new SearchHistory();
        sh.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString());
        sh.setIp(ip);
        if (!StringUtil.isNullOrEmpty(key) && key.length() > 30)
            key = key.substring(0, 30);
        sh.setKey(key);
        if (uid != null) {
            UserInfo user = new UserInfo();
            user.setId(uid);
            sh.setUser(user);
        }
 
        searchDao.create(sh);
        // 查询数据库
 
        //专辑数量
        long albumCount = 0L;
        List<VideoInfo> list = new ArrayList<>();
        List<VideoInfo> localList = null;
        try {
            long startt = System.currentTimeMillis();
            localList = null;
 
            localList = new ArrayList<>();
            //先搜索专辑
            int pageSize = 20;
            SearchResult searchResult = searchAlbum(videoType, key, resourceList, page, pageSize, false);
 
            if (page == 1 && (searchResult == null || searchResult.videoInfoList == null || searchResult.videoInfoList.size() == 0)) {
                searchResult = searchAlbum(videoType, key, resourceList, page, pageSize, true);
            }
 
            if (searchResult != null && searchResult.videoInfoList != null) {
                localList.addAll(searchResult.videoInfoList);
            }
 
            Set<String> solrAlbumVids = new HashSet<>();
            for (VideoInfo videoInfo : localList) {
                solrAlbumVids.add(videoInfo.getId());
            }
 
 
            int commonSolrPage = page - searchResult.getTotalCount() / pageSize;
 
            if (videoType == 0 && commonSolrPage > 0) {
                //搜索原始的
                try {
                    List<VideoInfo> solrList = SolrUtil.search(key, commonSolrPage);
                    for (VideoInfo vi : solrList) {
                        if (!solrAlbumVids.contains(vi.getId())) {
                            localList.add(vi);
                        }
                    }
                } catch (Exception e) {
 
                }
            }
            System.out.println("搜索耗时:" + (System.currentTimeMillis() - startt));
            filterSearchResult(localList, resourceList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        if (localList != null && localList.size() > 0) {
            list.addAll(localList);
        }
 
        //获取附加信息
        list = videoInfoExtraService.batchExtra(list, resourceList);
 
        return new VideoListResultVO(list, videoType == 0 ? albumCount : 1000L);
    }
 
    public long searchCount(String key, String system) {
        String sql = "";
        if (system.equalsIgnoreCase("2")) {
            sql = "select count(*)  from VideoInfo v where v.show='1' and (v.name like ?) and (v.videoType.id=105 or v.videoType.id=106 or v.videoType.id=107 or v.videoType.id=108))";
        } else {
            sql = "select count(*)  from VideoInfo v where v.show='1' and (v.name like ?)";
        }
 
        long count = videoInfoDao.getCount(sql,
                new String[]{(new StringBuilder("")).append(key).append("%").toString()});
        return count;
    }
 
    @SuppressWarnings({"rawtypes"})
    @Cacheable(value = "userCache", key = "'getHotSearchList'+'-'+#system")
    public List<String> getHotSearchList(String system) {
        List<String> list = new ArrayList<>();
        try {
            List li = searchDao.sqlList(
                    "SELECT h.`name` FROM wk_video_super_hotsearch sh LEFT JOIN wk_video_hotsearch h ON sh.`hotsearchid`=h.`id`  WHERE sh.`detailsystem`=? ORDER BY h.`orderby` DESC",
                    0, Constant.HOT_SEARCH_COUNT, new Serializable[]{system});
            for (int i = 0; i < li.size(); i++) {
                list.add(li.get(i) + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
 
        }
        return list;
    }
 
    public List<HotSearch> getHotSearchListByDS(String dsid) {
        return hotSearchDao.list("from HotSearch hs where hs.system.id=" + dsid + " order by hs.orderby desc");
    }
 
    public List<HotSearch> getHotSearchList() {
        return hotSearchDao.list("from HotSearch");
    }
 
    /**
     * 热门搜索
     */
 
    public void addSuperHotSearch(SuperHotSearch sv) {
        List<SuperHotSearch> list = superHotSearchDao.list("from SuperHotSearch sv where sv.hotSearch.id="
                + sv.getHotSearch().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superHotSearchDao.create(sv);
    }
 
    public void updateSuperHotSearch(SuperHotSearch hotSearch) {
        superHotSearchDao.update(hotSearch);
    }
 
    public void deleteSuperHotSearch(SuperHotSearch hotSearch) {
        superHotSearchDao.delete(hotSearch);
    }
 
    @SuppressWarnings("unchecked")
    public void updateSuperHotSearchList(final String detailSystemId, final List<SuperHotSearch> typeList) {
        searchDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHotSearch> list = session
                            .createQuery("from SuperHotSearch sh where sh.detailSystem.id=" + detailSystemId).list();
                    session.getTransaction().begin();
                    for (SuperHotSearch ad : list) {
                        session.delete(ad);
                    }
 
                    for (SuperHotSearch videoType : typeList) {
                        session.persist(videoType);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
 
                return null;
            }
        });
 
    }
 
    @SuppressWarnings("unchecked")
    public List<HotSearchAdmin> getHotSearchAdmin(String key, String systemId, int detailSystem, int page) {
        List<HotSearchAdmin> zhiBoClassList = new ArrayList<>();
        try {
            List<DetailSystem> detailSystemList = detailSystemDao.list("from DetailSystem ds where ds.system.id=" + systemId);
            String sql = "";
            if (detailSystem > 0)
                sql = "select sh.hotSearch from SuperHotSearch sh where sh.hotSearch.name like ? and sh.detailSystem.id="
                        + detailSystem + " order by  sh.createtime desc";
            else
                sql = "from HotSearch zb where zb.name like ? and zb.system.id=" + systemId + " order by zb.createtime desc";
 
            List<HotSearch> list = hotSearchDao.list(sql, (page - 1) * Constant.pageCount, Constant.pageCount,
                    new Serializable[]{"%" + key + "%"});
            for (HotSearch vb : list) {
                List<DetailSystem> detailSystemS = detailSystemDao
                        .list("select vb.detailSystem from SuperHotSearch vb where vb.hotSearch.id=" + vb.getId());
 
                List<DetailSystemSelect> dssList = new ArrayList<>();
 
                for (DetailSystem ds : detailSystemList) {
                    DetailSystemSelect dss = new DetailSystemSelect();
                    dss.setDetailSystem(ds);
                    dss.setSelected(false);
                    dssList.add(dss);
                }
 
                // 设置已经存在的
                for (DetailSystem ds : detailSystemS) {
                    for (DetailSystemSelect dss : dssList) {
                        if (dss.getDetailSystem().getId().equalsIgnoreCase(ds.getId())) {
                            dss.setSelected(true);
                            break;
                        }
                    }
                }
                SuperHotSearch sz = new SuperHotSearch();
                sz.setDetailSystem(null);
                sz.setHotSearch(vb);
                zhiBoClassList.add(new HotSearchAdmin(sz, dssList));
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zhiBoClassList;
    }
 
    public long getHotSearchAdminCount(String key, String systemId, int detailSystem) {
        String sql = "";
        if (detailSystem > 0)
            sql = "select count(*) from  (select count(*) from wk_video_super_hotsearch zb left join wk_video_hotsearch c on c.id=zb.hotsearchid where zb.detailsystem="
                    + detailSystem + " and c.name like '%" + key + "%' group by zb.hotsearchid) s";
        else
            sql = "select count(*) from wk_video_hotsearch h where h.name like '%" + key + "%' and h.system=" + systemId;
 
        return videoBanQuanVideoDao.getCountSQL(sql);
    }
 
    /**
     * 添加热门搜索
     *
     * @param search
     */
    public void addHotSearch(HotSearch search) {
        hotSearchDao.create(search);
    }
 
    /**
     * 修改热门搜索
     *
     * @param search
     */
    public void updateHotSearch(HotSearch search) {
 
        hotSearchDao.update(search);
    }
 
    /**
     * 删除热门搜索
     *
     * @param search
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void deleteHotSearch(final HotSearch search) {
        searchDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    List<SuperHotSearch> list = session.createQuery("from SuperHotSearch sv where sv.hotSearch.id=?")
                            .setParameter(0, search.getId()).list();
                    for (SuperHotSearch sv : list)
                        session.delete(sv);
                    session.createSQLQuery("delete from wk_video_hotsearch  where id=" + search.getId())
                            .executeUpdate();
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    /**
     * 获取热门搜索
     *
     * @param id
     * @return
     */
    public HotSearch getHotSearch(String id) {
        List<HotSearch> list = hotSearchDao.getSession().createQuery("from HotSearch where id=?").setParameter(0, id)
                .list();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHotSearchAdmin(final String classId, final String detailSystemId) {
        searchDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
 
                    List<SuperHotSearch> list = session
                            .createQuery("from SuperHotSearch vb where vb.hotSearch.id=? and vb.detailSystem.id=?")
                            .setParameter(0, classId).setParameter(1, detailSystemId).list();
                    if (list != null && list.size() > 0) {
                        session.getTransaction().begin();
                        for (SuperHotSearch vb : list)
                            session.delete(vb);
                        session.flush();
                        session.getTransaction().commit();
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
 
    }
 
 
    /**
     * 将搜索结果对象转为视频对象
     *
     * @param solrAlbumVideoList
     * @param resourceList
     * @return
     */
    public List<VideoInfo> convertSolrAlbumResultToVideo(List<SolrAlbumVideo> solrAlbumVideoList, List<Long> resourceList) {
        List<VideoInfo> videoInfoList = new ArrayList<>();
        for (SolrAlbumVideo sv : solrAlbumVideoList) {
            VideoInfo video = VideoInfoFactory.create(sv);
            videoInfoList.add(video);
        }
        //获取附加信息
        videoInfoList = videoInfoExtraService.batchExtra(videoInfoList, resourceList);
        return videoInfoList;
    }
 
 
    public static class SearchResult {
        private int totalCount;
        private List<VideoInfo> videoInfoList;
 
        public SearchResult(int totalCount, List<VideoInfo> videoInfoList) {
            this.totalCount = totalCount;
            this.videoInfoList = videoInfoList;
        }
 
        public int getTotalCount() {
            return totalCount;
        }
 
        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }
 
        public List<VideoInfo> getVideoInfoList() {
            return videoInfoList;
        }
 
        public void setVideoInfoList(List<VideoInfo> videoInfoList) {
            this.videoInfoList = videoInfoList;
        }
    }
 
}