From a3716f72066d25d745f4d5103ff23a553c3e102b Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期四, 17 二月 2022 11:41:19 +0800
Subject: [PATCH] Merge branch 'sit' into uat

---
 pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java |  110 +++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 74 insertions(+), 36 deletions(-)

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 9f8ee6c..1bf3c20 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java
@@ -1,28 +1,37 @@
 package com.pollex.pam.service;
 
-import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
 
+import com.pollex.pam.enums.SatisfactionTypeEnum;
+import com.pollex.pam.security.SecurityUtils;
+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;
 
 import com.pollex.pam.domain.Appointment;
-import com.pollex.pam.domain.Consultant;
 import com.pollex.pam.domain.Satisfaction;
+import com.pollex.pam.enums.SatisfactionStatusEnum;
 import com.pollex.pam.repository.ConsultantRepository;
 import com.pollex.pam.repository.CustomerRepository;
 import com.pollex.pam.repository.SatisfactionRepository;
-import com.pollex.pam.service.dto.SatisfactionCustomerCreateDTO;
+import com.pollex.pam.service.dto.SatisfactionCustomerScoreDTO;
 import com.pollex.pam.service.dto.SatisfactionDTO;
 import com.pollex.pam.service.mapper.AppointmentMapper;
 import com.pollex.pam.service.mapper.SatisfactionDTOMapper;
 import com.pollex.pam.service.mapper.SatisfactionMapper;
