CommentsController.java 10.9 KB
package com.cnlive.shenhe.controller;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.bean.SessionUser;
import com.cnlive.shenhe.entity.ShyComments;
import com.cnlive.shenhe.entity.ShyContent;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.serviceImpl.CommentsServiceImpl;
import com.cnlive.shenhe.serviceImpl.ContentServiceImpl;
import com.cnlive.shenhe.serviceImpl.SitesServiceImpl;
import com.cnlive.shenhe.serviceImpl.UsersServiceImpl;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/comments")
public class CommentsController {
    private static Logger logger = LoggerFactory.getLogger(CommentsController.class);
    @Autowired
    AuditPass Pass;
    @Autowired
    CommentsServiceImpl commentsService;

    @Autowired
    UsersServiceImpl usersService;
    
    @Autowired
    SitesServiceImpl sitesService;
    @Autowired
    ContentServiceImpl contentService;

    /**
     * 评论列表页
     *
     * @param activity_id
     * @param content
     * @param is_hot
     * @param start_date
     * @param end_date
     * @param state
     * @param page
     * @param pageSize
     * @param orderByClause
     * @return
     */
    @GetMapping("/page")
    public String getPage(Model model, String activity_id, String content, Integer is_hot,
                          String start_date, String end_date, String state, Integer receive,
                          Integer page, Integer pageSize, String orderByClause, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        if(CommonUtils.isNull(sessionUser)){
        	return "redirect:/users/login";
        }
        Integer spid = sessionUser.getSpid();
        String userId = sessionUser.getUserId();
        if (spid == 0) spid = null;
        if (CommonUtils.isNull(page)) page = 1;
        if (CommonUtils.isNull(pageSize)) pageSize = 10;
        Date sDate = null;
        Date eDate = null;

        if (CommonUtils.isNotEmpty(start_date)) {
            try {
                sDate = CommonUtils.parseDate(start_date, "yyyy-MM-dd");
            } catch (Exception e) {
            }
        }
        if (CommonUtils.isNotEmpty(end_date)) {
            try {
                eDate = CommonUtils.parseDate(end_date, "yyyy-MM-dd");
            } catch (Exception e) {
            }
        }
        PageInfo<ShyComments> commentPage = commentsService.getPage(activity_id, content, is_hot, sDate, eDate, state, receive, null, page, pageSize, orderByClause, spid, userId);
        List<ShyComments> list = commentPage.getList();
        if (!list.isEmpty()) {
            for (ShyComments comment : list) {
                if (comment.getState() != CommonConst.AUDIT_INTT) {
                	String updater = CommonUtils.isEmpty(comment.getUpdater()) || "null".equals(comment.getUpdater())? "白名单":comment.getUpdater();
                    String auditor_id = comment.getAuditor_id();
                    if (CommonUtils.isNotEmpty(auditor_id)) {
                        ShyUsers user = new ShyUsers();
                        user.setUser_id(auditor_id);
                        user = usersService.select(user);
                        if (CommonUtils.isNotNull(user)) {
                        	String email = CommonUtils.isEmpty(user.getEmail()) || "null".equals(user.getEmail()) ? "" : user.getEmail();
                            String userName = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
                            updater = userName ;
                        }
                    }
                    comment.setUpdater(updater);
                }
                if(null!=comment.getSpid()){
                	//显示spid简称
                	ShySites shySites = sitesService.findspidDescByspid(comment.getSpid());
                	if (CommonUtils.isNotNull(shySites)) {
                		comment.setSpid_desc(shySites.getName());
                	}
                }else{
                	comment.setSpid_desc("sp无法显示");
                }
                
                //获取朋友圈的评论内容
                if(comment.getProgram_id().split("_").length==1
                	&& comment.getProgram_id().split(":").length==3){
                	ShyContent shyContent=new ShyContent();
                	shyContent.setSp_id(comment.getSpid());
                	shyContent.setContent_id(comment.getProgram_id());
                	shyContent =contentService.getShyContent(shyContent);
                	if(CommonUtils.isNotNull(shyContent)&& shyContent.getId()!=null){
                		String title=CommonUtils.isEmpty(shyContent.getTitle())?"朋友圈发布无标题内容":shyContent.getTitle();
                		comment.setComment_title(title);
                		comment.setComment_original_url(shyContent.getContent_url());
                	}
                }
            }
        }
        model.addAttribute("commentPage", commentPage);
        return "page/comment.html";
    }

