package com.backendsys.modules.sdk.tencentcloud.ems.service.impl;


import com.backendsys.exception.CustException;
import com.backendsys.modules.sdk.tencentcloud.ems.service.TencentEmsService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ses.v20201002.SesClient;
import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
import com.tencentcloudapi.ses.v20201002.models.Template;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class TencentEmsServiceImpl implements TencentEmsService {
    @Value("${tencent.ems.secret-id}")
    private String SECRET_ID;

    @Value("${tencent.ems.secret-key}")
    private String SECRET_KEY;

    @Value("${tencent.ems.http-profile}")
    private String HPF;

    @Value("${tencent.ems.ses-client}")
    private String SES_CLIENT;

    @Value("${tencent.ems.from-email-address}")
    private String FROM_EMAIL_ADDRESS;

    @Override
    public SendEmailResponse send(String title, Long template_id, String templateParamSet, String email) {

        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(HPF);
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SesClient client = new SesClient(cred, SES_CLIENT, clientProfile);

        SendEmailRequest req = new SendEmailRequest();
        req.setFromEmailAddress(FROM_EMAIL_ADDRESS);

        String[] destination = {email};
        req.setDestination(destination);
        req.setSubject(title);
        Template template = new Template();
        template.setTemplateID(template_id);

        //设置参数模板
        template.setTemplateData(templateParamSet);
        req.setTemplate(template);

        try {
            SendEmailResponse resp = client.SendEmail(req);
            return resp;
        } catch (TencentCloudSDKException e) {
            throw new CustException(e.getMessage());
        }

    }
}



