package com.ssm.core.utils;


import com.ssm.manage.query.RlStaffVo;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtils {
    @Autowired
    private RedisClient redisClient;

    /**
     * 根据手机号获取员工信息
     * @param phone
     * @return
     */
    public RlStaffVo getStaff(String phone){
     return   (RlStaffVo) redisClient.get(getStaffKey(phone));
    }

    /**
     * 根据Session获取员工信息
     * @param session
     * @return
     */
    public RlStaffVo getStaff(HttpSession session){
        return   (RlStaffVo) redisClient.get(getStaffKey(SessionHelp.getLoginAccount(session)));
    }

    public void setStaff(RlStaffVo rlStaffVo){
        redisClient.set(getStaffKey(rlStaffVo.getMobilePhone()),rlStaffVo,RedisClient.EXPIRE_TIME_ONE_DAY);
    }

    public void setRedis(String key, Object o, Long time, TimeUnit unit){
        redisClient.set(key,o,time,unit);
    }
    public Object getRedis(String key ){
      return    redisClient.get(key);
    }
    private String getStaffKey(String phone){
        return Constant.staffKey + phone;
    }
}
