package com.newvideo.service.imp; import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.newvideo.dao.CollectionDao; import com.newvideo.dao.VideoInfoDao; import com.newvideo.domain.Collection; import com.newvideo.domain.UserInfo; import com.newvideo.domain.VideoInfo; import com.newvideo.domain.VideoType; import com.newvideo.util.Constant; @Service public class CollectionService { @Resource private CollectionDao collectionDao; @Resource private VideoInfoDao videoInfoDao; @Resource private CategoryVideoService categoryVideoService; public VideoInfoDao getVideoInfoDao() { return videoInfoDao; } public void setVideoInfoDao(VideoInfoDao videoInfoDao) { this.videoInfoDao = videoInfoDao; } public CollectionDao getCollectionDao() { return collectionDao; } public void setCollectionDao(CollectionDao collectionDao) { this.collectionDao = collectionDao; } @SuppressWarnings("rawtypes") @Cacheable(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-'+#page") public List getCollectVideo(String uid, int page) { List videoList; List list; videoList = new ArrayList(); list = new ArrayList(); try { List li = collectionDao .sqlList( (new StringBuilder( "select uid,videoid,thirdtype from wk_video_collection where uid=? order by createtime desc limit ")) .append(Constant.pageCount * (page - 1)) .append(",").append(Constant.pageCount) .toString(), new String[] { uid }); for (int i = 0; i < li.size(); i++) { Collection c = new Collection(); Object obs[] = (Object[]) li.get(i); c.setUser(new UserInfo(obs[0].toString())); c.setVideo(new VideoInfo(obs[1].toString())); c.setThirdType(obs[2].toString()); list.add(c); } } catch (Exception e) { e.printStackTrace(); } finally { } for (Iterator iterator = list.iterator(); iterator .hasNext();) { Collection collection = (Collection) iterator.next(); videoList.add(videoInfoDao.find(VideoInfo.class, collection .getVideo().getId())); } // 获取分类名称 for (VideoInfo video : videoList) { VideoType vt = new VideoType(); vt.setName(categoryVideoService.getTypeNameByVid(video.getId())); video.setVideoType(vt); } return videoList; } @Caching(evict = { @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#c.user.id+'-1'"), @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#c.user.id+'-2'"), @CacheEvict(value = "userCache", key = "'getCollectVideoCount'+'-'+#c.user.id") }) public void saveCollection(Collection c) { try { collectionDao.save(c); } catch (Exception e) { } } @Cacheable(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid") public long getCollectVideoCount(String uid) { return collectionDao.getCount( "select count(*) from Collection c where c.user.id=?", new String[] { uid }); } @Caching(evict = { @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-1'"), @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-2'"), @CacheEvict(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid") }) @SuppressWarnings("rawtypes") public boolean cancelCollect(final String uid, final String videoId, final String thirdType) { boolean s = false; s = (Boolean) collectionDao.excute(new HibernateCallback() { public Boolean doInHibernate(Session session) throws HibernateException { boolean s = false; try { session.getTransaction().begin(); List stList = new ArrayList(); List list = session .createSQLQuery( (new StringBuilder( "select id from wk_video_collection c where c.uid=")) .append(uid) .append(" and c.videoid=") .append(videoId) .append(" and c.thirdtype=") .append(thirdType).toString()) .list(); for (int i = 0; i < list.size(); i++) { BigInteger bi = (BigInteger) list.get(i); if (bi != null) stList.add(bi.toString()); } if ((stList != null) & (stList.size() > 0)) { Collection c = new Collection(); c.setId((new StringBuilder(String .valueOf((String) stList.get(0)))).toString()); session.delete(c); s = true; } else { s = false; } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return s; } }); return s; } @Caching(evict = { @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-1'"), @CacheEvict(value = "userCache", key = "'getCollectVideo'+'-'+#uid+'-2'"), @CacheEvict(value = "userCache", key = "'getCollectVideoCount'+'-'+#uid") }) public boolean cancelCollect(String uid, String[] videoIds, String[] thirdTypes) { String where = "("; for (int i = 0; i < videoIds.length; i++) where += " (c.videoid=" + videoIds[i] + " and c.thirdtype=" + thirdTypes[i] + ") or"; if (where.endsWith("or")) where = where.substring(0, where.length() - 2); where += ")"; boolean s = false; try { List stList = new ArrayList(); List list = collectionDao.sqlList((new StringBuilder( "select id from wk_video_collection c where c.uid=")) .append(uid).append(" and ").append(where).toString()); for (int i = 0; i < list.size(); i++) { BigInteger bi = (BigInteger) list.get(i); if (bi != null) stList.add(bi.toString()); } if ((stList != null) & (stList.size() > 0)) { for (String ss : stList) { Collection c = new Collection(); c.setId((new StringBuilder(String.valueOf(ss))).toString()); collectionDao.delete(c); } s = true; } else { s = false; } } catch (Exception e) { e.printStackTrace(); } finally { } return s; } public boolean isCollect(String uid, String videoId, String thirdType) { long count = collectionDao.getCountSQL((new StringBuilder( "select count(*) from wk_video_collection c where c.uid=")) .append(uid).append(" and c.videoid=").append(videoId) .append(" and c.thirdtype=").append(thirdType).toString()); return count > 0L; } public boolean isCollect(String uid, String videoId) { long count = collectionDao.getCountSQL((new StringBuilder( "select count(*) from wk_video_collection c where c.uid=")) .append(uid).append(" and c.videoid=").append(videoId) .toString()); return count > 0L; } }