From 40ba87b958d3b53f2eef4f95eb3add6548f35006 Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期五, 03 十二月 2021 11:18:48 +0800
Subject: [PATCH] [update] 我的顧問清單新增預約單歷史明細、預約單的更新API、預約單的邏輯刪除API

---
 pamapi/src/main/java/com/pollex/pam/service/ConsultantService.java |   67 ++++++++++++++++++++++++---------
 1 files changed, 49 insertions(+), 18 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..7a9c552 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.domain.Satisfaction;
 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;
@@ -42,37 +44,66 @@
     @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.setLatestAppointmentId(null);
+                dto.setLatestAppointmentDate(null);
+                dto.setLatestAppointmentScore(null);
+                dto.setCreateTime(customerFavoriteConsultantRelation.getLastModifiedDate());
 
-                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 dto, List<AppointmentCustomerView> appointmentList) {
+        List<AppointmentCustomerView> appointments = appointmentList.stream()
+            .sorted(Comparator.comparing(AppointmentCustomerView::getAppointmentDate).reversed())
+            .collect(Collectors.toList());
+
+        dto.setAppointments(appointmentCustomerViewMapper.toAppointmentCustomerViewDTO(appointments));
+
+        if (!appointments.isEmpty()) {
+            AppointmentCustomerView latestAppointment = appointments.get(0);
+            dto.setContactStatus(latestAppointment.getCommunicateStatus());
+            dto.setLatestAppointmentId(latestAppointment.getId());
+            dto.setLatestAppointmentDate(latestAppointment.getAppointmentDate());
+            dto.setUpdateTime(latestAppointment.getLastModifiedDate());
+
+            setLatestAppointmentScore(dto, latestAppointment);
+        }
+    }
+
+    private void setLatestAppointmentScore(CustomerFavoriteConsultantDTO dto, AppointmentCustomerView latestAppointment) {
+        Optional<Satisfaction> satisfactionOptional = satisfactionService.getByAppointmentId(latestAppointment.getId());
+        if(satisfactionOptional.isPresent()) {
+            Satisfaction satisfaction = satisfactionOptional.get();
+            dto.setLatestAppointmentScore(satisfaction.getScore());
+        }
+        else {
+            dto.setLatestAppointmentScore(null);
+        }
+    }
+
     public List<ConsultantDTO> getRecommendConsultantList() {
         return consultantRepository.findAllByRecommendIsTrue()
             .stream()

--
Gitblit v1.8.0