package com.yihu.hos.system.controller;

import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.system.service.intf.ISystemManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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


/**
 * 数据采集配置页面
 * Created by hzp on 2015/8/12.
 */
@RequestMapping("/system")
@Controller("systemController")
public class SystemController {

    @Autowired
    private ISystemManager ehr;

    /**
     * 判断是否登录
     * @return
     */
    @RequestMapping("isLogin")
    @ResponseBody
    public Result isLogin(HttpServletRequest request, HttpServletResponse response)
    {
        try {
            HttpSession session = request.getSession();
            Object userInfo = session.getAttribute("userInfo");
            if(userInfo==null){
                //重定向到指定页面
                response.sendRedirect(request.getContextPath() + "/loginPage");
                return Result.success("未登录！");
            }
            return Result.success("已登录！");
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /*
    登录操作
     */
    @RequestMapping("loginAction")
    @ResponseBody
    public Result loginAction(HttpServletRequest request,String user,String password) {
        try {
            HttpSession session = request.getSession();
            return ehr.loginAction(session,user, password);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /*
       退出操作
        */
    @RequestMapping("logoutAction")
    @ResponseBody
    public Result logoutAction(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            session.removeAttribute("userInfo");
            return Result.success("退出登录");
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

}
