From fe99ba1caaebaa40c33ad1e3f1ee2c4dfd012795 Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期一, 24 一月 2022 12:20:24 +0800
Subject: [PATCH] Merge branch 'Phase3' of https://192.168.0.10:8443/r/pcalife/PAM into Phase3

---
 pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java |   90 +++++++++++++++-----------------------------
 1 files changed, 31 insertions(+), 59 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java b/pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java
index 60bb60a..57324b5 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java
@@ -1,15 +1,13 @@
 package com.pollex.pam.service;
 
 import com.pollex.pam.config.ApplicationProperties;
-import com.pollex.pam.domain.Appointment;
-import com.pollex.pam.domain.AppointmentExpiringNotifyRecord;
-import com.pollex.pam.domain.Consultant;
-import com.pollex.pam.domain.Satisfaction;
+import com.pollex.pam.config.Constants;
+import com.pollex.pam.domain.*;
 import com.pollex.pam.enums.AppointmentStatusEnum;
 import com.pollex.pam.enums.ContactStatusEnum;
 import com.pollex.pam.enums.SatisfactionStatusEnum;
+import com.pollex.pam.repository.AppointmentCustomerViewRepository;
 import com.pollex.pam.repository.AppointmentExpiringNotifyRecordRepository;
-import com.pollex.pam.repository.AppointmentRepository;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,8 +19,6 @@
 import org.thymeleaf.spring5.SpringTemplateEngine;
 
 import java.time.Instant;
