package com.skyline.base.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.skyline.base.model.Comment;
import com.skyline.base.service.CommentService;
import com.skyline.base.type.CommentType;
import com.skyline.base.type.VoteType;
import com.skyline.common.bean.Page;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.User;
/**
* 评论模块的Controller
*
* @author Jairus Chan
*
* @version 0.1
*
* */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {
private static final Log LOGGER = LogFactory.getLog(CommentController.class);
@Autowired
private CommentService commentservice;
// @Value("${view.comment.comment}")
// private String commentView;
/**
* 获取评论(Ajax方式)
*
* @param commentType
* 评论的类型
* @param resourceid
* 资源Id
* @param page
* 分页
* @return
*/
@RequestMapping(value = "/getComment/{commentType}/{resourceid}", method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> getComment(@PathVariable CommentType commentType, @PathVariable long resourceid, Page page) {
try {
page.setSize(18);
List<Comment> comments = commentservice.lsComments(page, resourceid, commentType);
Map<String, Object> map = new HashMap<String, Object>();
map.put("page", page);
map.put("data", comments);
return map;
} catch (Exception e) {
LOGGER.error("获取评论出错:", e);
}
return null;
}
/**
* 获取评论(Ajax方式)(建议不要使用)
*
* @param commentType
* 评论类型
* @param resourceid
* 资源Id
* @param pageSize
* 分页大小
* @param page
* 分页
* @return
*/
@RequestMapping(value = "/getComment/{commentType}/{resourceid}/{pageSize}", method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> getCommentInPageSize(@PathVariable CommentType commentType, @PathVariable long resourceid,
@PathVariable Integer pageSize, Page page) {
try {
page.setSize(pageSize);
List<Comment> comments = commentservice.lsComments(page, resourceid, commentType);
Map<String, Object> map = new HashMap<String, Object>();
map.put("page", page);
map.put("data", comments);
return map;
} catch (Exception e) {
LOGGER.error("", e);
}
return null;
}
/**
* 进行评论
*
* @param commentType
* 评论类型
* @param resourceid
* 资源Id
* @param resourceName
* 资源名称
* @param commentContent
* 评论内容
* @param refComment
* 所回复的评论
* @param resourceOwner
* 资源的所有者
* @param shareResourceId
* 被分享的资源Id,只有评论一个分享并同步到分享时才会使用
* @param shareOwnerId
* 被分享的资源的所有者Id,只有评论一个分享并同步到分享时才会使用
* @param shareCommentType
* 被分享的资源的评论类型,只有评论一个分享并同步到分享时才会使用
* @param request
* @return
*/
@RequestMapping(value = "/commentResource/{commentType}/{resourceid}", method = RequestMethod.POST)
public @ResponseBody
String commentResource(@PathVariable CommentType commentType, @PathVariable Long resourceid, String resourceName,
String commentContent, Long refComment, Long resourceOwner, Long shareResourceId, Long shareOwnerId, String shareCommentType) {
try {
Long userid = Long.valueOf(0);
String nickname = "游客";
String portrait = "portrait";
User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
if (user != null) {
userid = user.getId();
nickname = user.getNickname();
portrait = user.getPortrait();
}
LOGGER.debug("commentType:"+commentType+";shareResourceId:"+shareResourceId+";shareOwnerId:"+shareOwnerId+";shareCommentType:"+shareCommentType);
commentservice.commentResource(userid, nickname, portrait, commentContent, refComment, resourceid, resourceName, commentType,
resourceOwner);
// 当shareResourceId != null && shareOwnerId != null &&
// shareCommentType != null时,则该评论是出自对一个分享的评论,并且需要同步到出处
if (shareResourceId != null && shareOwnerId != null && shareCommentType != null) {
commentservice.commentResource(userid, nickname, portrait, commentContent, refComment, shareResourceId, resourceName,
CommentType.valueOf(shareCommentType), shareOwnerId);
}
return "success";
} catch (Exception e) {
LOGGER.error("进行评论出错:", e);
}
return "error";
}
/**
* 对评论进行投票<br>
* 在0.1版本中,不会对用户的评论行为进行处理以防止刷票,而只是在前台js中进行了简单的处理
*
* @param commentType
* 评论的类型
* @param voteType
* 投票类型(AGREE,DISAGREE)
* @param resourceid
* 资源的Id
* @param commentid
* 评论的id
* @return
*/
@RequestMapping(value = "/{commentType}/vote/{voteType}/{resourceid}", method = RequestMethod.POST)
public @ResponseBody
String vote(@PathVariable CommentType commentType, @PathVariable VoteType voteType, @PathVariable long resourceid, long commentid) {
try {
if (voteType == VoteType.AGREE) {
commentservice.commentAgree(resourceid, commentid, commentType);
} else {
commentservice.commentDisagree(resourceid, commentid, commentType);
}
return "success";
} catch (Exception e) {
LOGGER.error("评论投票出错:", e);
}
return "fail";
}
/**
* 获取评论,这是一个评论的demo,在正式发布时会被删除
*
* @param page
* @return
*/
@RequestMapping("/getcomment")
public ModelAndView getComment(Page page) {
WebHelper.initRequest(null).setAttribute("pager", page);
ModelAndView mav = new ModelAndView();
mav.setViewName(ViewPaths.DEMO_COMMENTDEMO);
return mav;
}
/**
* 删除评论
*
* @param commentType
* 评论的类型
* @param commentId,l
* 评论的id
* @return
*/
@RequestMapping(value = "/{commentType}/delete", method = RequestMethod.POST)
public @ResponseBody
String deleteComment(@PathVariable CommentType commentType, long commentid,long resourceid) {
try {
User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
Long userid = Long.valueOf(0);
if (user != null) {
userid = user.getId();
}
commentservice.deleteComment(resourceid, commentid, commentType, userid);
return "success";
} catch (Exception e) {
LOGGER.error("删除评论出错:", e);
}
return "fail";
}
}