保誠-保戶業務員媒合平台
wayne
2022-01-26 6fa4bba623713c396432ba8b863846883d6a1906
pamapi/src/main/java/com/pollex/pam/service/InterviewRecordService.java
比對新檔案
@@ -0,0 +1,84 @@
package com.pollex.pam.service;
import java.util.List;
import org.hibernate.boot.model.naming.IllegalIdentifierException;
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.InterviewRecord;
import com.pollex.pam.enums.InterviewRecordStatusEnum;
import com.pollex.pam.repository.InterviewRecordRepository;
import com.pollex.pam.security.SecurityUtils;
import com.pollex.pam.service.dto.InterviewRecordDTO;
import com.pollex.pam.service.mapper.InterviewRecordMapper;
import com.pollex.pam.web.rest.errors.InterviewRecordNotFoundException;
@Service
@Transactional
public class InterviewRecordService {
   @Autowired
   InterviewRecordRepository interviewRecordRepository;
   @Autowired
   InterviewRecordMapper interviewRecordMapper;
   @Autowired
   AppointmentService appointmentService;
   public InterviewRecord create(InterviewRecordDTO dto) {
      if(dto.getId()!=null) {
         throw new IllegalArgumentException();
      }
      InterviewRecord record = interviewRecordMapper.toInterviewRecord(dto);
      checkAuth(record);
      record.setStatus(InterviewRecordStatusEnum.AVAILABLE);
      interviewRecordRepository.save(record);
      return record;
   }
   public InterviewRecord update(InterviewRecordDTO dto) {
      if(dto.getId()==null) {
         throw new IllegalArgumentException();
      }
      InterviewRecord record = findById(dto.getId());
      checkAuth(record);
      interviewRecordMapper.copyToInterviewRecord(dto, record);
      interviewRecordRepository.save(record);
      return record;
   }
   public void checkAuth(InterviewRecord record) {
      Appointment appointment = appointmentService.findById(record.getAppointmentId());
      if(!appointment.getAgentNo().equals(SecurityUtils.getAgentNo())) {
         throw new IllegalAccessError("The account can not edit the appointment");
      }
   }
   public void delete(Long interviewRecordId) {
      InterviewRecord record = findById(interviewRecordId);
      record.setStatus(InterviewRecordStatusEnum.DELETED);
      interviewRecordRepository.save(record);
   }
   public InterviewRecord findById(Long id) {
      return interviewRecordRepository.findById(id)
      .orElseThrow(InterviewRecordNotFoundException::new);
   }
   public List<InterviewRecordDTO> findByAppointmentId(Long appointmentId) {
      List<InterviewRecord> records = interviewRecordRepository.findByAppointmentId(appointmentId);
      return interviewRecordMapper.toInterviewRecordDTO(records);
   }
   public List<InterviewRecordDTO> findByAppointmentIdAndStatus(Long appointmentId, InterviewRecordStatusEnum status) {
      List<InterviewRecord> records = interviewRecordRepository.findByAppointmentIdAndStatus(appointmentId, status);
      return interviewRecordMapper.toInterviewRecordDTO(records);
   }
}