From d54da4786548ef4c4ea4d62b2754cfed2b24a698 Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期四, 10 二月 2022 15:54:18 +0800
Subject: [PATCH] [update][todo 135158] 列出滿意度問卷也需要包含系統滿意度問卷

---
 pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java       |   34 +++++++++--
 pamapi/src/main/java/com/pollex/pam/service/mapper/SatisfactionMapper.java |   11 ++-
 pamapi/src/doc/滿意度/查詢登入者所有的滿意度問卷.txt                                       |   68 +++++++++++-----------
 pamapi/src/main/java/com/pollex/pam/repository/SatisfactionRepository.java |    2 
 pamapi/src/main/java/com/pollex/pam/web/rest/AppointmentResource.java      |    3 
 pamapi/src/main/java/com/pollex/pam/web/rest/SatisfactionResource.java     |    4 
 pamapi/src/main/java/com/pollex/pam/service/dto/SatisfactionDTO.java       |   12 +++-
 7 files changed, 81 insertions(+), 53 deletions(-)

diff --git "a/pamapi/src/doc/\346\273\277\346\204\217\345\272\246/\346\237\245\350\251\242\347\231\273\345\205\245\350\200\205\346\211\200\346\234\211\347\232\204\346\273\277\346\204\217\345\272\246\345\225\217\345\215\267.txt" "b/pamapi/src/doc/\346\273\277\346\204\217\345\272\246/\346\237\245\350\251\242\347\231\273\345\205\245\350\200\205\346\211\200\346\234\211\347\232\204\346\273\277\346\204\217\345\272\246\345\225\217\345\215\267.txt"
index 73ae4c7..920f61e 100644
--- "a/pamapi/src/doc/\346\273\277\346\204\217\345\272\246/\346\237\245\350\251\242\347\231\273\345\205\245\350\200\205\346\211\200\346\234\211\347\232\204\346\273\277\346\204\217\345\272\246\345\225\217\345\215\267.txt"
+++ "b/pamapi/src/doc/\346\273\277\346\204\217\345\272\246/\346\237\245\350\251\242\347\231\273\345\205\245\350\200\205\346\211\200\346\234\211\347\232\204\346\273\277\346\204\217\345\272\246\345\225\217\345\215\267.txt"
@@ -7,39 +7,41 @@
 
 [
     {
-        "id": 11,
-        "createdDate": "2021-12-03T03:59:07.854Z",
-        "lastModifiedDate": "2021-12-03T03:59:07.854Z",
-        "customerId": 90,
-        "agentNo": "B282677963",
-        "status": "FILLED",
-        "score": 4.0,
-        "appointmentId": 134,
-        "customerName": "Jack",
-        "agentName": "�憒�"
+        "id" : 85,
+        "createdDate" : "2022-01-24T11:48:58.039Z",
+        "lastModifiedDate" : "2022-01-24T11:48:58.039Z",
+        "customerId" : 165,
+        "agentNo" : "AGAM11249699",
+        "status" : "UNFILLED",
+        "score" : null,
+        "appointmentId" : 483,
+        "customerName" : "Mila la",
+        "agentName" : "���� lala",
+        "type" : "APPOINTMENT"      // "APPOINTMENT"�撠“�����遛��漲嚗�"SYSTEM"�撠像���遛��漲
     },
     {
-        "id": 12,
-        "createdDate": "2021-12-03T04:00:43.181Z",
-        "lastModifiedDate": "2021-12-03T04:00:43.181Z",
-        "customerId": 90,
-        "agentNo": "B282677963",
-        "status": "FILLED",
-        "score": 4.0,
-        "appointmentId": 135,
-        "customerName": "Jack",
-        "agentName": "�憒�"
-    },
-    {
-        "id": 13,
-        "createdDate": "2021-12-03T04:01:20.095Z",
-        "lastModifiedDate": "2021-12-03T04:01:20.095Z",
-        "customerId": 90,
-        "agentNo": "B282677963",
-        "status": "FILLED",
-        "score": 2.0,
-        "appointmentId": 136,
-        "customerName": "Jack",
-        "agentName": "�憒�"
+      "id" : 84,
+      "createdDate" : "2022-01-24T10:31:03.799Z",
+      "lastModifiedDate" : "2022-01-24T10:31:03.799Z",
+      "customerId" : 165,
+      "agentNo" : "AGAM11249699",
+      "status" : "FILLED",
+      "score" : 1.0,
+      "appointmentId" : 482,
+      "customerName" : "Mila la",
+      "agentName" : "���� lala",
+      "type" : "APPOINTMENT"
+    }, {
+      "id" : 95,
+      "createdDate" : "2022-02-10T07:28:39.095Z",
+      "lastModifiedDate" : "2022-02-10T07:28:39.095Z",
+      "customerId" : 165,
+      "agentNo" : null,
+      "status" : "UNFILLED",
+      "score" : null,
+      "appointmentId" : 527,
+      "customerName" : "Mila la",
+      "agentName" : null,
+      "type" : "SYSTEM"
     }
-]
\ No newline at end of file
+]
diff --git a/pamapi/src/main/java/com/pollex/pam/repository/SatisfactionRepository.java b/pamapi/src/main/java/com/pollex/pam/repository/SatisfactionRepository.java
index 70db2fc..1e670a3 100644
--- a/pamapi/src/main/java/com/pollex/pam/repository/SatisfactionRepository.java
+++ b/pamapi/src/main/java/com/pollex/pam/repository/SatisfactionRepository.java
@@ -17,7 +17,7 @@
 
 	List<Satisfaction> findByAgentNoAndType(String agentNo, SatisfactionTypeEnum type);
 
