package com.crm.rely.backend.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.crm.rely.backend.configuration.BaseActiveMqConfiguration;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.entity.system.sms.SysSmsEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.MqSendService;
import com.crm.rely.backend.service.SmsService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service
public class SmsServiceImpl extends BlackListServiceImpl implements SmsService {


    private final static String KEY_PREFIX_CODE = "SMS_VALIDATOR_CODE";

    @Autowired
    private BaseActiveMqConfiguration baseAcitveMqConfiguration;

    @Autowired
    private MqSendService mqSendService;

    public SmsServiceImpl() {
        super("SMS_BLACKLIST_", "SMS_BLACKLIST__BACK_", 60 * 1000L, 3 * 60 * 1000L, 3, 3, 24);
    }

    /**
     * 发送短信
     *
     * @param sysSmsEntity 发送短信实体
     * @throws ServiceException
     */
    @Override
    public void sendSms(SysSmsEntity sysSmsEntity) throws ServiceException {
        sendSms(sysSmsEntity, true);
    }

    @Override
    public void sendSms(SysSmsEntity sysSmsEntity, boolean validate) throws ServiceException {
        if (validate) {
            validate(sysSmsEntity.getReceive(), sysSmsEntity.getSmsSendEnum().getCode(), sysSmsEntity.getAddIp());
        }
        send(sysSmsEntity);
    }

    /**
     * 发送短信验证码并保存到redis 默认过期时间为Constants.SMS_VALIDATECODE_TIME五分钟
     *
     * @param sysSmsEntity 发送短信实体
     * @param code         验证码
     * @throws ServiceException
     */
    @Override
    public void sendCode(SysSmsEntity sysSmsEntity, String code) throws ServiceException {
        sendCode(sysSmsEntity, code, true);
    }

    @Override
    public void sendCode(SysSmsEntity sysSmsEntity, String code, boolean validate) throws ServiceException {
        //默认过期时间为五分钟
        sendCode(sysSmsEntity, code, validate,Constants.SMS_VALIDATECODE_TIME * 60 * 1000);
    }

    /**
     * 发送短信验证码并保存到redis 过期时间为time 毫秒
     *
     * @param sysSmsEntity 发送短信实体
     * @param code         验证码
     * @param time         过期时间
     * @throws ServiceException
     */
    @Override
    public void sendCode(SysSmsEntity sysSmsEntity, String code, long time) throws ServiceException {
        sendCode(sysSmsEntity, code, true, time);
    }

    @Override
    public void sendCode(SysSmsEntity sysSmsEntity, String code, boolean validate, long time) throws ServiceException {
        if (validate) {
            validate(sysSmsEntity.getReceive(), sysSmsEntity.getSmsSendEnum().getCode(), sysSmsEntity.getAddIp());
        }

        String key = KEY_PREFIX_CODE + sysSmsEntity.getReceive();
        redisService.save(key, code, time);

        send(sysSmsEntity);
    }

    /**
     * 验证code
     *
     * @param phone
     * @param code
     * @throws ServiceException
     */
    @Override
    public void validateCode(String phone, String code) throws ServiceException {

        String key = KEY_PREFIX_CODE + phone;
        //获取原来的验证码
        String oldCode = redisService.getObject(key);
        //判断验证码是否过期 验证码是否正确
        if (Strings.isNullOrEmpty(oldCode) || Strings.isNullOrEmpty(code) || !oldCode.equals(code)) {
            throw new ServiceException(Constants.PHONE_CODE_ERROR);
        }
        //移除验证码
        redisService.remove(key);
    }

    /**
     * 发送短信
     *
     * @param sysSmsEntity
     * @throws ServiceException
     */
    private void send(SysSmsEntity sysSmsEntity) throws ServiceException {
        String content = JSON.toJSONString(sysSmsEntity, SerializerFeature.WriteMapNullValue);
        mqSendService.send(baseAcitveMqConfiguration.smsQueue(), content);
    }


}
