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;
|
}
|
|
}
|