admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
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
package com.newvideo.service.imp.juhe;
 
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.newvideo.dao.VideoInfoDao;
import com.newvideo.dao.VideoResourceDao;
import com.newvideo.dao.VideoYouKuDao;
import com.newvideo.dao.juhe.ProgramDao;
import com.newvideo.dao.juhe.ProgramVideoDao;
import com.newvideo.dao.juhe.ProgramVideoMapDao;
import com.newvideo.domain.CategoryVideo;
import com.newvideo.domain.ResourceVideo;
import com.newvideo.domain.VideoDetailInfo;
import com.newvideo.domain.VideoInfo;
import com.newvideo.domain.VideoResource;
import com.newvideo.domain.VideoType;
import com.newvideo.domain.entity.PlayUrl;
import com.newvideo.domain.push.VideoPushHistory;
import com.newvideo.service.imp.BanQuanService;
import com.newvideo.service.imp.StatisticsService;
import com.newvideo.service.imp.VideoResourceService;
import com.newvideo.service.imp.VideoService;
import com.newvideo.service.imp.push.PushService;
import com.newvideo.util.Constant;
import com.newvideo.util.StringUtil;
import com.newvideo.youku.YouKuApi;
import com.newvideo.youku.YouKuUtil;
import com.newvideo.youku.entity.Program;
import com.newvideo.youku.entity.ProgramVideo;
import com.newvideo.youku.entity.ProgramVideoMap;
import com.newvideo.youku.entity.VideoYouKu;
 
import net.sf.json.JSONObject;
 
@Service
public class YouKuService {
    Logger log = Logger.getLogger(YouKuService.class);
    @Resource
    private VideoYouKuDao videoYouKuDao;
    @Resource
    private StatisticsService statisticsService;
    @Resource
    private PushService pushService;
    @Resource
    private ProgramDao programDao;
    @Resource
    private ProgramVideoDao programVideoDao;
    @Resource
    private VideoService videoService;
    @Resource
    private VideoResourceService videoResourceService;
    @Resource
    private BanQuanService banQuanService;
    @Resource
    private ProgramVideoMapDao programVideoMapDao;
 
    @Resource
    private VideoInfoDao videoInfoDao;
 
    @Resource
    private VideoResourceDao videoResourceDao;
 
    static Logger logger = Logger.getLogger(YouKuService.class);
 
    public VideoService getVideoService() {
        return videoService;
    }
 
    public void setVideoService(VideoService videoService) {
        this.videoService = videoService;
    }
 
    public VideoResourceService getVideoResourceService() {
        return videoResourceService;
    }
 
    public void setVideoResourceService(VideoResourceService videoResourceService) {
        this.videoResourceService = videoResourceService;
    }
 
    public ProgramVideoDao getProgramVideoDao() {
        return programVideoDao;
    }
 
    public void setProgramVideoDao(ProgramVideoDao programVideoDao) {
        this.programVideoDao = programVideoDao;
    }
 
    public ProgramDao getProgramDao() {
        return programDao;
    }
 
    public void setProgramDao(ProgramDao programDao) {
        this.programDao = programDao;
    }
 
    public PushService getPushService() {
        return pushService;
    }
 
    public void setPushService(PushService pushService) {
        this.pushService = pushService;
    }
 
    public StatisticsService getStatisticsService() {
        return statisticsService;
    }
 
    public void setStatisticsService(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }
 
    public VideoYouKuDao getVideoYouKuDao() {
        return videoYouKuDao;
    }
 
    public void setVideoYouKuDao(VideoYouKuDao videoYouKuDao) {
        this.videoYouKuDao = videoYouKuDao;
    }
 
    public void saveSigleProgram(Program pm) {
        programDao.create(pm);
    }
 
    public void saveProgramVideo(ProgramVideo pv) {
        programVideoDao.create(pv);
 
    }
 
    public void saveProgramVideoMap(ProgramVideoMap pvm) {
 
        programVideoMapDao.create(pvm);
 
    }
 
