package com.yihu.wlyy.interceptors;

import java.util.Date;

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

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import com.yihu.wlyy.entity.security.Token;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.util.SystemData;

/**
 * 患者权限校验
 * @author George
 *
 */
public class PatientInterceptor extends BaseInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		boolean flag = true;
		try {
			if(request.getRequestURI().contains("/patient/hosptail/getHositalByTownCode")){
				return true;
			}
			response.setCharacterEncoding("UTF-8");
			JSONObject json = getAgent(request);
			if (json == null) {
				// 未登录
				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作！").getBytes());
				return false;
			}
			String tokenStr = json.has("token") ? json.getString("token") : "";
			String user = json.has("uid") ? json.getString("uid") : "";
			String imei = json.has("imei") ? json.getString("imei") : "";
			String openid = json.has("openid") ? json.getString("openid") : "";
			int platform = json.has("platform") ? json.getInt("platform") : 1;
			if (StringUtils.isEmpty(imei)) {
				imei = openid;
			}

			Token token = null;
			if (platform == 1) {
				token = SystemData.patientTokens.get(user);
			} else if (platform == 3) {
				token = SystemData.wxPatientTokens.get(user);
			}
			if (token == null) {
				token = tokenDao.findByPatient(user, platform);
				// 加入缓存
				if (platform == 1) {
					SystemData.patientTokens.put(user, token);
				} else if (platform == 3) {
					SystemData.wxPatientTokens.put(user, token);
				}
			}
			if (token == null || StringUtils.isEmpty(tokenStr) || (token.getPlatform() != 1 && token.getPlatform() != 3)) {
				// 未登录
				response.getOutputStream().write(error(SystemConf.NOT_LOGIN, "请登录后再操作！").getBytes());
				flag = false;
			} else {
				if (!StringUtils.equals(tokenStr, token.getToken()) || !StringUtils.equals(user, token.getUser()) || !StringUtils.equals(imei, token.getImei())) {
					// 别处登录
					response.getOutputStream().write(error(SystemConf.LOGIN_OTHER, "帐号在别处登录，请重新登录").getBytes());
					flag = false;
				} else {
					// 一天只更新一次
					if (DateUtil.getDays(token.getCzrq(), DateUtil.getNowDateShort()) != 0) {
						// 今天未更新，则更新缓存
						token.setCzrq(new Date());
						// 更新内存
						if (platform == 1) {
							SystemData.patientTokens.put(user, token);
						} else if (platform == 3) {
							SystemData.wxPatientTokens.put(user, token);
						}
						// 更新数据库
						tokenDao.save(token);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

	}

}