package com.jfinal.club._admin.admin_interface;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.club.common.model.Admins;
import com.jfinal.club.util.CacheSessionUtil;
import com.jfinal.club.util.Cons;
import com.jfinal.club.vo.UserSessionVo;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import org.apache.commons.lang3.StringUtils;

/**
 * @descriptions: 用户session 拦截器
 * @author: lc
 * @date: 2020-05-09 19:47:07
 * @Version: 1.0
 **/
public class AdminSessionInterface implements Interceptor {

    private CacheSessionUtil sessionUtil = CacheSessionUtil.getInstance( );

    @Override
    public void intercept(Invocation inv) {

        String sessionId = inv.getController( ).getCookie(Cons.JSESSIONID_ADMIN);
        UserSessionVo session = sessionUtil.getSession(sessionId);
        if(sessionId == null || session == null){
            inv.getController().redirect("/admin/login");
        }else{
            inv.getController().setAttr("admins",session.getAdmins());

            Admins admins = session.getAdmins( );
            if(StringUtils.isNotBlank(admins.getRole())){
                String[] split = admins.getRole( ).split(",");
                for (int i = 0; i < split.length; i++) {
                    if(split[i].equals("1")){
                        inv.getController().setAttr("oneRole",true);
                    }
                    if(split[i].equals("2")){
                        inv.getController().setAttr("twoRole",true);
                    }
                    if(split[i].equals("3")){
                        inv.getController().setAttr("threeRole",true);
                    }
                    if(split[i].equals("4")){
                        inv.getController().setAttr("fourRole",true);
                    }
                    if(split[i].equals("5")){
                        inv.getController().setAttr("fiveRole",true);
                    }
                    if(split[i].equals("6")){
                        inv.getController().setAttr("sixRole",true);
                    }
                    if(split[i].equals("7")){
                        inv.getController().setAttr("sevenRole",true);
                    }
                }
            }

            inv.invoke();
        }
    }
}
