package com.yeshi.buwan.service.imp;
|
|
import com.yeshi.buwan.dao.VideoDetailInfoDao;
|
import com.yeshi.buwan.dao.VideoInfoDao;
|
import com.yeshi.buwan.dao.VideoTypeDao;
|
import com.yeshi.buwan.domain.*;
|
import com.yeshi.buwan.service.inter.juhe.AlbumVideoMapService;
|
import com.yeshi.buwan.util.LogUtil;
|
import com.yeshi.buwan.util.StringUtil;
|
import com.yeshi.buwan.util.mq.rabbit.RabbitmqManager;
|
import org.hibernate.HibernateException;
|
import org.hibernate.Session;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import javax.persistence.ManyToOne;
|
import javax.persistence.OneToMany;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Service
|
public class VideoManager {
|
@Resource
|
private VideoInfoDao videoInfoDao;
|
@Resource
|
private VideoTypeDao videoTypeDao;
|
@Resource
|
private VideoDetailInfoDao videoDetailInfoDao;
|
|
@Resource
|
private ClearService clearService;
|
|
/**
|
* 主键查询某一个视频的信息
|
*
|
* @param id
|
* @return
|
*/
|
public VideoInfo getVideoInfo(String id) {
|
VideoInfo info = videoInfoDao.find(VideoInfo.class, id);
|
info.setVideoDetailList(getVideoDetail(info.getId()));
|
return info;
|
}
|
|
/**
|
* 添加一个视频信息
|
*
|
* @param info
|
* @return
|
*/
|
public VideoInfo addVideoInfo(VideoInfo info) {
|
info.setUpdatetime(System.currentTimeMillis() + "");
|
|
videoInfoDao.save(info);
|
List<VideoInfo> list = videoInfoDao.list("from VideoInfo v where v.createtime=? and v.name=?",
|
new String[]{info.getCreatetime() + "", info.getName()});
|
return list.get(0);
|
}
|
|
String[] sts = {"彼得兔", "铁扇公主", "过猴山", "骄傲的将军", "夸口的青蛙", "小猫钓鱼", "好朋友", "神笔马良", "木头公主", "猪八戒吃西瓜", "小鲤鱼", "我知道",
|
"老小阿凡提", "阿狸通话日记"};
|
|
boolean isContains(String st) {
|
for (String s : sts) {
|
if (st.contains(s)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
// 获取大分类
|
public String getRootType(long id) {
|
VideoType type = videoTypeDao.find(VideoType.class, id);
|
while (type.getParent() != null)
|
type = type.getParent();
|
return type.getId() + "";
|
}
|
|
// 添加视频
|
@SuppressWarnings("unchecked")
|
public void addVideoInfo(List<VideoInfo> list) {
|
for (VideoInfo in : list) {
|
final VideoInfo info = in;
|
LogUtil.i("视频名称:" + info.getName());
|
// if (!info.getName().contains("三个奶爸")) continue;
|
final String parentId = getRootType(info.getVideoType().getId());
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
List<VideoInfo> vList = session
|
.createQuery("from VideoInfo v where v.name=? and v.show=1 and (v.videoType.id="
|
+ parentId + " or v.videoType.parent.id=" + parentId + ")")
|
.setParameter(0, info.getName()).list();
|
session.getTransaction().begin();
|
if (vList.size() <= 0) {// 不存在视频--可以添加进去
|
info.setUpdatetime(System.currentTimeMillis() + "");
|
session.persist(info);
|
List<VideoInfo> viList = session
|
.createQuery("from VideoInfo v where v.name=? and v.createtime=?")
|
.setParameter(0, info.getName()).setParameter(1, info.getCreatetime()).list();// 查询出刚刚添加的视频
|
if (viList != null && viList.size() > 0) {
|
VideoInfo newVideo = new VideoInfo(viList.get(0).getId());
|
LogUtil.i("查出的VideoId为:" + newVideo.getId());
|
for (int i = 0; i < info.getVideoDetailList().size(); i++) {
|
VideoDetailInfo detail = info.getVideoDetailList().get(i);
|
detail.setVideo(newVideo);
|
session.persist(detail);
|
session.createSQLQuery(
|
"update wk_video_video_detail d set d.videoid=? where d.name=? and d.tag=? and d.createtime=?")
|
.setParameter(0, newVideo.getId()).setParameter(1, detail.getName())
|
.setParameter(2, detail.getTag()).setParameter(3, detail.getCreatetime())
|
.executeUpdate();
|
}
|
} else {
|
LogUtil.i("videoId没有查出来");
|
}
|
|
} else {// 存在的视频,添加进UrlList
|
// 获取videoDetail;
|
vList.get(0).setTag(info.getTag());
|
session.update(vList.get(0));
|
vList.get(0).setNowNumber(info.getNowNumber());
|
vList.get(0).setTotalNumber(info.getTotalNumber());
|
|
List<VideoDetailInfo> detailList = getVideoDetail(session, vList.get(0).getId());// 获取原来的detailList
|
for (int n = 0; n < info.getVideoDetailList().size(); n++) {
|
if (n + 1 > detailList.size()) {// 原来不存在这一集
|
VideoInfo newVideo = vList.get(0);
|
// 保存详情
|
VideoDetailInfo detail = info.getVideoDetailList().get(n);
|
detail.setVideo(newVideo);
|
session.persist(detail);
|
session.createSQLQuery(
|
"update wk_video_video_detail d set d.videoid=? where d.name=? and d.tag=? and d.createtime=?")
|
.setParameter(0, newVideo.getId()).setParameter(1, detail.getName())
|
.setParameter(2, detail.getTag()).setParameter(3, detail.getCreatetime())
|
.executeUpdate();
|
|
} else {// 原来存在这个detail
|
VideoDetailInfo detail = detailList.get(n);// 获取原来那个url
|
List<VideoUrl> urlList = detail.getUrls();
|
VideoUrl vurl = null;
|
|
for (VideoUrl videoUrl : urlList) {
|
if (videoUrl.getResource().getId().equalsIgnoreCase(info.getVideoDetailList()
|
.get(n).getUrls().get(0).getResource().getId())) {// 存在
|
vurl = videoUrl;
|
}
|
}
|
|
if (vurl == null) {// 不存在--往里面添加
|
VideoUrl vu = null;
|
try {
|
vu = info.getVideoDetailList().get(n).getUrls().get(0);
|
} catch (Exception e) {
|
vu = info.getVideoDetailList().get(n).getUrls().get(0);
|
}
|
vu.setVideoDetail(detail);
|
// 更新url
|
session.createSQLQuery(
|
"insert into wk_video_video_url (videodetailid,resourceid,vdetailurl,createtime,adminid,beizhu,baseurl) values(?,?,?,?,?,?,?)")
|
.setParameter(0, detail.getId())
|
.setParameter(1, vu.getResource().getId()).setParameter(2, vu.getUrl())
|
.setParameter(3, vu.getCreatetime())
|
.setParameter(4, vu.getAdmin().getId()).setParameter(5, vu.getBeizhu())
|
.setParameter(6, vu.getBaseUrl()).executeUpdate();
|
} else {// 原来存在该URL
|
VideoUrl newUrl = info.getVideoDetailList().get(n).getUrls().get(0);
|
if (!newUrl.getUrl().equals(vurl.getUrl())) {
|
vurl.setUrl(newUrl.getUrl());
|
session.update(vurl);
|
detail.setTag(info.getVideoDetailList().get(n).getTag());
|
session.update(detail);
|
}
|
}
|
}
|
}
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
LogUtil.i("视频名称:" + info.getName());
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
|
return null;
|
}
|
});
|
|
}
|
}
|
|
public void updateVideoInfo(VideoInfo info) {
|
videoInfoDao.update(info);
|
}
|
|
public void updateVideoInfo(List<VideoInfo> list) {
|
for (VideoInfo vi : list)
|
videoInfoDao.update(vi);
|
}
|
|
@ManyToOne
|
private AdminInfo getAdmin() {
|
AdminInfo info = new AdminInfo();
|
info.setId("1");
|
return info;
|
}
|
|
/**
|
* 添加视频详情
|
*
|
* @param list
|
*/
|
public void addVideoDetail(final VideoInfo info, final List<VideoDetailInfo> list) {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
for (int i = 0; i < list.size(); i++) {
|
VideoInfo video = new VideoInfo(info.getId());
|
list.get(i).setVideo(video);
|
session.persist(list.get(i));
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
/**
|
* 添加视频详情
|
*
|
* @param list
|
*/
|
public void addVideoDetail(final List<VideoDetailInfo> list) {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
for (int i = 0; i < list.size(); i++) {
|
session.persist(list.get(i));
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
public VideoDetailInfo addVideoDetail(final VideoDetailInfo detail) {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
session.persist(detail);
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
});
|
|
List<VideoDetailInfo> list = getVideoDetail(detail.getVideo().getId());
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
else
|
return null;
|
}
|
|
public List<VideoDetailInfo> getVideoDetail(String videoId) {
|
LogUtil.i("请求开始:" + System.currentTimeMillis());
|
List<VideoDetailInfo> list = videoDetailInfoDao.list("from VideoDetailInfo d where d.video.id=?",
|
new String[]{videoId});
|
LogUtil.i("请求结束:" + System.currentTimeMillis());
|
return list;
|
}
|
|
public VideoDetailInfo getVideoDetailById(long videoDetailId) {
|
return videoDetailInfoDao.find(VideoDetailInfo.class, videoDetailId);
|
}
|
|
public List<VideoDetailInfo> getVideoDetailByVideoName(String name) {
|
LogUtil.i("请求开始:" + System.currentTimeMillis());
|
|
List<VideoDetailInfo> list = videoDetailInfoDao.list("from VideoDetailInfo d where d.name=?",
|
new String[]{name});
|
LogUtil.i("请求结束:" + System.currentTimeMillis());
|
return list;
|
}
|
|
public List<VideoDetailInfo> getVideoDetail(Session session, String videoId) {
|
@SuppressWarnings("unchecked")
|
List<VideoDetailInfo> list = session.createQuery("from VideoDetailInfo d where d.video.id=?")
|
.setParameter(0, videoId).list();
|
LogUtil.i("请求结束:" + System.currentTimeMillis());
|
return list;
|
}
|
|
/**
|
* 更新视频的详情
|
*
|
* @param list
|
*/
|
public void updateVideoDetail(final List<VideoDetailInfo> list) {
|
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
|
// BaseDao<VideoDetailInfo> dao = new
|
// BaseDao<VideoDetailInfo>();
|
// /BaseDao<VideoUrl> urlDao = new BaseDao<VideoUrl>();
|
// BaseDao<VideoDetailInfo> dao = new
|
// BaseDao<VideoDetailInfo>();
|
session.getTransaction().begin();
|
for (int i = 0; i < list.size(); i++) {
|
for (int j = 0; j < list.get(i).getUrls().size(); j++) {
|
session.createSQLQuery("update wk_video_video_url set vdetailurl=? where id=?")
|
.setParameter(0, list.get(i).getUrls().get(j).getUrl())
|
.setParameter(1, list.get(i).getUrls().get(j).getId() + "").executeUpdate();
|
}
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
public void updateVideoDetail(final VideoDetailInfo detail) {
|
if (detail.getUrls() == null || detail.getUrls().size() < 1)
|
return;
|
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
session.createQuery("delete from VideoUrl vu where vu.videoDetail.id=?")
|
.setParameter(0, detail.getId()).executeUpdate();
|
for (VideoUrl vu : detail.getUrls())
|
session.save(vu);
|
session.update(detail);
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
public void deleteVideoDetail(VideoDetailInfo detail) {
|
videoDetailInfoDao.delete(detail);
|
}
|
|
/**
|
* 更新视频的详情
|
*
|
* @param list
|
*/
|
public void updateVideoDetailByAdmin(final List<VideoDetailInfo> list) {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
for (int i = 0; i < list.size(); i++) {
|
try {
|
session.persist(list.get(i));
|
} catch (Exception e) {
|
session.update(list.get(i));
|
}
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
LogUtil.i("事物回滚");
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
|
@Resource
|
private AlbumVideoMapService albumVideoMapService;
|
|
@Resource
|
private RabbitmqManager rabbitmqManager;
|
|
/**
|
* 删除视频
|
*
|
* @param videoId
|
*/
|
public void deleteVideo(final String videoId) {
|
//清除sql数据表相关依赖
|
clearService.clearDependVideo(videoId);
|
//清除MongoDB相关依赖
|
albumVideoMapService.deleteByVideoId(videoId);
|
//更新搜索引擎数据
|
rabbitmqManager.addSolrMsg(videoId);
|
videoInfoDao.delete(new VideoInfo(videoId));
|
}
|
|
// 更新视频的观看数
|
public void updateVideoWatchCount(final String videoId) {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
session.createSQLQuery("update wk_video_video set watchcount=(watchcount+1) where id=" + videoId)
|
.executeUpdate();
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
@OneToMany
|
private List<VideoDetailInfo> getVideoDetailList() {
|
List<VideoDetailInfo> list = new ArrayList<VideoDetailInfo>();
|
VideoDetailInfo info = new VideoDetailInfo();
|
info.setAdmin(getAdmin());
|
info.setBeizhu("测试信息");
|
info.setCreatetime(System.currentTimeMillis() + "");
|
info.setName("功夫");
|
info.setTag("");
|
List<VideoUrl> urlList = info.getUrls();
|
VideoUrl url = new VideoUrl();
|
url.setAdmin(getAdmin());
|
url.setBeizhu("测试");
|
url.setCreatetime(System.currentTimeMillis() + "");
|
VideoResource resource = new VideoResource();
|
resource.setId("1");
|
url.setResource(resource);
|
url.setUrl("http://123333.mp4");
|
urlList.add(url);
|
info.setUrls(urlList);
|
list.add(info);
|
return list;
|
}
|
|
@SuppressWarnings("finally")
|
public boolean changeVideoShow(final VideoInfo info) {
|
return (Boolean) videoInfoDao.excute(new HibernateCallback<Boolean>() {
|
public Boolean doInHibernate(Session session) throws HibernateException {
|
int result = 0;
|
try {
|
session.getTransaction().begin();
|
result = session.createSQLQuery("update wk_video_video set `show`=? where `id`=?")
|
.setParameter(0, info.getShow()).setParameter(1, info.getId()).executeUpdate();
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (result > 0)
|
return true;
|
else
|
return false;
|
}
|
|
}
|
});
|
|
}
|
|
/**
|
* 生成视频的名称--唯一
|
*
|
* @return
|
*/
|
public String getVideoPathName(String type, String oldName) {
|
String src = (long) (Math.random() * 100000000)
|
+ StringUtil.Md5(System.currentTimeMillis() + (long) (Math.random() * 100000000) + "");
|
return StringUtil.getBase64(src) + "_" + StringUtil.getBase64(oldName) + "." + type;
|
}
|
|
@ManyToOne
|
private VideoType getVideoType() {
|
VideoType type = new VideoType();
|
type.setId(1);
|
return type;
|
}
|
|
// 隐藏视频
|
public void setVideoHidden(final String urlKey) {
|
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
List<String> listSt = new ArrayList<String>();
|
try {
|
List list = session
|
.createSQLQuery(
|
"select d.videoid from wk_video_video_url u left join wk_video_video_detail d on d.id=u.videodetailid where vdetailurl like ?")
|
.setParameter(0, "%" + urlKey + "%").list();
|
for (int i = 0; i < list.size(); i++) {
|
Object obj = (Object) list.get(i);
|
listSt.add(obj.toString());
|
LogUtil.i(obj.toString());
|
}
|
LogUtil.i(list.size());
|
for (int j = 0; j < listSt.size(); j++) {
|
VideoInfo vi = videoInfoDao.find(VideoInfo.class, listSt.get(j));
|
vi.setShow("0");
|
videoInfoDao.update(vi);
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
});
|
|
}
|
}
|