    public void saveProgram(Program pm, List<ProgramVideo> videoList) {
        try {
            if (programDao.find(Program.class, pm.getId()) == null)
                saveSigleProgram(pm);
        } catch (Exception e) {
            if (!e.getMessage().contains("Duplicate")) {
                log.error(e.getMessage());
            }
        }
 
        for (ProgramVideo pv : videoList) {
            try {
                if (programVideoDao.find(ProgramVideo.class, pv.getId()) == null) {
                    ProgramVideo v = YouKuApi.getVideoDetail(pv.getId());
                    if (v != null) {
                        pv.setOperationLimit(v.getOperationLimit());
                        pv.setCopyright_type(v.getCopyright_type());
                        pv.setPublic_type(v.getPublic_type());
                    }
                    saveProgramVideo(pv);
                }
            } catch (Exception e) {
                if (!e.getMessage().contains("Duplicate")) {
                    log.error(e.getMessage());
                }
            }
 
            ProgramVideoMap pvm = new ProgramVideoMap();
            pvm.setProgram(pm);
            pvm.setProgramVideo(pv);
            try {
                saveProgramVideoMap(pvm);
            } catch (Exception e) {
                if (!e.getMessage().contains("Duplicate")) {
                    log.error(e.getMessage());
                }
            }
        }
    }
 
    public void saveVideo(ProgramVideo video) {
        try {
            if (programVideoDao.find(ProgramVideo.class, video.getId()) == null)
                saveProgramVideo(video);
        } catch (Exception e) {
            if (!e.getMessage().contains("Duplicate")) {
                log.error(e.getMessage());
            }
        }
    }
 