-	List<Satisfaction> findByCustomerIdAndType(Long customerId, SatisfactionTypeEnum type);
+	List<Satisfaction> findByCustomerId(Long customerId);
 
 	Optional<Satisfaction> findOneByAppointmentIdAndType(Long appointmentId, SatisfactionTypeEnum type);
 
diff --git a/pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java b/pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java
index 0d8a786..1d700eb 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java
@@ -7,6 +7,8 @@
 import com.pollex.pam.enums.SatisfactionTypeEnum;
 import com.pollex.pam.security.SecurityUtils;
 import com.pollex.pam.service.dto.SatisfactionSystemScoreDTO;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -28,6 +30,8 @@
 @Service
 @Transactional
 public class SatisfactionService {
+
+    private static final Logger log = LoggerFactory.getLogger(SatisfactionService.class);
 
 	@Autowired
 	SatisfactionRepository satisfactionRepository;
@@ -89,8 +93,8 @@
 		return satisfactionMapper.toDTO(satisfactionList);
 	}
 
-	public List<SatisfactionDTO> getByCustomerIdAndType(Long customerDBId, SatisfactionTypeEnum type) {
-		List<Satisfaction> satisfactionList = satisfactionRepository.findByCustomerIdAndType(customerDBId, type);
+	public List<SatisfactionDTO> getByCustomerId(Long customerDBId) {
+		List<Satisfaction> satisfactionList = satisfactionRepository.findByCustomerId(customerDBId);
 		return satisfactionMapper.toDTO(satisfactionList);
 	}
 
@@ -110,13 +114,29 @@
 		return satisfactionList;
 	}
 
-    public Satisfaction createSystemSatisfaction(SatisfactionSystemScoreDTO scoreDTO) {
+    public void createUnfilledSystemSatisfaction(Appointment appointment) {
         Satisfaction satisfaction = new Satisfaction();
+        satisfaction.setAppointmentId(appointment.getId());
         satisfaction.setCustomerId(SecurityUtils.getCustomerDBId());
-        satisfaction.setAppointmentId(scoreDTO.getAppointmentId());
-        satisfaction.setStatus(SatisfactionStatusEnum.FILLED);
-        satisfaction.setScore(scoreDTO.getScore());
+        satisfaction.setStatus(SatisfactionStatusEnum.UNFILLED);
         satisfaction.setType(SatisfactionTypeEnum.SYSTEM);
-        return satisfactionRepository.save(satisfaction);
+
+        satisfactionRepository.save(satisfaction);
+    }
+
+    public Satisfaction fillSystemSatisfaction(SatisfactionSystemScoreDTO scoreDTO) {
+        Optional<Satisfaction> systemSatisfactionOptional =
+            satisfactionRepository.findOneByAppointmentIdAndType(scoreDTO.getAppointmentId(), SatisfactionTypeEnum.SYSTEM);
+
+        if(systemSatisfactionOptional.isPresent()) {
+            Satisfaction satisfaction = systemSatisfactionOptional.get();
+            satisfaction.setStatus(SatisfactionStatusEnum.FILLED);
+            satisfaction.setScore(scoreDTO.getScore());
+            return satisfactionRepository.save(satisfaction);
+        }
+        else {
+            log.warn("not found the satisfaction record, so can't fill system satisfaction");
+            return null;
+        }
     }
 }
diff --git a/pamapi/src/main/java/com/pollex/pam/service/dto/SatisfactionDTO.java b/pamapi/src/main/java/com/pollex/pam/service/dto/SatisfactionDTO.java
index 5a6c4ae..152a82d 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/dto/SatisfactionDTO.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/dto/SatisfactionDTO.java
@@ -3,6 +3,7 @@
 import java.time.Instant;
 
 import com.pollex.pam.enums.SatisfactionStatusEnum;
