admin
2021-04-02 d551bef5e2b09bd98681cf97807988c9863e66fc
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
package com.yeshi.buwan.service.imp;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.yeshi.buwan.service.inter.video.VideoInfoExtraService;
import org.hibernate.CacheMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.buwan.dao.SuperVideoTypeDao;
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.VideoTypeDao;
import com.yeshi.buwan.domain.CategoryVideo;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.SuperHotType;
import com.yeshi.buwan.domain.SuperVideoType;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.domain.VideoType;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.StringUtil;
 
@Service
public class ClassService {
    @Resource
    private VideoTypeDao videoTypeDao;
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private SuperVideoTypeDao superVideoTypeDao;
 
    @Resource
    private VideoInfoExtraService videoInfoExtraService;
 
    public List<VideoType> getTypeList() {
        List<VideoType> list = videoTypeDao.list("from VideoType v where v.show='1' and v.parent=null");
        return list;
    }
 
    public VideoType getTypeById(String id) {
 
        return videoTypeDao.find(VideoType.class, id);
    }
 
    public VideoType getTypeById(long id) {
 
        return videoTypeDao.find(VideoType.class, id);
    }
 
    @Cacheable(value = "classCache", key = "'getFirstTypeList'+'-'+#parentId")
    public List<VideoType> getFirstTypeList(String parentId) {
        if (StringUtil.isNullOrEmpty(parentId) || parentId.equalsIgnoreCase("0"))
            return getTypeList();
 
        List<VideoType> list = videoTypeDao
                .list("from VideoType v where v.show='1' and v.parent.id=" + parentId + " order by v.orderby desc");
        return list;
    }
 
    public List<VideoType> getAllTypeList() {
 
        List<VideoType> list = videoTypeDao.list("from VideoType v order by v.orderby desc");
        return list;
    }
 
    public List<VideoType> getNextTypeList(String pid) {
 
        if (StringUtil.isNullOrEmpty(pid)) {
            List<VideoType> list = videoTypeDao
                    .list("from VideoType v where v.show='1' and v.parent=null order by v.orderby desc");
            return list;
        }
        if (Integer.parseInt(pid) > 0) {
            List<VideoType> list = videoTypeDao.list(
                    (new StringBuilder("from VideoType v where v.show='1' and v.parent.id=")).append(pid).toString()
                            + " order by v.orderby desc");
            return list;
        } else {
            List<VideoType> list = videoTypeDao
                    .list("from VideoType v where v.show='1' and v.parent=null order by v.orderby desc");
            return list;
        }
    }
 
    @SuppressWarnings("unchecked")
    public void deleteVideoType(final String id) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    List<SuperVideoType> list = session.createQuery("from SuperVideoType sv where sv.type.id=?")
                            .setParameter(0, Long.parseLong(id)).list();
                    for (SuperVideoType sv : list)
                        session.delete(sv);
                    session.delete(new VideoType(Long.parseLong(id)));
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    public VideoType getType(long id) {
 
        return videoTypeDao.find(VideoType.class, Long.valueOf(id));
    }
 
    public void createType(VideoType type) {
 
        videoTypeDao.create(type);
    }
 
    public void updateType(VideoType type) {
 
        videoTypeDao.update(type);
    }
 
    public long getTypeVideoCount(String type) {
        String wheres = "";
        String sts[] = getChildrenType(Integer.parseInt(type));
        String as[];
        int j = (as = sts).length;
        for (int i = 0; i < j; i++) {
            String st = as[i];
            wheres = (new StringBuilder(String.valueOf(wheres))).append("or h.videoType.id=").append(Long.parseLong(st))
                    .append(" ").toString();
        }
 
        if (wheres.startsWith("or"))
            wheres = wheres.substring(2, wheres.length());
        long count = videoInfoDao
                .getCount("select count(*)  from CategoryVideo as h where h.video.show='1' and (h.videoType.id=" + type
                        + " or h.videoType.parent.id=" + type + ")");
        return count;
    }
 
