保誠-保戶業務員媒合平台
wayne
2022-01-26 6fa4bba623713c396432ba8b863846883d6a1906
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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);
    }
    
}