保誠-保戶業務員媒合平台
wayne
2022-02-10 d54da4786548ef4c4ea4d62b2754cfed2b24a698
pamapi/src/main/java/com/pollex/pam/service/SatisfactionService.java
@@ -1,8 +1,14 @@
package com.pollex.pam.service;
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 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;
@@ -25,6 +31,8 @@
@Transactional
public class SatisfactionService {
    private static final Logger log = LoggerFactory.getLogger(SatisfactionService.class);
   @Autowired
   SatisfactionRepository satisfactionRepository;
@@ -39,34 +47,39 @@
   @Autowired
   CustomerRepository customerRepository;
   @Autowired
   ConsultantRepository consultantRepository;
   @Autowired
   ConsultantService consultantService;
   @Autowired
   PersonalNotificationService personalNotificationService;
   public Satisfaction save(Satisfaction satisfaction) {
      satisfaction = satisfactionRepository.save(satisfaction);
      consultantService.setConsultantAvgScore(satisfaction);
      return satisfaction;
   }
   public Satisfaction scorefaction(SatisfactionCustomerScoreDTO scoreDTO) {
      Optional<Satisfaction> satisfactionOP = getByAppointmentId(scoreDTO.getAppointmentId());
      Optional<Satisfaction> satisfactionOP = getByAppointmentIdAndType(scoreDTO.getAppointmentId(), SatisfactionTypeEnum.APPOINTMENT);
      Satisfaction satisfaction = satisfactionOP.orElseThrow(SatisfactionNotFoundException::new);
      satisfaction.setScore(scoreDTO.getScore());
      satisfaction.setStatus(SatisfactionStatusEnum.FILLED);
      return save(satisfaction);
        satisfaction.setType(SatisfactionTypeEnum.APPOINTMENT);
      save(satisfaction);
      personalNotificationService.createScorefactionToConsultant(satisfaction);
      return satisfaction;
   }
   public Satisfaction createSatisfaction(Appointment appointment) {
      boolean isexist = getByAppointmentId(appointment.getId()).isPresent();
   public Satisfaction createAppointmentSatisfaction(Appointment appointment) {
      boolean isexist = getByAppointmentIdAndType(appointment.getId(), SatisfactionTypeEnum.APPOINTMENT).isPresent();
      if(isexist) {
         throw new SatisfactionAlreadyExistException();
      }
      Satisfaction satisfaction = appointmentMapper.toSatisfaction(appointment);
      Satisfaction satisfaction = appointmentMapper.toAppointmentSatisfaction(appointment);
      return save(satisfaction);
   }
//
@@ -75,8 +88,8 @@
//      return save(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);
   }
@@ -85,7 +98,45 @@
      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);
    }
    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;
        }
    }
}