package com.yeshi.buwan.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.yeshi.buwan.dao.CategoryVideoDao;
|
import com.yeshi.buwan.dao.juhe.sohu.SoHuAlbumDao;
|
import com.yeshi.buwan.dao.juhe.sohu.SoHuVideoDao;
|
import com.yeshi.buwan.domain.CategoryVideo;
|
import com.yeshi.buwan.domain.ResourceVideo;
|
import com.yeshi.buwan.domain.VideoDetailInfo;
|
import com.yeshi.buwan.domain.VideoInfo;
|
import com.yeshi.buwan.domain.VideoResource;
|
import com.yeshi.buwan.domain.VideoType;
|
import com.yeshi.buwan.domain.push.VideoPushHistory;
|
import com.yeshi.buwan.service.imp.StatisticsService;
|
import com.yeshi.buwan.service.imp.push.PushService;
|
import com.yeshi.buwan.sohu.SoHuUtil;
|
import com.yeshi.buwan.sohu.entity.SoHuAlbum;
|
import com.yeshi.buwan.sohu.entity.SoHuVideo;
|
import com.yeshi.buwan.sohu.entity.VideoSoHu;
|
import com.yeshi.buwan.util.StringUtil;
|
|
@Service
|
public class SoHuService {
|
@Resource
|
private SoHuAlbumDao soHuAlbumDao;
|
@Resource
|
private SoHuVideoDao soHuVideoDao;
|
@Resource
|
private StatisticsService statisticsService;
|
@Resource
|
private PushService pushService;
|
@Resource
|
private CategoryVideoDao categoryVideoDao;
|
|
@SuppressWarnings("unchecked")
|
public String addAlbum(final SoHuAlbum album) {
|
// 对VideoList去重
|
System.out.println(album.getAid() + "--" + album.getAlbum_name());
|
return soHuAlbumDao.excute(new HibernateCallback<String>() {
|
public String doInHibernate(Session session) throws HibernateException {
|
String id = "";
|
try {
|
List<SoHuAlbum> list = session.createQuery("from SoHuAlbum sa where sa.aid=?")
|
.setParameter(0, album.getAid()).list();
|
|
boolean isInsert;
|
if (list == null || list.size() == 0) {
|
id = saveSoHuAlbum(album) + "";
|
isInsert = true;
|
} else {
|
isInsert = album.getLatest_video_count() != list.get(0).getLatest_video_count();
|
if (isInsert) {
|
list.get(0).setModerator(album.getModerator());
|
list.get(0).setLatest_video_count(album.getLatest_video_count());
|
list.get(0).setTotal_video_count(album.getTotal_video_count());
|
updateSoHuAlbum(list.get(0));
|
}
|
id = list.get(0).getId();
|
}
|
|
if (album.getVideoList() != null && isInsert) {
|
Collections.sort(album.getVideoList(), new Comparator<SoHuVideo>() {
|
public int compare(SoHuVideo o1, SoHuVideo o2) {
|
return o2.getVideo_order() - o1.getVideo_order();
|
}
|
});
|
|
List<SoHuVideo> soHuList = session
|
.createQuery("from SoHuVideo sh where sh.aid=? order by sh.video_order desc")
|
.setParameter(0, album.getAid()).setFirstResult(0).setMaxResults(1).list();
|
|
SoHuVideo latestSoHuVideo = soHuList != null && soHuList.size() > 0 ? soHuList.get(0) : null;
|
int startP = 0;
|
if (latestSoHuVideo != null) {
|
for (int i = 0; i < album.getVideoList().size(); i++) {
|
if (latestSoHuVideo.getVid().equalsIgnoreCase(album.getVideoList().get(i).getVid())) {
|
startP = i + 1;
|
break;
|
}
|
}
|
}
|
|
if (startP <= album.getVideoList().size())// 有更新内容
|
{
|
startP = startP == 0 ? album.getVideoList().size() : startP;
|
List<SoHuVideo> videoList = album.getVideoList().subList(0, startP);
|
for (SoHuVideo info : videoList)
|
addSoHuVideo(info);
|
|
}
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return id;
|
}
|
}) + "";
|
|
}
|
|
public void addSoHuVideo(SoHuVideo info) {
|
soHuVideoDao.create(info);
|
}
|
|
public Serializable saveSoHuAlbum(SoHuAlbum info) {
|
return soHuAlbumDao.save(info);
|
}
|
|
public void updateSoHuAlbum(SoHuAlbum info) {
|
soHuAlbumDao.update(info);
|
}
|
|
public List<SoHuVideo> getLatestVideo(String aid) {
|
return soHuVideoDao.list("from SoHuVideo sv where sv.aid=? order by UNIX_TIMESTAMP(sv.create_date) DESC", 0, 1,
|
new String[] { aid });
|
}
|
|
private String saveVideoInfo(final VideoInfo vi, final SoHuAlbum sa) {
|
return soHuVideoDao.excute(new HibernateCallback<String>() {
|
public String doInHibernate(Session session) throws HibernateException {
|
session.getTransaction().begin();
|
String vid = session.save(vi) + "";
|
VideoSoHu vs = new VideoSoHu();
|
vs.setAlbum(sa);
|
vs.setVideo(new VideoInfo(vid));
|
session.persist(vs);
|
ResourceVideo rv = new ResourceVideo();
|
rv.setResource(new VideoResource("14"));
|
rv.setVideo(new VideoInfo(vid));
|
session.persist(rv);
|
session.flush();
|
session.getTransaction().commit();
|
return vid;
|
}
|
}) + "";
|
|
}
|
|
// 将专辑添加到视频
|
@SuppressWarnings("unchecked")
|
public String addToVideoInfo(final SoHuAlbum sa, final boolean isUpdate) {
|
String videoid = soHuAlbumDao.excute(new HibernateCallback<String>() {
|
public String doInHibernate(Session session) throws HibernateException {
|
sa.setVideoList(getLatestVideo(sa.getAid()));
|
VideoInfo vi = SoHuUtil.convertAlBumToVideoInfo(sa);
|
String vid = "";
|
try {
|
session.getTransaction().begin();
|
List<VideoInfo> list = session.createQuery("from VideoInfo vi where vi.name=?")
|
.setParameter(0, vi.getName()).list();
|
|
if (list == null || list.size() == 0)// 没有加入VideoInfo
|
{
|
vid = session.save(vi) + "";
|
VideoSoHu vs = new VideoSoHu();
|
vs.setAlbum(sa);
|
vs.setVideo(new VideoInfo(vid));
|
session.persist(vs);
|
|
ResourceVideo rv = new ResourceVideo();
|
rv.setResource(new VideoResource("14"));
|
rv.setVideo(new VideoInfo(vid));
|
session.persist(rv);
|
|
} else// 加入VideoInfo
|
{
|
vid = list.get(0).getId();
|
// 更新videoInfo
|
list.get(0).setTag(vi.getTag());
|
if (isUpdate)
|
list.get(0).setUpdatetime(System.currentTimeMillis() + "");
|
list.get(0).setVideocount(vi.getVideocount());
|
list.get(0).setLatestHpicture(vi.getLatestHpicture());
|
list.get(0).setLatestVpicture(vi.getLatestVpicture());
|
session.update(list.get(0));
|
// 是否已经加入到了SohuVideo
|
|
Object count = session
|
.createQuery("select count(*) from VideoSoHu vs where vs.video.id=? and vs.album.aid=?")
|
.setParameter(0, list.get(0).getId()).setParameter(1, sa.getAid()).uniqueResult();
|
if (Integer.parseInt(count + "") <= 0)// 加入到VideoSoHu
|
{
|
VideoSoHu vs = new VideoSoHu();
|
vs.setAlbum(sa);
|
vs.setVideo(list.get(0));
|
session.persist(vs);
|
|
ResourceVideo rv = new ResourceVideo();
|
rv.setResource(new VideoResource("14"));
|
rv.setVideo(list.get(0));
|
session.persist(rv);
|
}
|
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
if (session.getTransaction().isActive())
|
session.getTransaction().rollback();
|
}
|
return vid;
|
}
|
}) + "";
|
|
if (isUpdate) {// 确实是更新
|
try {
|
VideoPushHistory vph = new VideoPushHistory();
|
vph.setCreatetime(System.currentTimeMillis() + "");
|
vph.setDetailId(convertSoHuVideoToDETAIL(sa.getVideoList().get(0)).getId() + "");
|
vph.setResourceId("13");
|
vph.setTag(sa.getTip());
|
vph.setType("");
|
vph.setVideoInfo(new VideoInfo(videoid));
|
pushService.addVideoPushHistory(vph);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
return videoid;
|
}
|
|
public List<VideoType> getVideoTypeList(SoHuAlbum sa) {
|
List<VideoType> list = new ArrayList<>();
|
String cateCode = sa.getCate_code();
|
String[] ca = cateCode.split(";");
|
for (int i = 1; i < ca.length; i++) {
|
List<Integer> typeList = SoHuUtil.getSecondCategory(Integer.parseInt(ca[i]));
|
for (Integer type : typeList) {
|
if (type > 0) {
|
boolean can = true;
|
for (VideoType vtt : list)
|
if (vtt.getId() == type)
|
can = false;
|
if (can)
|
list.add(new VideoType(type));
|
}
|
}
|
}
|
|
if (list.size() == 0) {// 找不到主分类
|
if (ca[0].equalsIgnoreCase("100")) {
|
list.add(new VideoType(151));
|
} else if (ca[0].equalsIgnoreCase("101")) {
|
list.add(new VideoType(150));
|
} else if (ca[0].equalsIgnoreCase("115")) {
|
list.add(new VideoType(153));
|
} else if (ca[0].equalsIgnoreCase("106")) {
|
list.add(new VideoType(152));
|
} else if (ca[0].equalsIgnoreCase("133")) {
|
list.add(new VideoType(216));
|
}
|
}
|
return list;
|
}
|
|
public void addToCategoryVideo(String vid, long type) {
|
|
long count = soHuAlbumDao.getCount("from CategoryVideo cv where cv.video.id=? and cv.videoType.id=" + type,
|
new String[] { vid });
|
if (count <= 0) {// 还没添加
|
CategoryVideo cv = new CategoryVideo();
|
cv.setVideo(new VideoInfo(vid));
|
cv.setVideoType(new VideoType(type));
|
categoryVideoDao.create(cv);
|
}
|
}
|
|
private VideoDetailInfo convertSoHuVideoToDETAIL(SoHuVideo sv) {
|
VideoDetailInfo vd = new VideoDetailInfo(Integer.parseInt(sv.getId()));
|
if (!StringUtil.isNullOrEmpty(sv.getPeriod()))
|
vd.setTag(sv.getPeriod() + "");
|
else
|
vd.setTag(sv.getVideo_order() + "");
|
vd.setType("1");
|
vd.setExtraId(vd.getId() + "");
|
return vd;
|
}
|
|
@SuppressWarnings("unchecked")
|
public List<VideoDetailInfo> getVideoDetailList(final String videoid,int page,int pageSize) {
|
return (List<VideoDetailInfo>) soHuAlbumDao.excute(new HibernateCallback<List<VideoDetailInfo>>() {
|
public List<VideoDetailInfo> doInHibernate(Session session) throws HibernateException {
|
List<VideoDetailInfo> detailList = new ArrayList<>();
|
try {
|
List<SoHuAlbum> list = session.createQuery("select vs.album from VideoSoHu vs where vs.video.id=?")
|
.setParameter(0, videoid).list();
|
if (list != null && list.size() > 0) {
|
SoHuAlbum sa = list.get(0);
|
String order = " asc";
|
if (sa.getCate_code().contains("106;"))// 综艺
|
order = " desc";
|
List<SoHuVideo> svList = session
|
.createQuery("from SoHuVideo sv where sv.aid=? order by sv.video_order" + order)
|
.setParameter(0, sa.getAid()).list();
|
for (SoHuVideo sv : svList) {
|
detailList.add(convertSoHuVideoToDETAIL(sv));
|
}
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return detailList;
|
|
}
|
});
|
|
}
|
|
@SuppressWarnings("unchecked")
|
public VideoDetailInfo getLatestVideoDetail(final String videoid) {
|
return (VideoDetailInfo) soHuAlbumDao.excute(new HibernateCallback<VideoDetailInfo>() {
|
public VideoDetailInfo doInHibernate(Session session) throws HibernateException {
|
List<VideoDetailInfo> detailList = new ArrayList<>();
|
try {
|
List<SoHuAlbum> list = session.createQuery("select vs.album from VideoSoHu vs where vs.video.id=?")
|
.setParameter(0, videoid).list();
|
if (list != null && list.size() > 0) {
|
SoHuAlbum sa = list.get(0);
|
String order = " asc";
|
|
order = " desc";
|
List<SoHuVideo> svList = session
|
.createQuery("from SoHuVideo sv where sv.aid=? order by sv.video_order" + order)
|
.setParameter(0, sa.getAid()).setFirstResult(0).setMaxResults(1).list();
|
for (SoHuVideo sv : svList) {
|
detailList.add(convertSoHuVideoToDETAIL(sv));
|
}
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return detailList.get(0);
|
|
}
|
});
|
|
}
|
|
// 添加播放统计
|
public void addPlayStatistics(String detailSystemId, SoHuVideo sv) {
|
List list = soHuAlbumDao.sqlList(
|
"SELECT vs.`videoid` FROM wk_sohu_album a LEFT JOIN wk_video_sohu vs ON vs.`albumid`=a.`id` WHERE a.`aid`=?",
|
new Serializable[] { sv.getAid() });
|
if (list != null && list.size() > 0) {
|
statisticsService.addStatistics(detailSystemId, list.get(0) + "");
|
}
|
}
|
|
// 获取最近的一条数据
|
|
public SoHuVideo getLatestSoHuVideo(String aid) {
|
List<SoHuVideo> svList = soHuVideoDao.list("from SoHuVideo sv where sv.aid=? order by sv.video_order desc", 0,
|
1, new Serializable[] { aid });
|
if (svList != null && svList.size() > 0)
|
return svList.get(0);
|
return null;
|
}
|
|
}
|