| 25822 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 29198 |
manish |
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.HashSet;
|
| 25822 |
amit.gupta |
6 |
import java.util.List;
|
| 29198 |
manish |
7 |
import java.util.Map;
|
|
|
8 |
import java.util.stream.Collectors;
|
| 25822 |
amit.gupta |
9 |
|
|
|
10 |
import org.apache.logging.log4j.LogManager;
|
|
|
11 |
import org.apache.logging.log4j.Logger;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 25835 |
amit.gupta |
13 |
import org.springframework.stereotype.Component;
|
| 25822 |
amit.gupta |
14 |
|
|
|
15 |
import com.google.gson.Gson;
|
| 29198 |
manish |
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
17 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 25822 |
amit.gupta |
18 |
import com.spice.profitmandi.common.model.SendNotificationModel;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
|
|
|
20 |
import com.spice.profitmandi.dao.entity.dtr.PushNotifications;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
|
| 29198 |
manish |
22 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 25822 |
amit.gupta |
23 |
import com.spice.profitmandi.dao.entity.user.Device;
|
|
|
24 |
import com.spice.profitmandi.dao.model.SimpleCampaign;
|
|
|
25 |
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
|
|
|
26 |
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
|
| 29198 |
manish |
27 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 25822 |
amit.gupta |
28 |
import com.spice.profitmandi.dao.repository.dtr.NotificationCampaignRepository;
|
|
|
29 |
import com.spice.profitmandi.dao.repository.dtr.PushNotificationRepository;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.dtr.UserCampaignRepository;
|
| 29198 |
manish |
31 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 25822 |
amit.gupta |
32 |
|
| 25835 |
amit.gupta |
33 |
@Component
|
| 25822 |
amit.gupta |
34 |
public class NotificationServiceImpl implements NotificationService {
|
|
|
35 |
|
|
|
36 |
@Autowired
|
|
|
37 |
UserCampaignRepository userCampaignRepository;
|
|
|
38 |
|
|
|
39 |
@Autowired
|
|
|
40 |
NotificationCampaignRepository notificationCampaignRepository;
|
|
|
41 |
|
|
|
42 |
@Autowired
|
|
|
43 |
DeviceRepository deviceRepository;
|
|
|
44 |
|
|
|
45 |
@Autowired
|
| 29198 |
manish |
46 |
FofoStoreRepository fofoStoreRepository;
|
|
|
47 |
|
|
|
48 |
@Autowired
|
|
|
49 |
RetailerService retailerService;
|
|
|
50 |
|
|
|
51 |
@Autowired
|
| 25822 |
amit.gupta |
52 |
PushNotificationRepository pushNotificationRepository;
|
|
|
53 |
|
| 25850 |
amit.gupta |
54 |
@Autowired
|
| 25851 |
amit.gupta |
55 |
private Gson gson;
|
| 25822 |
amit.gupta |
56 |
|
|
|
57 |
private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
|
|
|
58 |
|
|
|
59 |
@Override
|
| 29198 |
manish |
60 |
public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
| 25822 |
amit.gupta |
61 |
|
|
|
62 |
SimpleCampaignParams scp = new SimpleCampaignParams();
|
|
|
63 |
scp.setMessage(sendNotificationModel.getMessage());
|
|
|
64 |
scp.setTitle(sendNotificationModel.getTitle());
|
|
|
65 |
scp.setImageUrl(sendNotificationModel.getImageUrl());
|
|
|
66 |
scp.setType(sendNotificationModel.getType());
|
|
|
67 |
scp.setUrl(sendNotificationModel.getUrl());
|
|
|
68 |
scp.setShowImage(sendNotificationModel.getShowImage());
|
|
|
69 |
scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
|
|
|
70 |
SimpleCampaign sc = new SimpleCampaign(scp);
|
|
|
71 |
sc.setSimpleCampaignParams(scp);
|
|
|
72 |
|
|
|
73 |
NotificationCampaign nc = new NotificationCampaign();
|
|
|
74 |
nc.setName(sendNotificationModel.getCampaignName());
|
|
|
75 |
nc.setImplementationType("SimpleCampaign");
|
|
|
76 |
nc.setImplementationParams(gson.toJson(scp));
|
|
|
77 |
nc.setMessageType(sendNotificationModel.getMessageType());
|
|
|
78 |
nc.setDocumentId(sendNotificationModel.getDocumentId());
|
|
|
79 |
nc.setCreatedTimestamp(LocalDateTime.now());
|
|
|
80 |
notificationCampaignRepository.persist(nc);
|
|
|
81 |
|
|
|
82 |
UserCampaign uc = null;
|
| 29198 |
manish |
83 |
|
|
|
84 |
List<Integer> fofoIds = fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
|
|
|
85 |
.map(x -> x.getId()).collect(Collectors.toList());
|
|
|
86 |
|
|
|
87 |
Map<Integer, CustomRetailer> userIdMap = retailerService.getFofoRetailerUserId(fofoIds);
|
|
|
88 |
List<Integer> users = sendNotificationModel.getUserIds();
|
|
|
89 |
|
|
|
90 |
if (!fofoIds.isEmpty()) {
|
|
|
91 |
users.addAll(userIdMap.keySet().stream().map(x -> x).collect(Collectors.toList()));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
LOGGER.info("users" + users);
|
|
|
95 |
HashSet<Integer> userHashset = new HashSet<Integer>();
|
|
|
96 |
userHashset.addAll(users);
|
|
|
97 |
|
|
|
98 |
List<Integer> userIds = new ArrayList<>(userHashset);
|
|
|
99 |
|
|
|
100 |
LOGGER.info("userIds" + userIds);
|
|
|
101 |
|
| 25822 |
amit.gupta |
102 |
for (Integer userId : userIds) {
|
|
|
103 |
uc = new UserCampaign();
|
|
|
104 |
uc.setCampaignId(nc.getId());
|
|
|
105 |
uc.setUserId(userId);
|
|
|
106 |
uc.setPushTimestamp(LocalDateTime.now());
|
|
|
107 |
userCampaignRepository.persist(uc);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(userIds,
|
|
|
111 |
LocalDateTime.now().minusMonths(3), LocalDateTime.now());
|
|
|
112 |
|
|
|
113 |
LOGGER.info("devices" + devices);
|
| 28400 |
tejbeer |
114 |
pushNotification(nc.getId(), devices);
|
|
|
115 |
|
| 25822 |
amit.gupta |
116 |
}
|
|
|
117 |
|
|
|
118 |
public void pushNotification(int cid, List<Device> devices) {
|
| 28397 |
tejbeer |
119 |
|
| 25822 |
amit.gupta |
120 |
for (Device device : devices) {
|
|
|
121 |
PushNotifications pn = new PushNotifications();
|
|
|
122 |
pn.setNotificationCampaignid(cid);
|
|
|
123 |
pn.setDeviceId(device.getId());
|
|
|
124 |
pn.setUserId(device.getUser_id());
|
|
|
125 |
pushNotificationRepository.persist(pn);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
}
|