    @SuppressWarnings("unchecked")
    public String[] getChildrenType(final int type) {
        return (String[]) videoTypeDao.excute(new HibernateCallback<String[]>() {
            public String[] doInHibernate(Session session) throws HibernateException {
                String sts[] = new String[]{};
                try {
                    List<String> list = session.createSQLQuery(
                            (new StringBuilder("select queryChildrenType(")).append(type).append(")").toString())
                            .list();
                    sts = list.get(0).toString().substring(2, list.get(0).toString().length()).split(",");
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                return sts;
            }
        });
 
    }
 
    /**
     * @param type
     * @param page
     * @param order 1-最近更新 2-最热 4-评论最多
     * @return
     */
    public List<VideoInfo> getTypeVideoList(String type, int page, int order) {
        String orderby = "";
        if (order == 1) {
            orderby = " order by FROM_UNIXTIME(h.updatetime/1000) desc";
        } else if (order == 2) {
            orderby = " order by h.latestWatchCount desc";
        } else if (order == 3) {
            orderby = " order by h.commentCount desc";
        }
        String wheres = "";
        if (StringUtil.isNullOrEmpty(type))
            return videoInfoDao.list("from VideoInfo as h where h.videoType.parent=null and h.show='1' ?",
                    (page - 1) * Constant.pageCount, Constant.pageCount, new String[]{orderby});
        String sts[] = getChildrenType(Integer.parseInt(type));
        String as[];
        int j = (as = sts).length;
        for (int i = 0; i < j; i++) {
            String st = as[i];
            wheres = (new StringBuilder(String.valueOf(wheres))).append("or h.videoType.id=").append(Long.parseLong(st))
                    .append(" ").toString();
        }
 
        if (wheres.startsWith("or"))
            wheres = wheres.substring(2, wheres.length());
        List<VideoInfo> list = videoInfoDao.list("from VideoInfo as h where h.show='1' and (" + wheres + ") " + orderby,
                (page - 1) * Constant.pageCount, Constant.pageCount, new String[]{});
        return list;
    }
 
    /**
     * @param type
     * @param ds
     * @param page
     * @param order
     * @param resourceList
     * @return
     */
    @SuppressWarnings("unchecked")
    @Cacheable(value = "longTimeCache", key = "'getTypeVideoList'+'-'+#type+'-'+#ds.id+'-'+#page+'-'+#pageCount+'-'+#order+'-'+#cachemd5")
    public List<VideoInfo> getTypeVideoList(String type, DetailSystem ds, final int page, final int pageCount,
                                            int order, 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);
 
        String orderby = "";
        if (order == 1) {
            orderby = " order by FROM_UNIXTIME(v.updatetime/1000) desc";
        } else if (order == 2) {
            orderby = " order by v.watchcount desc";
        } else if (order == 3) {
            orderby = " order by v.commentcount desc";
        }
 
        String wheres = "";
        if (StringUtil.isNullOrEmpty(type)) {
            return getVideoInfoData(videoInfoDao.sqlList(
                    "select v.id,v.`name`,v.`tag`,v.`picture`,v.`hpicture`,v.`latest_hpicture`,v.watchcount,v.commentcount from wk_video_video v INNER JOIN  (SELECT  DISTINCT(v.id) FROM wk_category_video ca LEFT JOIN wk_video_video v ON v.id= ca.videoid LEFT JOIN wk_resource_video rv ON rv.`videoid`= v.`id`  AND( ? ) WHERE v.show= 1 and ca.id is not null AND rv.`resourceid` IS NOT NULL ? ) a using(id)",
                    (page - 1) * pageCount, pageCount, new Serializable[]{resourceWhere, orderby,}), resourceList);
        }
 
        List<VideoInfo> list = new ArrayList<>();
 
        long startTime = System.currentTimeMillis();
 
        final String typeNew = type;
        final String resourceWhereNew = resourceWhere;
        final String orderbyNew = orderby;
        list = (List<VideoInfo>) videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                List<VideoInfo> list = new ArrayList<>();
                try {
                    session.setCacheMode(CacheMode.GET);
                    String sql = String.format(
                            "select v.id,v.`name`,v.`tag`,v.`picture`,v.`hpicture`,v.`latest_hpicture`,v.watchcount,v.commentcount from wk_video_video v INNER JOIN  (SELECT  DISTINCT(v.id) FROM wk_video_video v LEFT JOIN wk_category_video ca  ON v.id=ca.videoid  LEFT JOIN wk_video_type t ON t.`id`=ca.`videotypeid` AND (t.`id`=%s OR t.`pid`=%s) LEFT JOIN wk_resource_video rv ON rv.`videoid`=v.`id` AND (%s) WHERE v.show=1 AND ca.id>0 AND t.`id`>0 AND rv.`resourceid`>0  %s) a using(id)",
                            typeNew, typeNew, resourceWhereNew, orderbyNew);
                    System.out.println(sql);
 
                    list = session.createSQLQuery(sql).setFirstResult((page - 1) * pageCount).setCacheable(true)
                            .setMaxResults(pageCount).list();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return list;
            }
        });
 
        System.out.println("查询SQL费时:" + (System.currentTimeMillis() - startTime));
        return getVideoInfoData(list, resourceList);
    }
 
    public List<VideoInfo> getVideoInfoData(List list, List<Long> resourceIdList) {
        List<VideoInfo> dataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Object[] obj = (Object[]) list.get(i);
            VideoInfo info = new VideoInfo();
            info.setId(obj[0] + "");
            info.setName(obj[1] + "");
            info.setTag(obj[2] + "");
            info.setPicture(obj[3] + "");
            info.setHpicture(obj[4] + "");
            info.setLatestHpicture(obj[5] + "");
            info.setWatchCount(obj[6] + "");
            info.setCommentCount(obj[7]!=null? Integer.parseInt(obj[7] + ""):0);
            if (obj.length > 8) {
                String[] resourceIds = (obj[8] + "").split(",");
                List<VideoResource> resourceList = new ArrayList<>();
                for (String rid : resourceIds)
                    resourceList.add(new VideoResource(rid));
                info.setResourceList(resourceList);
            }
            if (obj.length > 9) {
                info.setVpicture(obj[9] + "");
            }
            dataList.add(info);
        }
 
        dataList = videoInfoExtraService.batchExtra(dataList, resourceIdList);
 
        return dataList;
    }
 
    @SuppressWarnings("unchecked")
    public List<VideoInfo> getTypeVideoList(String type, String startYear, String endYear, DetailSystem ds,
                                            final int page, int order) {
        String orderby = "";
        if (order == 1) {
            orderby = " order by v.watchcount desc, v.updatetime desc";
        } else if (order == 2) {
            // orderby = " order by v.updatetime desc,v.watchcount desc";
            // 豆豆系
            // if (ds.getPackageName().contains("doudou"))
            orderby = " order by FROM_UNIXTIME(v.createtime/1000) desc";
 
        } else if (order == 3) {
            orderby = " order by v.orderby desc,v.watchcount desc, v.updatetime desc";
        } else if (order == 4) {
            orderby = " order by v.score desc,v.updatetime desc";
        }
 
        String yearWhere = "";
        if (!StringUtil.isNullOrEmpty(startYear) && !StringUtil.isNullOrEmpty(endYear)) {
            yearWhere = " and (v.year>=" + startYear + " and v.year<=" + endYear + ") ";
        }
 
        String wheres = "";
        if (StringUtil.isNullOrEmpty(type)) {
            final String yearWhereNew = yearWhere;
            final String orderbyNew = orderby;
            return (List<VideoInfo>) videoTypeDao.excute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    try {
                        return session
                                .createSQLQuery(
                                        "SELECT  v.* FROM wk_category_video ca left join wk_video_video v on v.id=ca.videoid where v.show=1 "
                                                + yearWhereNew + " group by v.id ?")
                                .addEntity(VideoInfo.class).setParameter(0, orderbyNew)
                                .setFirstResult((page - 1) * Constant.pageCount).setMaxResults(Constant.pageCount)
                                .list();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
 
        }
 
        String sts[] = getChildrenType(Integer.parseInt(type));
        String as[];
        int j = (as = sts).length;
        for (int i = 0; i < j; i++) {
            String st = as[i];
            wheres = (new StringBuilder(String.valueOf(wheres))).append("or ca.videotypeid=").append(Long.parseLong(st))
                    .append(" ").toString();
        }
 
        if (wheres.startsWith("or"))
            wheres = wheres.substring(2, wheres.length());
        List<VideoInfo> list = new ArrayList<>();
 
        final String wheresNew = wheres;
        final String yearWhereNew = yearWhere;
        final String orderbyNew = orderby;
        list = (List<VideoInfo>) videoTypeDao.excute(new HibernateCallback<List<VideoInfo>>() {
            public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
                List<VideoInfo> list = new ArrayList<>();
                try {
                    list = session
                            .createSQLQuery(
                                    "select v.* from wk_category_video ca left join wk_video_video v on v.id=ca.videoid where v.show=1 and ("
                                            + wheresNew + ") " + yearWhereNew + " group by v.id " + orderbyNew)
                            .addEntity(VideoInfo.class).setFirstResult((page - 1) * Constant.pageCount)
                            .setMaxResults(Constant.pageCount).list();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return list;
            }
        });
 
        return list;
    }
 
    private List<VideoInfo> parseVideoListAmin(List list) {
        List<VideoInfo> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Object[] objs = (Object[]) list.get(i);
            VideoInfo video = new VideoInfo();
            video.setId(objs[0] + "");
            video.setName(objs[1] + "");
            video.setPicture(objs[2] + "");
            video.setLatestHpicture(objs[3] + "");
            video.setHpicture(objs[4] + "");
            video.setCreatetime(Long.parseLong(objs[5] + ""));
            video.setShow(objs[6] + "");
 
            boolean isEx = false;
            for (VideoInfo info : resultList) {
                if (info.getId().equalsIgnoreCase(video.getId())) {
                    isEx = true;
                    break;
                }
            }
            if (!isEx)
                resultList.add(video);
        }
        return resultList;
    }
 
    public List<VideoInfo> getTypeVideoListAdmin(String type, int page, String key, int contenttype) {
        String ctwhere = "";
        if (contenttype == 1)
            ctwhere = " and v.`contenttype`=1 ";
        else if (contenttype > 1) {
            ctwhere = " and v.`contenttype`>1 ";
        }
        String sql = "";
        if (StringUtil.isNullOrEmpty(type))
            sql = String.format(
                    "SELECT v.`id`,v.name,v.`picture`,v.`latest_hpicture`,v.`hpicture`,v.`createtime`,v.`show` FROM wk_video_video v LEFT JOIN wk_category_video cv ON cv.`videoid`=v.`id` LEFT JOIN wk_video_type ty ON ty.`id`=cv.`videotypeid` WHERE  v.`name` LIKE %s %s ORDER BY  v.`createtime` DESC",
                    "'" + key + "%'", ctwhere);
        else {
            sql = String.format(
                    "SELECT v.`id`,v.name,v.`picture`,v.`latest_hpicture`,v.`hpicture`,v.`createtime`,v.`show` FROM wk_video_video v LEFT JOIN wk_category_video cv ON cv.`videoid`=v.`id` LEFT JOIN wk_video_type ty ON ty.`id`=cv.`videotypeid` WHERE (ty.`id`=%s OR ty.`pid`=%s)  AND v.`name` LIKE %s %s ORDER BY  v.`createtime` DESC",
                    type, type, "'" + key + "%'", ctwhere);
        }
 
        List result = videoInfoDao.sqlList(sql, (page - 1) * Constant.pageCount, Constant.pageCount, null);
 
        List<VideoInfo> list = parseVideoListAmin(result);
        return list;
    }
 
    public long getTypeVideoListAdminCount(String type, String key, int contenttype) {
 
        String wheres = "";
        String ctwhere = "";
        if (contenttype == 1)
            ctwhere = " and v.`contenttype`=1 ";
        else if (contenttype > 1) {
            ctwhere = " and v.`contenttype`>1 ";
        }
 
        String sql = "";
        if (StringUtil.isNullOrEmpty(type))
            sql = String.format(
                    "SELECT count(v.`id`) FROM wk_video_video v LEFT JOIN wk_category_video cv ON cv.`videoid`=v.`id` LEFT JOIN wk_video_type ty ON ty.`id`=cv.`videotypeid` WHERE  v.`name` LIKE %s %s",
                    "'" + key + "%'", ctwhere);
        else {
            sql = String.format(
                    "SELECT count(v.`id`) FROM wk_video_video v LEFT JOIN wk_category_video cv ON cv.`videoid`=v.`id` LEFT JOIN wk_video_type ty ON ty.`id`=cv.`videotypeid` WHERE (ty.`id`=%s OR ty.`pid`=%s)  AND v.`name` LIKE %s %s",
                    type, type, "'" + key + "%'", ctwhere);
        }
 
        return videoInfoDao.getCountSQL(sql);
    }
 
    public long getTypeVideoListAdminPage(String type, String key, int contenttype) {
        long count = getTypeVideoListAdminCount(type, key, contenttype);
        return count % (long) Constant.pageCount != 0L ? count / (long) Constant.pageCount + 1L
                : count / (long) Constant.pageCount;
    }
 
    public VideoType getRootVideoType(VideoType ty) {
 
        ty = videoTypeDao.find(VideoType.class, ty.getId());
        while (ty.getParent() != null)
            ty = ty.getParent();
        return ty;
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHotTypeAdmin(final String hotTypeId) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    List<SuperHotType> list = session.createQuery("from SuperHotType sh where sh.hotType.id=?")
                            .setParameter(0, hotTypeId).list();
                    if (list != null && list.size() > 0) {
                        for (SuperHotType st : list) {
                            session.delete(st);
                        }
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    @SuppressWarnings("unchecked")
    public void deleteVideoTypeAdmin(final String videoTypeId) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperVideoType> list = session.createQuery("from SuperVideoType sh where sh.type.id=?")
                            .setParameter(0, videoTypeId).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperVideoType st : list) {
                            session.delete(st);
                        }
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    @SuppressWarnings("unchecked")
    public void deleteVideoTypeAdmin(final String videtype, final String detailSystem) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperVideoType> list = session
                            .createQuery("from SuperVideoType sh where sh.type.id=? and sh.detailSystem.id=?")
                            .setParameter(0, Long.parseLong(videtype)).setParameter(1, detailSystem).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperVideoType st : list) {
                            session.delete(st);
                        }
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    @SuppressWarnings("unchecked")
    public void deleteHotTypeAdmin(final String hotTypeId, final String detailSystem) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperHotType> list = session
                            .createQuery("from SuperHotType sh where sh.hotType.id=? and sh.detailSystem.id=?")
                            .setParameter(0, hotTypeId).setParameter(1, detailSystem).list();
                    session.getTransaction().begin();
                    if (list != null && list.size() > 0) {
                        for (SuperHotType st : list) {
                            session.delete(st);
                        }
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    /**
     * 首页大分类--后台编辑使用
     *
     * @param detailSystem
     * @param page
     * @return
     */
 
    @SuppressWarnings("unchecked")
    public List<com.yeshi.buwan.domain.web.VideoTypeAdmin> getVideoTypeAdmin(final String systemId, final int detailSystem, final int pid,
                                                                             final int page) {
        return (List<com.yeshi.buwan.domain.web.VideoTypeAdmin>) videoTypeDao
                .excute(new HibernateCallback<List<com.yeshi.buwan.domain.web.VideoTypeAdmin>>() {
                    public List<com.yeshi.buwan.domain.web.VideoTypeAdmin> doInHibernate(Session session)
                            throws HibernateException {
                        List<com.yeshi.buwan.domain.web.VideoTypeAdmin> hotTypeList = new ArrayList<>();
                        try {
                            List<DetailSystem> detailSystemList = session.createQuery("from DetailSystem ds where ds.system.id=" + systemId).list();
                            String where = "";
 
                            List<VideoType> list = null;
                            if (detailSystem > 0) {
                                where += " where vb.detailSystem.id= " + detailSystem;
 
                                if (pid > 0)
 
                                    list = session
                                            .createQuery("select vb.type from SuperVideoType vb " + where
                                                    + " and vb.type.parent!=null and vb.type.parent.id=" + pid
                                                    + " group by vb.type.id order by vb.createtime desc")
                                            .setFirstResult((page - 1) * Constant.pageCount)
                                            .setMaxResults(Constant.pageCount).list();
                                else
                                    list = session
                                            .createQuery("select vb.type from SuperVideoType vb " + where
                                                    + " and vb.type.parent=null group by vb.type.id order by vb.createtime desc")
                                            .setFirstResult((page - 1) * Constant.pageCount)
                                            .setMaxResults(Constant.pageCount).list();
                            } else {
                                if (pid > 0)
                                    list = session
                                            .createQuery("from VideoType vb where vb.parent!=null and vb.parent.id="
                                                    + pid + " order by vb.createtime desc")
                                            .setFirstResult((page - 1) * Constant.pageCount)
                                            .setMaxResults(Constant.pageCount).list();
                                else
                                    list = session
                                            .createQuery(
                                                    "from VideoType vb where vb.parent=null order by vb.createtime desc")
                                            .setFirstResult((page - 1) * Constant.pageCount)
                                            .setMaxResults(Constant.pageCount).list();
                            }
                            for (VideoType vb : list) {
                                List<DetailSystem> detailSystemS = session
                                        .createQuery("select vb.detailSystem from SuperVideoType vb where vb.type.id=?")
                                        .setParameter(0, vb.getId()).list();
 
                                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;
                                        }
                                    }
                                }
 
                                hotTypeList.add(new com.yeshi.buwan.domain.web.VideoTypeAdmin(vb, dssList));
                            }
 
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return hotTypeList;
                    }
                });
 
    }
 
    /**
     * 获取热门分类数量
     *
     * @param detailSystem
     * @return
     */
    public long getVideoTypeAdminCount(int detailSystem, int pid) {
 
        String where = "";
        if (detailSystem > 0) {
 
            where += " where bv.detailsystem= " + detailSystem;
            if (pid > 0)
                return videoTypeDao.getCountSQL(
                        "select count(*) from  (select count(*) from wk_video_super_class bv left join wk_video_type v on v.id=bv.classid "
                                + where + " and v.pid=" + pid + " group by bv.hottypeid) s");
            else
                return videoTypeDao.getCountSQL(
                        "select count(*) from  (select count(*) from wk_video_super_class bv left join wk_video_type v on v.id=bv.classid "
                                + where + " and v.pid IS NULL group by bv.hottypeid) s");
        } else {
            if (pid > 0)
                return videoTypeDao
                        .getCount("select count(*) from VideoType vt where vt.parent!=null and vt.parent.id=" + pid);
            else
                return videoTypeDao.getCount("select count(*) from VideoType vt where vt.parent=null");
        }
    }
 
    public void updateVideoTypes(final String videoid, final List<VideoType> typeList) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.createQuery("delete from CategoryVideo cv where cv.video.id=?").setParameter(0, videoid)
                            .executeUpdate();
                    session.getTransaction().begin();
                    for (VideoType vt : typeList) {
                        CategoryVideo cv = new CategoryVideo();
                        cv.setVideo(new VideoInfo(videoid));
                        cv.setVideoType(vt);
                        session.persist(cv);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
    /**
     * 获取某个视频的分类
     *
     * @param vid
     * @return
     */
    @Cacheable(value = "homeCache", key = "'getVideoTypeList'+'-'+#vid")
    public List<VideoType> getVideoTypeList(String vid) {
        return videoTypeDao.list("select cv.videoType from CategoryVideo cv where cv.video.id=?", new String[]{vid});
    }
 
    /**
     * 显示视频大分类区
     */
 
    @Cacheable(value = "classCache", key = "'getSuperVideoTypeList'+'-'+#detailSystem")
    public List<SuperVideoType> getSuperVideoTypeList(String detailSystem) {
        return superVideoTypeDao.list(
                "from  SuperVideoType s where s.detailSystem.id=" + detailSystem + " order by s.type.orderby desc");
    }
 
    @Cacheable(value = "classCache", key = "'getSuperVideoTypeTitleList'+'-'+#detailSystem")
    public List<SuperVideoType> getSuperVideoTypeTitleList(String detailSystem) {
        return superVideoTypeDao.list(
                "from  SuperVideoType s where s.detailSystem.id=" + detailSystem + " and s.type.showTitle='1' order by s.type.orderby desc");
    }
 
    public void addSuperVideoType(SuperVideoType sv) {
        List<SuperVideoType> list = superVideoTypeDao.list("from SuperVideoType sv where sv.type.id="
                + sv.getType().getId() + " and sv.detailSystem.id=" + sv.getDetailSystem().getId());
        if (list != null && list.size() > 0)
            return;
        superVideoTypeDao.create(sv);
    }
 
    @SuppressWarnings("unchecked")
    public void updateSuperVideoTypeList(final String detailSystemId, final List<SuperVideoType> typeList) {
        videoTypeDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    List<SuperVideoType> list = session
                            .createQuery("from SuperVideoType sh where sh.detailSystem.id=" + detailSystemId).list();
                    for (SuperVideoType ad : list) {
                        session.delete(ad);
                    }
 
                    for (SuperVideoType videoType : typeList) {
                        session.persist(videoType);
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
 
    }
 
    public void deleteSuperVideoType(SuperVideoType videoType) {
        superVideoTypeDao.delete(videoType);
    }
 
    public List<VideoType> getAllVideoType() {
        return videoTypeDao.list("from VideoType vt where vt.parent = null");
    }
 
 
}