package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.mns.model.Message;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SendSmsUtils;
import com.ruoyi.system.service.AlicomMessageListenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 实现接听类
 *
 * @author tjf
 * @Date: 2021/12/28/9:44
 */
@Service
public class AlicomMessageListenterServiceImpl implements AlicomMessageListenterService {

    @Autowired
    private RedisCache redisCache;

    //短信参数
    @Value("${sms_aliyun_accessKeyId}")
    String accessKeyId;// accessKeyId
    @Value("${sms_aliyun_accessKeySecret}")
    String accessKeySecret;// accessKeySecret
    @Value("${sms_aliyun_signNameNum}")
    String signNameNum;
    @Value("${sms_aliyun_templateCodeNum}")
    String templateCodeNum;

    @SuppressWarnings("unchecked")
    @Override
    public boolean dealMessage(Message message) {
        try {
            System.out.println("收到短信回复");
            Map<String, Object> contentMap = JSON.parseObject(message.getMessageBodyAsString(), HashMap.class);
            //获取用户回复的内容
            String content = (String) contentMap.get("content");
            String phoneNumber = (String) contentMap.get("phone_number");
            //TODO 这里开始编写您的业务代码
            //首先判断回复的是不是Y
            if ("Y".equals(content)) {
                //去redis中查看是否是查询码的回复
                String verifyKey = UserConstants.QUERY_NUM_CREATE + phoneNumber;
                Object phone = redisCache.getCacheObject(verifyKey);
                if (phone != null){
                    System.out.println("发送查询码短信开始");
                    redisCache.deleteObject(verifyKey);
                    //生成查询码
                    String code = SendSmsUtils.getCode(4);
                    SendSmsResponse sendSmsResponse = SendSmsUtils.sendSms(phone.toString(), "{\"code\":\"" + code + "\"}", accessKeyId, accessKeySecret, signNameNum, templateCodeNum);
                    QuerySendDetailsResponse querySendDetailsResponse = SendSmsUtils.querySendDetails(sendSmsResponse.getBizId(), phone.toString(),accessKeyId,accessKeySecret);
                    String ok = querySendDetailsResponse.getCode();
                    if ("OK".equals(ok)){
                        //设置查询码10分钟有效期
                        redisCache.setCacheObject(UserConstants.QUERY_NUM_KEY + phone,code,10, TimeUnit.MINUTES);
                        //删除询问查询码key
                        redisCache.deleteObject(verifyKey);
                    }
                }
            }
            System.out.println("短信回复结束");
        } catch (com.google.gson.JsonSyntaxException e) {
            return true;
        } catch (Throwable e) {
            //您自己的代码部分导致的异常，应该return false,这样消息不会被delete掉，而会根据策略进行重推
            return false;
        }
        //消息处理成功，返回true, SDK将调用MNS的delete方法将消息从队列中删除掉
        return true;
    }
}
