package com.cf.imes.module.system.service.sms.handler.impl;

import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.core.KeyValue;
import com.cf.imes.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
import com.cf.imes.module.system.dal.redis.RedisKeyConstants;
import com.cf.imes.module.system.enums.sms.SmsTemplateTypeEnum;
import com.cf.imes.module.system.framework.sms.config.SmsCodeProperties;
import com.cf.imes.module.system.service.sms.handler.SmsSendAfterSendHandler;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * @author Gqr
 * @since 2024/8/12 18:49
 */
@Service
public class SmsSendAfterSendHandlerImpl implements SmsSendAfterSendHandler {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SmsCodeProperties smsCodeProperties;

    @Override
    public boolean checkTemplateType(Integer templateType) {
        SmsTemplateTypeEnum smsTemplateTypeEnum = SmsTemplateTypeEnum.valueOf(templateType);
        return ObjectUtil.isNotNull(smsTemplateTypeEnum);
    }

    @Override
    public void afterSend(SmsSendRespDTO sendResponse, List<KeyValue<String, Object>> params) {
        // 发送成功把验证码存入redis
        if (sendResponse.getSuccess()) {
            for (KeyValue<String, Object> keyValue : params) {
                if ("code".equals(keyValue.getKey())) {
                    redisTemplate.opsForValue().set(String.format(RedisKeyConstants.SMS_CAPTCHA_VERIFICATION, sendResponse.getMobile()), keyValue.getValue(), smsCodeProperties.getExpireTimes());
                }
            }
        }
    }
}
