package com.bootdo.system.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.IPUtils;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.UserOnline;
import com.bootdo.system.service.SessionService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

@RequestMapping("/sys/online")
@Controller
public class SessionController {
	@Autowired
	SessionService sessionService;

    @Log("访问在线用户")
    @GetMapping()
    public String online() {
        return "system/online/online";
    }

	@ResponseBody
	@RequestMapping("/list")
	public List<UserOnline> list(HttpServletRequest request) {
		try {
			return sessionService.list(request);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@ResponseBody
	@RequestMapping("/forceLogout/{sessionId}")
	public R forceLogout(@PathVariable("sessionId") String sessionId,HttpServletRequest request) {
		try {
			sessionService.forceLogout(sessionId,request);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}

	}

	@ResponseBody
	@RequestMapping("/sessionList")
	public Collection<Session> sessionList() {
		return sessionService.sessionList();
	}


}
