From bdae23a40c461c2c6b6ee614f661eac731c949c8 Mon Sep 17 00:00:00 2001 From: Mila <Mila@pollex.com.tw> Date: 星期三, 22 十二月 2021 14:12:05 +0800 Subject: [PATCH] Merge branch 'master' of https://192.168.0.10:8443/r/pcalife/PAM --- pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java | 136 ++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 122 insertions(+), 14 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 650fbac..eb758f6 100644 --- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java +++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java @@ -1,16 +1,23 @@ 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.domain.Appointment; +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; import com.pollex.pam.web.rest.errors.SendSMSFailException; +import io.jsonwebtoken.lang.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.thymeleaf.context.Context; +import org.thymeleaf.spring5.SpringTemplateEngine; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; @@ -27,10 +34,95 @@ private static final Logger log = LoggerFactory.getLogger(SendMsgService.class); private final Encoder encoder = Base64.getEncoder(); + private static final String EMAIL_SUBJECT = "靽���像�蝟餌絞�嚗���"; + @Autowired ApplicationProperties applicationProperties; - public void sendMsgBySMS(String toMobile, String content) throws SendSMSFailException{ + @Autowired + ConsultantRepository consultantRepository; + + @Autowired + SpringTemplateEngine springTemplateEngine; + + public void sendAppointmentNotify(Appointment appointment) { + Assert.notNull(appointment); + + log.debug("is need sending notify msg = {}", applicationProperties.isSendNotifyMsg()); + if(applicationProperties.isSendNotifyMsg()) { + log.debug("sending appointment notify, appointmentId = {}", appointment.getId()); + sendAppointmentNotifyBySMS(appointment); + sendAppointmentNotifyByEmail(appointment); + + // todo ��蝣箄�������瘙蝙�html mail + // sendAppointmentNotifyByHtmlEmail(appointment); + } + } + + private void sendAppointmentNotifyBySMS(Appointment appointment) throws SendSMSFailException { + String msg = getAppointmentNotifyWording(appointment); + String consultantMobile = consultantRepository.findOneByAgentNo(appointment.getAgentNo()).get().getPhoneNumber(); + + try { + sendMsgBySMS(consultantMobile, msg); + } catch (SendSMSFailException e) { + log.debug("send sms failed, appointment Id = {}", appointment.getId(), e); + } + } + + private void sendAppointmentNotifyByEmail(Appointment appointment) { + // todo ��敺靽�頂蝯勗�辣靽∠拳 (銝行�閮剖��葉) + String senderEmail = applicationProperties.getEmail().getSenderEmail(); + String consultantEmail = consultantRepository.findOneByAgentNo(appointment.getAgentNo()).get().getEmail(); + String content = getAppointmentNotifyWording(appointment); + + try { + sendMsgByEmail(senderEmail, consultantEmail, EMAIL_SUBJECT, content, false); + } catch (SendEmailFailException e) { + log.debug("send email failed, appointment Id = {}", appointment.getId(), e); + } + } + + private void sendAppointmentNotifyByHtmlEmail(Appointment appointment) { + // todo ��敺靽�頂蝯勗�辣靽∠拳 (銝行�閮剖��葉) + String senderEmail = applicationProperties.getEmail().getSenderEmail(); + String consultantEmail = consultantRepository.findOneByAgentNo(appointment.getAgentNo()).get().getEmail(); + String customerMobile = appointment.getPhone(); + String normalContent; + + if(StringUtils.hasText(customerMobile)) { + normalContent = "閬芣��“��憟踝����蝑�靽���像������嚗府摰X����Ⅳ�" + customerMobile + "\n"; + } + else { + normalContent = "閬芣��“��憟踝����蝑�靽���像������\n"; + } + + Context context = new Context(); + context.setVariable("content", normalContent); + context.setVariable("urlHint", getAppointmentDetailUrl(appointment.getId())); + String content = springTemplateEngine.process("mail/appointmentNotifyEmail", context); + + try { + sendMsgByEmail(senderEmail, consultantEmail, EMAIL_SUBJECT, content, true); + } catch (SendEmailFailException e) { + log.debug("send email failed, appointment Id = {}", appointment.getId(), e); + } + } + + private String getAppointmentNotifyWording(Appointment appointment) { + String normalContent; + if(StringUtils.hasText(appointment.getPhone())) { + normalContent = "閬芣��“��憟踝����蝑�靽���像������嚗府摰X����Ⅳ�" + appointment.getPhone() + "\n"; + } + else { + normalContent = "閬芣��“��憟踝����蝑�靽���像������\n"; + } + + String urlContent = "暺�雯��嚗�" + getAppointmentDetailUrl(appointment.getId()) + " �����像������"; + return normalContent + urlContent; + } + + public SendSMSResponse sendMsgBySMS(String toMobile, String content) throws SendSMSFailException { SMS smsProperties = applicationProperties.getSms(); SendSMSRequest sendSMSRequest = new SendSMSRequest(); @@ -54,6 +146,7 @@ log.debug("response status code = {}", responseEntity.getStatusCode()); log.debug("smsResponse = {}", responseEntity.getBody()); + return responseEntity.getBody(); // todo ����閬�����隤方������葫�� } catch (Exception e) { @@ -62,11 +155,20 @@ } } - 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 sendMsgByHtmlTestTemplateEmail(String from, String to) { + Context context = new Context(); + context.setVariable("content", "閬芣��“��憟踝����蝑�靽���像������\n"); + context.setVariable("urlHint", getAppointmentDetailUrl(0L)); + + String content = springTemplateEngine.process("mail/appointmentNotifyEmail", context); + return sendMsgByEmail(from, to, EMAIL_SUBJECT, content, true); } - public void sendMsgByEmail( + 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 String sendMsgByEmail( String fromAddress, String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress, List<String> attachments) throws SendEmailFailException { @@ -80,21 +182,23 @@ sendMailRequest.setHtmlFormat(htmlFormat); sendMailRequest.setFunctionId(applicationProperties.getEmail().getFunctionId()); - sendMsgByEmail(sendMailRequest); + return sendMsgByEmail(sendMailRequest); } - public void sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{ + private 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()); + SendMailResponse sendMailResponse = new ObjectMapper().readValue(responseEntity.getBody(), 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(); +// } + + return responseEntity.getBody(); } catch (SendEmailFailException e) { throw e; @@ -104,4 +208,8 @@ throw new SendEmailFailException(); } } + + private String getAppointmentDetailUrl(Long appointmentId) { + return applicationProperties.getFrontEndDomain() + "/myAppointmentList/contactedList?appointmentId=" + appointmentId; + } } -- Gitblit v1.8.0