From a3716f72066d25d745f4d5103ff23a553c3e102b Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期四, 17 二月 2022 11:41:19 +0800
Subject: [PATCH] Merge branch 'sit' into uat

---
 pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java |  128 ++++++++++++++++++++++++++++++++----------
 1 files changed, 96 insertions(+), 32 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
index 6bac287..a5be71e 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -1,8 +1,12 @@
 package com.pollex.pam.service;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.pollex.pam.config.ApplicationProperties;
 import com.pollex.pam.config.ApplicationProperties.Email;
 import com.pollex.pam.config.ApplicationProperties.SMS;
+import com.pollex.pam.config.Constants;
+import com.pollex.pam.enums.SendEmailMsgMethod;
+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;
@@ -10,17 +14,18 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.Profiles;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
+import org.thymeleaf.spring5.SpringTemplateEngine;
+import tech.jhipster.config.JHipsterConstants;
 
 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 {
@@ -28,11 +33,30 @@
     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 subject, String toMobile, String content) throws SendSMSFailException{
+    @Autowired
+    ConsultantRepository consultantRepository;
+
+    @Autowired
+    SpringTemplateEngine springTemplateEngine;
+
+    @Autowired
+    Environment environment;
+
+    @Autowired
+    MailService mailService;
+
+    public SendSMSResponse sendMsgBySMS(String toMobile, String content) throws SendSMSFailException {
+
         SMS smsProperties = applicationProperties.getSms();
+        if(!smsProperties.isSendNotifyMsg()) {
+//    		return getMockSMSResponse();
+            return null;
+        }
 
         SendSMSRequest sendSMSRequest = new SendSMSRequest();
         sendSMSRequest.setpKey(UUID.randomUUID().toString());
@@ -40,7 +64,7 @@
         sendSMSRequest.setSender(smsProperties.getSender());
         sendSMSRequest.setMsgTypeSet(smsProperties.getSmsType());
         sendSMSRequest.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")));
-        sendSMSRequest.setSubject(subject);
+        sendSMSRequest.setSubject(encoder.encodeToString(smsProperties.getSubject().getBytes(StandardCharsets.UTF_8)));
         sendSMSRequest.setActivityId("");
 
         SMSDetail smsDetail = new SMSDetail();
@@ -51,26 +75,38 @@
 
         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());
+//    private SendSMSResponse getMockSMSResponse() {
+//    	SendSMSResponse mock = new SendSMSResponse();
+//    	mock.set
+//		return null;
+//	}
+
+	public String sendMsgByEmail(String to, String subject, String content, boolean htmlFormat) throws SendEmailFailException{
+        return sendMsgByEmail(to, subject, content, htmlFormat, Collections.emptyList(), Collections.emptyList());
     }
 
-    public void sendMsgByEmail(
-        String fromAddress, String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
-        List<String> attachments) throws SendEmailFailException
-    {
+    public String sendMsgByEmail(String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
+        List<String> attachments) throws SendEmailFailException {
+    	String fromAddress = applicationProperties.getEmail().getSenderEmail();
+
         SendMailRequest sendMailRequest = new SendMailRequest();
         sendMailRequest.setSendMailAddresses(Collections.singletonList(toAddress));
         sendMailRequest.setFrom(fromAddress);
@@ -81,28 +117,56 @@
         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{
+        final Email emailProperties = applicationProperties.getEmail();
+
+        if(!emailProperties.isSendNotifyMsg()) {
+            return null;
+        }
+
+        if(emailProperties.getMethod() == SendEmailMsgMethod.POLLEX_GMAIL) {
+            return sendMsgByPollexGmail(sendMailRequest);
+        }
+        else if(emailProperties.getMethod() == SendEmailMsgMethod.PAM_EMAIL_SERVICE) {
+            return sendMsgByPamEmailService(sendMailRequest);
+        }
+
+        return null;
+    }
+
+    private String sendMsgByPollexGmail(SendMailRequest sendMailRequest) {
+        String subject = sendMailRequest.getSubject();
+        String content = sendMailRequest.getContent();
+        boolean isHtml = sendMailRequest.isHtmlFormat();
+        sendMailRequest.getSendMailAddresses().forEach(receiver -> mailService.sendEmail(receiver, subject, content, false, isHtml));
+
+        return null;
+    }
+
+    private String sendMsgByPamEmailService(SendMailRequest sendMailRequest) {
+        final Email emailProperties = applicationProperties.getEmail();
         try {
-            ResponseEntity<SendMailResponse> responseEntity =
-                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, SendMailResponse.class);
+            ResponseEntity<String> responseEntity =
+                HttpRequestUtil.postWithJson(emailProperties.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);
             }
-        }
-        catch (SendEmailFailException e) {
+
+            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!");
         }
     }
 }

--
Gitblit v1.8.0