package com.yeshi.buwan.service.imp;
|
|
import java.io.Serializable;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Comparator;
|
import java.util.List;
|
|
import com.yeshi.buwan.service.inter.VideoResourceMapExtraInfoService;
|
import com.yeshi.buwan.util.JuHe.VideoResourceUtil;
|
import org.hibernate.HibernateException;
|
import org.hibernate.SQLQuery;
|
import org.hibernate.Session;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.cache.annotation.CacheEvict;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.buwan.dao.CategoryRecommendCacheVideoDao;
|
import com.yeshi.buwan.domain.VideoInfo;
|
import com.yeshi.buwan.domain.VideoInfoRank;
|
import com.yeshi.buwan.domain.recommend.CategoryRecommendCacheVideo;
|
import com.yeshi.buwan.service.imp.recommend.CategoryRecommendVideoService;
|
|
import javax.annotation.Resource;
|
|
@Service
|
public class CategoryRecommendCacheVideoService {
|
|
@Autowired
|
private CategoryRecommendCacheVideoDao dao;
|
|
@Autowired
|
private CategoryRecommendVideoService categoryRecommendVideoService;
|
|
@Resource
|
private VideoResourceMapExtraInfoService videoResourceMapExtraInfoService;
|
|
@Resource
|
private VideoResourceUtil videoResourceUtil;
|
|
public List<CategoryRecommendCacheVideo> getList(long videotype, String key, int page, int pageSize) {
|
|
List<CategoryRecommendCacheVideo> list = null;
|
|
int start = (page - 1) * pageSize;
|
|
if (videotype <= 0) { // 全部
|
String hql = "";
|
if (key != null && !"".equals(key.trim())) {
|
hql = "from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc";
|
list = dao.list(hql, start, pageSize, new Serializable[]{"%" + key + "%"});
|
} else {
|
hql = "from CategoryRecommendCacheVideo crcv order by crcv.rank desc , crcv.orderby desc";
|
list = dao.list(hql, start, pageSize, new Serializable[]{});
|
}
|
} else {
|
if (key != null && !"".equals(key.trim())) {
|
// list = dao.sqlList("select * from wk_recommend_category_cache
|
// cc left join wk_video_video vv on vv.id=cc.videoid where
|
// cc.videotypeid = ? and vv.name like ? order by cc.rank =
|
// 0,cc.rank,cc.orderby desc", start, pageSize, params);
|
list = dao.list(
|
"from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? order by crcv.rank desc , crcv.orderby desc",
|
start, pageSize, new Serializable[]{videotype, "%" + key + "%"});
|
} else {
|
list = dao.list(
|
"from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? order by crcv.rank desc , crcv.orderby desc",
|
start, pageSize, new Serializable[]{videotype});
|
}
|
}
|
|
// if(list != null && list.size() > 0){
|
// Collections.sort(list, new Comparator<CategoryRecommendCacheVideo>()
|
// {
|
// @Override
|
// public int compare(CategoryRecommendCacheVideo o1,
|
// CategoryRecommendCacheVideo o2) {
|
// int rank1= o1.getRank();
|
// int rank2 = o2.getRank();
|
// int b1 =o1.getOrderby();
|
// int b2=o2.getOrderby();
|
// if(rank2 == 0 && rank1 == 0){
|
// return b2-b1;
|
// }else if(rank2 == 0 && rank1 !=0){
|
// return -1;
|
// }else if(rank2 != 0 && rank1 == 0){
|
// return 1;
|
// }else if(rank2 != 0 && rank1 != 0){
|
// int i = rank1 - rank2;
|
// if(i==0){
|
// return b2-b1;
|
// }
|
// return i;
|
// }
|
// return 0;
|
// }
|
// });
|
// }
|
return list;
|
|
}
|
|
public int getCount(long videotype, String key) {
|
if (videotype <= 0) {
|
if (key != null && !"".equals(key.trim())) {
|
return (int) dao.getCount(
|
"select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoInfo.name like ? ",
|
new Serializable[]{"%" + key + "%"});
|
} else {
|
return (int) dao.getCount("select count(*) from CategoryRecommendCacheVideo crcv",
|
new Serializable[]{});
|
}
|
} else {
|
if (key != null && !"".equals(key.trim())) {
|
return (int) dao.getCount(
|
"select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? and crcv.videoInfo.name like ? ",
|
new Serializable[]{videotype, "%" + key + "%"});
|
}
|
return (int) dao.getCount(
|
"select count(*) from CategoryRecommendCacheVideo crcv where crcv.videoType.id = ? ",
|
new Serializable[]{videotype});
|
}
|
|
}
|
|
public CategoryRecommendCacheVideo getTop(long id) {
|
return dao.find(CategoryRecommendCacheVideo.class, id + "");
|
}
|
|
@Transactional
|
@CacheEvict(value = "topCache", allEntries = true)
|
public void update(CategoryRecommendCacheVideo crcv) {
|
dao.update(crcv);
|
}
|
|
@Transactional
|
public void save(CategoryRecommendCacheVideo crcv) {
|
dao.save(crcv);
|
}
|
|
@Transactional
|
@CacheEvict(value = "topCache", allEntries = true)
|
public void delete(long id) {
|
CategoryRecommendCacheVideo crcv = new CategoryRecommendCacheVideo();
|
crcv.setId(id + "");
|
dao.delete(crcv);
|
}
|
|
public CategoryRecommendCacheVideo getTop(long vid, long tid) {
|
List<CategoryRecommendCacheVideo> list = dao.list(
|
"from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? and crcv.videoType.id = ?",
|
new Serializable[]{vid + "", tid});
|
if (list.size() > 0) {
|
return list.get(0);
|
}
|
return null;
|
}
|
|
|
@SuppressWarnings("unchecked")
|
@Cacheable(value = "topCache", key = "'getVideoListByRank'+'-'+#videotype+'-'+#detailSystem+'-'+#platform+'-'+#version")
|
public List<VideoInfo> getVideoListByRank(final int videotype, final String detailSystem, String platform, Integer version) {
|
|
List<Long> resourceList = videoResourceUtil.getAvailableResourceIds(platform, version);
|
|
final int count = categoryRecommendVideoService.getRankVideoNumber(videotype);
|
List<VideoInfo> videoInfoList = (List<VideoInfo>) dao.excute(new HibernateCallback<List<VideoInfo>>() {
|
@Override
|
public List<VideoInfo> doInHibernate(Session session) throws HibernateException {
|
SQLQuery query = session.createSQLQuery("SELECT cc.rank,"
|
+ "vv.id,vv.picture,vv.name,vv.vpicture,vv.hpicture,vv.latest_vpicture,vv.latest_hpicture,vv.watchcount,vv.commentcount,cc.orderby,vv.tag "
|
+ "FROM wk_recommend_category_cache cc LEFT JOIN wk_video_video vv ON cc.videoid = vv.id LEFT JOIN wk_resource_video rv ON rv.videoid=vv.id LEFT JOIN wk_video_banquan_video bv on cc.videoid = bv.videoid WHERE bv.id is null and (bv.detailsystemid=? OR bv.detailsystemid IS null ) and rv.resourceid <> 16 AND vv.id IS NOT NULL AND vv.show = 1 AND cc.videotypeid=? AND cc.videoid = vv.id AND cc.rank <= ? AND cc.rank >= 0 GROUP BY cc.id ORDER BY cc.rank !=0 DESC ,cc.rank desc,cc.orderby desc");
|
query.setParameter(0, detailSystem);
|
query.setParameter(1, videotype);
|
query.setParameter(2, count);
|
query.setFirstResult(0);
|
query.setMaxResults(count);
|
List<Object[]> qdata = query.list();
|
List<VideoInfoRank> rks = new ArrayList<>();
|
List<VideoInfo> vs = new ArrayList<>();
|
VideoInfoRank vr;
|
for (Object[] objs : qdata) {
|
vr = new VideoInfoRank();
|
vr.setRank(Integer.parseInt(objs[0] + ""));
|
vr.setId(objs[1] + "");
|
vr.setPicture(objs[2] + "");
|
vr.setName(objs[3] + "");
|
vr.setVpicture(objs[4] + "");
|
vr.setHpicture(objs[5] + "");
|
vr.setLatestVpicture(objs[6] + "");
|
vr.setLatestHpicture(objs[7] + "");
|
vr.setWatchCount(objs[8] + "");
|
vr.setCommentCount(Integer.parseInt(objs[9] + ""));
|
vr.setOrderBy(Integer.parseInt(objs[10] + ""));
|
vr.setTag(objs[11] + "");
|
if (vr.getRank() > 0) {
|
rks.add(vr);
|
} else
|
vs.add(vr);
|
}
|
|
Comparator<VideoInfoRank> c = new Comparator<VideoInfoRank>() {
|
|
@Override
|
public int compare(VideoInfoRank arg0, VideoInfoRank arg1) {
|
return arg0.getRank() - arg1.getRank();
|
}
|
};
|
|
Collections.sort(rks, c);
|
|
// 找出rank=0的视频
|
|
for (int i = 1; i <= count; i++) {
|
boolean exist = false;
|
for (VideoInfoRank vir : rks)
|
if (vir.getRank() == i) {
|
exist = true;
|
break;
|
}
|
// 需要加入rks
|
if (!exist) {
|
if (vs.size() > 0) {
|
VideoInfoRank rr = (VideoInfoRank) vs.get(0);
|
rr.setRank(i);
|
rks.add(rr);
|
vs.remove(0);
|
}
|
}
|
}
|
|
vs.clear();
|
|
Collections.sort(rks, c);
|
|
for (VideoInfoRank videoInfoRank : rks)
|
vs.add(videoInfoRank);
|
return vs;
|
}
|
});
|
|
return videoResourceMapExtraInfoService.batchExtraInfo(videoInfoList, resourceList);
|
}
|
|
public CategoryRecommendCacheVideo getByVid(String vid) {
|
List<CategoryRecommendCacheVideo> list = dao
|
.list("from CategoryRecommendCacheVideo crcv where crcv.videoInfo.id = ? ", new Serializable[]{vid});
|
if (list.size() > 0) {
|
return list.get(0);
|
}
|
return null;
|
}
|
|
}
|