保誠-保戶業務員媒合平台
wayne
2021-12-24 49e325f40f9721521de975fdd5f3d37f6fe78852
pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -45,83 +45,6 @@
    @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 = "親愛的顧問您好,您有一筆來自保誠媒合平台的新預約單,該客戶手機號碼為" + 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 = "親愛的顧問您好,您有一筆來自保誠媒合平台的新預約單,該客戶手機號碼為" + 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();
@@ -131,7 +54,7 @@
        sendSMSRequest.setSender(smsProperties.getSender());
        sendSMSRequest.setMsgTypeSet(smsProperties.getSmsType());
        sendSMSRequest.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")));
        sendSMSRequest.setSubject(smsProperties.getSubject());
        sendSMSRequest.setSubject(encoder.encodeToString(smsProperties.getSubject().getBytes(StandardCharsets.UTF_8)));
        sendSMSRequest.setActivityId("");
        SMSDetail smsDetail = new SMSDetail();
@@ -194,7 +117,7 @@
            SendMailResponse sendMailResponse = new ObjectMapper().readValue(responseEntity.getBody(), SendMailResponse.class);
            log.debug("sendMailResponse = {}", sendMailResponse);
//            if(sendMailResponse == null || sendMailResponse.getData() == null || "ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
//            if(sendMailResponse == null || sendMailResponse.getData() == null || !"ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
//                throw new SendEmailFailException();
//            }