+import com.pollex.pam.enums.SatisfactionTypeEnum;
 
 public class SatisfactionDTO {
 
@@ -16,7 +17,8 @@
     private Long appointmentId;
     private String customerName;
     private String agentName;
-    
+    private SatisfactionTypeEnum type;
+
 	public Long getId() {
 		return id;
 	}
@@ -77,6 +79,10 @@
 	public void setAgentName(String agentName) {
 		this.agentName = agentName;
 	}
-	
-
+    public SatisfactionTypeEnum getType() {
+        return type;
+    }
+    public void setType(SatisfactionTypeEnum type) {
+        this.type = type;
+    }
 }
diff --git a/pamapi/src/main/java/com/pollex/pam/service/mapper/SatisfactionMapper.java b/pamapi/src/main/java/com/pollex/pam/service/mapper/SatisfactionMapper.java
index 97669a7..8a757a4 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/mapper/SatisfactionMapper.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/mapper/SatisfactionMapper.java
@@ -3,6 +3,7 @@
 import static java.util.stream.Collectors.toList;
 
 import java.util.List;
+import java.util.Objects;
 
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -17,19 +18,19 @@
 
 @Service
 public class SatisfactionMapper {
-	
+
 	@Autowired
 	ConsultantRepository consultantRepository;
-	
+
 	@Autowired
 	CustomerRepository customerRepository;
-	
+
 	public SatisfactionDTO toDTO(Satisfaction source) {
 		SatisfactionDTO target = new SatisfactionDTO();
 		BeanUtils.copyProperties(source, target);
 		target.setScore(source.getScore());
-		Consultant consultant= consultantRepository.findOneByAgentNo(source.getAgentNo()).get();
-		target.setAgentName(consultant.getName());
+		Consultant consultant= consultantRepository.findOneByAgentNo(source.getAgentNo()).orElse(null);
+		target.setAgentName(Objects.isNull(consultant) ? null : consultant.getName());
 		Customer customer = customerRepository.findById(source.getCustomerId()).get();
 		target.setCustomerName(customer.getName());
 		return target;
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/AppointmentResource.java b/pamapi/src/main/java/com/pollex/pam/web/rest/AppointmentResource.java
index fe72710..6a8e6b7 100644
--- a/pamapi/src/main/java/com/pollex/pam/web/rest/AppointmentResource.java
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/AppointmentResource.java
@@ -16,8 +16,6 @@
 import com.pollex.pam.service.PersonalNotificationService;
 import com.pollex.pam.service.SatisfactionService;
 
-import java.util.Objects;
-
 @RestController
 @RequestMapping("/api/appointment")
 public class AppointmentResource {
@@ -57,6 +55,7 @@
 	public AppointmentDTO clientCreateAppointment(@RequestBody AppointmentCreateDTO appointmentCreateDTO) {
         Appointment appointment = appointmentService.customerCreateAppointment(appointmentCreateDTO);
         appointmentService.sendAppointmentNotify(appointment);
+        satisfactionService.createUnfilledSystemSatisfaction(appointment);
 
         return appointmentMapper.toAppointmentDTO(appointment);
     }
diff --git a/pamapi/src/main/java/com/pollex/pam/web/rest/SatisfactionResource.java b/pamapi/src/main/java/com/pollex/pam/web/rest/SatisfactionResource.java
index 2289880..b4dbfdc 100644
--- a/pamapi/src/main/java/com/pollex/pam/web/rest/SatisfactionResource.java
+++ b/pamapi/src/main/java/com/pollex/pam/web/rest/SatisfactionResource.java
@@ -45,7 +45,7 @@
 		if(StringUtils.hasText(SecurityUtils.getAgentNo())) {
 			return satisfactionService.getByAgentNoAndType(SecurityUtils.getAgentNo(), SatisfactionTypeEnum.APPOINTMENT);
 		}else if(SecurityUtils.getCustomerDBId()!=null){
-			return satisfactionService.getByCustomerIdAndType(SecurityUtils.getCustomerDBId(), SatisfactionTypeEnum.APPOINTMENT);
+			return satisfactionService.getByCustomerId(SecurityUtils.getCustomerDBId());
 		}
 		log.error("Not has agent code and customer id");
 		throw new IllegalArgumentException("Not has agent code and customer id");
@@ -53,6 +53,6 @@
 
     @PostMapping("/system/score")
     public Satisfaction createSystemSatisfaction(@RequestBody SatisfactionSystemScoreDTO scoreDTO) {
-        return satisfactionService.createSystemSatisfaction(scoreDTO);
+        return satisfactionService.fillSystemSatisfaction(scoreDTO);
     }
 }

--
Gitblit v1.8.0