package com.emr.controller;

import com.alibaba.fastjson.JSONObject;
import com.emr.entity.Power_User;
import com.emr.util.HttpClientUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author HJL
 * @create 2019/4/29
 */
@Controller
public class LoginController {

    @Value("${POWER_URLHEAD}")
    private String POWER_URLHEAD;

    @Value("${POWER_JSP}")
    private String POWER_JSP;

    @Value("${powerUrl}")
    private String powerUrl;

    @Value("${STR_SPLIT}")
    private String STR_SPLIT;

    @Value("${WEBSOCKET_URLHEAD}")
    private String WEBSOCKET_URLHEAD;

    @RequestMapping(value = "/toLogin")
    public String toLogin(Model model) {
        // 从session获取用户名
        // Subject currentUser = SecurityUtils.getSubject();
        // Session session = currentUser.getSession();
        //  session.removeAttribute("username");
        //  session.removeAttribute("menuList");
        //  session.removeAttribute("userSession");
        //session.invalidate();

        return "redirect:/login.jsp";
    }

    //实现用户登录@PathVariable("username")
    @RequestMapping(value = "/login")
    public String login(Model model, HttpServletRequest request) throws Exception {
        String token = request.getParameter("token");
        String userName = request.getParameter("userName");
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName, "456789");
        Subject subject = SecurityUtils.getSubject();
        subject.login(userToken);
        model.addAttribute("POWER_URLHEAD", POWER_JSP);
        //传递通知需要的数据
        model.addAttribute("WEBSOCKET_URLHEAD", WEBSOCKET_URLHEAD);
        model.addAttribute("STR_SPLIT", STR_SPLIT);
        request.getSession().setAttribute("token", token);
        JAXDynamicClientFactory dcf = JAXDynamicClientFactory.newInstance();
        Client client = dcf.createClient(POWER_URLHEAD + "/WebService/PowerWebService?wsdl");
        Object[] objects = client.invoke("getInfosByUserId", token, "emr_medical_record");
        ObjectMapper mapper = new ObjectMapper();
        Power_User powerUser = mapper.readValue(objects[0].toString(), Power_User.class);
        //设置进session
        request.getSession().setAttribute("CURRENT_USER", powerUser);
        request.getSession().setAttribute("userSession", powerUser.getUserPosition());
        String url = powerUrl + "?sysFlag=emr_medical_record&userName=" + userName;
        JSONObject obj = HttpClientUtils.httpGet(url);
        if (StringUtils.isNotBlank((obj.getString("code")))) {
            if ((obj.getString("code")).equals("100")) {
                //当验证都通过后，把用户信息放在 session 里
                Session session = SecurityUtils.getSubject().getSession();
                //session.setTimeout(4 * 3600);//以秒为单位，即在没有活动4小时后，session将失效
                session.setTimeout(-1);//以秒为单位，session将失效
                session.setAttribute("menuList", obj);
            }
        }
        return "index";
    }

    @RequestMapping(value = "/index")
    public String Login(HttpServletResponse response, HttpServletRequest request, Model model) throws IOException {
        return "index";
    }

    @RequestMapping(value = "/toError")
    public String error(Model model) {
        // 从session获取用户名
        // Subject currentUser = SecurityUtils.getSubject();
        // Session session = currentUser.getSession();
        //  session.removeAttribute("username");
        //  session.removeAttribute("menuList");
        //  session.removeAttribute("userSession");
        //session.invalidate();

        return "redirect:/error.jsp";
    }
}
 

