UsersController.java 6.86 KB
package com.cnlive.shenhe.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.SessionUser;
import com.cnlive.shenhe.dto.UsersDTO;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.serviceImpl.SitesServiceImpl;
import com.cnlive.shenhe.serviceImpl.UsersServiceImpl;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageInfo;
import org.jasig.cas.client.authentication.AttributePrincipal;
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.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: liwei
 * @Date: 2019/08/20 14:22
 * @Description:
 */
@Controller
@RequestMapping("/users")
public class UsersController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(UsersController.class);

    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    SitesServiceImpl sitesService;

    /**
     * 登录入口
     */
    @Value("${SERVICE_LOGIN}")
    private String SERVICE_LOGIN;

    /**
     * 本机的名称
     */
    @Value("${spring.localhost.url}")
    private String redirectURL;

    @GetMapping(value = "/{id}")
    @ResponseBody
    public Object getTest(@PathVariable Integer id) {
        return usersService.get(id);
    }

    @RequestMapping("/login")
    public String index(HttpServletRequest request) {
        AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
        if (CommonUtils.isNull(principal)) {
            return "redirect:https://user.cnlive.com/OpenSSO2/login?service=" + SERVICE_LOGIN;
        }
        //用户ID
        String id = principal.getName();
        if (CommonUtils.isNotEmpty(id)) {
            ShyUsers user = usersService.get(Integer.parseInt(id));
            if (CommonUtils.isNotNull(user) && user.getState()) {
                SessionUser sessionUser = new SessionUser();
                sessionUser.setUserId(user.getUser_id());
                sessionUser.setSpid(Integer.parseInt(principal.getAttributes().get("new_sp_id").toString()));
                sessionUser.setUserName(user.getUsername());
                sessionUser.setCompany(user.getCompany_brief());
                sessionUser.setEmail(user.getEmail());
                sessionUser.setLogo(user.getUser_logo());
                sessionUser.setMaster(user.getMaster());
                request.getSession().setAttribute(SessionUser.SESSION_KEY, sessionUser);
                return "redirect:" + redirectURL + "/videos/page?video_title=&start_date=&end_date=&state=0&receive=0&video_priority=";
            }
        }
        return "page/error.html";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:https://user.cnlive.com/OpenSSO2/logout?service=" + SERVICE_LOGIN;
    }


    @GetMapping(value = "/page")
    public String page(Model model, UsersDTO usersDTO, HttpSession session) {
        logger.info("列表请求参数:{}", JSON.toJSONString(usersDTO));
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        if (CommonUtils.isNull(sessionUser)) {
            return "redirect:/users/login";
        }
        Integer spid = sessionUser.getSpid();
        usersDTO.setSp_id(spid);
        if (spid == 0) {
            usersDTO.setSp_id(null);
        }

        PageInfo<ShyUsers> usersPage = usersService.getPage(usersDTO);
        List<ShyUsers> list = usersPage.getList();
        JSONObject notice = CommonConst.NOTICE;
        if (!list.isEmpty()) {
            for (ShyUsers users : list) {
                //显示spid简称
                ShySites shySites = sitesService.findspidDescByspid(users.getSp_id());
                if (CommonUtils.isNotNull(shySites)) {
                    users.setSp_desc(shySites.getName());
                }
            }
        }
        model.addAttribute("usersPage", usersPage);
        model.addAttribute("pageSize", usersDTO.getPageSize());
        model.addAttribute("notice", notice);
        return "page/users.html";
    }

    /**
     * 获取通知是否显示
     *
     * @param
     * @param session
     * @return
     */
    @PostMapping(value = "/getNoticeShow")
    @ResponseBody
    public Map<String, Object> getNoticeShow(HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        ShyUsers shyUser = new ShyUsers();
        shyUser.setUser_id(sessionUser.getUserId());
        shyUser = usersService.select(shyUser);
        boolean isShow_chinnal = false;//是否显示频道消息框
        boolean isShow_liveApply = false;//是否显示直播申请消息框
        boolean isShow_topic = false;//是否显示话题审核消息框
        if (CommonUtils.isNotEmpty(shyUser.getNotice_show()) && shyUser.getNotice_show().contains(CommonConst.NOTICE_CHANNAL.toString())) {
            isShow_chinnal = true;
        }
        if (CommonUtils.isNotEmpty(shyUser.getNotice_show()) && shyUser.getNotice_show().contains(CommonConst.NOTICE_LIVEAPPLY.toString())) {
            isShow_liveApply = true;
        }
        if (CommonUtils.isNotEmpty(shyUser.getNotice_show()) && shyUser.getNotice_show().contains(CommonConst.NOTICE_TOPIC.toString())) {
            isShow_topic = true;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isShow_chinnal", isShow_chinnal);
        map.put("isShow_liveApply", isShow_liveApply);
        map.put("isShow_topic", isShow_topic);
        return map;
    }


    @PostMapping(value = "/write")
    public String write(Model model, Integer userId, String[] write_notice, HttpSession session, String redirect_url) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
//    	Integer spid = sessionUser.getSpid();
//    	if (spid != 0) {
//    		return "page/error.html";
//    	}
        ShyUsers user = usersService.get(userId);
        String write = "";
        if (CommonUtils.isNotNull(write_notice) && write_notice.length > 0) {
            for (String w : write_notice) {
                write = write + "," + w;
            }
            write = write.substring(1);
        }
        user.setNotice_show(write);
        boolean success = usersService.update(user);
        if (success) {
            return "redirect:" + redirect_url;
        } else {
            setmodelResult(model);
            return "error.html";
        }
    }

}