From 0e73f60f5e9acede5438b0c38332183916f9d552 Mon Sep 17 00:00:00 2001
From: Jack <jack.su@pollex.com.tw>
Date: 星期五, 03 十二月 2021 16:54:57 +0800
Subject: [PATCH] Merge branch 'master' of ssh://192.168.0.10:29418/pcalife/PAM

---
 pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java |   55 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java b/pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java
index a06f4a5..adbb6ab 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java
@@ -1,6 +1,6 @@
 package com.pollex.pam.service;
 
-import com.pollex.pam.domain.Appointment;
+import com.pollex.pam.domain.AppointmentCustomerView;
 import com.pollex.pam.domain.Consultant;
 import com.pollex.pam.domain.CustomerFavoriteConsultant;
 import com.pollex.pam.enums.ContactStatusEnum;
@@ -9,6 +9,7 @@
 import com.pollex.pam.repository.CustomerFavoriteConsultantRepository;
 import com.pollex.pam.security.SecurityUtils;
 import com.pollex.pam.service.dto.*;
+import com.pollex.pam.service.mapper.AppointmentCustomerViewMapper;
 import com.pollex.pam.service.mapper.ConsultantMapper;
 import com.pollex.pam.web.rest.errors.ConsultantNotFoundException;
 import org.slf4j.Logger;
@@ -19,7 +20,6 @@
 
 import java.util.Comparator;
 import java.util.List;
-import java.util.Optional;
 import java.util.stream.Collectors;
 
 @Service
@@ -42,35 +42,50 @@
     @Autowired
     LoginRecordService loginRecordService;
 
-    public List<ConsultantDTO> getMyConsultantList() {
-        Long customerId = SecurityUtils.getCustomerDBId();
+    @Autowired
+    AppointmentCustomerViewMapper appointmentCustomerViewMapper;
 
+    @Autowired
+    SatisfactionService satisfactionService;
+
+    public List<CustomerFavoriteConsultantDTO> getMyConsultantList() {
+        Long customerId = SecurityUtils.getCustomerDBId();
 
         return customerFavoriteConsultantRepository.findAllByCustomerId(customerId)
             .stream()
             .map(customerFavoriteConsultantRelation -> {
                 Consultant consultant = customerFavoriteConsultantRelation.getConsultant();
-                ConsultantDTO dto = consultantMapper.toDto(consultant);
+                CustomerFavoriteConsultantDTO dto = consultantMapper.toCustomerFavoriteConsultantDto(consultant);
 
-                final Optional<Appointment> latestAppointmentOptional = appointmentService.findByAgentNoAndCustomerId(consultant.getAgentNo(), customerId)
-                    .stream()
-                    .max(Comparator.comparing(Appointment::getAppointmentDate));
+                dto.setContactStatus(ContactStatusEnum.PICKED);
+                dto.setCreateTime(customerFavoriteConsultantRelation.getCreatedDate());
 
-                if(latestAppointmentOptional.isPresent()) {
-                    Appointment latestAppointment = latestAppointmentOptional.get();
-                    dto.setContactStatus(latestAppointment.getCommunicateStatus());
-                    dto.setLatestAppointmentId(latestAppointment.getId());
-                    dto.setUpdateTime(latestAppointment.getAppointmentDate());
-                }
-                else {
-                    dto.setContactStatus(ContactStatusEnum.PICKED);
-                    dto.setLatestAppointmentId(null);
-                    dto.setUpdateTime(customerFavoriteConsultantRelation.getLastModifiedDate());
-                }
+                setAppointmentInfo(
+                    dto,
+                    appointmentService.findAvailableByAgentNoAndCustomerId(consultant.getAgentNo(), customerId)
+                );
 
                 return dto;
 
             }).collect(Collectors.toList());
+    }
+
+    private void setAppointmentInfo(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 latestAppointment = appointments.get(0);
+            customerFavoriteConsultantDTO.setContactStatus(latestAppointment.getCommunicateStatus());
+            customerFavoriteConsultantDTO.setUpdateTime(latestAppointment.getLastModifiedDate());
+        }
     }
 
     public List<ConsultantDTO> getRecommendConsultantList() {
@@ -129,7 +144,7 @@
 
 	public List<AppointmentCustomerViewDTO> getMyAppointment() {
 		String agentNo = SecurityUtils.getAgentNo();
-		return appointmentService.getConsultantAppointments(agentNo);
+		return appointmentService.getConsultantAvailableAppointments(agentNo);
 	}
 
     public void removeConsultantFromCustomList(String agentNo) {

--
Gitblit v1.8.0