package com.newvideo.service.imp;
|
|
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.CategoryVideoDao;
|
import com.newvideo.dao.VideoInfoDao;
|
import com.newvideo.domain.CategoryVideo;
|
import com.newvideo.domain.VideoInfo;
|
import com.newvideo.util.Constant;
|
|
@Service
|
public class VideoService {
|
@Resource
|
private VideoInfoDao videoInfoDao;
|
@Resource
|
private ClassService classServcie;
|
@Resource
|
private CategoryVideoDao categoryVideoDao;
|
|
public CategoryVideoDao getCategoryVideoDao() {
|
return categoryVideoDao;
|
}
|
|
public void setCategoryVideoDao(CategoryVideoDao categoryVideoDao) {
|
this.categoryVideoDao = categoryVideoDao;
|
}
|
|
public ClassService getClassServcie() {
|
return classServcie;
|
}
|
|
public void setClassServcie(ClassService classServcie) {
|
this.classServcie = classServcie;
|
}
|
|
public VideoInfoDao getVideoInfoDao() {
|
return videoInfoDao;
|
}
|
|
public void setVideoInfoDao(VideoInfoDao videoInfoDao) {
|
this.videoInfoDao = videoInfoDao;
|
}
|
|
public VideoService() {
|
|
}
|
|
public VideoInfo getVideoInfo(String id) {
|
return videoInfoDao.find(VideoInfo.class, id);
|
}
|
|
public List<VideoInfo> getVideoListByClass(int type, int page) {
|
String types[] = classServcie.getChildrenType(type);
|
String wheres = "";
|
String as[];
|
int j = (as = types).length;
|
for (int i = 0; i < j; i++) {
|
String st = as[i];
|
wheres = (new StringBuilder(String.valueOf(wheres))).append("or v.videoType.id=").append(st).append(" ")
|
.toString();
|
}
|
|
if (wheres.startsWith("or"))
|
wheres = wheres.substring(2, wheres.length());
|
List<VideoInfo> list = videoInfoDao.list(
|
(new StringBuilder("from VideoInfo v where v.show='1' and (")).append(wheres).append(")").toString(),
|
(page - 1) * Constant.pageCount, 20, new String[] {});
|
return list;
|
}
|
|
public List<VideoInfo> searchVideoUsedByAdmin(String name) {
|
return videoInfoDao.list("from VideoInfo v where v.name like ? order by REPLACE(v.name,?,'')", 0, 20,
|
new String[] { "%" + name + "%", name });
|
}
|
|
public long getVideoCountByClass(int type) {
|
String types[] = classServcie.getChildrenType(type);
|
String wheres = "";
|
String as[];
|
int j = (as = types).length;
|
for (int i = 0; i < j; i++) {
|
String st = as[i];
|
wheres = (new StringBuilder(String.valueOf(wheres))).append("or v.videoType.id=").append(st).append(" ")
|
.toString();
|
}
|
|
if (wheres.startsWith("or"))
|
wheres = wheres.substring(2, wheres.length());
|
return videoInfoDao
|
.getCount((new StringBuilder("select count(*) from VideoInfo v where ")).append(wheres).toString());
|
}
|
|
public void addVideoWatch(final String vid) {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
session.createSQLQuery("update wk_video_video v set v.watchcount=v.watchcount+1 where v.id=?")
|
.setParameter(0, vid).executeUpdate();
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception exception) {
|
exception.printStackTrace();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
public void addCategoryVideo(CategoryVideo cv) {
|
categoryVideoDao.save(cv);
|
}
|
|
/**
|
* 将没有添加进分裂视频的视频加入杂类
|
*/
|
public void addNOAddToCategoryVideo() {
|
videoInfoDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
List list = session
|
.createSQLQuery(
|
"SELECT DISTINCT(v.`id`) FROM wk_video_video v LEFT JOIN wk_category_video cv ON cv.`videoid`=v.`id` WHERE cv.`id` IS NULL")
|
.setFirstResult(0).setMaxResults(10000).list();
|
for (int i = 0; i < list.size(); i++) {
|
String videoId = list.get(i) + "";
|
session.createSQLQuery(
|
"INSERT INTO wk_category_video(videoid,videotypeid) VALUES(" + videoId + ",299)")
|
.executeUpdate();
|
}
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception exception) {
|
exception.printStackTrace();
|
}
|
return null;
|
}
|
});
|
|
}
|
}
|