From f8ab133a7dc20562c25a092a402266f5e7b0b296 Mon Sep 17 00:00:00 2001 From: Jack <jack.su@pollex.com.tw> Date: 星期一, 24 一月 2022 10:29:38 +0800 Subject: [PATCH] Merge branch 'Phase3' of ssh://dev.pollex.com.tw:29418/pcalife/PAM into Phase3 --- pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 158 insertions(+), 0 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 new file mode 100644 index 0000000..604479e --- /dev/null +++ b/pamapi/src/main/java/com/pollex/pam/service/ScheduleTaskService.java @@ -0,0 +1,158 @@ +package com.pollex.pam.service; + +import com.pollex.pam.config.ApplicationProperties; +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import org.thymeleaf.context.Context; +import org.thymeleaf.spring5.SpringTemplateEngine; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +@Transactional +public class ScheduleTaskService { + + private static final String NOT_CONTACTED_NOTIFY_SUBJECT = "�����脰�蝜恍�"; + private static final Logger log = LoggerFactory.getLogger(ScheduleTaskService.class); + + @Autowired + ConsultantService consultantService; + + @Autowired + AppointmentService appointmentService; + + @Autowired + AppointmentCustomerViewRepository appointmentCustomerViewRepository; + + @Autowired + SendMsgService sendMsgService; + + @Autowired + SpringTemplateEngine springTemplateEngine; + + @Autowired + ApplicationProperties applicationProperties; + + @Autowired + AppointmentExpiringNotifyRecordRepository appointmentExpiringNotifyRecordRepository; + + @Autowired + SatisfactionService satisfactionService; + + @Autowired + PersonalNotificationService personalNotificationService; + + @Scheduled(cron = "0 30 8 * * *") + public void sendAppointmentPendingNotifyToConsultant() { + log.info("Starting send appointment pending notify to consultant"); + + Map<String, List<AppointmentCustomerView>> consultantWithPendingAppointments = + appointmentCustomerViewRepository.findAllByCommunicateStatusAndStatus(ContactStatusEnum.RESERVED, AppointmentStatusEnum.AVAILABLE) + .stream() + .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(); + String emailContent = getAppointmentPendingNotifyEmailContent(pendingAppointmentsSum); + + sendMsgService.sendMsgBySMS(consultantPhoneNumber, String.format("����%s������脰�蝜恬������", pendingAppointmentsSum)); + sendMsgService.sendMsgByEmail(consultantEmail, NOT_CONTACTED_NOTIFY_SUBJECT, emailContent, true); + }); + + log.info("Sending appointment pending notify to consultant finish"); + } + + @Scheduled(cron = "0 30 8 * * *") + public void sendAppointmentExpiringNotifyToCustomer() { + log.info("Starting send appointment expiring notify to customer"); + + List<AppointmentCustomerView> allByCommunicateStatus = + appointmentCustomerViewRepository.findAllByCommunicateStatusAndStatus(ContactStatusEnum.RESERVED, AppointmentStatusEnum.AVAILABLE) + .stream() + .filter(appointment -> + appointmentService.isAppointmentDateNotInIntervalFromNow(appointment, Constants.APPOINTMENT_EXPIRING_PHONE_INTERVAL, Constants.APPOINTMENT_EXPIRING_EMAIL_INTERVAL) + ) + .filter(this::isAppointmentNotifyNotOnLimit) + .collect(Collectors.toList()); + + allByCommunicateStatus.forEach(appointment -> { + Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo()); + Optional<String> optionalPhone = Optional.ofNullable(appointment.getPhone()).filter(StringUtils::hasText); + Optional<String> optionalEmail = Optional.ofNullable(appointment.getEmail()).filter(StringUtils::hasText); + + optionalPhone.ifPresent(phone -> + sendMsgService.sendMsgBySMS(phone, String.format("敺甇�����%s憿批�迤敹�葉嚗������蒂���隞“������雯��嚗�%s" + , consultant.getName(), getAppointmentUrl(appointment.getId()))) + ); + optionalEmail.ifPresent(email -> + sendMsgService.sendMsgByEmail(email, NOT_CONTACTED_NOTIFY_SUBJECT, getAppointmentExpiringNotifyEmail(consultant.getName(), getAppointmentUrl(appointment.getId())), true) + ); + + AppointmentExpiringNotifyRecord record = new AppointmentExpiringNotifyRecord(); + record.setAppointmentId(appointment.getId()); + record.setSendTime(Instant.now()); + + appointmentExpiringNotifyRecordRepository.save(record); + }); + + log.info("Sending appointment expiring notify to customer finish"); + } + + // todo ��蝣箄�府����, otis todo=134497 + @Scheduled(cron = "0 0 9 * * *") + public void sendNotFillSatisfactionToPersonalNotification() { + Map<Long, List<Satisfaction>> customerNotFillSatisfactions = satisfactionService.getByStatus(SatisfactionStatusEnum.UNFILLED) + .stream() + .collect(Collectors.groupingBy(Satisfaction::getCustomerId)); + + customerNotFillSatisfactions.forEach((customerId, notFillSatisfactions) -> + personalNotificationService.createNotFillSatisfactionSumToCustomer(customerId, notFillSatisfactions.size()) + ); + } + + private boolean isAppointmentNotifyNotOnLimit(AppointmentCustomerView appointment) { + int sendNotifyToCustomerRecordSum = + appointmentExpiringNotifyRecordRepository.findAllByAppointmentId(appointment.getId()).size(); + + return sendNotifyToCustomerRecordSum < Constants.SEND_EXPIRING_NOTIFY_LIMIT; + } + + private String getAppointmentUrl(Long appointmentId) { + return applicationProperties.getFrontEndDomain() + "?notContactAppointmentId=" + appointmentId; + } + + private String getAppointmentPendingNotifyEmailContent(int sum) { + Context context = new Context(); + context.setVariable("pendingAppointmentSum", sum); + return springTemplateEngine.process("mail/appointmentPendingNotifyEmail", context); + } + + private String getAppointmentExpiringNotifyEmail(String consultantName, String notifyUrl) { + Context context = new Context(); + context.setVariable("consultantName", consultantName); + context.setVariable("notifyUrl", notifyUrl); + return springTemplateEngine.process("mail/appointmentExpiringNotifyEmail", context); + } +} -- Gitblit v1.8.0