package com.yihu.hos.system.controller;

import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.interceptor.LocalContext;
import com.yihu.hos.system.service.SystemManager;
import com.yihu.hos.tenant.model.TenantSession;
import com.yihu.hos.tenant.service.AuthenticateService;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
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.annotation.Resource;
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 {

    @Resource(name = SystemManager.BEAN_ID)
    private SystemManager ehr;

    @Autowired
    private AuthenticateService authenticateService;

    /**
     * 判断是否登录
     * @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 ActionResult loginAction(HttpServletRequest request,String user,String password) {
        try {
            HttpSession session = request.getSession();
            return ehr.loginAction(session,user, password);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            ActionResult result = new ActionResult(false,"登录失败！密码错误！");
            result.setData(ex.getMessage());
            return result;
        }
    }

    /*
       退出操作
        */
    @RequestMapping("logoutAction")
    @ResponseBody
    public ActionResult logoutAction(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            String userCode = "";
            String tenant = "";
            if (session==null){
                tenant =  LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
            }else {
                TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION);
                if (tenantSession !=null){
                    userCode = tenantSession.getUserCode();
                    authenticateService.logout(session);
                    tenant = tenantSession.getTenant();
                }else {
                    tenant =  LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
                }
            }
            session.removeAttribute("userInfo");
            LocalContext.getContext().removeAttachment(ContextAttributes.TENANT_NAME);
            ActionResult re = new ActionResult(true, "登出成功！");
            re.setData(tenant);
            return re;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return new ActionResult(false, "登出异常！");
        }
    }

}
