From ea43a98d7943fb440ce3c1bfe3aca8555866864a Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期五, 17 十二月 2021 15:30:22 +0800
Subject: [PATCH] [add][todo 132507] 新增發送email service

---
 pamapi/src/main/resources/config/application-dev.yml                            |    3 
 pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java           |   28 +++-
 pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java                 |   81 +++++++++++++---
 pamapi/src/main/java/com/pollex/pam/service/dto/SendMailRequest.java            |  103 ++++++++++++++++++++
 pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendSMSFailException.java   |    8 +
 pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java           |   30 ++++++
 pamapi/src/main/java/com/pollex/pam/service/dto/SendMailResponse.java           |   25 +++++
 pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendEmailFailException.java |    9 +
 pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java           |    4 
 9 files changed, 265 insertions(+), 26 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java b/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
index 8f9876a..3841d13 100644
--- a/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
+++ b/pamapi/src/main/java/com/pollex/pam/config/ApplicationProperties.java
@@ -19,6 +19,7 @@
     private String eServiceLoginFunc;
     private String eServiceLoginSys;
     private SMS sms;
+    private Email email;
 
     public boolean isMockLogin() {
         return mockLogin;
@@ -84,6 +85,14 @@
         this.sms = sms;
     }
 
+    public Email getEmail() {
+        return email;
+    }
+
+    public void setEmail(Email email) {
+        this.email = email;
+    }
+
     public static class SMS {
         private String url;
         private String sourceCode;
@@ -122,4 +131,25 @@
             this.smsType = smsType;
         }
     }
+
+    public static class Email {
+        private String url;
+        private String functionId;
+
+        public String getUrl() {
+            return url;
+        }
+
+        public void setUrl(String url) {
+            this.url = url;
+        }
+
+        public String getFunctionId() {
+            return functionId;
+        }
+
+        public void setFunctionId(String functionId) {
+            this.functionId = functionId;
+        }
+    }
 }
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 5280732..6bac287 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -1,46 +1,44 @@
 package com.pollex.pam.service;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.pollex.pam.config.ApplicationProperties;
-import com.pollex.pam.service.dto.SMSDetail;
-import com.pollex.pam.service.dto.SendSMSRequest;
-import com.pollex.pam.service.dto.SendSMSResponse;
+import com.pollex.pam.config.ApplicationProperties.Email;
+import com.pollex.pam.config.ApplicationProperties.SMS;
+import com.pollex.pam.service.dto.*;
 import com.pollex.pam.service.util.HttpRequestUtil;
+import com.pollex.pam.web.rest.errors.SendEmailFailException;
+import com.pollex.pam.web.rest.errors.SendSMSFailException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 import java.nio.charset.StandardCharsets;
-import java.security.KeyManagementException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Base64;
 import java.util.Base64.Encoder;
 import java.util.Collections;