    /**
     * 评论详情
     *
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public String getDetailsComments(@PathVariable Integer id, Model model) {
        ShyComments comment = commentsService.getDetailsComment(id);
        model.addAttribute("comment", comment);
        return "page/commentDetail.html";
    }


    /**
     * 审核通过
     *
     * @param ids
     * @return
     */
    @PostMapping("/pass")
    @ResponseBody
    public ResponseBean pass(String ids, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        boolean success = commentsService.updateState(ids, CommonConst.AUDIT_SUCCESS, userId,"","审核通过");
        if (success) {
            return new ResponseBean();
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }
    }

    /**
     * 审核拒绝
     *
     * @param ids
     * @return
     */
    @PostMapping("/refuse")
    @ResponseBody
    public ResponseBean refuse(String ids, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        boolean success = commentsService.updateState(ids, CommonConst.AUDIT_REFUSE, userId,"","审核拒绝");
        if (success) {
            return new ResponseBean();
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }

    }


    /**
     * 认领
     *
     * @param ids     根据id认领
     * @param num     认领条数
     * @param session
     * @return
     */
    @PostMapping("/receive")
    @ResponseBody
    public ResponseBean receive(String ids, Integer num, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        Integer spid = sessionUser.getSpid();
        Integer n = commentsService.receive(ids, num, userId, spid);
        return new ResponseBean(ErrorEnum.SUCCESS.getErrorCode(), "认领到" + n + "条内容");
    }


    @PostMapping("/retReceive")
    @ResponseBody
    public ResponseBean retReceive(String ids, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        Integer n = commentsService.retReceive(ids, userId);
        return new ResponseBean(ErrorEnum.SUCCESS.getErrorCode(), "退领了" + n + "条内容");
    }

    /**
     * 打回(审核拒绝,应要求不加权限、状态等限制)
     *
     * @param ids
     * @param msg
     * @param session
     * @return
     */
    @PostMapping("/repulse")
    @ResponseBody
    public ResponseBean repulse(String ids, String msg, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        ResponseBean responseBean = new ResponseBean();
        String[] commentsIds = ids.split(",");
        String showMsg = "";
        for (String commentsId : commentsIds) {
            ShyComments comments = commentsService.selectByPrimaryKey(Integer.parseInt(commentsId));
            JSONObject result = null;
            JSONObject json = new JSONObject();
            json.put("activity_id", comments.getActivity_id());
            json.put("state", 4);
            json.put("attr_tags", "");
            json.put("msg", msg);
            try {
                result = Pass.commentPass(json, comments.getCallback());
            } catch (Exception e) {
                logger.error("评论回调失败,activity_id:{}", comments.getActivity_id());
                showMsg = showMsg + "," + comments.getComment_user_name() + "的评论拒绝请求失败";
                break;
            }
            if (result == null) {
                logger.error("评论回调错误,且无返回信息。activity_id:{}", comments.getActivity_id());
                showMsg = showMsg + "," + comments.getComment_user_name() + "的评论拒绝请求失败";
                break;
            } else {
                Integer code = result.getInteger("errorCode");
                if (code != 0) {
                    logger.error("评论回调错误,activity_id:{},errorCode:{},errorMessage:{}", comments.getActivity_id(), code, result.getString("errorMessage"));
                    showMsg = showMsg + "," + comments.getComment_user_name() + "的评论拒绝请求失败";
                    break;
                }
                comments.setState(2);
                comments.setAuditor_id(userId);
                comments.setMsg(msg);
                int i = commentsService.updateByPrimaryKeySelective(comments);
                if (i != 1) {
                    logger.error("评论回调失败,修改结果为0条activity_id:{}", comments.getActivity_id());
                    showMsg = showMsg + "," + comments.getComment_user_name() + "的评论拒绝请求失败";
                }
            }
        }
        if (CommonUtils.isNotEmpty(showMsg)) {
            responseBean.setErrorCode(ErrorEnum.ERROR.getErrorCode());
            responseBean.setErrorMessage(showMsg);
        }
        return responseBean;
    }
}