Browse Source

点赞相关逻辑

huangct 8 years ago
parent
commit
e9dc177df8

+ 10 - 0
jpress-model/src/main/java/io/jpress/model/Comment.java

@@ -20,6 +20,7 @@ import io.jpress.model.core.Table;
 import io.jpress.model.query.CommentQuery;
 import io.jpress.model.query.ContentQuery;
 import io.jpress.model.query.UserQuery;
+import io.jpress.model.query.VoteQuery;
 
 import java.math.BigInteger;
 
@@ -138,4 +139,13 @@ public class Comment extends BaseComment<Comment> {
 	public Comment getlatestSon(BigInteger id){
         return CommentQuery.me().findLatestSon(id);
 	}
+
+	public boolean updateVoteUpCount() {
+		long count = VoteQuery.me().findCountByCommentId(getId());
+		if (count > 0) {
+			setVoteUp(count);
+			return this.update();
+		}
+		return false;
+	}
 }

+ 31 - 0
jpress-model/src/main/java/io/jpress/model/Vote.java

@@ -0,0 +1,31 @@
+/**
+ * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
+ *
+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.gnu.org/licenses/lgpl-3.0.txt
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.jpress.model;
+
+import io.jpress.model.base.BaseVote;
+import io.jpress.model.core.Table;
+
+@Table(tableName = "vote", primaryKey = "id")
+public class Vote extends BaseVote<Vote> {
+
+	private static final long serialVersionUID = 1L;
+
+	public static String STATUS_DELETE = "delete";
+	public static String STATUS_DRAFT = "draft";
+	public static String STATUS_NORMAL = "normal";
+
+
+}

+ 216 - 0
jpress-model/src/main/java/io/jpress/model/base/BaseVote.java

@@ -0,0 +1,216 @@
+/**
+ * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
+ *
+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.gnu.org/licenses/lgpl-3.0.txt
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.jpress.model.base;
+
+import com.jfinal.plugin.activerecord.IBean;
+import com.jfinal.plugin.ehcache.CacheKit;
+import com.jfinal.plugin.ehcache.IDataLoader;
+import io.jpress.message.MessageKit;
+import io.jpress.model.Metadata;
+import io.jpress.model.core.JModel;
+import io.jpress.model.query.MetaDataQuery;
+
+import java.math.BigInteger;
+
+/**
+ * @author 黄诚天
+ * @param <M>
+ */
+@SuppressWarnings("serial")
+public abstract class BaseVote<M extends BaseVote<M>> extends JModel<M> implements IBean {
+
+	public static final String CACHE_NAME = "vote";
+	public static final String METADATA_TYPE = "vote";
+
+	public static final String ACTION_ADD = "vote:add";
+	public static final String ACTION_DELETE = "vote:delete";
+	public static final String ACTION_UPDATE = "vote:update";
+
+	public void removeCache(Object key){
+		if(key == null) return;
+		CacheKit.remove(CACHE_NAME, key);
+	}
+
+	public void putCache(Object key,Object value){
+		CacheKit.put(CACHE_NAME, key, value);
+	}
+
+	public M getCache(Object key){
+		return CacheKit.get(CACHE_NAME, key);
+	}
+
+	public M getCache(Object key,IDataLoader dataloader){
+		return CacheKit.get(CACHE_NAME, key, dataloader);
+	}
+
+	public Metadata createMetadata(){
+		Metadata md = new Metadata();
+		md.setObjectId(getId());
+		md.setObjectType(METADATA_TYPE);
+		return md;
+	}
+
+	public Metadata createMetadata(String key,String value){
+		Metadata md = new Metadata();
+		md.setObjectId(getId());
+		md.setObjectType(METADATA_TYPE);
+		md.setMetaKey(key);
+		md.setMetaValue(value);
+		return md;
+	}
+
+	public boolean saveOrUpdateMetadta(String key,String value){
+		Metadata metadata = MetaDataQuery.me().findByTypeAndIdAndKey(METADATA_TYPE, getId(), key);
+		if (metadata == null) {
+			metadata = createMetadata(key, value);
+			return metadata.save();
+		}
+		metadata.setMetaValue(value);
+		return metadata.update();
+	}
+
+	public String metadata(String key) {
+		Metadata m = MetaDataQuery.me().findByTypeAndIdAndKey(METADATA_TYPE, getId(), key);
+		if (m != null) {
+			return m.getMetaValue();
+		}
+		return null;
+	}
+
+	@Override
+	public boolean equals(Object o) {
+		if(o == null){ return false; }
+		if(!(o instanceof BaseVote<?>)){return false;}
+
+		BaseVote<?> m = (BaseVote<?>) o;
+		if(m.getId() == null){return false;}
+
+		return m.getId().compareTo(this.getId()) == 0;
+	}
+
+	@Override
+	public boolean save() {
+		boolean saved = super.save();
+		if (saved) { MessageKit.sendMessage(ACTION_ADD, this); }
+		return saved;
+	}
+
+	@Override
+	public boolean delete() {
+		boolean deleted = super.delete();
+		if (deleted) { MessageKit.sendMessage(ACTION_DELETE, this); }
+		return deleted;
+	}
+
+	@Override
+	public boolean deleteById(Object idValue) {
+		boolean deleted = super.deleteById(idValue);
+		if (deleted) { MessageKit.sendMessage(ACTION_DELETE, this); }
+		return deleted;
+	}
+
+	@Override
+	public boolean update() {
+		boolean update = super.update();
+		if (update) { MessageKit.sendMessage(ACTION_UPDATE, this); }
+		return update;
+	}
+
+	public void setId(BigInteger id) {
+		set("id", id);
+	}
+
+	public BigInteger getId() {
+		Object id = get("id");
+		if (id == null)
+			return null;
+
+		return id instanceof BigInteger ? (BigInteger)id : new BigInteger(id.toString());
+	}
+
+	public void setCommentId(BigInteger commentId) {
+		set("comment_id", commentId);
+	}
+
+	public BigInteger getCommentId() {
+		return get("comment_id");
+	}
+
+	public void setUserId(BigInteger userId) {
+		set("user_id", userId);
+	}
+
+	public BigInteger getUserId() {
+		return get("user_id");
+	}
+
+	public void setUuUserId(BigInteger uuUserId) {
+		set("uu_user_id", uuUserId);
+	}
+
+	public BigInteger getUuUserId() {
+		return get("uu_user_id");
+	}
+
+	public void setUp(java.lang.Boolean up) {
+		set("up", up);
+	}
+
+	public java.lang.Boolean getUp() {
+		return get("up");
+	}
+
+	public void setDown(java.lang.Boolean down) {
+		set("down", down);
+	}
+
+	public java.lang.Boolean getDown() {
+		return get("down");
+	}
+
+	public void setStatus(java.lang.String status) {
+		set("status", status);
+	}
+
+	public java.lang.String getStatus() {
+		return get("status");
+	}
+
+	public void setIp(java.lang.String ip) {
+		set("ip", ip);
+	}
+
+	public java.lang.String getIp() {
+		return get("ip");
+	}
+
+	public void setAgent(java.lang.String agent) {
+		set("agent", agent);
+	}
+
+	public java.lang.String getAgent() {
+		return get("agent");
+	}
+
+	public void setCreated(java.util.Date created) {
+		set("created", created);
+	}
+
+	public java.util.Date getCreated() {
+		return get("created");
+	}
+
+}