+import java.util.List;
 import java.util.UUID;
 
 @Service
 public class SendMsgService {
 
-    private final static Logger log = LoggerFactory.getLogger(SendMsgService.class);
+    private static final Logger log = LoggerFactory.getLogger(SendMsgService.class);
     private final Encoder encoder = Base64.getEncoder();
 
     @Autowired
     ApplicationProperties applicationProperties;
 
-    public SendSMSResponse sendMsgBySMS(String subject, String toMobile, String content) throws Exception{
-        final String SMS_URL = applicationProperties.getSms().getUrl();
+    public void sendMsgBySMS(String subject, String toMobile, String content) throws SendSMSFailException{
+        SMS smsProperties = applicationProperties.getSms();
 
         SendSMSRequest sendSMSRequest = new SendSMSRequest();
         sendSMSRequest.setpKey(UUID.randomUUID().toString());
-        sendSMSRequest.setSourceCode(applicationProperties.getSms().getSourceCode());
-        sendSMSRequest.setSender(applicationProperties.getSms().getSender());
-        sendSMSRequest.setMsgTypeSet(applicationProperties.getSms().getSmsType());
+        sendSMSRequest.setSourceCode(smsProperties.getSourceCode());
+        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.setActivityId("");
@@ -51,9 +49,60 @@
 
         sendSMSRequest.setDetail(Collections.singletonList(smsDetail));
 
-        ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(SMS_URL, sendSMSRequest, SendSMSResponse.class);
-        return responseEntity.getBody();
+        try {
+            ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(smsProperties.getUrl(), sendSMSRequest, SendSMSResponse.class);
+
+            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();
+        }
     }
 
+    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 void sendMsgByEmail(
+        String fromAddress, String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
+        List<String> attachments) throws SendEmailFailException
+    {
+        SendMailRequest sendMailRequest = new SendMailRequest();
+        sendMailRequest.setSendMailAddresses(Collections.singletonList(toAddress));
+        sendMailRequest.setFrom(fromAddress);
+        sendMailRequest.setContent(content);
+        sendMailRequest.setSubject(subject);
+        sendMailRequest.setSendCCMailAddresses(toCCAddress);
+        sendMailRequest.setAttachments(attachments);
+        sendMailRequest.setHtmlFormat(htmlFormat);
+        sendMailRequest.setFunctionId(applicationProperties.getEmail().getFunctionId());
+
+        sendMsgByEmail(sendMailRequest);
+    }
+
+    public void sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
+        try {
+            ResponseEntity<SendMailResponse> responseEntity =
+                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, SendMailResponse.class);
+
+            SendMailResponse sendMailResponse = responseEntity.getBody();
+            log.debug("response status code = {}", responseEntity.getStatusCode());
+            log.debug("emailResponse = {}", responseEntity.getBody());
+
+            if(sendMailResponse == null || sendMailResponse.getData() == null || "ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
+                throw new SendEmailFailException();
+            }
+        }
+        catch (SendEmailFailException e) {
+            throw e;
+        }
+        catch (Exception e) {
+            log.debug("send email failed!", e);
+            throw new SendEmailFailException();
+        }
+    }
 }
diff --git a/pamapi/src/main/java/com/pollex/pam/service/dto/SendMailRequest.java b/pamapi/src/main/java/com/pollex/pam/service/dto/SendMailRequest.java
new file mode 100644
index 0000000..2470a67
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/service/dto/SendMailRequest.java
@@ -0,0 +1,103 @@
+package com.pollex.pam.service.dto;
+
+import java.util.List;
+
+public class SendMailRequest {
+
+    /**
+     * �隞嗉��
+     */
+    private List<String> sendMailAddresses;
+    /**
+     * ��
+     */
+    private List<String> sendCCMailAddresses;
+    /**
+     * 銝駁��
+     */
+    private String subject;
+    /**
+     * ����
+     */
+    private String content;
+    /**
+     * 撖辣��
+     */
+    private String from;
+    /**
+     * ��辣頝臬��
+     */
+    private List<String> attachments;
+    /**
+     * �� html format
+     */
+    private boolean htmlFormat;
+    /**
+     * �摰儔閮���� (������������神甇餃�pos)
+     */
+    private String functionId;
+
+    public List<String> getSendMailAddresses() {
+        return sendMailAddresses;
+    }
+
+    public void setSendMailAddresses(List<String> sendMailAddresses) {
+        this.sendMailAddresses = sendMailAddresses;
+    }
+
+    public List<String> getSendCCMailAddresses() {
+        return sendCCMailAddresses;
+    }
+
+    public void setSendCCMailAddresses(List<String> sendCCMailAddresses) {
+        this.sendCCMailAddresses = sendCCMailAddresses;
+    }
+
+    public String getSubject() {
+        return subject;
+    }
+
+    public void setSubject(String subject) {
+        this.subject = subject;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public String getFrom() {
+        return from;
+    }
+
+    public void setFrom(String from) {
+        this.from = from;
+    }
+
+    public List<String> getAttachments() {
+        return attachments;
+    }
+
+    public void setAttachments(List<String> attachments) {
+        this.attachments = attachments;
+    }
+
+    public boolean isHtmlFormat() {
+        return htmlFormat;
+    }
+
+    public void setHtmlFormat(boolean htmlFormat) {
+        this.htmlFormat = htmlFormat;
+    }
+
+    public String getFunctionId() {
+        return functionId;
+    }
+
+    public void setFunctionId(String functionId) {
+        this.functionId = functionId;
+    }
+}
diff --git a/pamapi/src/main/java/com/pollex/pam/service/dto/SendMailResponse.java b/pamapi/src/main/java/com/pollex/pam/service/dto/SendMailResponse.java
new file mode 100644
index 0000000..bc29f12
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/service/dto/SendMailResponse.java
@@ -0,0 +1,25 @@
+package com.pollex.pam.service.dto;
+
+public class SendMailResponse {
+    private Data data;
+
+    public Data getData() {
+        return data;
+    }
+
+    public void setData(Data data) {
+        this.data = data;
+    }
+
+    public static class Data {
+        private String messageStatus;
+
+        public String getMessageStatus() {
+            return messageStatus;
+        }
+
+        public void setMessageStatus(String messageStatus) {
+            this.messageStatus = messageStatus;
+        }
+    }
+}
diff --git a/pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java b/pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java
index f277b6b..48cc43b 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/util/HttpRequestUtil.java
@@ -68,7 +68,7 @@
         throws JsonProcessingException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
 
         String parameters = new ObjectMapper().writeValueAsString(jsonData);
-        //憒��葆Base64,debug��靘������
+        // ���摨阡��脰��log����
         if(parameters.length() < 1000){
             log.debug("parameters : {}",parameters);
         }
@@ -80,6 +80,8 @@
         setHeaders(headers, addedHeaders);
 
         HttpEntity<String> entity = new HttpEntity<>(parameters, headers);
+
+        log.debug("rest post with json, url = {}", url);
         return restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
     }
 
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java b/pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java
index 4a47d67..ab40016 100644
--- a/pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/TestSendMsgResource.java
@@ -1,23 +1,33 @@
 package com.pollex.pam.web.rest;
 
