ChannelController.java 10.8 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.ShyChannel;
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.ChannelServiceImpl;
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.beans.factory.annotation.Value;
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.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/channel")
public class ChannelController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(ChannelController.class);
    @Value("${spring.localhost.url}")
	private String localhost_url;//本机地址
    @Autowired
    AuditPass Pass;
    @Autowired
    ChannelServiceImpl channelService;

    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    SitesServiceImpl sitesService;

   /**
    * 评论列表页
    * 
    * @param model
    * @param channelId
    * @param channelName
    * @param offline
    * @param start_date
    * @param end_date
    * @param state
    * @param sp_desc
    * @param page
    * @param pageSize
    * @param orderByClause
    * @param session
    * @return
    */
    @GetMapping("/page")
    public String getPage(Model model, String channel_id, String channel_name, Integer receive,
                          String start_date, String end_date,Integer shenhe_status, String sp_desc,
                          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 (CommonUtils.isNull(page)) page = 1;
        if (CommonUtils.isNull(pageSize)) pageSize = 10;
        if (CommonUtils.isNotEmpty(sp_desc) && spid == 0) {
        	ShySites shySite=sitesService.findspidBySpdesc(sp_desc);
        	if(CommonUtils.isNotNull(shySite)){
        		spid = shySite.getSpid();
        	}else{
        		setmodel_site(model);
        		return "error.html";
        	}
        } else if (CommonUtils.isEmpty(sp_desc) && spid == 0) {
            spid = null;
        }
        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<ShyChannel> channelPage = channelService.getPage(channel_id, channel_name, receive, sDate, eDate, shenhe_status, null, page, pageSize, orderByClause, spid, userId);
        List<ShyChannel> list = channelPage.getList();
        if (!list.isEmpty()) {
            for (ShyChannel channel : list) {
                if (channel.getShenhe_status() != CommonConst.AUDIT_INTT) {
                	String updater = CommonUtils.isEmpty(channel.getUpdater()) || "null".equals(channel.getUpdater())? "白名单":channel.getUpdater();
                    String auditor_id = channel.getAuditor();
                    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 ;
                        }
                    }
                    channel.setUpdater(updater);
                }
                if(null!=channel.getSp_id()){
                	//显示spid简称
                	ShySites shySites = sitesService.findspidDescByspid(channel.getSp_id());
                	if (CommonUtils.isNotNull(shySites)) {
                		channel.setSp_desc(shySites.getName());
                	}
                }else{
                	channel.setSp_desc("sp无法显示");
                }
                
            }
        }
        /* 获取参数值给前台 用来判断是否需要把查看显示出来 state recive*/
        String state1 = shenhe_status == null ? "" : shenhe_status.toString();
        model.addAttribute("state1", state1);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("channelPage", channelPage);
        return "page/channel.html";
    }

    /**
     * 评论详情
     *
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public String getDetailsChannel(@PathVariable Integer id, Model model) {
        ShyChannel comment = channelService.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 = channelService.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, String msg, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        boolean success = channelService.updateState(ids, CommonConst.AUDIT_REFUSE, userId,"",msg);
        if (success) {
            return new ResponseBean();
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }

    }


    /**
     * 打回(审核拒绝,应要求不加权限、状态等限制)
     *
     * @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[] channelIds = ids.split(",");
        String showMsg = "";
        for (String channelId : channelIds) {
            ShyChannel channel = channelService.selectByPrimaryKey(Integer.parseInt(channelId));
            JSONObject result = null;
            JSONObject json = new JSONObject();
            json.put("channel_id", channel.getChannel_id());
            json.put("state", 4);
            json.put("attr_tags", "");
            json.put("msg", msg);
            try {
                result = Pass.commentPass(json, channel.getCallback());
            } catch (Exception e) {
                logger.error("评论回调失败,activity_id:{}", channel.getChannel_id());
                showMsg = showMsg + "," + channel.getChannel_name() + "的评论拒绝请求失败";
                break;
            }
            if (result == null) {
                logger.error("评论回调错误,且无返回信息。activity_id:{}", channel.getChannel_id());
                showMsg = showMsg + "," + channel.getChannel_name() + "的评论拒绝请求失败";
                break;
            } else {
                Integer code = result.getInteger("errorCode");
                if (code != 0) {
                    logger.error("评论回调错误,activity_id:{},errorCode:{},errorMessage:{}", channel.getChannel_id(), code, result.getString("errorMessage"));
                    showMsg = showMsg + "," + channel.getChannel_name() + "的评论拒绝请求失败";
                    break;
                }
                channel.setShenhe_status(2);
                channel.setAuditor(userId);
                channel.setShenhe_msg(msg);
                int i = channelService.updateByPrimaryKeySelective(channel);
                if (i != 1) {
                    logger.error("评论回调失败,修改结果为0条activity_id:{}", channel.getChannel_id());
                    showMsg = showMsg + "," + channel.getChannel_name() + "的评论拒绝请求失败";
                }
            }
        }
        if (CommonUtils.isNotEmpty(showMsg)) {
            responseBean.setErrorCode(ErrorEnum.ERROR.getErrorCode());
            responseBean.setErrorMessage(showMsg);
        }
        return responseBean;
    }
    
    
    /**
     * @Auther: liwei
     * @Date: 2019/08/17 11:17
     * @Description: 频道前台通知接口
     */
    @GetMapping("/channelNotice")
    @ResponseBody
    public Map<String,Object> channelNotice() {
    Map<String,Object> map=new HashMap<>();
   	 ShyChannel shyChannel = new ShyChannel();
        shyChannel.setShenhe_type(CommonConst.AUDIT_TYPE_USER);// 审核类型为人工审
        shyChannel.setShenhe_status(CommonConst.AUDIT_INTT);
        List<ShyChannel> channelList = channelService.findshyChannel(shyChannel);
        if (channelList.size() > 0) {
       	logger.info("您有"+channelList.size()+"条新的频道审核申请,请注意查看!");
       	map.put("id", "1");//第一个提示框,避免冲突
       	map.put("title", "新频道审核通知");
       	map.put("content","您有"+channelList.size()+"条新的频道审核申请,请注意查看!" );
       	map.put("url",localhost_url+"/channel/page?shenhe_status=0&receive=0" );
       	return map;
        }
   	return null;
   }
}