    @SuppressWarnings("unchecked")
    public void updateVideoInfo(final VideoInfo info, final VideoInfo video, final Program p, final boolean isUpdate) {
        final VideoInfo vi = video;
        videoYouKuDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
 
                vi.setTag(info.getTag());
                vi.setVideocount(p.getVideoList() != null ? p.getVideoList().size() : 1);
                vi.setLatestHpicture(info.getLatestHpicture());
                vi.setLatestVpicture(info.getLatestVpicture());
                try {
                    session.getTransaction().begin();
 
                    if (isUpdate)
                        vi.setUpdatetime(System.currentTimeMillis() + "");
                    session.update(vi);
 
                    VideoYouKu vyk = new VideoYouKu();
                    vyk.setProgram(new Program(p.getId()));
                    vyk.setVideo(new VideoInfo(vi.getId()));
                    vyk.setYvideo(null);
                    Object obj = session
                            .createQuery(
                                    "select count(*)  from VideoYouKu vyk where vyk.video.id=? and vyk.program.id=?")
                            .setParameter(0, vi.getId()).setParameter(1, p.getId()).uniqueResult();
                    if (Integer.parseInt(obj + "") <= 0) {// 加入
                        // session.persist(vyk);
                        session.createSQLQuery(
                                String.format("insert into wk_video_youku (videoid, programid) values (%s, '%s')",
                                        vyk.getVideo().getId(), vyk.getProgram().getId()))
                                .executeUpdate();
                    }
 
                    obj = session.createQuery(String.format(
                            "select count(*) from ResourceVideo rv where rv.video.id=%s and rv.resource.id=%s",
                            vi.getId(), 15 + "")).uniqueResult();
                    if (Integer.parseInt(obj + "") <= 0) {// 加入
                        ResourceVideo rv = new ResourceVideo();
                        rv.setResource(new VideoResource(15 + ""));
                        rv.setVideo(new VideoInfo(vi.getId()));
                        session.createSQLQuery(String.format(
                                "insert into wk_resource_video(videoid,resourceid) values(%s,%s)", vi.getId(), 15 + ""))
                                .executeUpdate();
                    }
 
                    // 获取最新一集的信息
                    List<ProgramVideo> plist = session
                            .createQuery(
                                    "select map.programVideo from ProgramVideoMap map where map.program.id=? order by CAST(map.programVideo.stage as integer) desc")
                            .setParameter(0, p.getId()).setFirstResult(0).setMaxResults(1).list();
                    if (plist.size() > 0) {
                        List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                        detailList.add(YouKuUtil.convertProgramVideoToVideoDetail(plist.get(0), p));
                        vi.setVideoDetailList(detailList);
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    System.out.println("出错:" + e.getMessage());
                }
 
                return null;
            }
        });
        if (isUpdate) {
            if (vi.getVideoDetailList() != null && vi.getVideoDetailList().size() > 0) {
                VideoPushHistory vh = new VideoPushHistory();
                vh.setCreatetime(System.currentTimeMillis() + "");
                vh.setDetailId(vi.getVideoDetailList().get(0).getExtraId());
                vh.setResourceId(15 + "");
                vh.setTag(vi.getTag());
                vh.setType(vi.getVideoDetailList().get(0).getType());
                vh.setVideoInfo(vi);
                pushService.addVideoPushHistory(vh);
            }
        }
    }
 
    // 添加单个视频进入
    @SuppressWarnings("unchecked")
    public void updateVideoInfo(final VideoInfo info, final VideoInfo video, final ProgramVideo p, final String genre) {
        final VideoInfo vi = video;
        videoYouKuDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
 
                vi.setTag(info.getTag());
                vi.setLatestHpicture(info.getLatestHpicture());
                vi.setLatestVpicture(info.getLatestVpicture());
                try {
                    session.getTransaction().begin();
                    session.update(vi);
                    VideoYouKu vyk = new VideoYouKu();
                    vyk.setProgram(null);
                    vyk.setVideo(new VideoInfo(vi.getId()));
                    vyk.setYvideo(new ProgramVideo(p.getId()));
                    Object obj = session
                            .createQuery(
                                    "select count(*)  from VideoYouKu vyk where vyk.video.id=? and vyk.yvideo.id=?")
                            .setParameter(0, vi.getId()).setParameter(1, p.getId()).uniqueResult();
                    if (Integer.parseInt(obj + "") <= 0) {// 加入
                        session.createSQLQuery(
                                String.format("insert into wk_video_youku (videoid, yvideoid) values (%s, '%s')",
                                        vyk.getVideo().getId(), vyk.getYvideo().getId()))
                                .executeUpdate();
                    }
 
                    List<Integer> typeList = YouKuUtil.getProgramVideoType(p, genre);
                    if (typeList != null)
                        for (Integer ty : typeList) {
                            CategoryVideo cv = new CategoryVideo();
                            cv.setVideo(new VideoInfo(vi.getId()));
                            cv.setVideoType(new VideoType(ty));
                            obj = session
                                    .createQuery(
                                            "select count(*)  from CategoryVideo cv where cv.videoType.id=? and cv.video.id=?")
                                    .setParameter(0, (long) ty).setParameter(1, vi.getId()).uniqueResult();
                            if (Integer.parseInt(obj + "") <= 0)
                                videoService.addCategoryVideo(cv);
                        }
 
                    obj = session.createQuery(String.format(
                            "select count(*) from ResourceVideo rv where rv.video.id=%s and rv.resource.id=%s",
                            vi.getId(), 15 + "")).uniqueResult();
                    if (Integer.parseInt(obj + "") <= 0) {// 加入
                        ResourceVideo rv = new ResourceVideo();
                        rv.setResource(new VideoResource(15 + ""));
                        rv.setVideo(new VideoInfo(vi.getId()));
                        session.createSQLQuery(String.format(
                                "insert into wk_resource_video(videoid,resourceid) values(%s,%s)", vi.getId(), 15 + ""))
                                .executeUpdate();
                    }
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    System.out.println("出错:" + e.getMessage());
                }
 
                return null;
            }
        });
 
    }
 
    public Serializable addVideoInfo(VideoInfo info, Program p) {
        Serializable id = videoInfoDao.save(info);
        return id;
    }
 
    public Serializable addVideoInfo(VideoInfo info, ProgramVideo p) {
        Serializable id = videoInfoDao.save(info);
        return id;
    }
 
    public void saveVideoYouKu(VideoYouKu vyk) {
        try {
            videoYouKuDao.save(vyk);
        } catch (Exception e) {
 
        }
 
    }
 
    public void addPlayStatistics(String detailSystemId, String vid) {
        List list = videoYouKuDao.sqlList(
                "SELECT vy.`videoid` FROM wk_youku_program_video pv LEFT JOIN wk_youku_program p ON p.`id`=pv.`programid` LEFT JOIN wk_video_youku vy ON vy.`programid`=p.`id` WHERE pv.`videoid`=?",
                new Serializable[] { vid });
        if (list != null && list.size() > 0) {
            statisticsService.addStatistics(detailSystemId, list.get(0) + "");
        } else {
            list = videoYouKuDao.sqlList("select y.videoid from wk_video_youku y where y.yvideoid=?",
                    new Serializable[] { vid });
            if (list != null && list.size() > 0)
                statisticsService.addStatistics(detailSystemId, list.get(0) + "");
        }
    }
 
    public long getProgramVideoCount(String pid) {
        return videoYouKuDao.getCount("select count(*) from ProgramVideoMap m where m.program.id=?",
                new Serializable[] { pid });
    }
 
    public VideoDetailInfo getLatestVideoDetail(String videoid) {
 
        List<VideoYouKu> list = videoYouKuDao.list("from VideoYouKu vy where vy.video.id=?", new String[] { videoid });
        if (list != null && list.size() > 0) {
            final VideoYouKu vyk = list.get(0);
            if (vyk.getProgram() != null) {// 按节目查找视频
                List<ProgramVideo> videoList = programVideoDao.list(
                        "select map.programVideo from ProgramVideoMap map where map.program.id=? order by CAST(map.programVideo.stage as integer)  desc",
                        0, 1, new String[] { vyk.getProgram().getId() });
 
                List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                for (ProgramVideo pv : videoList)
                    detailList.add(YouKuUtil.convertProgramVideoToVideoDetail(pv, vyk.getProgram()));
                return detailList.get(0);
            } else if (vyk.getYvideo() != null) {// 单个视频----待定
                programDao.list("");
            }
        }
 
        return null;
    }
 
    @SuppressWarnings("unchecked")
    public void addProgramToVideoInfo(final Program p, final boolean isUpdate) {
 
        final VideoInfo info = YouKuUtil.convertProgramToVideoInfo(p);
        // 类型计算
        final List<Integer> typeList = new ArrayList<Integer>();
        String[] gs = p.getGenre().split(",");
        for (String g : gs) {
            int type = YouKuUtil.getProgramType(p, g);
            boolean exist = false;
            for (Integer i : typeList)
                if (i == type)
                    exist = true;
            if (!exist && type > 0)
                typeList.add(type);
        }
 
        if (typeList.size() == 0)
            typeList.add(299);
 
        try {
            List<VideoInfo> infoList = videoInfoDao.list("from VideoInfo vi where vi.name=?",
                    new Serializable[] { p.getName() });
            boolean isSame = false;
            VideoInfo video = null;
            for (VideoInfo vi : infoList) {
                final VideoInfo newVi = vi;
                boolean issame = (Boolean) videoInfoDao.excute(new HibernateCallback<Boolean>() {
                    public Boolean doInHibernate(Session session) throws HibernateException {
                        return YouKuUtil.isSameVideo(typeList, p, newVi, session);
                    }
                });
                if (issame) {
                    isSame = true;
                    video = vi;
                    break;
                }
            }
 
            if (infoList != null && infoList.size() > 0 && isSame) {// 无需加入
                updateVideoInfo(info, video, p, isUpdate);
            } else {// 需要新加入videoinfo
                Serializable id = addVideoInfo(info, p);
 
                if (id != null) {
                    info.setId(id.toString());
 
                    for (Integer ty : typeList) {
                        CategoryVideo cv = new CategoryVideo();
                        cv.setVideo(new VideoInfo(info.getId()));
                        cv.setVideoType(new VideoType(ty));
                        videoService.addCategoryVideo(cv);
                    }
 
                    VideoYouKu vyk = new VideoYouKu();
                    vyk.setProgram(new Program(p.getId()));
                    vyk.setVideo(new VideoInfo(info.getId()));
                    vyk.setYvideo(null);
                    saveVideoYouKu(vyk);
 
                    videoResourceService.excuteSQL(String.format(
                            "insert into wk_resource_video(videoid,resourceid) values(%s,%s)", info.getId(), 15 + ""));
 
                    // ResourceVideo rv = new ResourceVideo();
                    // rv.setResource(new VideoResource(15 + ""));
                    // rv.setVideo(new VideoInfo(info.getId()));
                    // session.persist(rv);
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    @SuppressWarnings("unchecked")
    public void addVideoToVideoInfo(final ProgramVideo p, String genre) {
 
        VideoInfo info = YouKuUtil.convertVideoToVideoInfo(p);
        try {
            List<VideoInfo> infoList = videoInfoDao.list("from VideoInfo vi where vi.name=?",
                    new Serializable[] { p.getTitle() });
            if (infoList != null && infoList.size() > 0) {// 无需加入
                updateVideoInfo(info, infoList.get(0), p, genre);
            } else {// 需要新加入videoinfo
                Serializable id = addVideoInfo(info, p);
                List<Integer> typeList = new ArrayList<Integer>();
                typeList = YouKuUtil.getProgramVideoType(p, genre);
                if (id != null) {
                    info.setId(id.toString());
 
                    for (Integer ty : typeList) {
                        CategoryVideo cv = new CategoryVideo();
                        cv.setVideo(new VideoInfo(info.getId()));
                        cv.setVideoType(new VideoType(ty));
                        videoService.addCategoryVideo(cv);
                    }
 
                    VideoYouKu vyk = new VideoYouKu();
                    vyk.setProgram(null);
                    vyk.setVideo(new VideoInfo(info.getId()));
                    vyk.setYvideo(p);
                    saveVideoYouKu(vyk);
 
                    videoResourceService.excuteSQL(String.format(
                            "insert into wk_resource_video(videoid,resourceid) values(%s,%s)", info.getId(), 15 + ""));
                }
            }
 
        } catch (
 
        Exception e)
 
        {
            e.printStackTrace();
        }
 
    }
 
    public int getShowType(String vid) {
        List<Program> list = programDao.list("select vs.program From VideoYouKu vs where vs.video.id=?",
                new Serializable[] { vid });
        if (list != null && list.size() > 0) {
            if (list.get(0) != null) {
                if (list.get(0).getCategory().contains("电视剧") || list.get(0).getCategory().contains("动漫"))
                    return 2;
            } else {
                return 1;
            }
        }
 
        return 1;
    }
 
    @SuppressWarnings("unchecked")
    public PlayUrl getPlayUrl(String detailSystemId, String id, String type, int resourceid, String videoId) {
        logger.info(String.format("%s#%s#%s#%s", detailSystemId, resourceid, type, id));
        PlayUrl playUrl = new PlayUrl();
        playUrl.setPlayType(2);
 
        List<VideoResource> list = videoResourceDao.list("from VideoResource vr where vr.id=" + resourceid);
        if (list != null && list.size() > 0)
            playUrl.setResource(list.get(0));
        if (StringUtil.isNullOrEmpty(videoId))
            addPlayStatistics(detailSystemId, id);
        else
            statisticsService.addStatistics(detailSystemId, videoId);
        if (!type.equalsIgnoreCase("youkuvideo")) {
            List<BigInteger> idList = (List<BigInteger>) videoResourceDao.sqlList(
                    "SELECT yk.`videoid` FROM wk_video_youku yk WHERE yk.`programid`= ( SELECT p.`programid` FROM wk_youku_video v LEFT JOIN wk_youku_program_video p ON p.`videoid`=v.`id`  WHERE v.`id`=?)",
                    new Serializable[] { id });
            if (idList != null && idList.size() > 0) {
                boolean isNeedWeb = banQuanService.isNeedWebPlay(detailSystemId, idList.get(0) + "");
                if (isNeedWeb)
                    playUrl.setPlayType(1);
            }
        }
 
        if (!type.equalsIgnoreCase("youkuvideo")) {
            ProgramVideo pv = YouKuApi.getVideoDetail(id);
            if ("original".equalsIgnoreCase(pv.getCopyright_type()))
                playUrl.setPlayType(1);
        } else if ("43".equalsIgnoreCase(detailSystemId)) {
            ProgramVideo pv = YouKuApi.getVideoDetail(id);
            if ("original".equalsIgnoreCase(pv.getCopyright_type()))
                playUrl.setPlayType(1);
        }
        // else {//小视频暂时不设置权限
        // List<BigInteger> idList = (List<BigInteger>)
        // videoResourceDao.sqlList(
        // "SELECT yk.`videoid` FROM wk_video_youku yk WHERE yk.`yvideoid`= ?",
        // new Serializable[] { id });
        // if (idList != null && idList.size() > 0) {
        // boolean isNeedWeb = banQuanService.isNeedWebPlay(detailSystemId,
        // idList.get(0) + "");
        // if (isNeedWeb)
        // playUrl.setPlayType(1);
        // }
        // }
 
        JSONObject object = new JSONObject();
        object.put("vid", id);
        playUrl.setParams(object.toString());
        playUrl.setUrl(String.format("http://v.youku.com/v_show/id_%s.html", id));
        playUrl.setPlayType(1);//全部跳转网页
        return playUrl;
    }
 
    public List<VideoDetailInfo> getVideoDetailList(String videoid) {
 
        List<VideoYouKu> list = videoYouKuDao.list("from VideoYouKu vy where vy.video.id=?", new String[] { videoid });
        if (list != null && list.size() > 0) {
            final VideoYouKu vyk = list.get(0);
            if (vyk.getProgram() != null) {// 按节目查找视频
                List<ProgramVideo> videoList = programVideoDao.list(
                        "select map.programVideo from ProgramVideoMap map where map.program.id=?",
                        new String[] { vyk.getProgram().getId() });
                Comparator<ProgramVideo> cp = new Comparator<ProgramVideo>() {
                    public int compare(ProgramVideo o1, ProgramVideo o2) {
                        if (StringUtil.isNullOrEmpty(o1.getStage()) || StringUtil.isNullOrEmpty(o2.getStage()))
                            System.out.println("空值");
                        long t1 = Long.parseLong(StringUtil.isNullOrEmpty(o1.getStage()) ? "0" : o1.getStage());
                        long t2 = Long.parseLong(StringUtil.isNullOrEmpty(o2.getStage()) ? "0" : o2.getStage());
                        if (vyk.getProgram().getCategory().contains("电影")) {
                            return (int) (t2 - t1);
                        } else if (vyk.getProgram().getCategory().contains("电视剧")) {
                            return (int) (t1 - t2);
                        } else if (vyk.getProgram().getCategory().contains("综艺")) {
                            return (int) (t2 - t1);
                        } else if (vyk.getProgram().getCategory().contains("动漫")) {
                            return (int) (t1 - t2);
                        } else
                            return (int) (t2 - t1);
                    }
                };
                Collections.sort(videoList, cp);
                // int freeCount =
                // YouKuApi.getProgramNotPayCount(vyk.getProgram().getId());
                // if (videoList.size() > freeCount) {
                // videoList = videoList.subList(0, freeCount);
                // }
 
                List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                for (ProgramVideo pv : videoList)
                    detailList.add(YouKuUtil.convertProgramVideoToVideoDetail(pv, vyk.getProgram()));
                return detailList;
            } else if (vyk.getYvideo() != null) {// 单个视频
                ProgramVideo pv = vyk.getYvideo();
                List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                VideoDetailInfo detail = new VideoDetailInfo();
                detail.setExtraId(pv.getId());
                detail.setName(pv.getTitle());
                detail.setTag(pv.getTitle());
                detail.setType("youkuvideo");
                detailList.add(detail);
                return detailList;
            }
        }
 
        return null;
    }
 
    public void addProgramToVideoInfo(int page) {
        List<Program> pList = programDao.list("from Program p where p.category='动漫'", (page - 1) * Constant.pageCount,
                Constant.pageCount, new String[] {});
        for (Program p : pList) {
            List<ProgramVideo> list = programVideoDao.list(
                    "select m.programVideo from ProgramVideoMap m where m.program.id=?", new String[] { p.getId() });
            if (list != null && list.size() > 0) {
                System.out.println(p.getName());
                p.setVideoList(list);
                addProgramToVideoInfo(p, false);
            }
        }
    }
 
    public ProgramVideo getLatestProgramVideo(String programid) {
        List<ProgramVideo> videoList = programVideoDao.list(
                "select map.programVideo from ProgramVideoMap map where map.program.id=? order by CAST(map.programVideo.stage as integer)  desc",
                0, 1, new String[] { programid });
        if (videoList != null && videoList.size() > 0)
            return videoList.get(0);
        return null;
    }
 
    public void hiddenOriginalVideo() {
 
        programDao.excute(new HibernateCallback() {
            @SuppressWarnings("unchecked")
            public Object doInHibernate(Session session) throws HibernateException {
                List list = session
                        .createSQLQuery(
                                "SELECT pv.`programid` FROM wk_youku_program_video pv LEFT JOIN wk_youku_video v ON v.`id`=pv.`videoid` WHERE v.`copyright_type`='original' GROUP BY pv.`programid`")
                        .list();
 
                for (int i = 0; i < list.size(); i++) {
                    List<VideoInfo> videoList = (List<VideoInfo>) session
                            .createQuery(
                                    "select yk.video from VideoYouKu yk where yk.program!=null and yk.program.id=?")
                            .setParameter(0, list.get(i) + "").list();
                    session.getTransaction().begin();
                    for (VideoInfo video : videoList) {
                        List<ResourceVideo> rvList = (List<ResourceVideo>) session
                                .createQuery("from ResourceVideo rv where rv.video.id=?").setParameter(0, video.getId())
                                .list();
                        if (rvList != null && rvList.size() > 1) {
                            for (ResourceVideo rv : rvList) {
                                if (rv.getResource().getId().equalsIgnoreCase("15")) {
                                    session.delete(rv);
                                }
                            }
                        } else {
                            video.setShow("0");
                            session.update(video);
                        }
                    }
                    session.flush();
                    session.getTransaction().commit();
                }
 
                return null;
            }
        });
 
    }
 
}