@Service
public class NotificationService {
private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);
private final SimpMessagingTemplate simpMessagingTemplate; //1
@Autowired
private ProfileDao profileDao;
@Autowired
private CompanyDao companyDao;
@Autowired
private ProjectDao projectDao;
public NotificationService(SimpMessagingTemplate simpMessagingTemplate){
System.out.println("NotificationService 생성자 호출");
this.simpMessagingTemplate = simpMessagingTemplate; //1
//
}
public void sendToUser(Notification notification){
simpMessagingTemplate.convertAndSendToUser(notification.getProfileId(), "/msg", notification);
}
public void sendToUser(SendNotification sendNotification){
simpMessagingTemplate.convertAndSendToUser(sendNotification.getProfileId(), "/msg", sendNotification);
}
public void requestNotification(ReceiveNotification receiveNotification) {
System.out.println(receiveNotification.toString());
SendNotification result = null;
switch (receiveNotification.getNotiCode()){
case NotificationCodes.NOTI_002: //회사 로스트 승인
processNoti002(receiveNotification);
break;
case NotificationCodes.NOTI_003: //회사 로스터 제외
processNoti003(receiveNotification);
break;
case NotificationCodes.NOTI_005: //Mattermost 메시지 도착
processNoti005(receiveNotification);
break;
case NotificationCodes.NOTI_001: //여권 인증
case NotificationCodes.NOTI_004: //IPI Name Number 인증
case NotificationCodes.NOTI_008: //신고
case NotificationCodes.NOTI_010: //Account 인증(회사인증)
case NotificationCodes.NOTI_011: //Account 인증(그룹인증)
processNormalSystem(receiveNotification);
break;
case NotificationCodes.NOTI_012:
processNoti012(receiveNotification);
}
}
private void processNoti012(ReceiveNotification receiveNotification) {
String projectId = receiveNotification.getData().get("project_id");
Map<String, String> project = projectDao.getProjectInfo(projectId);
Map<String, String> data = new HashMap<>();
data.put("project_id", String.valueOf(project.get("proj_id")));
data.put("project_name", String.valueOf(project.get("proj_title")));
Map<String, String> profile = profileDao.getProfileInfo(receiveNotification.getNotiOwner());
for (String profileId : receiveNotification.getToUsers()) {
SendNotification sendNotification = SendNotification.builder()
.profileId(profileId)
.notiCode(receiveNotification.getNotiCode())
.notiOwner(receiveNotification.getNotiOwner())
.notiImg(profile.get("profile_img_url"))
.isView(false)
.regUTCTime(String.valueOf(Instant.now().getEpochSecond()))
.data(data)
.build();
System.out.println(sendNotification);
saveToDatabase(sendNotification);
sendToUser(sendNotification);
}
}
private void processNormalSystem(ReceiveNotification receiveNotification) {
for (String profileId : receiveNotification.getToUsers()) {
SendNotification sendNotification = SendNotification.builder()
.profileId(profileId)
.notiCode(receiveNotification.getNotiCode())
.notiOwner(NotificationCodes.NOTI_OWNER_SYSTEN)
.notiImg("-")
.isView(false)
.regUTCTime(String.valueOf(Instant.now().getEpochSecond()))
.build();
System.out.println(sendNotification);
saveToDatabase(sendNotification);
sendToUser(sendNotification);
}
}
private void processNoti005(ReceiveNotification receiveNotification) {
for (String profileId : receiveNotification.getToUsers()) {
Map<String, String> profile = profileDao.getProfileInfo(profileId);
Map<String, String> data = new HashMap<>();
data.put("mattermost_id", String.valueOf(profile.get("mattermost_id")));
SendNotification sendNotification = SendNotification.builder()
.profileId(profileId)
.notiCode(receiveNotification.getNotiCode())
.notiOwner(NotificationCodes.NOTI_OWNER_SYSTEN)
.notiImg("-")
.isView(false)
.regUTCTime(String.valueOf(Instant.now().getEpochSecond()))
.data(data)
.build();
System.out.println(sendNotification);
saveToDatabase(sendNotification);
sendToUser(sendNotification);
}
}
private void processNoti003(ReceiveNotification receiveNotification) {
Map<String, String> company = companyDao.getCompanyInfo(receiveNotification.getNotiOwner());
for (String profileId : receiveNotification.getToUsers()) {
Map<String, String> data = new HashMap<>();
data.put("company_id", String.valueOf(company.get("profile_id")));
data.put("company_name", company.get("profile_company_name"));
SendNotification sendNotification = SendNotification.builder()
.profileId(profileId)
.notiCode(receiveNotification.getNotiCode())
.notiOwner(receiveNotification.getNotiOwner())
.notiImg(company.get("profile_img_url"))
.isView(false)
.regUTCTime(String.valueOf(Instant.now().getEpochSecond()))
.data(data)
.build();
System.out.println(sendNotification);
saveToDatabase(sendNotification);
sendToUser(sendNotification);
}
}
private void processNoti002(ReceiveNotification receiveNotification) {
Map<String, String> company = companyDao.getCompanyInfo(receiveNotification.getNotiOwner());
for (String profileId : receiveNotification.getToUsers()) {
Map<String, String> data = new HashMap<>();
data.put("company_id", String.valueOf(company.get("profile_id")));
data.put("company_name", company.get("profile_company_name"));
SendNotification sendNotification = SendNotification.builder()
.profileId(profileId)
.notiCode(receiveNotification.getNotiCode())
.notiOwner(receiveNotification.getNotiOwner())
.notiImg(company.get("profile_img_url"))
.isView(false)
.regUTCTime(String.valueOf(Instant.now().getEpochSecond()))
.data(data)
.build();
System.out.println(sendNotification);
saveToDatabase(sendNotification);
sendToUser(sendNotification);
}
}
private void saveToDatabase(SendNotification sendNotification){
}
}