package com.sys.core;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.sys.bus.email.EmailController;
import com.sys.bus.excel.controller.ExcelController;
import com.sys.bus.hr.model.Person;
import com.sys.bus.jx.controller.*;
import com.sys.bus.syscode.controller.SysCodeController;
import com.sys.core.model.DataBean;
import com.sys.core.utils.SessionUtil;
import com.sys.core.utils.Tool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Arrays;
import java.util.List;

public class AuthInterceptor implements Interceptor{
	Log log = LogFactory.getLog(this.getClass());
	/**
	 * 拦截器判断是否登陆或者添加权限控制
	 * 
	 */
	@Override
	public void intercept(Invocation inc) {
		log.info("actionKey:" + inc.getActionKey() + "\n"+
				"controllerKey:" + inc.getControllerKey() +"\n"+
				"methodname:" + inc.getMethodName() + "\n"+
				"viewPath:" + inc.getViewPath());
				String actionKey = inc.getActionKey();
		//		String controllerKey = inc.getControllerKey();
		Controller controller = inc.getController();
		//System.out.println(actionKey);
		String pass ="/login,/index,/error,/nologin,/jx/saveFeedback,/jx/cycle";//不登录可以访问的路径
		String pass2 = "/jx/toFeedbackPage,/jx/getScore,/jx/delJxKpi,/jx/toSelectBaseKpi,/jx/toAddExp,/jxr/toRatingPage,/jxr/saveOrSubmitRating,/jxr/submitRatingByJxOrHR,/jxr/backRating,/jxr/backRatingCenter,/jxr/backRatingDept,/jxr/backRatingHR";//需验权的controller里  不需验权的访问
		List<String> list = Arrays.asList(pass.split(","));
		List<String> list2 = Arrays.asList(pass2.split(","));

		if(list.contains(actionKey)){
			inc.invoke(); 
		}else{
			//controller.getSession().setAttribute("", "fpVLfNqdS/Gh8N7Bs6yiFYDvfe0=");
			Person p = (Person)controller.getSession().getAttribute("person");
			Boolean isAdmin = (Boolean)controller.getSession().getAttribute("admin");
			Boolean isJxjl = (Boolean)controller.getSession().getAttribute("jxjl");
			if (p==null) {
				try{
					JSONObject jsonObj = Tool.getRequestObject(controller.getRequest());
					log.info("====掉线提交数据:"+jsonObj.toJSONString());
				}catch (Exception e){
					log.info("====掉线提交数据转json失败");
				}
				controller.redirect("/nologin");
				return;
			}
			SessionUtil.setCurrentPerson(p);
			SessionUtil.setAdmin(isAdmin);
			SessionUtil.setJxjl(isJxjl);
			try{
				if(list2.contains(actionKey)){//不需验权的访问
					inc.invoke();
				}else{
					if(controller instanceof OrgController ||controller instanceof SysCodeController||controller instanceof JxCycleController
							||controller instanceof JxCycleController ){//组织考核 参数设置 需要管理员权限
						if(!needJxManager(controller))return;
					}

					if(controller instanceof JxManagerController||controller instanceof ExcelController ||controller instanceof EmailController){//绩效管理  需要是绩效专员或者绩效管理员
						if(!needJxjl(controller) && !needJxManager(controller))return;
					}
					log.info("==========================用户:"+p.getName()+",访问了"+inc.getActionKey());
					inc.invoke();
				}
			}finally {
				SessionUtil.remove();
			}


			
		}
	}
	private boolean needJxManager(Controller controller) {
		boolean admin = (boolean) controller.getSession().getAttribute("admin");

		if (!admin) {

			DataBean dBean = new DataBean();
			dBean.setCode(-1);
			dBean.setMsg("您的权限不足，无法进行此操作！");
			//返回异常信息，json格式
			controller.renderJson(dBean);
			return false;
		}
		return true;
	}
	private boolean needJxjl(Controller controller) {
		boolean jxjl = (boolean) controller.getSession().getAttribute("jxjl");

		if (!jxjl) {
			DataBean dBean = new DataBean();
			dBean.setCode(-1);
			dBean.setMsg("您的权限不足，无法进行此操作！");
			//返回异常信息，json格式
			controller.renderJson(dBean);
			return false;
		}
		return true;
	}
}