package com.newvideo.service.imp; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.newvideo.dao.CategoryContryDao; import com.newvideo.dao.VideoInfoDao; import com.newvideo.dao.VideoTypeDao; import com.newvideo.domain.CategoryContry; import com.newvideo.domain.VideoInfo; import com.newvideo.domain.VideoType; import com.newvideo.util.SolrUtil; import com.newvideo.util.StringUtil; //推荐服务 @Service public class RecommendService { @Resource private VideoInfoDao videoInfoDao; @Resource private BanQuanService banQuanService; @Resource private VideoTypeDao videoTypeDao; @Resource private CategoryContryDao categoryContryDao; public BanQuanService getBanQuanService() { return banQuanService; } public void setBanQuanService(BanQuanService banQuanService) { this.banQuanService = banQuanService; } public VideoInfoDao getVideoInfoDao() { return videoInfoDao; } public void setVideoInfoDao(VideoInfoDao videoInfoDao) { this.videoInfoDao = videoInfoDao; } @Cacheable(value = "userCache", key = "'guessLike'+'-'+#videoId+'-'+#detailSystem+'-'+#cachemd5") public List guessLike(String videoId, String detailSystem, List resourceList, String cachemd5) { String resourceWhere = ""; for (Long rid : resourceList) resourceWhere += String.format(" rv.`resourceid`=%d or", rid); if (resourceWhere.endsWith("or")) resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); videoId = StringUtil.isNullOrEmpty(videoId) ? "" : videoId; List list = new ArrayList(); int start = (int) (Math.random() * 100); String sql = String.format( "SELECT DISTINCT v.`id` FROM wk_category_video cv LEFT JOIN wk_resource_video rv ON cv.`videoid`=rv.`videoid` AND (%s) LEFT JOIN wk_video_video v ON cv.`videoid`=v.`id` " + (StringUtil.isNullOrEmpty(videoId) ? "" : "where cv.videoid!=" + videoId), resourceWhere, videoId); System.out.println(sql); List idList = videoInfoDao.sqlList(sql, start, 3, null); for (int i = 0; i < idList.size(); i++) { list.add((VideoInfo) videoInfoDao.find(VideoInfo.class, idList.get(i) + "")); } List videoList = new ArrayList(); for (VideoInfo v : list) { boolean can = true; for (int i = 0; i < videoList.size(); i++) { if (v.getId().equalsIgnoreCase(videoList.get(i).getId())) { can = false; break; } } if (can) videoList.add(v); if (videoList.size() >= 3) break; } return videoList; } public List testIOSGuessLike(String videoId) { return videoInfoDao.list("from VideoInfo v where v.show=1 and v.videoType.id=129", (int) (Math.random() * 100), 3, new String[] {}); } // 大区推荐视频 public List getCategoryRecommendVideoList(long videoTypeId) { return videoInfoDao.list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id=" + videoTypeId + " order by cv.orderby desc"); } // 大区推荐视频 @Cacheable(value = "classCache", key = "'getCategoryRecommendVideoList'+'-'+#videoTypeId+'-'+#count") public List getCategoryRecommendVideoList(long videoTypeId, int count) { return videoInfoDao .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoInfo is not null and cv.videoInfo.show=1 and cv.videoType.id=" + videoTypeId + " order by cv.orderby desc", 0, count, null); } // 相关视频 @Cacheable(value = "userCache", key = "'getRelativeVideoList'+'-'+#number+'-'+#detailSystemId+'-'+#vid+'-'+#cacheMD5") public List getRelativeVideoList(String detailSystemId, int number, String vid, List resourceList, String cachemd5) { String resourceWhere = ""; for (Long re : resourceList) { resourceWhere += " rv.resourceid=" + re + " or"; } if (resourceWhere.endsWith("or")) resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); VideoInfo vi = videoInfoDao.find(VideoInfo.class, vid); List infoList = SolrUtil.search(vi.getName(), 1); for (VideoInfo v : infoList) if (v.getId().equalsIgnoreCase(vi.getId())) { infoList.remove(v); break; } String cacheMD5 = ""; if (infoList != null) { for (VideoInfo info : infoList) { cacheMD5 += info.getId() + "#"; } } infoList = banQuanService.getBanQuanVideo(infoList, detailSystemId, cacheMD5); if (infoList.size() > number) infoList = infoList.subList(0, number); String sql = ""; for (int i = 0; i < infoList.size(); i++) { sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId() + " and (" + resourceWhere + ") union all"; } if (sql.endsWith("union all")) sql = sql.substring(0, sql.length() - 9); List rlist = null; if (!StringUtil.isNullOrEmpty(sql)) rlist = videoInfoDao.sqlList(sql); for (int i = 0; i < infoList.size(); i++) { if (rlist != null) if (Integer.parseInt(rlist.get(i) + "") < 1) { infoList.remove(i); i--; } } return infoList; } // 无视频的猜你喜欢 // @Cacheable(value = "userCache", key = // "'guessLikeList'+'-'+#number+'-'+#detailSystemId+'-'+#cachemd5") public List guessLikeList(String detailSystemId, int number, List resourceList, String cachemd5) { String resourceWhere = ""; for (Long re : resourceList) { resourceWhere += " rv.resourceid=" + re + " or"; } if (resourceWhere.endsWith("or")) resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); try { long count = videoInfoDao.getCount("select count(*) from CategoryRecommendCacheVideo cv "); List infoList = videoInfoDao.list( "select cv.videoInfo from CategoryRecommendCacheVideo cv order by cv.orderby desc", (int) Math.random() * 20, 100, null); String cacheMD5 = ""; if (infoList != null) { for (VideoInfo info : infoList) { cacheMD5 += info.getId() + "#"; } } infoList = banQuanService.getBanQuanVideo(infoList, detailSystemId, cacheMD5); String sql = ""; for (int i = 0; i < infoList.size(); i++) { sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId() + " and (" + resourceWhere + ") union all"; } if (sql.endsWith("union all")) sql = sql.substring(0, sql.length() - 9); if (!"".equals(sql)) { List rlist = videoInfoDao.sqlList(sql); for (int i = 0; i < infoList.size(); i++) { if (Integer.parseInt(rlist.get(i) + "") < 1) { infoList.remove(i); i--; } } } if (infoList.size() > number) return infoList.subList(0, number + 0); else return infoList; } catch (Exception e) { e.printStackTrace(); } return new ArrayList(); } // 猜你喜欢 @SuppressWarnings("unchecked") @Cacheable(value = "userCache", key = "'guessLikeList'+'-'+#number+'-'+#detailSystemId+'-'+#vid+'-'+#cachemd5") public List guessLikeList(String detailSystemId, int number, String vid, List resourceList, String cachemd5) { String resourceWhere = ""; for (Long re : resourceList) { resourceWhere += " rv.resourceid=" + re + " or"; } if (resourceWhere.endsWith("or")) resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); try { VideoType videoType = null; List list = videoTypeDao .list("select cv.videoType from CategoryVideo cv where cv.video.id=" + vid); if (list != null && list.size() > 0) videoType = list.get(0); else videoType = (VideoType) videoTypeDao.find(VideoType.class, 150L); long typeid = 0; if (videoType.getParent() != null) typeid = videoType.getParent().getId(); else typeid = videoType.getId(); long count = videoInfoDao .getCount("select count(*) from CategoryRecommendCacheVideo cv where cv.videoType.id=" + typeid); List infoList = videoInfoDao .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id=" + typeid + " order by cv.orderby desc", (int) (Math.random() * 20), 100, null); String cacheMD5 = ""; if (infoList != null) { for (VideoInfo info : infoList) { cacheMD5 += info.getId() + "#"; } } infoList = banQuanService.getBanQuanVideo(infoList, detailSystemId, cacheMD5); String sql = ""; for (int i = 0; i < infoList.size(); i++) { sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId() + " and (" + resourceWhere + ") union all"; } if (sql.endsWith("union all")) sql = sql.substring(0, sql.length() - 9); if (!"".equals(sql)) { List rlist = videoInfoDao.sqlList(sql); for (int i = 0; i < infoList.size(); i++) { if (Integer.parseInt(rlist.get(i) + "") < 1) { infoList.remove(i); i--; } } } if (infoList.size() > number) return infoList.subList(0, number); else return infoList; } catch (Exception e) { e.printStackTrace(); } return new ArrayList(); } // 大家都在看 @Cacheable(value = "userCache", key = "'peopleSee'+'-'+#number+'-'+#detailSystem+'-'+#vid+'-'+#cachemd5") public List peopleSee(String detailSystem, int number, String vid, List resourceList, String cachemd5) { vid = "4969470"; String resourceWhere = ""; for (Long re : resourceList) { resourceWhere += " rv.resourceid=" + re + " or"; } if (resourceWhere.endsWith("or")) resourceWhere = resourceWhere.substring(0, resourceWhere.length() - 2); try { VideoType videoType = null; List list = videoTypeDao .list("select cv.videoType from CategoryVideo cv where cv.video.id=" + vid); if (list != null && list.size() > 0) videoType = list.get(0); else videoType = (VideoType) videoTypeDao.find(VideoType.class, 170); long typeid = 0; if (videoType.getParent() != null) typeid = videoType.getParent().getId(); else typeid = videoType.getId(); List infoList = null; VideoInfo vi = (VideoInfo) videoInfoDao.find(VideoInfo.class, vid); if (typeid < 154 && !StringUtil.isNullOrEmpty(vi.getArea()))// 从地区视频缓存中 { List thirdList = categoryContryDao .list("from CategoryContry cc where cc.parent.parent.cid=" + typeid); long areaId = 0; for (CategoryContry cc : thirdList) { if (vi.getArea().contains(cc.getName())) { areaId = cc.getParent().getId(); break; } } if (areaId > 0) { infoList = videoInfoDao .list("select cc.videoInfo from AreaVideoCache cc where cc.categoryContry.id=" + areaId); } } else {// infoList = videoInfoDao .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id=" + videoType.getId() + " order by cv.orderby desc", 0, 100, null); } if (infoList == null || infoList.size() == 0) { infoList = videoInfoDao .list("select cv.videoInfo from CategoryRecommendCacheVideo cv where cv.videoType.id=" + typeid + " order by cv.orderby desc", 0, 100, null); } String cacheMD5 = ""; if (infoList != null) { for (VideoInfo info : infoList) { cacheMD5 += info.getId() + "#"; } } infoList = banQuanService.getBanQuanVideo(infoList, detailSystem, cacheMD5); String sql = ""; for (int i = 0; i < infoList.size(); i++) { sql += " select count(*) from wk_resource_video rv where rv.videoid=" + infoList.get(i).getId() + " and (" + resourceWhere + ") union all"; } if (sql.endsWith("union all")) sql = sql.substring(0, sql.length() - 9); if (!"".equals(sql)) { List rlist = videoInfoDao.sqlList(sql); for (int i = 0; i < infoList.size(); i++) { if (Integer.parseInt(rlist.get(i) + "") < 1) { infoList.remove(i); i--; } } } if (infoList.size() > number) return infoList.subList(0, number); else return infoList; } catch (Exception e) { e.printStackTrace(); } return new ArrayList(); } }