| 25822 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
|
|
|
6 |
import org.apache.logging.log4j.LogManager;
|
|
|
7 |
import org.apache.logging.log4j.Logger;
|
|
|
8 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 25835 |
amit.gupta |
9 |
import org.springframework.stereotype.Component;
|
| 25822 |
amit.gupta |
10 |
|
|
|
11 |
import com.google.gson.Gson;
|
|
|
12 |
import com.spice.profitmandi.common.model.SendNotificationModel;
|
|
|
13 |
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
|
|
|
14 |
import com.spice.profitmandi.dao.entity.dtr.PushNotifications;
|
|
|
15 |
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
|
|
|
16 |
import com.spice.profitmandi.dao.entity.user.Device;
|
|
|
17 |
import com.spice.profitmandi.dao.model.SimpleCampaign;
|
|
|
18 |
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
|
|
|
20 |
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
|
|
|
21 |
import com.spice.profitmandi.dao.repository.dtr.PushNotificationRepository;
|
|
|
22 |
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
|
|
|
23 |
|
| 25835 |
amit.gupta |
24 |
@Component
|
| 25822 |
amit.gupta |
25 |
public class NotificationServiceImpl implements NotificationService {
|
|
|
26 |
|
|
|
27 |
@Autowired
|
|
|
28 |
UserCampaignRepository userCampaignRepository;
|
|
|
29 |
|
|
|
30 |
@Autowired
|
|
|
31 |
NotificationCampaignRepository notificationCampaignRepository;
|
|
|
32 |
|
|
|
33 |
@Autowired
|
|
|
34 |
DeviceRepository deviceRepository;
|
|
|
35 |
|
|
|
36 |
@Autowired
|
|
|
37 |
PushNotificationRepository pushNotificationRepository;
|
|
|
38 |
|
| 25850 |
amit.gupta |
39 |
@Autowired
|
| 25851 |
amit.gupta |
40 |
private Gson gson;
|
| 25822 |
amit.gupta |
41 |
|
|
|
42 |
private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
|
|
|
43 |
|
|
|
44 |
@Override
|
|
|
45 |
public void sendNotification(SendNotificationModel sendNotificationModel) {
|
|
|
46 |
|
|
|
47 |
SimpleCampaignParams scp = new SimpleCampaignParams();
|
|
|
48 |
scp.setMessage(sendNotificationModel.getMessage());
|
|
|
49 |
scp.setTitle(sendNotificationModel.getTitle());
|
|
|
50 |
scp.setImageUrl(sendNotificationModel.getImageUrl());
|
|
|
51 |
scp.setType(sendNotificationModel.getType());
|
|
|
52 |
scp.setUrl(sendNotificationModel.getUrl());
|
|
|
53 |
scp.setShowImage(sendNotificationModel.getShowImage());
|
|
|
54 |
scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
|
|
|
55 |
SimpleCampaign sc = new SimpleCampaign(scp);
|
|
|
56 |
sc.setSimpleCampaignParams(scp);
|
|
|
57 |
|
|
|
58 |
NotificationCampaign nc = new NotificationCampaign();
|
|
|
59 |
nc.setName(sendNotificationModel.getCampaignName());
|
|
|
60 |
nc.setImplementationType("SimpleCampaign");
|
|
|
61 |
nc.setImplementationParams(gson.toJson(scp));
|
|
|
62 |
nc.setMessageType(sendNotificationModel.getMessageType());
|
|
|
63 |
nc.setDocumentId(sendNotificationModel.getDocumentId());
|
|
|
64 |
nc.setCreatedTimestamp(LocalDateTime.now());
|
|
|
65 |
notificationCampaignRepository.persist(nc);
|
|
|
66 |
|
|
|
67 |
UserCampaign uc = null;
|
|
|
68 |
List<Integer> userIds = sendNotificationModel.getUserIds();
|
|
|
69 |
for (Integer userId : userIds) {
|
|
|
70 |
uc = new UserCampaign();
|
|
|
71 |
uc.setCampaignId(nc.getId());
|
|
|
72 |
uc.setUserId(userId);
|
|
|
73 |
uc.setPushTimestamp(LocalDateTime.now());
|
|
|
74 |
userCampaignRepository.persist(uc);
|
|
|
75 |
}
|
|
|
76 |
LOGGER.info("userIds" + userIds);
|
|
|
77 |
|
|
|
78 |
List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(userIds,
|
|
|
79 |
LocalDateTime.now().minusMonths(3), LocalDateTime.now());
|
|
|
80 |
|
|
|
81 |
LOGGER.info("devices" + devices);
|
| 28400 |
tejbeer |
82 |
pushNotification(nc.getId(), devices);
|
|
|
83 |
|
| 25822 |
amit.gupta |
84 |
}
|
|
|
85 |
|
|
|
86 |
public void pushNotification(int cid, List<Device> devices) {
|
| 28397 |
tejbeer |
87 |
|
| 25822 |
amit.gupta |
88 |
for (Device device : devices) {
|
|
|
89 |
PushNotifications pn = new PushNotifications();
|
|
|
90 |
pn.setNotificationCampaignid(cid);
|
|
|
91 |
pn.setDeviceId(device.getId());
|
|
|
92 |
pn.setUserId(device.getUser_id());
|
|
|
93 |
pushNotificationRepository.persist(pn);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
}
|