package com.yuchen.saas.park.api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.yuchen.saas.park.api.dto.people.PmgBatParkPeopleQryDto;
import lombok.extern.slf4j.Slf4j;

/**
 * @description:
 * @author: 洪铭铭
 * @time: 2024/6/4 16:17
 */
@Slf4j
public class SmsTool {



    private static final String signName = "厦门火炬智慧平台";

    /**
     * 忘记密码发送验证码
     */
    private static final String FORGOT_PASSWORD_CODE_TEMPLATE = "SMS_461580420";


    private static final com.aliyun.dysmsapi20170525.Client client =
            SmsTool.createClient("LTAI5tHiPLsbT1fisuHLu7cp", "6wb3x55Fh5Qd32qvDaZu9F6evtfWAl");

    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId,
                                                                  String accessKeySecret) {
        try {
            Config config = new Config();
            config.accessKeyId = accessKeyId;
            config.accessKeySecret = accessKeySecret;
            config.endpoint = "192.168.0.230:550";
            config.protocol = "HTTP";
            return new com.aliyun.dysmsapi20170525.Client(config);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("发送短信配置信息错误: {}", e.getMessage());
        }
        return null;
    }



    /**
     * @param phone
     * @param dto
     * @description: 给业主发确认短信
     * @return: void
     * @author: 洪铭铭
     * @time: 2024/6/3 15:43
     */
    public static void sendOwnerConfirmMsg(String phone, PmgBatParkPeopleQryDto dto) {
        // 短信模版
        String templateCode = FORGOT_PASSWORD_CODE_TEMPLATE;
        // 1.发送短信
        JSONObject object = new JSONObject();
        object.put("name", dto.getName());
        object.put("phone", dto.getPhone());
        object.put("idCard", dto.getIdCard());
        object.put("roomName", dto.getRoomId());
        object.put("peopleType", dto.getPeopleType());
        SendSmsRequest sendReq = null;
        try {
            sendReq = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(signName)
                    .setTemplateCode(templateCode)
                    .setTemplateParam(JSON.toJSONString(object));
            System.out.println(client._protocol);
            SendSmsResponse sendResp = client.sendSms(sendReq);
            String responseCode = sendResp.body.code;
            if (responseCode.equals("OK")) {
                log.info("验证码登录功能短信通知,调用阿里短信接口成功 {} {}", phone, templateCode);
            } else {
                log.error("验证码登录功能短信通知,调用阿里短信接口失败 {} {} {}", phone, templateCode, JSON.toJSONString(sendResp.body));
            }
        } catch (Exception e) {
            log.error("验证码登录功能短信通知,调用阿里短信接口失败 {}", phone, e);
        }

    }


}
