package com.newvideo.service.imp; import java.io.Serializable; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.cache.annotation.Cacheable; import org.springframework.orm.hibernate4.HibernateCallback; import org.springframework.stereotype.Service; import com.newvideo.dao.CommentDao; import com.newvideo.dao.CommentReplyDao; import com.newvideo.domain.Comment2; import com.newvideo.domain.CommentReply; import com.newvideo.domain.LoginUser; import com.newvideo.util.Constant; import com.newvideo.util.StringUtil; @Service public class CommentService { @Resource private CommentDao commentDao; @Resource private CommentReplyDao commentReplyDao; public void addComment2(Comment2 c) { commentDao.create(c); } @SuppressWarnings("unchecked") @Cacheable(value = "homeCache",key="'getComment2List'+'-'+#videoId+'-'+#thirdType+'-'+#page") public List getComment2List(final String videoId, final String thirdType, final int page) { return (List) commentDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { List list = new ArrayList(); try { list = session .createQuery( "from Comment2 c where c.thirdType=? and c.video.id=? and c.show=1 order by c.createtime desc") .setParameter(0, thirdType).setParameter(1, videoId) .setFirstResult((page - 1) * Constant.pageCount).setMaxResults(Constant.pageCount).list(); for (Comment2 c : list) { List li = session.createQuery("from CommentReply cr where cr.comment.id=?") .setParameter(0, c.getId()).list(); c.setReplyList(li); } } catch (Exception e) { e.printStackTrace(); } return list; } }); } @Cacheable(value = "homeCache",key="'getComment2ListCount'+'-'+#videoId+'-'+#thirdType") public long getComment2ListCount(String videoId, String thirdType) { return commentDao.getCount( "from Comment2 c where c.thirdType=? and c.video.id=? and c.show=1 order by c.createtime desc", new String[] { thirdType, videoId }); } public Comment2 getComment2ById(String id) { return commentDao.find(Comment2.class, id); } public boolean isHaveNewMessage(String uid) { long count = commentDao.getCount( "select count(*) from CommentReply cr where cr.parent!=null and cr.parent.user.id=? and cr.read=0", new String[] { uid }); if (count > 0) return true; else return false; } @SuppressWarnings("unchecked") public List getCommentReplyList(final String uid, final int pageIndex) { return (List) commentDao.excute(new HibernateCallback>() { public List doInHibernate(Session session) throws HibernateException { List crlist = new ArrayList(); try { List list = session .createSQLQuery( "SELECT cr.id FROM wk_video_comment_reply cr LEFT JOIN wk_video_comment2 c ON cr.`commentid`=c.`id` LEFT JOIN wk_video_comment_reply ccr ON ccr.`id`=cr.`parentid` WHERE (cr.`parentid` IS NULL AND c.`loginuid`=?) OR (cr.`parentid` IS NOT NULL AND ccr.`uid`=?) order by cr.createtime desc") .setParameter(0, uid).setParameter(1, uid) .setFirstResult((pageIndex - 1) * Constant.pageCount).setMaxResults(Constant.pageCount) .list(); for (BigInteger c : list) { crlist.add((CommentReply) session.get(CommentReply.class, c + "")); } } catch (Exception e) { e.printStackTrace(); } return crlist; } }); } public void updateCommentReply(CommentReply cr) { commentReplyDao.update(cr); } public CommentReply getCommentReplay(String id) { return commentReplyDao.find(CommentReply.class, id); } public long getCommentReplyListCount(String uid) { return commentReplyDao.getCountSQL( "SELECT count(cr.id) FROM wk_video_comment_reply cr LEFT JOIN wk_video_comment2 c ON cr.`commentid`=c.`id` LEFT JOIN wk_video_comment_reply ccr ON ccr.`id`=cr.`parentid` WHERE (cr.`parentid` IS NULL AND c.`loginuid`=" + uid + ") OR (cr.`parentid` IS NOT NULL AND ccr.`uid`=" + uid + ")"); } @SuppressWarnings("unchecked") public void setCommentReplyRead(final String uid) { commentDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { List list = session .createQuery( "from CommentReply cr where (cr.parent!=null and cr.parent.user.id=?) or (cr.parent = null and cr.comment.user.id=?)") .setParameter(0, uid).setParameter(1, uid).list(); session.getTransaction().begin(); for (CommentReply cr : list) { cr.setRead(true); session.update(cr); } session.flush(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } return null; } }); } public Serializable replyComment(String commentid, String commentReplyId, String uid, String content) { CommentReply cr = new CommentReply(); cr.setComment(new Comment2(commentid)); cr.setContent(content); cr.setCreatetime(System.currentTimeMillis() + ""); if (!StringUtil.isNullOrEmpty(commentReplyId)) cr.setParent(new CommentReply(commentReplyId)); cr.setRead(false); cr.setUser(new LoginUser(uid)); Serializable result = commentReplyDao.save(cr); return result; } }