保誠-保戶業務員媒合平台
wayne
2022-02-17 34b08e1c461f5e08675fcff95525956d7c4bef11
pamapi/src/main/java/com/pollex/pam/service/PersonalNotificationService.java
@@ -1,24 +1,159 @@
package com.pollex.pam.service;
import java.time.Instant;
import java.util.List;
import javax.management.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.pollex.pam.domain.Appointment;
import com.pollex.pam.domain.Consultant;
import com.pollex.pam.domain.Customer;
import com.pollex.pam.domain.PersonalNotification;
import com.pollex.pam.domain.Satisfaction;
import com.pollex.pam.enums.NotificationTypeEnum;
import com.pollex.pam.enums.PersonalNotificationRoleEnum;
import com.pollex.pam.repository.CustomerRepository;
import com.pollex.pam.repository.PersonalNotificationRepository;
import com.pollex.pam.security.SecurityUtils;
import com.pollex.pam.service.dto.AppointmentUpdateDTO;
@Service
@Transactional
public class PersonalNotificationService {
   @Autowired
   PersonalNotificationRepository personalNotificationRepository;
   @Autowired
   ConsultantService consultantService;
   @Autowired
   AppointmentService appointmentService;
   @Autowired
   CustomerService customerService;
   @Autowired
   CustomerRepository customerRepository;
   @Autowired
   SatisfactionService satisfactionService;
   public List<PersonalNotification> getMyPersonalNotification(Long ownerId, PersonalNotificationRoleEnum role) {
      return personalNotificationRepository.findAllByOwnerRoleAndOwnerId(role, ownerId);
   }
   public void createSendSatisfactionToClientToCustomer(Appointment appointment) {
      PersonalNotification entity = new PersonalNotification();
      Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
      String content = consultant.getName()+"顧問請您填寫滿意度評比";
      entity.setContent(content);
      entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
      entity.setOwnerId(appointment.getCustomerId());
      entity.setOwnerRole(PersonalNotificationRoleEnum.CUSTOMER);
      entity.setTitle("填寫滿意度");
      personalNotificationRepository.save(entity);
   }
   public void createSendNoticeToCustomer(Long appointmentId) {
      Appointment appointment = appointmentService.findById(appointmentId);
      PersonalNotification entity = new PersonalNotification();
      Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
      String content = "您有 "+consultant.getName()+"顧問的約訪通知";
      entity.setContent(content);
      entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
      entity.setOwnerId(appointment.getCustomerId());
      entity.setOwnerRole(PersonalNotificationRoleEnum.CUSTOMER);
      entity.setTitle("顧問約訪通知");
      personalNotificationRepository.save(entity);
   }
    public void createNotFillAppointmentSatisfactionNumberToCustomer(Long customerId, int notFillSatisfactionSum) {
        PersonalNotification entity = new PersonalNotification();
        String content = "您有 "+notFillSatisfactionSum+" 筆顧問的滿意度需要填寫";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(customerId);
        entity.setOwnerRole(PersonalNotificationRoleEnum.CUSTOMER);
        entity.setTitle("客戶滿意度");
        personalNotificationRepository.save(entity);
    }
   public void createEditConsultantToConsultant(Consultant consultant) {
      PersonalNotification entity = new PersonalNotification();
      String content = "您的個人帳號設定已進行更新";
      entity.setContent(content);
      entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
      entity.setOwnerId(consultant.getId());
      entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
      entity.setTitle("變更帳號資料");
      personalNotificationRepository.save(entity);
   }
   public void createMarkAppointmentDeletedToConsultant(Appointment appointment) {
      PersonalNotification entity = new PersonalNotification();
      Customer customer = customerRepository.findById(appointment.getCustomerId()).get();
      Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
      String content = customer.getName()+"客戶已取消您的預約";
      entity.setContent(content);
      entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
      entity.setOwnerId(consultant.getId());
      entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
      entity.setTitle("取消預約提醒");
      personalNotificationRepository.save(entity);
   }
   public void createUpdateAppointmentToConsultant(Appointment appointment) {
      PersonalNotification entity = new PersonalNotification();
      Customer customer = customerRepository.findById(appointment.getCustomerId()).get();
      Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
      String content = customer.getName()+"客戶已更新您的預約資訊";
      entity.setContent(content);
      entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
      entity.setOwnerId(consultant.getId());
      entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
      entity.setTitle("更新預約提醒");
      personalNotificationRepository.save(entity);
   }
   public void createScorefactionToConsultant(Satisfaction satisfaction) {
      PersonalNotification entity = new PersonalNotification();
      Appointment appointment = appointmentService.findById(satisfaction.getAppointmentId());
      Customer customer = customerRepository.findById(appointment.getCustomerId()).get();
      Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
      String content = customer.getName()+"客戶已對您進行滿意度評比";
      entity.setContent(content);
      entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
      entity.setOwnerId(consultant.getId());
      entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
      entity.setTitle("客戶滿意度");
      personalNotificationRepository.save(entity);
   }
   public void readAllMyNotification() {
      if(StringUtils.hasText(SecurityUtils.getAgentNo())) {
         Long consultantId = consultantService.findByAgentNo(SecurityUtils.getAgentNo()).getId();
         readAllNotification(PersonalNotificationRoleEnum.CONSULTANT, consultantId);
      }else if(SecurityUtils.getCustomerDBId()!=null){
         readAllNotification(PersonalNotificationRoleEnum.CUSTOMER, SecurityUtils.getCustomerDBId());
      }
   }
   public void readAllNotification(PersonalNotificationRoleEnum ownerRole
         , Long ownerId) {
      List<PersonalNotification> allNotification = personalNotificationRepository.findAllByOwnerRoleAndOwnerId(ownerRole, ownerId);
      Instant today = Instant.now();
      allNotification.stream()
      .filter(notification ->  notification.getReadDate()==null)
      .forEach(notification ->{
         notification.setReadDate(today);
         personalNotificationRepository.saveAll(allNotification);
      });
   }
}