package com.pollex.pam.service; import java.math.BigDecimal; import java.util.List; import java.util.Optional; 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.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.SatisfactionDTO; import com.pollex.pam.service.mapper.AppointmentMapper; import com.pollex.pam.service.mapper.SatisfactionDTOMapper; import com.pollex.pam.service.mapper.SatisfactionMapper; @Service @Transactional public class SatisfactionService { @Autowired SatisfactionRepository satisfactionRepository; @Autowired AppointmentMapper appointmentMapper; @Autowired SatisfactionDTOMapper satisfactionDTOMapper; @Autowired SatisfactionMapper satisfactionMapper; @Autowired CustomerRepository customerRepository; @Autowired ConsultantRepository consultantRepository; public Satisfaction createSatisfaction(Satisfaction satisfaction) { satisfaction = satisfactionRepository.save(satisfaction); setConsultantAvgScore(satisfaction); return satisfaction; } private void setConsultantAvgScore(Satisfaction satisfaction) { float avgScore = getAgentAvgScore(satisfaction); Consultant consultant = consultantRepository.findOneByAgentNo(satisfaction.getAgentNo()) .get(); consultant.setAvgScore(avgScore); consultantRepository.save(consultant); } 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 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 getByAgentNo(String agentNo) { List satisfactionList = satisfactionRepository.findByAgentNo(agentNo); return satisfactionMapper.toDTO(satisfactionList); } public List getByCustomerId(Long customerDBId) { List satisfactionList = satisfactionRepository.findByCustomerId(customerDBId); return satisfactionMapper.toDTO(satisfactionList); } public Optional getByAppointmentId(Long appointmentId) { return satisfactionRepository.findOneByAppointmentId(appointmentId); } }