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
package com.newvideo.service.imp.juhe;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import javax.annotation.Resource;
 
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.juhe.LeTVAlbumDao;
import com.newvideo.dao.juhe.LeTVAlbumVideoDao;
import com.newvideo.dao.juhe.LeTVVideoDao;
import com.newvideo.dao.juhe.VideoLeTVDao;
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.letv.LeAPI;
import com.newvideo.letv.LeTVUtil;
import com.newvideo.letv.entity.LeTVAlbum;
import com.newvideo.letv.entity.LeTVAlbumVideo;
import com.newvideo.letv.entity.LeTVVideo;
import com.newvideo.letv.entity.VideoLeTV;
import com.newvideo.service.imp.StatisticsService;
import com.newvideo.service.imp.VideoService;
import com.newvideo.service.imp.push.PushService;
import com.newvideo.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class LeService {
    @Resource
    private LeTVAlbumDao leTVAlbumDao;
    @Resource
    private LeTVVideoDao leTVVideoDao;
 
    @Resource
    private LeTVAlbumVideoDao leTVAlbumVideoDao;
 
    @Resource
    private VideoInfoDao videoInfoDao;
    @Resource
    private VideoService videoService;
    @Resource
    private VideoLeTVDao videoLeTVDao;
    @Resource
    private PushService pushService;
    @Resource
    private VideoResourceDao videoResourceDao;
 
    @Resource
    private StatisticsService statisticsService;
 
    public Serializable addLeTVAlbum(LeTVAlbum album) {
        Serializable id = null;
        try {
            id = leTVAlbumDao.save(album);
        } catch (Exception e) {
 
        }
        if (StringUtil.isNullOrEmpty(id + "")) {
            List<LeTVAlbum> list = leTVAlbumDao.list("from LeTVAlbum a where a.albumId=?",
                    new Serializable[] { album.getAlbumId() });
            if (list != null && list.size() > 0)
                id = list.get(0).getId();
        }
        for (LeTVVideo video : album.getVideoList()) {
            String tvid = "";
            try {
                tvid = leTVVideoDao.save(video) + "";
            } catch (Exception e) {
            }
 
            if (!StringUtil.isNullOrEmpty(tvid))
                try {
                    LeTVAlbumVideo av = new LeTVAlbumVideo();
                    av.setLeTVAlbum(new LeTVAlbum(id + ""));
                    av.setLeTVVideo(new LeTVVideo(tvid + ""));
                    leTVAlbumVideoDao.create(av);
                } catch (Exception e) {
 
                }
        }
 
        return id;
    }
 
    public Serializable addLeTVVideo(LeTVVideo video) {
        try {
            return leTVVideoDao.save(video);
        } catch (Exception e) {
        }
        return null;
    }
 
    public String addToVideo(final LeTVAlbum album, final boolean update) {
        final VideoInfo info = LeTVUtil.convertToVideoInfo(album);
        // 类型计算
        final List<Integer> typeList = new ArrayList<Integer>();
        int type = LeTVUtil.getVideoType(album.getCategoryName());
        typeList.add(type);
 
        try {
            List<VideoInfo> infoList = videoInfoDao.list("from VideoInfo vi where vi.name=?",
                    new Serializable[] { album.getAlbumName() });
            boolean isSame = false;
            VideoInfo video = null;
            for (VideoInfo vi : infoList) {
                final VideoInfo newVi = vi;
                boolean issame = LeTVUtil.isSameVideo(newVi, info);
                if (issame) {
                    isSame = true;
                    video = vi;
                    break;
                }
            }
 
            if (infoList != null && infoList.size() > 0 && isSame) {// 无需加入
                updateVideoInfo(info, video, album, update);
            } else {// 需要新加入videoinfo
                Serializable id = videoInfoDao.save(info);
 
                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);
                    }
 
                    VideoLeTV vyk = new VideoLeTV();
                    vyk.setVideo(new VideoInfo(info.getId()));
                    vyk.setTvVideo(null);
                    vyk.setAlbum(album);
                    videoLeTVDao.create(vyk);
 
                    videoLeTVDao.excute(new HibernateCallback<VideoLeTV>() {
                        public VideoLeTV doInHibernate(Session session) throws HibernateException {
                            session.getTransaction().begin();
                            session.createSQLQuery(
                                    String.format("insert into wk_resource_video(videoid,resourceid) values(%s,%s)",
                                            info.getId(), 18 + ""))
                                    .executeUpdate();
                            session.flush();
                            session.getTransaction().commit();
                            return null;
                        }
                    });
 
                    final ResourceVideo rv = new ResourceVideo();
                    rv.setResource(new VideoResource(18 + ""));
                    rv.setVideo(new VideoInfo(info.getId()));
                    videoLeTVDao.excute(new HibernateCallback<VideoLeTV>() {
                        public VideoLeTV doInHibernate(Session session) throws HibernateException {
                            session.getTransaction().begin();
                            session.persist(rv);
                            session.flush();
                            session.getTransaction().commit();
                            return null;
                        }
                    });
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public String addToVideo(final LeTVVideo levideo) {
        final VideoInfo info = LeTVUtil.convertToVideoInfo(levideo);
        // 类型计算
        final List<Integer> typeList = new ArrayList<Integer>();
        int type = LeTVUtil.getVideoType(levideo.getCategoryName());
        typeList.add(type);
 
        try {
            List<VideoInfo> infoList = videoInfoDao.list("from VideoInfo vi where vi.name=?",
                    new Serializable[] { levideo.getVideoName() });
            boolean isSame = false;
            VideoInfo video = null;
            for (VideoInfo vi : infoList) {
                final VideoInfo newVi = vi;
                boolean issame = LeTVUtil.isSameVideo(newVi, info);
                if (issame) {
                    isSame = true;
                    video = vi;
                    break;
                }
            }
 
            if (infoList != null && infoList.size() > 0 && isSame) {// 无需加入
 
            } else {// 需要新加入videoinfo
                Serializable id = videoInfoDao.save(info);
 
                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);
                    }
 
                    VideoLeTV vyk = new VideoLeTV();
                    vyk.setVideo(new VideoInfo(info.getId()));
                    vyk.setTvVideo(levideo);
                    videoLeTVDao.create(vyk);
 
                    videoLeTVDao.excute(new HibernateCallback<VideoLeTV>() {
                        public VideoLeTV doInHibernate(Session session) throws HibernateException {
                            session.getTransaction().begin();
                            session.createSQLQuery(
                                    String.format("insert into wk_resource_video(videoid,resourceid) values(%s,%s)",
                                            info.getId(), 18 + ""))
                                    .executeUpdate();
                            session.flush();
                            session.getTransaction().commit();
                            return null;
                        }
                    });
 
                    final ResourceVideo rv = new ResourceVideo();
                    rv.setResource(new VideoResource(18 + ""));
                    rv.setVideo(new VideoInfo(info.getId()));
                    videoLeTVDao.excute(new HibernateCallback<VideoLeTV>() {
                        public VideoLeTV doInHibernate(Session session) throws HibernateException {
                            session.getTransaction().begin();
                            session.persist(rv);
                            session.flush();
                            session.getTransaction().commit();
                            return null;
                        }
                    });
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public void updateVideoInfo(final VideoInfo info, final VideoInfo video, final LeTVAlbum album,
            final boolean isUpdate) {
        final VideoInfo vi = video;
        videoLeTVDao.excute(new HibernateCallback() {
            @SuppressWarnings("unchecked")
            public Object doInHibernate(Session session) throws HibernateException {
 
                vi.setTag(info.getTag());
                vi.setVideocount(album.getVideoList() != null ? album.getVideoList().size() : 1);
                vi.setLatestHpicture(info.getLatestHpicture());
                vi.setLatestVpicture(info.getLatestVpicture());
                try {
                    session.getTransaction().begin();
 
                    if (isUpdate)
                        vi.setUpdatetime(System.currentTimeMillis() + "");
                    session.update(vi);
 
                    VideoLeTV vyk = new VideoLeTV();
                    vyk.setAlbum(album);
                    vyk.setVideo(new VideoInfo(vi.getId()));
                    Object obj = session
                            .createQuery("select count(*)  from VideoLeTV vyk where vyk.video.id=? and vyk.album.id=?")
                            .setParameter(0, vi.getId()).setParameter(1, album.getId()).uniqueResult();
                    if (Integer.parseInt(obj + "") <= 0) {// 加入
                        // session.persist(vyk);
                        session.createSQLQuery(
                                String.format("insert into wk_video_letv (videoid, albumid) values (%s, '%s')",
                                        vyk.getVideo().getId(), album.getId()))
                                .executeUpdate();
                    }
 
                    obj = session.createQuery(String.format(
                            "select count(*) from ResourceVideo rv where rv.video.id=%s and rv.resource.id=%s",
                            vi.getId(), 18 + "")).uniqueResult();
                    if (Integer.parseInt(obj + "") <= 0) {// 加入
                        ResourceVideo rv = new ResourceVideo();
                        rv.setResource(new VideoResource(18 + ""));
                        rv.setVideo(new VideoInfo(vi.getId()));
                        session.createSQLQuery(String.format(
                                "insert into wk_resource_video(videoid,resourceid) values(%s,%s)", vi.getId(), 18 + ""))
                                .executeUpdate();
                    }
 
                    // 获取最新一集的信息
                    List<LeTVVideo> plist = session
                            .createQuery(
                                    "select map.leTVVideo from LeTVAlbumVideo map where map.leTVAlbum.id=? order by CAST(map.leTVVideo.episode as integer) desc")
                            .setParameter(0, album.getId()).setFirstResult(0).setMaxResults(1).list();
                    if (plist.size() > 0) {
                        List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                        detailList.add(LeTVUtil.convertLeTVVideoToVideoDetail(plist.get(0)));
                        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);
            }
        }
 
    }
 
    public List<VideoDetailInfo> getVideoDetailList(String videoid) {
 
        List<VideoLeTV> list = videoLeTVDao.list("from VideoLeTV vy where vy.video.id=?", new String[] { videoid });
        if (list != null && list.size() > 0) {
            final VideoLeTV vyk = list.get(0);
            if (vyk.getAlbum() != null) {// 按节目查找视频
                List<LeTVVideo> videoList = leTVVideoDao.list(
                        "select map.leTVVideo from LeTVAlbumVideo map where map.leTVAlbum.id=?",
                        new String[] { vyk.getAlbum().getId() });
                Comparator<LeTVVideo> cp = new Comparator<LeTVVideo>() {
                    public int compare(LeTVVideo o1, LeTVVideo o2) {
                        if (StringUtil.isNullOrEmpty(o1.getEpisode()) || StringUtil.isNullOrEmpty(o2.getEpisode()))
                            System.out.println("空值");
                        long t1 = Long.parseLong(StringUtil.isNullOrEmpty(o1.getEpisode()) ? "0" : o1.getEpisode());
                        long t2 = Long.parseLong(StringUtil.isNullOrEmpty(o2.getEpisode()) ? "0" : o2.getEpisode());
                        if (vyk.getAlbum().getCategoryName().contains("电影")) {
                            return (int) (t2 - t1);
                        } else if (vyk.getAlbum().getCategoryName().contains("电视剧")) {
                            return (int) (t1 - t2);
                        } else if (vyk.getAlbum().getCategoryName().contains("综艺")) {
                            return (int) (t2 - t1);
                        } else if (vyk.getAlbum().getCategoryName().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 (LeTVVideo pv : videoList)
                    detailList.add(LeTVUtil.convertLeTVVideoToVideoDetail(pv));
                return detailList;
            } else if (vyk.getTvVideo() != null) {// 单个视频
                LeTVVideo pv = vyk.getTvVideo();
                List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                VideoDetailInfo detail = new VideoDetailInfo();
                detail.setExtraId(pv.getVideoId());
                detail.setName(pv.getVideoName());
                detail.setTag(pv.getVideoName());
                detail.setType("letvvideo");
                detailList.add(detail);
                return detailList;
            }
        }
 
        return null;
    }
 
    public VideoDetailInfo getLatestVideoDetail(String videoid) {
 
        List<VideoLeTV> list = videoLeTVDao.list("from VideoLeTV vy where vy.video.id=?", new String[] { videoid });
        if (list != null && list.size() > 0) {
            final VideoLeTV vyk = list.get(0);
            if (vyk.getAlbum() != null) {// 按节目查找视频
                List<LeTVVideo> videoList = leTVVideoDao.list(
                        "select map.leTVVideo from LeTVAlbumVideo map where map.leTVAlbum.id=? order by CAST(map.leTVVideo.episode as integer)  desc",
                        0, 1, new String[] { vyk.getAlbum().getId() });
 
                List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
                for (LeTVVideo pv : videoList)
                    detailList.add(LeTVUtil.convertLeTVVideoToVideoDetail(pv));
                return detailList.get(0);
            } else if (vyk.getTvVideo() != null) {// 单个视频----待定
 
            }
        }
 
        return null;
    }
 
    public int getShowType(String vid) {
        List<LeTVAlbum> list = leTVAlbumDao.list("select vs.album From VideoLeTV vs where vs.video.id=?",
                new Serializable[] { vid });
        if (list != null && list.size() > 0) {
            if (list.get(0) != null) {
                if (list.get(0).getCategoryName().contains("电视剧") || list.get(0).getCategoryName().contains("动漫"))
                    return 2;
            } else {
                return 1;
            }
        }
 
        return 1;
    }
 
    public void addPlayStatistics(String detailSystemId, final String vid) {
        String videoid = videoLeTVDao.excute(new HibernateCallback<String>() {
            public String doInHibernate(Session session) throws HibernateException {
                List list = session
                        .createSQLQuery(
                                "SELECT vl.`videoid` FROM wk_video_letv vl LEFT JOIN wk_leshi_album_video av ON av.`albumid`=vl.`albumid` WHERE av.`videoid`=?")
                        .setParameter(0, vid).list();
                if (list != null && list.size() > 0)
                    return list.get(0) + "";
                return null;
            }
        }) + "";
 
        if (!StringUtil.isNullOrEmpty(videoid)) {
            statisticsService.addStatistics(detailSystemId, videoid + "");
        } else {
            //
            videoid = videoLeTVDao.excute(new HibernateCallback<String>() {
                public String doInHibernate(Session session) throws HibernateException {
                    List list = session
                            .createSQLQuery("SELECT vl.`videoid` FROM wk_video_letv vl  WHERE vl.`levideoid`=?")
                            .setParameter(0, vid).list();
                    if (list != null && list.size() > 0)
                        return list.get(0) + "";
                    return null;
                }
            }) + "";
 
            if (!StringUtil.isNullOrEmpty(videoid))
                statisticsService.addStatistics(detailSystemId, videoid + "");
        }
    }
 
    public PlayUrl getPlayUrl(String detailSystemId, String id, String type, int resourceid, String videoId) {
        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);
 
        List<LeTVVideo> vlist = leTVVideoDao.list("from LeTVVideo tv where tv.videoId=?", new String[] { id });
        JSONObject object = new JSONObject();
        if (vlist != null && vlist.size() > 0) {
            object.put("vid", vlist.get(0).getMmsId());
            object.put("url", String.format("http://yuntv.letv.com/player/vrp/vrp.html?uu=%s&vu=%s&p=102&pu=2128",
                    LeTVUtil.UID + "", vlist.get(0).getMmsId()));
        }
        playUrl.setParams(object.toString());
        playUrl.setUrl(String.format("http://yuntv.letv.com/player/vrp/vrp.html?uu=%s&vu=%s&p=102&pu=2128",
                LeTVUtil.UID + "", vlist.get(0).getMmsId()));
        // if (!detailSystemId.equalsIgnoreCase("43"))
        playUrl.setPlayType(1);
        return playUrl;
    }
 
    public boolean isUpdate(LeTVAlbum album) {
        long count = leTVVideoDao.getCount("select count(*) from LeTVAlbumVideo a where a.leTVAlbum.id=?",
                new Serializable[] { album.getAlbumId() });
        return album.getVideoList().size() > count;
    }
 
}