保誠-保戶業務員媒合平台
wayne
2022-02-17 a3716f72066d25d745f4d5103ff23a553c3e102b
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
package com.pollex.pam.service;
 
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.AppointmentMemo;
import com.pollex.pam.repository.AppointmentMemoRepository;
import com.pollex.pam.repository.AppointmentRepository;
import com.pollex.pam.security.SecurityUtils;
import com.pollex.pam.service.dto.AppointmentMemoCreateDTO;
import com.pollex.pam.service.dto.AppointmentMemoUpdateDTO;
import com.pollex.pam.service.mapper.AppointmentMemoMapper;
import com.pollex.pam.web.rest.errors.AppointmentMemoNotFoundException;
import com.pollex.pam.web.rest.errors.AppointmentNotFoundException;
 
@Service
@Transactional
public class AppointmentMemoService {
    
    @Autowired
    AppointmentMemoRepository appointmentMemoRepository;
    
    @Autowired
    AppointmentMemoMapper appointmentMemoMapper;
    
    @Autowired
    AppointmentRepository appointmentRepository;
 
    public AppointmentMemo create(AppointmentMemoCreateDTO memoDTO) {
        AppointmentMemo memo = appointmentMemoMapper.toAppointmentMemo(memoDTO);
        return appointmentMemoRepository.save(memo);
    }
 
    public void checkPermission(Long appointmentId) {
        Appointment appointment = appointmentRepository.findById(appointmentId)
                .orElseThrow(AppointmentNotFoundException::new);
        if(!appointment.getAgentNo().equals(SecurityUtils.getAgentNo())) {
            throw new IllegalAccessError("not have permission");
        }
    }
 
    public AppointmentMemo update(AppointmentMemoUpdateDTO memoDTO) {
        AppointmentMemo memo = appointmentMemoRepository
                .findById(memoDTO.getId())
                .orElseThrow(AppointmentMemoNotFoundException::new);
        checkPermission(memo.getAppointmentId());
        appointmentMemoMapper.copyToAppointmentMemo(memoDTO, memo);
        return appointmentMemoRepository.save(memo);
    }
 
    public void delete(Long memoId) {
        AppointmentMemo memo = appointmentMemoRepository
                .findById(memoId)
                .orElseThrow(AppointmentMemoNotFoundException::new);
        checkPermission(memo.getAppointmentId());
        appointmentMemoRepository.delete(memo);
    }
}