保誠-保戶業務員媒合平台
pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java
@@ -97,49 +97,53 @@
            .map(relation -> {
                Consultant consultant = relation.getConsultant();
                CustomerFavoriteConsultantDTO dto = consultantMapper.toCustomerFavoriteConsultantDto(consultant);
                dto.setContactStatus(ContactStatusEnum.PICKED);
                dto.setCreateTime(relation.getCreatedDate());
                dto.setUpdateTime(relation.getCreatedDate());
                dto.setCustomerViewTime(relation.getViewTime());
                setAvailableAppointmentInfo(
                setInfoByAvailableAppointment(
                    dto,
                    appointmentService.findAvailableByAgentNoAndCustomerId(consultant.getAgentNo(), customerId)
                );
                appointmentService.findLatestAppointmentByAgentNoAndCustomerId(consultant.getAgentNo(), customerId)
                    .ifPresent(latestAppointment -> {
                        dto.setUpdateTime(latestAppointment.getLastModifiedDate());
                    });
                if(dto.getUpdateTime().isBefore(relation.getCreatedDate())) {
                    dto.setUpdateTime(relation.getCreatedDate());
                }
                setFavoriteConsultantUpdatedTime(relation, dto);
                return dto;
            }).collect(Collectors.toList());
    }
    private void setAvailableAppointmentInfo(CustomerFavoriteConsultantDTO customerFavoriteConsultantDTO, List<AppointmentCustomerView> appointmentList) {
   public void setFavoriteConsultantUpdatedTime(CustomerFavoriteConsultant relation,
         CustomerFavoriteConsultantDTO dto) {
      Consultant consultant = relation.getConsultant();
      appointmentService.findLatestAppointmentByAgentNoAndCustomerId(consultant.getAgentNo(), relation.getCustomerId())
          .ifPresent(latestAppointment -> {
              dto.setUpdateTime(latestAppointment.getLastModifiedDate());
          });
      if(dto.getUpdateTime().isBefore(relation.getCreatedDate())) {
          dto.setUpdateTime(relation.getCreatedDate());
      }
   }
    private void setInfoByAvailableAppointment(CustomerFavoriteConsultantDTO customerFavoriteConsultantDTO, List<AppointmentCustomerView> appointmentList) {
        List<AppointmentCustomerView> appointments = appointmentList.stream()
            .sorted(Comparator.comparing(AppointmentCustomerView::getAppointmentDate).reversed())
            .collect(Collectors.toList());
        List<AppointmentCustomerViewDTO> appointmentCustomerViewDTOS = appointmentCustomerViewMapper.toAppointmentCustomerViewDTO(appointments);
        appointmentCustomerViewDTOS.forEach(appointmentCustomerViewDTO -> {
            appointmentService.setSatisfactionScore(appointmentCustomerViewDTO, appointmentCustomerViewDTO.getId());
        });
        customerFavoriteConsultantDTO.setAppointments(appointmentCustomerViewDTOS);
        if (!appointments.isEmpty()) {
            AppointmentCustomerView latestAvailableAppointment = appointments.get(0);
            if(latestAvailableAppointment.getCommunicateStatus() == RESERVED)
                customerFavoriteConsultantDTO.setContactStatus(RESERVED);
            ContactStatusEnum latestStatus = latestAvailableAppointment.getCommunicateStatus();
            if( latestStatus != ContactStatusEnum.DONE
                  || latestStatus != ContactStatusEnum.CLOSED)
                customerFavoriteConsultantDTO.setContactStatus(latestStatus);
            else
                customerFavoriteConsultantDTO.setContactStatus(PICKED);
        }else {
           customerFavoriteConsultantDTO.setContactStatus(PICKED);
        }
    }