+import com.pollex.pam.web.rest.errors.SatisfactionAlreadyExistException;
+import com.pollex.pam.web.rest.errors.SatisfactionNotFoundException;
+import org.springframework.util.Assert;
 
 @Service
 @Transactional
 public class SatisfactionService {
+
+    private static final Logger log = LoggerFactory.getLogger(SatisfactionService.class);
 
 	@Autowired
 	SatisfactionRepository satisfactionRepository;
@@ -38,50 +47,57 @@
 
 	@Autowired
 	CustomerRepository customerRepository;
-	
+
 	@Autowired
 	ConsultantRepository consultantRepository;
 
-	public Satisfaction createSatisfaction(Satisfaction satisfaction) {
+	@Autowired
+	ConsultantService consultantService;
+
+	@Autowired
+	PersonalNotificationService personalNotificationService;
+
+	public Satisfaction save(Satisfaction satisfaction) {
 		satisfaction = satisfactionRepository.save(satisfaction);
-		setConsultantAvgScore(satisfaction);
+        if(satisfaction.getType() == SatisfactionTypeEnum.APPOINTMENT) {
+            consultantService.setConsultantAvgScore(satisfaction);
+        }
 		return satisfaction;
 	}
 
+	public Satisfaction scorefaction(SatisfactionCustomerScoreDTO scoreDTO) {
+		Optional<Satisfaction> satisfactionOP = satisfactionRepository.findOneByAppointmentIdAndType(scoreDTO.getAppointmentId(), scoreDTO.getType());
+		Satisfaction satisfaction = satisfactionOP.orElseThrow(SatisfactionNotFoundException::new);
 
+        boolean isSameCustomer = satisfaction.getCustomerId().equals(SecurityUtils.getCustomerDBId());
+        Assert.isTrue(isSameCustomer, "The currently logged in customer has a different ID than the customer on Satisfaction");
 
-	private void setConsultantAvgScore(Satisfaction satisfaction) {
-		float avgScore = getAgentAvgScore(satisfaction);
-		Consultant consultant = consultantRepository.findOneByAgentNo(satisfaction.getAgentNo())
-				.get();
-		consultant.setAvgScore(avgScore);
-		consultantRepository.save(consultant);
+		satisfaction.setScore(scoreDTO.getScore());
+		satisfaction.setStatus(SatisfactionStatusEnum.FILLED);
+		save(satisfaction);
+
+        if(satisfaction.getType() == SatisfactionTypeEnum.APPOINTMENT) {
+            personalNotificationService.createScorefactionToConsultant(satisfaction);
+        }
+        return satisfaction;
 	}
 
-
-
-	private float getAgentAvgScore(Satisfaction satisfaction) {
-		Float avgScore = satisfactionRepository.getAgentScoreAvg(satisfaction.getAgentNo());
-		BigDecimal bigDecimal = new BigDecimal(avgScore);  
-		return avgScore = bigDecimal.setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
+	public Satisfaction createAppointmentSatisfaction(Appointment appointment) {
+		boolean isexist = getByAppointmentIdAndType(appointment.getId(), SatisfactionTypeEnum.APPOINTMENT).isPresent();
+		if(isexist) {
+			throw new SatisfactionAlreadyExistException();
+		}
+		Satisfaction satisfaction = appointmentMapper.toAppointmentSatisfaction(appointment);
+		return save(satisfaction);
 	}
-	 
-	
+//
+//	public Satisfaction createSatisfaction(SatisfactionCustomerScoreDTO createDTO) {
+//		Satisfaction satisfaction = satisfactionDTOMapper.toSatisfaction(createDTO);
+//		return save(satisfaction);
+//	}
 
-	public Satisfaction createSatisfaction(Appointment appointment) {
-		Satisfaction satisfaction = appointmentMapper.toSatisfaction(appointment);
-		return createSatisfaction(satisfaction);
-	}
-
-	public Satisfaction createSatisfaction(SatisfactionCustomerCreateDTO createDTO) {
-		// todo : 撠璅�歇�蝯∠���銝府�隞交憓遛��漲閰��
-		// todo : ��撌梁���銝府�隞仿�脰����
-		Satisfaction satisfaction = satisfactionDTOMapper.toSatisfaction(createDTO);
-		return createSatisfaction(satisfaction);
-	}
-
-	public List<SatisfactionDTO> getByAgentNo(String agentNo) {
-		List<Satisfaction> satisfactionList = satisfactionRepository.findByAgentNo(agentNo);
+	public List<SatisfactionDTO> getByAgentNoAndType(String agentNo, SatisfactionTypeEnum type) {
+		List<Satisfaction> satisfactionList = satisfactionRepository.findByAgentNoAndType(agentNo, type);
 		return satisfactionMapper.toDTO(satisfactionList);
 	}
 
@@ -90,7 +106,29 @@
 		return satisfactionMapper.toDTO(satisfactionList);
 	}
 
-    public Optional<Satisfaction> getByAppointmentId(Long appointmentId) {
-        return satisfactionRepository.findOneByAppointmentId(appointmentId);
+    public Optional<Satisfaction> getByAppointmentIdAndType(Long appointmentId, SatisfactionTypeEnum type) {
+        return satisfactionRepository.findOneByAppointmentIdAndType(appointmentId, type);
+    }
+
+    public List<Satisfaction> getByStatusAndType(SatisfactionStatusEnum status, SatisfactionTypeEnum type) {
+        return satisfactionRepository.findAllByStatusAndType(status, type);
+    }
+
+	public List<Satisfaction> scoreAllfaction(List<SatisfactionCustomerScoreDTO> scoreDTO) {
+		List<Satisfaction> satisfactionList = new ArrayList<>();
+		scoreDTO.stream().forEach(dto ->{
+			satisfactionList.add(scorefaction(dto));
+		});
+		return satisfactionList;
+	}
+
+    public void createUnfilledSystemSatisfaction(Appointment appointment) {
+        Satisfaction satisfaction = new Satisfaction();
+        satisfaction.setAppointmentId(appointment.getId());
+        satisfaction.setCustomerId(SecurityUtils.getCustomerDBId());
+        satisfaction.setStatus(SatisfactionStatusEnum.UNFILLED);
+        satisfaction.setType(SatisfactionTypeEnum.SYSTEM);
+
+        satisfactionRepository.save(satisfaction);
     }
 }

--
Gitblit v1.8.0