保誠-保戶業務員媒合平台
wayne
2021-12-29 c1c065fdb4b88062236633004e974f54bf6cd67c
pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -1,7 +1,9 @@
package com.pollex.pam.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pollex.pam.config.ApplicationProperties;
import com.pollex.pam.config.ApplicationProperties.SMS;
import com.pollex.pam.repository.ConsultantRepository;
import com.pollex.pam.service.dto.*;
import com.pollex.pam.service.util.HttpRequestUtil;
import com.pollex.pam.web.rest.errors.SendEmailFailException;
@@ -11,15 +13,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring5.SpringTemplateEngine;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.*;
import java.util.Base64.Encoder;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@Service
public class SendMsgService {
@@ -27,10 +27,18 @@
    private static final Logger log = LoggerFactory.getLogger(SendMsgService.class);
    private final Encoder encoder = Base64.getEncoder();
    private static final String SEND_SMS_SUCCESS_CODE = "1";
    @Autowired
    ApplicationProperties applicationProperties;
    public void sendMsgBySMS(String toMobile, String content) throws SendSMSFailException{
    @Autowired
    ConsultantRepository consultantRepository;
    @Autowired
    SpringTemplateEngine springTemplateEngine;
    public SendSMSResponse sendMsgBySMS(String toMobile, String content) throws SendSMSFailException {
        SMS smsProperties = applicationProperties.getSms();
        SendSMSRequest sendSMSRequest = new SendSMSRequest();
@@ -39,7 +47,7 @@
        sendSMSRequest.setSender(smsProperties.getSender());
        sendSMSRequest.setMsgTypeSet(smsProperties.getSmsType());
        sendSMSRequest.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")));
        sendSMSRequest.setSubject("");
        sendSMSRequest.setSubject(encoder.encodeToString(smsProperties.getSubject().getBytes(StandardCharsets.UTF_8)));
        sendSMSRequest.setActivityId("");
        SMSDetail smsDetail = new SMSDetail();
@@ -50,23 +58,29 @@
        try {
            ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(smsProperties.getUrl(), sendSMSRequest, SendSMSResponse.class);
            SendSMSResponse response = responseEntity.getBody();
            log.debug("response status code = {}", responseEntity.getStatusCode());
            log.debug("smsResponse = {}", responseEntity.getBody());
            // todo 可能需要再補上後續錯誤處理,但要先測通
        }
        catch (Exception e) {
            log.debug("send sms failed!", e);
            throw new SendSMSFailException();
            if(!SEND_SMS_SUCCESS_CODE.equals(response.getReturnCode())) {
                throw new SendSMSFailException("sms service return code = " + response.getReturnCode() + ", error_msg = " + response.getErrorMsg());
            }
            return responseEntity.getBody();
        } catch (SendSMSFailException e) {
            throw e;
        } catch (Exception e) {
            log.warn("send sms fail by other reason!", e);
            throw new SendSMSFailException("send sms fail by other reason!");
        }
    }
    public void sendMsgByEmail(String from, String to, String subject, String content, boolean htmlFormat) throws SendEmailFailException{
        sendMsgByEmail(from, to, subject, content, htmlFormat, Collections.emptyList(), Collections.emptyList());
    public String sendMsgByEmail(String from, String to, String subject, String content, boolean htmlFormat) throws SendEmailFailException{
        return sendMsgByEmail(from, to, subject, content, htmlFormat, Collections.emptyList(), Collections.emptyList());
    }
    public void sendMsgByEmail(
    public String sendMsgByEmail(
        String fromAddress, String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
        List<String> attachments) throws SendEmailFailException
    {
@@ -80,28 +94,30 @@
        sendMailRequest.setHtmlFormat(htmlFormat);
        sendMailRequest.setFunctionId(applicationProperties.getEmail().getFunctionId());
        sendMsgByEmail(sendMailRequest);
        return sendMsgByEmail(sendMailRequest);
    }
    public void sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
    public String sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
        try {
            ResponseEntity<SendMailResponse> responseEntity =
                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, SendMailResponse.class);
            ResponseEntity<String> responseEntity =
                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, String.class);
            log.debug("responseEntity = {}", responseEntity);
            SendMailResponse sendMailResponse = responseEntity.getBody();
            log.debug("response status code = {}", responseEntity.getStatusCode());
            log.debug("emailResponse = {}", responseEntity.getBody());
            String rawResponseString = responseEntity.getBody();
            SendMailResponse sendMailResponse = new ObjectMapper().readValue(rawResponseString, SendMailResponse.class);
            log.debug("sendMailResponse = {}", sendMailResponse);
            if(sendMailResponse == null || sendMailResponse.getData() == null || "ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
                throw new SendEmailFailException();
            if(sendMailResponse == null || sendMailResponse.getData() == null || !"ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
                throw new SendEmailFailException("send email service return error msg! raw response string= " + rawResponseString);
            }
            return responseEntity.getBody();
        }
        catch (SendEmailFailException e) {
            throw e;
        }
        catch (Exception e) {
            log.debug("send email failed!", e);
            throw new SendEmailFailException();
        } catch (Exception e) {
            log.warn("send email fail by other reason", e);
            throw new SendEmailFailException("send email failed!");
        }
    }
}