package com.ningdatech.pmapi.sms.helper;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ningdatech.cache.repository.CachePlusOps;
import com.ningdatech.pmapi.sms.constant.VerificationCodeType;
import com.ningdatech.pmapi.sms.model.dto.VerifyCodeCacheDTO;
import com.ningdatech.pmapi.sms.utils.SmsRedisKeyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author liuxinxin
 * @date 2022/8/16 上午11:43
 */
@Component
@RequiredArgsConstructor
public class VerifyCodeCheckHelper {

    private final CachePlusOps cachePlusOps;

    /**
     * 对某种类型的验证码进行校验
     * @param type
     * @param mobile
     * @param verificationCode
     * @return
     */
    public boolean verification(VerificationCodeType type, String mobile, String verificationCode) {
        if (StringUtils.isBlank(mobile) || Objects.isNull(type)) {
            return false;
        }
        String key = SmsRedisKeyUtils.smsCodeVerifyKey(type, mobile);
        Object cacheObj = cachePlusOps.get(key);
        if (Objects.isNull(cacheObj)) {
            return false;
        }
        VerifyCodeCacheDTO cache = (VerifyCodeCacheDTO) cacheObj;
        return verificationCode.trim().equals(cache.getCode());
    }

}
