From bdae23a40c461c2c6b6ee614f661eac731c949c8 Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期三, 22 十二月 2021 14:12:05 +0800
Subject: [PATCH] Merge branch 'master' of https://192.168.0.10:8443/r/pcalife/PAM

---
 pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java |   91 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 70 insertions(+), 21 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..8c109fe 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java
@@ -1,14 +1,16 @@
 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.AppointmentStatusEnum;
 import com.pollex.pam.enums.ContactStatusEnum;
 import com.pollex.pam.enums.LoginResult;
 import com.pollex.pam.repository.ConsultantRepository;
 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;
@@ -17,10 +19,13 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.time.Instant;
 import java.util.Comparator;
 import java.util.List;
-import java.util.Optional;
+import java.util.Objects;
 import java.util.stream.Collectors;
+
+import static com.pollex.pam.enums.ContactStatusEnum.*;
 
 @Service
 public class ConsultantService {
@@ -42,35 +47,64 @@
     @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);
+            .map(relation -> {
+                Consultant consultant = relation.getConsultant();
+                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(relation.getCreatedDate());
+                dto.setUpdateTime(relation.getCreatedDate());
+                dto.setCustomerViewTime(relation.getViewTime());
 
-                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());
+                setAvailableAppointmentInfo(
+                    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());
                 }
 
                 return dto;
 
             }).collect(Collectors.toList());
+    }
+
+    private void setAvailableAppointmentInfo(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);
+            else
+                customerFavoriteConsultantDTO.setContactStatus(PICKED);
+        }
     }
 
     public List<ConsultantDTO> getRecommendConsultantList() {
@@ -129,7 +163,7 @@
 
 	public List<AppointmentCustomerViewDTO> getMyAppointment() {
 		String agentNo = SecurityUtils.getAgentNo();
-		return appointmentService.getConsultantAppointments(agentNo);
+		return appointmentService.getConsultantAvailableAppointments(agentNo);
 	}
 
     public void removeConsultantFromCustomList(String agentNo) {
@@ -149,4 +183,19 @@
         String agentNo = SecurityUtils.getAgentNo();
         appointmentService.recordAllAppointmentsView(agentNo);
     }
+
+    public void recordMyConsultantListView() {
+        Long customerId = SecurityUtils.getCustomerDBId();
+        List<CustomerFavoriteConsultant> notViewRelation = customerFavoriteConsultantRepository
+            .findAllByCustomerId(customerId)
+            .stream()
+            .filter(relation -> Objects.isNull(relation.getViewTime()))
+            .collect(Collectors.toList());
+
+        notViewRelation.forEach(relation -> {
+            relation.setViewTime(Instant.now());
+        });
+
+        customerFavoriteConsultantRepository.saveAll(notViewRelation);
+    }
 }

--
Gitblit v1.8.0