admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.yeshi.buwan.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 org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.buwan.dao.CommentDao;
import com.yeshi.buwan.dao.CommentReplyDao;
import com.yeshi.buwan.domain.Comment2;
import com.yeshi.buwan.domain.CommentReply;
import com.yeshi.buwan.domain.user.LoginUser;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.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<Comment2> getComment2List(final String videoId, final String thirdType, final int page) {
 
        return (List<Comment2>) commentDao.excute(new HibernateCallback<List<Comment2>>() {
            public List<Comment2> doInHibernate(Session session) throws HibernateException {
                List<Comment2> list = new ArrayList<Comment2>();
                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<CommentReply> 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<CommentReply> getCommentReplyList(final String uid, final int pageIndex) {
        return (List<CommentReply>) commentDao.excute(new HibernateCallback<List<CommentReply>>() {
            public List<CommentReply> doInHibernate(Session session) throws HibernateException {
                List<CommentReply> crlist = new ArrayList<CommentReply>();
                try {
                    List<BigInteger> 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<CommentReply> 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;
    }
 
    /**
     * 查询某个用户或者内容的信息
     * 
     * @param key
     * @param page
     * @return
     */
    public List<Comment2> getCommentList(String key, int page) {
        List<Comment2> list = commentDao.list(
                "from Comment2 c where c.user.name=? or c.content like ?  order by c.id desc",
                (page - 1) * Constant.pageCount, Constant.pageCount, new Serializable[] { key, "%" + key + "%" });
        return list;
    }
 
    /**
     * 查询评论的数量
     * 
     * @param key
     * @return
     */
    public long getCommentCount(String key) {
        long count = commentDao.getCount("select count(*)  from Comment2 c where c.user.name=? or c.content like ?  ",
                new Serializable[] { key, "%" + key + "%" });
        return count;
    }
 
    @Transactional
    public void deleteComment(String id) {
        List<CommentReply> list = commentReplyDao.list("from CommentReply cr where cr.comment.id=" + id);
        if (list != null)
            for (CommentReply cr : list)
                commentReplyDao.delete(cr);
        commentDao.delete(new Comment2(id));
    }
 
}