package com.hwrj.cloud.portal.service.impl;

import com.hwrj.cloud.portal.mapper.UmsMemberMapper;
import com.hwrj.cloud.portal.model.UmsMember;
import com.hwrj.cloud.portal.service.UmsMemberCacheService;
import com.hwrj.cloud.security.annotation.CacheException;
import com.hwrj.cloud.security.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * UmsMemberCacheService实现类
 * Created by macro on 2020/3/14.
 */
@Service
public class UmsMemberCacheServiceImpl implements UmsMemberCacheService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private UmsMemberMapper memberMapper;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.expire.authCode}")
    private Long REDIS_EXPIRE_AUTH_CODE;
    @Value("${redis.expire.authLoginCode}")
    private Long REDIS_EXPIRE_LOGIN_CODE;
    @Value("${redis.key.member}")
    private String REDIS_KEY_MEMBER;
    @Value("${redis.key.authCode}")
    private String REDIS_KEY_AUTH_CODE;

    @Value("${redis.key.telLoginCode}")
    private String REDIS_KEY_LOGIN_CODE;

    @Value("${redis.key.token}")
    private String REDIS_KEY_TOKEN;

    @Value("${jwt.expiration}")
    private Long JWT_EXPIRATION;

    @Override
    public void delMember(Long memberId) {
        UmsMember umsMember = memberMapper.selectByPrimaryKey(memberId);
        if (umsMember != null) {
            String key = REDIS_DATABASE + ":" + REDIS_KEY_MEMBER + ":" + umsMember.getUsername();
            redisService.del(key);
        }
    }

    @Override
    public UmsMember getMember(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_MEMBER + ":" + username;
        return (UmsMember) redisService.get(key);
    }

    @Override
    public void setMember(UmsMember member) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_MEMBER + ":" + member.getUsername();
        redisService.set(key, member, REDIS_EXPIRE);
    }

    @CacheException
    @Override
    public void setAuthCode(String telephone, String authCode) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_AUTH_CODE + ":" + telephone;
        redisService.set(key, authCode, REDIS_EXPIRE_AUTH_CODE);
    }

    @Override
    public void setLoginCode(String telephone, String authCode) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_LOGIN_CODE + ":" + telephone;
        redisService.set(key, authCode, REDIS_EXPIRE_LOGIN_CODE);
    }

    @CacheException
    @Override
    public String getAuthCode(String telephone) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_AUTH_CODE + ":" + telephone;
        return (String) redisService.get(key);
    }

    @Override
    public String getLoginCode(String telephone) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_LOGIN_CODE + ":" + telephone;
        return (String) redisService.get(key);
    }

    /**
     * 获取 token
     *
     * @param username
     */
    @Override
    public String getToken(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_TOKEN + ":" + username;
        return (String) redisService.get(key);
    }

    @Override
    public Boolean delToken(String userName) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_TOKEN + ":" + userName;
        Boolean del = redisService.del(key);
        return del;
    }

    /**
     * 设置 token
     *
     * @param username
     * @param token
     */
    @Override
    public void setToken(String token, String username) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_TOKEN + ":" + username;
        redisService.set(key, token, JWT_EXPIRATION);
    }
}