+import com.pollex.pam.service.SendMsgService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 @Deprecated
 @RestController
 @RequestMapping("/api/test/sendMsg")
 public class TestSendMsgResource {
 
-    @PostMapping("/bySMS")
-    public ResponseEntity<Void> bySMS(@RequestBody Object param) {
+    @Autowired
+    SendMsgService sendMsgService;
 
+    @GetMapping("/bySMS")
+    public ResponseEntity<Void> bySMS(@RequestParam String subject, @RequestParam String toMobile, @RequestParam String content) {
+        sendMsgService.sendMsgBySMS(subject, toMobile, content);
+        return ResponseEntity.noContent().build();
     }
 
-    @PostMapping("/byEmail")
-    public ResponseEntity<Void> byEmail(@RequestBody Object param) {
-
+    @GetMapping("/byEmail")
+    public ResponseEntity<Void> byEmail(
+        @RequestParam String from,
+        @RequestParam String to,
+        @RequestParam String subject,
+        @RequestParam String content,
+        @RequestParam boolean htmlFormat
+    ) {
+        sendMsgService.sendMsgByEmail(from, to, subject, content, htmlFormat);
+        return ResponseEntity.noContent().build();
     }
 }
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendEmailFailException.java b/pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendEmailFailException.java
new file mode 100644
index 0000000..209e562
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendEmailFailException.java
@@ -0,0 +1,9 @@
+package com.pollex.pam.web.rest.errors;
+
+public class SendEmailFailException extends RuntimeException {
+    public SendEmailFailException() {
+    }
+    public SendEmailFailException(String message) {
+        super(message);
+    }
+}
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendSMSFailException.java b/pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendSMSFailException.java
new file mode 100644
index 0000000..688751d
--- /dev/null
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/errors/SendSMSFailException.java
@@ -0,0 +1,8 @@
+package com.pollex.pam.web.rest.errors;
+
+public class SendSMSFailException extends RuntimeException{
+    public SendSMSFailException(){}
+    public SendSMSFailException(String message) {
+        super(message);
+    }
+}
diff --git a/pamapi/src/main/resources/config/application-dev.yml b/pamapi/src/main/resources/config/application-dev.yml
index 5c2c5d0..0239098 100644
--- a/pamapi/src/main/resources/config/application-dev.yml
+++ b/pamapi/src/main/resources/config/application-dev.yml
@@ -124,3 +124,6 @@
     source-code: ePos
     sender: POS
     sms-type: 0017
+  email:
+    url: https://vtwlifeopensysuat.pru.intranet.asia/tsgw/mq/mqSendMail
+    function-id: epos

--
Gitblit v1.8.0