package com.sunxung.factoring.web.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunxung.factoring.component.util.SystemVariable;
import com.sunxung.factoring.entity.sys.vo.SearchOrg;
import com.sunxung.factoring.service.sys.AuthorityService;
import com.sunxung.factoring.service.sys.SessionService;

/**
 * @className:SessionController
 * @description:在线用户controller
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年1月23日 下午8:00:23
 */
@Controller
public class SessionController {

    private static String PREFIX = "sys/authority/";
    @Autowired
    private SessionService sessionService;
    @Autowired
    private AuthorityService authorityService;

    @RequestMapping("/session/list")
    public String goSessions(HttpServletRequest request, Model model) {
        String sessionId = (String) request.getSession().getAttribute(SystemVariable.SESSION_KEY);
        model.addAttribute("sessionId", sessionId);
        return PREFIX + "onlineUserList";
    }

    @RequestMapping(value = "/session/do_list", method = RequestMethod.GET)
    @ResponseBody
    public Object listData(SearchOrg search) {
        return sessionService.findSessionsForPage(search);
    }

    /**
     * 管理员注销用户
     *
     * @param id sessionID
     * @return Object
     */
    @RequestMapping(value = "/session/do_sginOut", method = RequestMethod.POST)
    @ResponseBody
    public Object do_SginOut(Long id, HttpServletRequest request) {
        return authorityService.doSginOut(id, request);
    }

    /**
     * 用户登出
     * 
     * @param httpRequest
     * @param response
     * @return
     */
    @RequestMapping("/session/sgin_out")
    public String sginOut(HttpServletRequest httpRequest, HttpServletResponse response) {
        httpRequest.getSession().invalidate();
        authorityService.sginOut(httpRequest, response);
        return PREFIX + "goLogin";
    }
}
