package com.hzs.member.sms.service.impl;

import com.hzs.common.core.config.BdConfig;
import com.hzs.common.core.constant.msg.SystemMsgConstants;
import com.hzs.common.core.enums.EEnv;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.sms.service.IApiSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 短信处理服务
 */
@Slf4j
@Service
public class ApiSmsServiceImpl implements IApiSmsService {

    @Autowired
    private RedisService redisService;

    @Override
    public String checkSms(String cacheKey, String code) {
        if (EEnv.TEST.getValue().equals(BdConfig.getEnv()) && "ASDF".equals(code)) {
            // 测试环境暂时不发放短信
            return null;
        }
        Object codeObj = redisService.getCacheObject(cacheKey);
        if (null != codeObj) {
            if (codeObj.toString().equals(code)) {
                // 校验正确之后，删除缓存
                redisService.deleteObject(cacheKey);
                return null;
            } else {
                // 验证码错误
                return TransactionUtils.getContent(SystemMsgConstants.CODE_ERROR);
            }
        }
        // 验证码已失效
        return TransactionUtils.getContent(SystemMsgConstants.CODE_EXPIRE);
    }

}
