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; import com.pollex.pam.domain.Appointment; 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.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; @Service @Transactional public class SatisfactionService { private static final Logger log = LoggerFactory.getLogger(SatisfactionService.class); @Autowired SatisfactionRepository satisfactionRepository; @Autowired AppointmentMapper appointmentMapper; @Autowired SatisfactionDTOMapper satisfactionDTOMapper; @Autowired SatisfactionMapper satisfactionMapper; @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 satisfactionOP = getByAppointmentIdAndType(scoreDTO.getAppointmentId(), SatisfactionTypeEnum.APPOINTMENT); Satisfaction satisfaction = satisfactionOP.orElseThrow(SatisfactionNotFoundException::new); satisfaction.setScore(scoreDTO.getScore()); satisfaction.setStatus(SatisfactionStatusEnum.FILLED); satisfaction.setType(SatisfactionTypeEnum.APPOINTMENT); save(satisfaction); personalNotificationService.createScorefactionToConsultant(satisfaction); return satisfaction; } 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 List getByAgentNoAndType(String agentNo, SatisfactionTypeEnum type) { List satisfactionList = satisfactionRepository.findByAgentNoAndType(agentNo, type); return satisfactionMapper.toDTO(satisfactionList); } public List getByCustomerId(Long customerDBId) { List satisfactionList = satisfactionRepository.findByCustomerId(customerDBId); return satisfactionMapper.toDTO(satisfactionList); } public Optional getByAppointmentIdAndType(Long appointmentId, SatisfactionTypeEnum type) { return satisfactionRepository.findOneByAppointmentIdAndType(appointmentId, type); } public List getByStatusAndType(SatisfactionStatusEnum status, SatisfactionTypeEnum type) { return satisfactionRepository.findAllByStatusAndType(status, type); } public List scoreAllfaction(List scoreDTO) { List 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 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; } } }