保誠-保戶業務員媒合平台
wayne
2022-02-17 34b08e1c461f5e08675fcff95525956d7c4bef11
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.pollex.pam.service;
 
import java.time.Instant;
import java.util.List;
 
import javax.management.Notification;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import com.pollex.pam.domain.Appointment;
import com.pollex.pam.domain.Consultant;
import com.pollex.pam.domain.Customer;
import com.pollex.pam.domain.PersonalNotification;
import com.pollex.pam.domain.Satisfaction;
import com.pollex.pam.enums.NotificationTypeEnum;
import com.pollex.pam.enums.PersonalNotificationRoleEnum;
import com.pollex.pam.repository.CustomerRepository;
import com.pollex.pam.repository.PersonalNotificationRepository;
import com.pollex.pam.security.SecurityUtils;
import com.pollex.pam.service.dto.AppointmentUpdateDTO;
 
@Service
@Transactional
public class PersonalNotificationService {
 
    @Autowired
    PersonalNotificationRepository personalNotificationRepository;
 
    @Autowired
    ConsultantService consultantService;
 
    @Autowired
    AppointmentService appointmentService;
 
    @Autowired
    CustomerService customerService;
 
    @Autowired
    CustomerRepository customerRepository;
 
    @Autowired
    SatisfactionService satisfactionService;
 
    public List<PersonalNotification> getMyPersonalNotification(Long ownerId, PersonalNotificationRoleEnum role) {
        return personalNotificationRepository.findAllByOwnerRoleAndOwnerId(role, ownerId);
    }
 
    public void createSendSatisfactionToClientToCustomer(Appointment appointment) {
        PersonalNotification entity = new PersonalNotification();
        Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
        String content = consultant.getName()+"顧問請您填寫滿意度評比";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(appointment.getCustomerId());
        entity.setOwnerRole(PersonalNotificationRoleEnum.CUSTOMER);
        entity.setTitle("填寫滿意度");
        personalNotificationRepository.save(entity);
    }
 
    public void createSendNoticeToCustomer(Long appointmentId) {
        Appointment appointment = appointmentService.findById(appointmentId);
        PersonalNotification entity = new PersonalNotification();
        Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
        String content = "您有 "+consultant.getName()+"顧問的約訪通知";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(appointment.getCustomerId());
        entity.setOwnerRole(PersonalNotificationRoleEnum.CUSTOMER);
        entity.setTitle("顧問約訪通知");
        personalNotificationRepository.save(entity);
    }
 
    public void createNotFillAppointmentSatisfactionNumberToCustomer(Long customerId, int notFillSatisfactionSum) {
        PersonalNotification entity = new PersonalNotification();
 
        String content = "您有 "+notFillSatisfactionSum+" 筆顧問的滿意度需要填寫";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(customerId);
        entity.setOwnerRole(PersonalNotificationRoleEnum.CUSTOMER);
        entity.setTitle("客戶滿意度");
        personalNotificationRepository.save(entity);
    }
 
    public void createEditConsultantToConsultant(Consultant consultant) {
        PersonalNotification entity = new PersonalNotification();
        String content = "您的個人帳號設定已進行更新";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(consultant.getId());
        entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
        entity.setTitle("變更帳號資料");
        personalNotificationRepository.save(entity);
    }
 
    public void createMarkAppointmentDeletedToConsultant(Appointment appointment) {
        PersonalNotification entity = new PersonalNotification();
        Customer customer = customerRepository.findById(appointment.getCustomerId()).get();
        Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
        String content = customer.getName()+"客戶已取消您的預約";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(consultant.getId());
        entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
        entity.setTitle("取消預約提醒");
        personalNotificationRepository.save(entity);
    }
 
    public void createUpdateAppointmentToConsultant(Appointment appointment) {
        PersonalNotification entity = new PersonalNotification();
        Customer customer = customerRepository.findById(appointment.getCustomerId()).get();
        Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
        String content = customer.getName()+"客戶已更新您的預約資訊";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(consultant.getId());
        entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
        entity.setTitle("更新預約提醒");
        personalNotificationRepository.save(entity);
    }
 
    public void createScorefactionToConsultant(Satisfaction satisfaction) {
        PersonalNotification entity = new PersonalNotification();
        Appointment appointment = appointmentService.findById(satisfaction.getAppointmentId());
        Customer customer = customerRepository.findById(appointment.getCustomerId()).get();
        Consultant consultant = consultantService.findByAgentNo(appointment.getAgentNo());
        String content = customer.getName()+"客戶已對您進行滿意度評比";
        entity.setContent(content);
        entity.setNotificationType(NotificationTypeEnum.ACTIVITY);
        entity.setOwnerId(consultant.getId());
        entity.setOwnerRole(PersonalNotificationRoleEnum.CONSULTANT);
        entity.setTitle("客戶滿意度");
        personalNotificationRepository.save(entity);
    }
 
    public void readAllMyNotification() {
        if(StringUtils.hasText(SecurityUtils.getAgentNo())) {
            Long consultantId = consultantService.findByAgentNo(SecurityUtils.getAgentNo()).getId();
            readAllNotification(PersonalNotificationRoleEnum.CONSULTANT, consultantId);
        }else if(SecurityUtils.getCustomerDBId()!=null){
            readAllNotification(PersonalNotificationRoleEnum.CUSTOMER, SecurityUtils.getCustomerDBId());
        }
    }
 
    public void readAllNotification(PersonalNotificationRoleEnum ownerRole
            , Long ownerId) {
        List<PersonalNotification> allNotification = personalNotificationRepository.findAllByOwnerRoleAndOwnerId(ownerRole, ownerId);
        Instant today = Instant.now();
        allNotification.stream()
        .filter(notification ->  notification.getReadDate()==null)
        .forEach(notification ->{
            notification.setReadDate(today);
            personalNotificationRepository.saveAll(allNotification);
        });
    }
}