保誠-保戶業務員媒合平台
wayne
2022-02-10 21d79d2a70b095545477472438d27a8827ee80b8
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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.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 {
 
    @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<Satisfaction> 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<SatisfactionDTO> getByAgentNoAndType(String agentNo, SatisfactionTypeEnum type) {
        List<Satisfaction> satisfactionList = satisfactionRepository.findByAgentNoAndType(agentNo, type);
        return satisfactionMapper.toDTO(satisfactionList);
    }
 
    public List<SatisfactionDTO> getByCustomerIdAndType(Long customerDBId, SatisfactionTypeEnum type) {
        List<Satisfaction> satisfactionList = satisfactionRepository.findByCustomerIdAndType(customerDBId, type);
        return satisfactionMapper.toDTO(satisfactionList);
    }
 
    public Optional<Satisfaction> getByAppointmentIdAndType(Long appointmentId, SatisfactionTypeEnum type) {
        return satisfactionRepository.findOneByAppointmentIdAndType(appointmentId, type);
    }
 
    public List<Satisfaction> getByStatusAndType(SatisfactionStatusEnum status, SatisfactionTypeEnum type) {
        return satisfactionRepository.findAllByStatusAndType(status, type);
    }
 
    public List<Satisfaction> scoreAllfaction(List<SatisfactionCustomerScoreDTO> scoreDTO) {
        List<Satisfaction> satisfactionList = new ArrayList<>();
        scoreDTO.stream().forEach(dto ->{
            satisfactionList.add(scorefaction(dto));
        });
        return satisfactionList;
    }
 
    public Satisfaction createSystemSatisfaction(SatisfactionSystemScoreDTO scoreDTO) {
        Satisfaction satisfaction = new Satisfaction();
        satisfaction.setCustomerId(SecurityUtils.getCustomerDBId());
        satisfaction.setAppointmentId(scoreDTO.getAppointmentId());
        satisfaction.setStatus(SatisfactionStatusEnum.FILLED);
        satisfaction.setScore(scoreDTO.getScore());
        satisfaction.setType(SatisfactionTypeEnum.SYSTEM);
        return satisfactionRepository.save(satisfaction);
    }
}