+ 1 - 1
jpress-model/src/main/java/io/jpress/model/query/CommentQuery.java

@@ -70,7 +70,7 @@ public class CommentQuery extends JBaseQuery {
 											 BigInteger contentId, BigInteger parentCommentId, String status) {
 
 		String select = " select DISTINCT c.id, c.*,content.title content_title,u.username,u.nickname,u.avatar, "
-				+ "quote_comment.text qc_content,quote_comment.author qc_author,"
+				+ "quote_comment.text qc_content,quote_comment.author qc_author,quote_comment.id qc_id,quote_comment.vote_up qc_vote_up,"
 				+ "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
 
 		StringBuilder fromBuilder = new StringBuilder("  from comment c");

+ 45 - 0
jpress-model/src/main/java/io/jpress/model/query/VoteQuery.java

@@ -0,0 +1,45 @@
+/**
+ * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
+ *
+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.gnu.org/licenses/lgpl-3.0.txt
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.jpress.model.query;
+
+import io.jpress.model.Vote;
+
+import java.math.BigInteger;
+import java.util.List;
+
+public class VoteQuery extends JBaseQuery {
+
+	protected static final Vote DAO = new Vote();
+	private static final VoteQuery QUERY = new VoteQuery();
+
+	public static VoteQuery me() {
+		return QUERY;
+	}
+
+	public long findCountByCommentId(BigInteger commentId) {
+		return DAO.doFindCount("comment_id = ? and up=1", commentId);
+	}
+
+	public Vote findVote(BigInteger commentId, BigInteger userId, BigInteger uuUserId){
+		List<Vote> list = DAO.doFind("comment_id = ? and (user_id = ? or uu_user_id = ?)", commentId, userId, uuUserId);
+		Vote vote = null;
+		if (list.size() > 0) {
+			vote = list.get(0);
+		}
+
+		return vote;
+	}
+}

+ 90 - 2
jpress-web-admin/src/main/java/io/jpress/admin/controller/_CommentController.java

@@ -23,8 +23,10 @@ import io.jpress.interceptor.UCodeInterceptor;
 import io.jpress.model.Comment;
 import io.jpress.model.Content;
 import io.jpress.model.User;
+import io.jpress.model.Vote;
 import io.jpress.model.query.CommentQuery;
 import io.jpress.model.query.UserQuery;
+import io.jpress.model.query.VoteQuery;
 import io.jpress.router.RouterMapping;
 import io.jpress.router.RouterNotAllowConvert;
 import io.jpress.template.TemplateManager;
@@ -261,14 +263,14 @@ public class _CommentController extends JBaseCRUDController<Comment> {
 			htmlBuilder.append("<div class=\"detail\">");
 			htmlBuilder.append("<div class=\"title\">");
 			htmlBuilder.append("<span>"+ comment.getAuthor() +"<em>" + sdf.format(comment.getCreated()) + "</em></span>");
-			htmlBuilder.append("<span><img src=\"/jpress/static/jpress/admin/image/hands.png\" alt=\"\"><em>" + comment.getVoteUp() + "</em></span>");
+			htmlBuilder.append("<span><img src=\"/jpress/static/jpress/admin/image/hands.png\" alt=\"\" onclick=\"vote("+ comment.getId()+")\"><em>" + comment.getVoteUp() + "</em></span>");
 			htmlBuilder.append("</div>");
 			htmlBuilder.append("<p>"+comment.getText()+"</p>");
 			if (comment.getlatestSon(comment.getId()) != null) {
 				Comment sonComment = comment.getlatestSon(comment.getId());
 				htmlBuilder.append("<div class=\"title reply\">");
 				htmlBuilder.append("<span>作者回复: <em>" + sdf.format(sonComment.getCreated()) + "</em></span>");
-				htmlBuilder.append("<span><img src=\"/jpress/static/jpress/admin/image/hands.png\" alt=\"\"><em>" + sonComment.getVoteUp() + "</em></span>");
+				htmlBuilder.append("<span><img src=\"/jpress/static/jpress/admin/image/hands.png\" alt=\"\" onclick=\"vote("+ sonComment.getId()+")\"><em>" + sonComment.getVoteUp() + "</em></span>");
 				htmlBuilder.append("</div>");
 				htmlBuilder.append("<p>"+sonComment.getText()+"</p>");
 			}
@@ -278,4 +280,90 @@ public class _CommentController extends JBaseCRUDController<Comment> {
 		renderAjaxResultForSuccess(htmlBuilder.toString());
 	}
 
+	//新增点赞
+	private void voteUp(Comment comment,BigInteger userId,BigInteger uuUserId){
+//		BigInteger commentId = getParaToBigInteger("comment_id");
+//		Comment comment = CommentQuery.me().findById(commentId);
+//		BigInteger userId = getParaToBigInteger("user_id");
+//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		Vote vote = getModel(Vote.class);
+		vote.setCommentId(comment.getId());
+		vote.setUserId(userId);
+		vote.setUuUserId(uuUserId);
+		vote.setUp(true);
+		vote.setDown(null);
+		vote.setIp(getIPAddress());
+		vote.setAgent(getUserAgent());
+		vote.setStatus(Vote.STATUS_NORMAL);
+		vote.setCreated(new Date());
+
+		if (vote.save()) {
+			//更新对应评论的点赞数
+			comment.updateVoteUpCount();
+			renderAjaxResultForSuccess();
+		} else {
+			renderAjaxResultForError();
+		}
+
+	}
+
+	//取消赞
+	private void cancelVoteUp(Comment comment,Vote vote){
+//		BigInteger commentId = getParaToBigInteger("comment_id");
+//		Comment comment = CommentQuery.me().findById(commentId);
+//		BigInteger userId = getParaToBigInteger("user_id");
+//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		vote.setUp(null);
+		if (vote.update()) {
+			//更新对应评论的点赞数
+			comment.updateVoteUpCount();
+			renderAjaxResultForSuccess();
+		} else {
+			renderAjaxResultForError();
+		}
+	}
+
+	//再次赞
+	private void againVoteUp(Comment comment,Vote vote){
+//		BigInteger commentId = getParaToBigInteger("comment_id");
+//		Comment comment = CommentQuery.me().findById(commentId);
+//		BigInteger userId = getParaToBigInteger("user_id");
+//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		vote.setUp(true);
+		if (vote.update()) {
+			//更新对应评论的点赞数
+			comment.updateVoteUpCount();
+			renderAjaxResultForSuccess();
+		} else {
+			renderAjaxResultForError();
+		}
+	}
+
+	//点赞接口
+	public void vote() {
+		BigInteger commentId = getParaToBigInteger("comment_id");
+		Comment comment = CommentQuery.me().findById(commentId);
+		BigInteger userId = getParaToBigInteger("user_id");
+		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		if ("0".equals(userId.toString())) {
+			userId = null;
+		}
+		if ("0".equals(uuUserId.toString())) {
+			uuUserId = null;
+		}
+
+		Vote vote = VoteQuery.me().findVote(commentId,userId,uuUserId);
+		//判断此用户是否给此评论点过赞
+		if (vote != null) {
+			if (vote.getUp() == null) {
+				againVoteUp(comment,vote);
+			} else {
+				cancelVoteUp(comment,vote);
+			}
+		} else {
+			voteUp(comment,userId,uuUserId);
+		}
+	}
+
+
 }

+ 16 - 5
jpress-web-template-usoftchina/src/main/webapp/templates/usoftchina/content_uuhelper.html

@@ -1,4 +1,5 @@
 <!--手机列表页-->
+<#if USER??> <#assign userId=USER.id></#if>
 <!DOCTYPE html>
 <html>
 <head lang="en">
@@ -254,13 +255,13 @@
                                 <div class="detail">
                                     <div class="title">
                                         <span>${comment.author!} <em>${(comment.created?string("yyyy-MM-dd"))!}</em></span>
-                                        <span><img src="/jpress/static/jpress/admin/image/hands.png" alt=""><em>${(comment.vote_up)!'0'}</em></span>
+                                        <span><img src="/jpress/static/jpress/admin/image/hands.png" alt="" onclick="vote(${comment.id})"><em>${(comment.vote_up)!'0'}</em></span>
                                     </div>
                                     <p>${comment.text!}</p>
                                     <#if comment.qc_content ??>
                                         <div class="title reply">
                                             <span>作者回复: <em>${(comment.qc_created?string("yyyy-MM-dd"))!}</em></span>
-                                            <span><img src="/jpress/static/jpress/admin/image/hands.png" alt=""><em>${(comment.vote_up)!'0'}</em></span>
+                                            <span><img src="/jpress/static/jpress/admin/image/hands.png" alt="" onclick="vote(${comment.qc_id})"><em>${(comment.qc_vote_up)!'0'}</em></span>
                                         </div>
                                         <p>${comment.qc_content!}</p>
                                     </#if>
@@ -325,9 +326,12 @@
 
     };
     //初始化
-    /*$(document).ready(function() {
-        LoadingDataFn();
-    });*/
+    var userId = null;
+    var uuUserId = null;
+    $(document).ready(function() {
+        userId = "${userId!0}"
+        uuUserId = "${userid!0}";
+    });
     //滚动加载方法
     $(window).scroll(function() {
         if (($(window).height() + $(window).scrollTop() + 60) >= $(document).height()) {
@@ -338,5 +342,12 @@
             }, 300);
         }
     });
+
+    //点赞
+    var vote = function(commentid) {
+        $.get("${CPATH}/admin/comment/vote?comment_id="+commentid+"&user_id="+userId+"&uu_user_id="+uuUserId, function(result){
+
+        });
+    }
 </script>
 </html>