-import java.time.LocalDate;
-import java.time.ZoneId;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -31,25 +27,6 @@
 @Service
 @Transactional
 public class ScheduleTaskService {
-
-    /**
-     * �閰勗�mail�撱箇���(T)敺�憭抵�������
-     * �����摰2
-     */
-    private static final int APPOINTMENT_PENDING_PHONE_INTERVAL = 2;
-    private static final int APPOINTMENT_PENDING_EMAIL_INTERVAL = 2;
-
-    /**
-     * �閰勗�mail�撱箇���(T)敺�憭拇�◤閬������嚗憭拇甈⊥����策憿批��
-     * �敺�憭�(T+N+1)嚗停���甈∠策摰X�� 閰脤“���敹�瘜������閬���
-     */
-    private static final int APPOINTMENT_EXPIRING_PHONE_INTERVAL = APPOINTMENT_PENDING_PHONE_INTERVAL + 1;
-    private static final int APPOINTMENT_EXPIRING_EMAIL_INTERVAL = APPOINTMENT_PENDING_EMAIL_INTERVAL + 1;
-
-    /**
-     * �摰X��活���
-     */
-    private static final int SEND_EXPIRING_NOTIFY_LIMIT = 1;
 
     private static final String NOT_CONTACTED_NOTIFY_SUBJECT = "�����脰�蝜恍�";
     private static final Logger log = LoggerFactory.getLogger(ScheduleTaskService.class);
@@ -61,7 +38,7 @@
     AppointmentService appointmentService;
 
     @Autowired
-    AppointmentRepository appointmentRepository;
+    AppointmentCustomerViewRepository appointmentCustomerViewRepository;
 
     @Autowired
     SendMsgService sendMsgService;
@@ -85,21 +62,28 @@
     public void sendAppointmentPendingNotifyToConsultant() {
         log.info("Starting send appointment pending notify to consultant");
 
-        Map<String, List<Appointment>> consultantWithPendingAppointments =
-            appointmentRepository.findAllByCommunicateStatusAndStatus(ContactStatusEnum.RESERVED, AppointmentStatusEnum.AVAILABLE)
+        Map<String, List<AppointmentCustomerView>> consultantWithPendingAppointments =
+            appointmentCustomerViewRepository.findAllByCommunicateStatusAndStatus(ContactStatusEnum.RESERVED, AppointmentStatusEnum.AVAILABLE)
                 .stream()
-                .filter(appointment -> isAppointmentInInterval(appointment, APPOINTMENT_PENDING_PHONE_INTERVAL, APPOINTMENT_PENDING_EMAIL_INTERVAL))
-                .collect(Collectors.groupingBy(Appointment::getAgentNo));
+                .filter(appointment ->
+                    appointmentService.isAppointmentDateNotInIntervalFromNow(appointment, Constants.APPOINTMENT_PENDING_PHONE_INTERVAL, Constants.APPOINTMENT_PENDING_EMAIL_INTERVAL)
+                )
+                .collect(Collectors.groupingBy(AppointmentCustomerView::getAgentNo));
 
         consultantWithPendingAppointments.forEach((agentNo, pendingAppointments) -> {
             int pendingAppointmentsSum = pendingAppointments.size();
             Consultant consultant = consultantService.findByAgentNo(agentNo);
-            String consultantPhoneNumber = consultant.getPhoneNumber();
-            String consultantEmail = consultant.getEmail();
+            Optional<String> optionalPhone = Optional.ofNullable(consultant.getPhoneNumber()).filter(StringUtils::hasText);
+            Optional<String> optionalEmail = Optional.ofNullable(consultant.getEmail()).filter(StringUtils::hasText);
+
             String emailContent = getAppointmentPendingNotifyEmailContent(pendingAppointmentsSum);
 
-            sendMsgService.sendMsgBySMS(consultantPhoneNumber, String.format("����%s������脰�蝜恬������", pendingAppointmentsSum));
-            sendMsgService.sendMsgByEmail(consultantEmail, NOT_CONTACTED_NOTIFY_SUBJECT, emailContent, true);
+            optionalPhone.ifPresent(phone -> {
+                sendMsgService.sendMsgBySMS(phone, String.format("����%s������脰�蝜恬������", pendingAppointmentsSum));
+            });
+            optionalEmail.ifPresent(email -> {
+                sendMsgService.sendMsgByEmail(email, NOT_CONTACTED_NOTIFY_SUBJECT, emailContent, true);
+            });
         });
 
         log.info("Sending appointment pending notify to consultant finish");
@@ -109,11 +93,13 @@
     public void sendAppointmentExpiringNotifyToCustomer() {
         log.info("Starting send appointment expiring notify to customer");
 
-        List<Appointment> allByCommunicateStatus =
-            appointmentRepository.findAllByCommunicateStatusAndStatus(ContactStatusEnum.RESERVED, AppointmentStatusEnum.AVAILABLE)
+        List<AppointmentCustomerView> allByCommunicateStatus =
+            appointmentCustomerViewRepository.findAllByCommunicateStatusAndStatus(ContactStatusEnum.RESERVED, AppointmentStatusEnum.AVAILABLE)
                 .stream()
-                .filter(appointment -> isAppointmentInInterval(appointment, APPOINTMENT_EXPIRING_PHONE_INTERVAL, APPOINTMENT_EXPIRING_EMAIL_INTERVAL))
-                .filter(this::isAppointmentNotifyNotOnLimit)
+                .filter(appointment ->
+                    appointmentService.isAppointmentDateNotInIntervalFromNow(appointment, Constants.APPOINTMENT_EXPIRING_PHONE_INTERVAL, Constants.APPOINTMENT_EXPIRING_EMAIL_INTERVAL)
+                )
+                .filter(this::isAppointmentExpiringNotifyNotOnLimit)
                 .collect(Collectors.toList());
 
         allByCommunicateStatus.forEach(appointment -> {
@@ -123,10 +109,10 @@
 
             optionalPhone.ifPresent(phone ->
                 sendMsgService.sendMsgBySMS(phone, String.format("敺甇�����%s憿批�迤敹�葉嚗������蒂���隞“������雯��嚗�%s"
-                    , consultant.getName(), getAppointmentUrl(appointment.getId())))
+                    , consultant.getName(), getAppointmentExpiringNotifyUrl(appointment.getId())))
             );
             optionalEmail.ifPresent(email ->
-                sendMsgService.sendMsgByEmail(email, NOT_CONTACTED_NOTIFY_SUBJECT, getAppointmentExpiringNotifyEmail(consultant.getName(), getAppointmentUrl(appointment.getId())), true)
+                sendMsgService.sendMsgByEmail(email, NOT_CONTACTED_NOTIFY_SUBJECT, getAppointmentExpiringNotifyEmail(consultant.getName(), getAppointmentExpiringNotifyUrl(appointment.getId())), true)
             );
 
             AppointmentExpiringNotifyRecord record = new AppointmentExpiringNotifyRecord();
@@ -140,7 +126,7 @@
     }
 
     // todo ��蝣箄�府����, otis todo=134497
-    @Scheduled(cron = "0 0 9 * * *")
+    @Scheduled(cron = "0 30 8 * * *")
     public void sendNotFillSatisfactionToPersonalNotification() {
         Map<Long, List<Satisfaction>> customerNotFillSatisfactions = satisfactionService.getByStatus(SatisfactionStatusEnum.UNFILLED)
                 .stream()
@@ -151,28 +137,14 @@
         );
     }
 
-    private boolean isAppointmentInInterval(Appointment appointment, int phoneInterval, int emailInterval) {
-        final boolean isHavePhone = StringUtils.hasText(appointment.getPhone());
-        final boolean isHaveEmail = StringUtils.hasText(appointment.getEmail());
-
-        LocalDate appointmentDate = appointment.getAppointmentDate().atZone(ZoneId.systemDefault()).toLocalDate();
-        LocalDate nowDate = Instant.now().atZone(ZoneId.systemDefault()).toLocalDate();
-        long intervalDays = nowDate.toEpochDay() - appointmentDate.toEpochDay();
-
-        final boolean isAppointmentExpiringByPhone = isHavePhone && intervalDays >= phoneInterval;
-        final boolean isAppointmentExpiringByEmail = isHaveEmail && intervalDays >= emailInterval;
-
-        return isAppointmentExpiringByPhone || isAppointmentExpiringByEmail;
-    }
-
-    private boolean isAppointmentNotifyNotOnLimit(Appointment appointment) {
+    private boolean isAppointmentExpiringNotifyNotOnLimit(AppointmentCustomerView appointment) {
         int sendNotifyToCustomerRecordSum =
             appointmentExpiringNotifyRecordRepository.findAllByAppointmentId(appointment.getId()).size();
 
-        return sendNotifyToCustomerRecordSum < SEND_EXPIRING_NOTIFY_LIMIT;
+        return sendNotifyToCustomerRecordSum < Constants.SEND_EXPIRING_NOTIFY_LIMIT;
     }
 
-    private String getAppointmentUrl(Long appointmentId) {
+    private String getAppointmentExpiringNotifyUrl(Long appointmentId) {
         return applicationProperties.getFrontEndDomain() + "?notContactAppointmentId=" + appointmentId;
     }
 

--
Gitblit v1.8.0