UsersController.java 6.89 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.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyUsersfree;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.attribute.HashAttributeSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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

    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    SitesServiceImpl sitesService;

    @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=http://test.shen.cnlive.com:82/users/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:http://test.shen.cnlive.com:82/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=http://test.shen.cnlive.com:82/users/login";
    }

    
    @GetMapping(value = "/page")
    public String page(Model model, Integer page, Integer pageSize, String orderByClause,
    		String username, String mobile, Integer sp_id, String company_brief, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        if(CommonUtils.isNull(sessionUser)){
        	return "redirect:/users/login";
        }
        Integer spid = sessionUser.getSpid();
        if (spid == 0) spid = null;
        if (CommonUtils.isNull(page)) page = 1;
        if (CommonUtils.isNull(pageSize)) pageSize = 10;
        PageInfo<ShyUsers> usersPage = usersService.getPage(page, pageSize, orderByClause, username,mobile,sp_id,company_brief);
        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", pageSize);
        model.addAttribute("notice", notice);
        return "page/users.html";
    }
    
    /**
     * 获取通知是否显示
     * @param showType
     * @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 {
    		setmodel_result(model);
			return "error.html";
    	}
    }
    
}