TopicController.java 7.17 KB
package com.cnlive.shenhe.controller;

import com.alibaba.fastjson.JSON;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.bean.SessionUser;
import com.cnlive.shenhe.dto.TopicDTO;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyTopic;
import com.cnlive.shenhe.service.SitesService;
import com.cnlive.shenhe.service.TopicService;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/topic")
public class TopicController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(TopicController.class);
    @Autowired
    TopicService topicService;
    @Autowired
    SitesService sitesService;

    @Value("${spring.localhost.url}")
    private String localhost_url;

    /**
     * 图文列表
     *
     * @param model
     * @param topicDTO
     * @param session
     * @return
     */
    @GetMapping("/page")
    public String getPage(Model model, TopicDTO topicDTO, HttpSession session) {
        logger.info("列表请求参数:{}", JSON.toJSONString(topicDTO));
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        if (CommonUtils.isNull(sessionUser)) {
            return "redirect:/users/login";
        }
        Integer spid = sessionUser.getSpid();
        String userId = sessionUser.getUserId();
        topicDTO.setSpid(spid);
        topicDTO.setUserId(userId);
        if (CommonUtils.isNotEmpty(topicDTO.getSp_desc()) && topicDTO.getSpid() == 0) {
            ShySites shySite = sitesService.findspidBySpdesc(topicDTO.getSp_desc());
            if (CommonUtils.isNotNull(shySite)) {
                topicDTO.setSpid(shySite.getSpid());
            } else {
                setmodelSite(model);
                return "error.html";
            }
        } else if (CommonUtils.isEmpty(topicDTO.getSp_desc()) && topicDTO.getSpid() == 0) {
            topicDTO.setSpid(null);
        }

        if (CommonUtils.isNotEmpty(topicDTO.getStart_date())) {
            try {
                topicDTO.setStart_date(String.valueOf(CommonUtils.parseDate(topicDTO.getStart_date(), "yyyy-MM-dd")));
            } catch (Exception e) {
            }
        }
        if (CommonUtils.isNotEmpty(topicDTO.getEnd_date())) {
            try {
                topicDTO.setEnd_date(String.valueOf(CommonUtils.parseDate(topicDTO.getEnd_date(), "yyyy-MM-dd")));
            } catch (Exception e) {
            }
        }
        PageInfo<ShyTopic> topicPage = topicService.getPage(topicDTO);
        List<ShyTopic> list = topicPage.getList();
        if (!list.isEmpty()) {
            for (ShyTopic topic : list) {
                if (!topic.getShenhe_status().equals(CommonConst.AUDIT_INTT)) {
                    String auditorId = topic.getAuditor_id();
                    String updater = CommonUtils.isEmpty(topic.getUpdater()) || "null".equals(topic.getUpdater()) ? "白名单" : topic.getUpdater();
                    updater = sitesService.getUpdater(updater, auditorId);
                    topic.setUpdater(updater);
                }
                //显示spid简称
                if (CommonUtils.isNotNull(topic.getSp_id())) {
                    ShySites shySites = sitesService.findspidDescByspid(topic.getSp_id());
                    if (CommonUtils.isNotNull(shySites)) {
                        topic.setSp_desc(shySites.getName());
                    }
                } else {
                    topic.setSp_desc("sp无法显示");
                }
            }
        }
        model.addAttribute("topicPage", topicPage);
        model.addAttribute("pageSize", topicDTO.getPageSize());
        model.addAttribute("Topic_id", topicDTO.getTopic_id());
        model.addAttribute("Topic_name", topicDTO.getTopic_name());
        model.addAttribute("Sp_desc", topicDTO.getSp_desc());
        model.addAttribute("Start_date", topicDTO.getStart_date());
        model.addAttribute("End_date", topicDTO.getEnd_date());
        model.addAttribute("TopicStatus",topicDTO.getShenhe_status());
        return "page/topic.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;
        String[] topicIds = ids.split(",");
        for (String id : topicIds) {
            success = topicService.callback(id, "审核通过", CommonConst.AUDIT_SUCCESS, userId, "");
            if (!success) {
                return new ResponseBean(ErrorEnum.ERROR);
            }
        }
        return new ResponseBean(ErrorEnum.SUCCESS);
    }

    /**
     * 审核拒绝
     *
     * @param ids
     * @return
     */
    @PostMapping("/refuse")
    @ResponseBody
    public ResponseBean refuse(String ids, String msg, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        boolean success;
        String[] contentIds = ids.split(",");
        for (String id : contentIds) {
            success = topicService.callback(id, msg, CommonConst.AUDIT_REFUSE, userId, "");
            if (!success) {
                return new ResponseBean(ErrorEnum.ERROR);
            }
        }
        return new ResponseBean(ErrorEnum.SUCCESS);
    }


    /**
     * 话题审核前台通知接口
     *
     * @return
     */
    @GetMapping("/topicNotice")
    @ResponseBody
    public Map<String, Object> topicNotice() {
        Map<String, Object> map = new HashMap<>(16);
        ShyTopic shyTopic = new ShyTopic();
        shyTopic.setShenhe_status(CommonConst.AUDIT_INTT);
        shyTopic.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
        List<ShyTopic> shyTopicList = topicService.findShyTopic(shyTopic);
        if (shyTopicList != null && shyTopicList.size() > 0) {
            logger.info("您有" + shyTopicList.size() + "条新的话题审核,请注意查看!");
            // 第三个提示框,避免冲突
            map.put("id", "3");
            map.put("title", "新话题审核通知");
            map.put("content", "您有" + shyTopicList.size() + "条新的话题审核,请注意查看!");
            map.put("url", localhost_url + "/topic/page?shenhe_status=0&receive=0");
            return map;
        }
        return null;
    }
}