| 25822 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import com.google.gson.Gson;
|
| 30025 |
amit.gupta |
4 |
import com.spice.profitmandi.common.enumuration.MessageType;
|
| 29198 |
manish |
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 29927 |
amit.gupta |
6 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 25822 |
amit.gupta |
7 |
import com.spice.profitmandi.common.model.SendNotificationModel;
|
| 29927 |
amit.gupta |
8 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 25822 |
amit.gupta |
9 |
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.dtr.PushNotifications;
|
| 29927 |
amit.gupta |
11 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
| 25822 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.entity.dtr.UserCampaign;
|
|
|
13 |
import com.spice.profitmandi.dao.entity.user.Device;
|
|
|
14 |
import com.spice.profitmandi.dao.model.SimpleCampaign;
|
|
|
15 |
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
|
| 29927 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
18 |
import com.spice.profitmandi.dao.repository.dtr.*;
|
| 29198 |
manish |
19 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 29927 |
amit.gupta |
20 |
import org.apache.logging.log4j.LogManager;
|
|
|
21 |
import org.apache.logging.log4j.Logger;
|
|
|
22 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
23 |
import org.springframework.stereotype.Component;
|
| 25822 |
amit.gupta |
24 |
|
| 29927 |
amit.gupta |
25 |
import java.time.LocalDateTime;
|
| 30025 |
amit.gupta |
26 |
import java.util.*;
|
| 29927 |
amit.gupta |
27 |
import java.util.stream.Collectors;
|
|
|
28 |
|
| 25835 |
amit.gupta |
29 |
@Component
|
| 25822 |
amit.gupta |
30 |
public class NotificationServiceImpl implements NotificationService {
|
|
|
31 |
|
|
|
32 |
@Autowired
|
|
|
33 |
UserCampaignRepository userCampaignRepository;
|
|
|
34 |
|
|
|
35 |
@Autowired
|
| 29927 |
amit.gupta |
36 |
UserRepository dtrUserRepository;
|
|
|
37 |
|
|
|
38 |
@Autowired
|
| 30025 |
amit.gupta |
39 |
UserAccountRepository userAccountRepository;
|
|
|
40 |
|
|
|
41 |
@Autowired
|
| 25822 |
amit.gupta |
42 |
NotificationCampaignRepository notificationCampaignRepository;
|
|
|
43 |
|
|
|
44 |
@Autowired
|
|
|
45 |
DeviceRepository deviceRepository;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
| 29198 |
manish |
48 |
FofoStoreRepository fofoStoreRepository;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
| 29927 |
amit.gupta |
51 |
CsService csService;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
| 29198 |
manish |
54 |
RetailerService retailerService;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
| 25822 |
amit.gupta |
57 |
PushNotificationRepository pushNotificationRepository;
|
|
|
58 |
|
| 25850 |
amit.gupta |
59 |
@Autowired
|
| 25851 |
amit.gupta |
60 |
private Gson gson;
|
| 25822 |
amit.gupta |
61 |
|
|
|
62 |
private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
|
|
|
63 |
|
|
|
64 |
@Override
|
| 29198 |
manish |
65 |
public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
| 25822 |
amit.gupta |
66 |
|
|
|
67 |
SimpleCampaignParams scp = new SimpleCampaignParams();
|
|
|
68 |
scp.setMessage(sendNotificationModel.getMessage());
|
|
|
69 |
scp.setTitle(sendNotificationModel.getTitle());
|
|
|
70 |
scp.setImageUrl(sendNotificationModel.getImageUrl());
|
|
|
71 |
scp.setType(sendNotificationModel.getType());
|
|
|
72 |
scp.setUrl(sendNotificationModel.getUrl());
|
|
|
73 |
scp.setShowImage(sendNotificationModel.getShowImage());
|
|
|
74 |
scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
|
|
|
75 |
SimpleCampaign sc = new SimpleCampaign(scp);
|
|
|
76 |
sc.setSimpleCampaignParams(scp);
|
|
|
77 |
|
|
|
78 |
NotificationCampaign nc = new NotificationCampaign();
|
|
|
79 |
nc.setName(sendNotificationModel.getCampaignName());
|
|
|
80 |
nc.setImplementationType("SimpleCampaign");
|
|
|
81 |
nc.setImplementationParams(gson.toJson(scp));
|
|
|
82 |
nc.setMessageType(sendNotificationModel.getMessageType());
|
|
|
83 |
nc.setDocumentId(sendNotificationModel.getDocumentId());
|
|
|
84 |
nc.setCreatedTimestamp(LocalDateTime.now());
|
|
|
85 |
notificationCampaignRepository.persist(nc);
|
|
|
86 |
|
|
|
87 |
UserCampaign uc = null;
|
| 29198 |
manish |
88 |
|
| 29204 |
amit.gupta |
89 |
Set<Integer> userIds = new HashSet<>();
|
| 30217 |
tejbeer |
90 |
if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
|
| 29204 |
amit.gupta |
91 |
userIds.addAll(sendNotificationModel.getUserIds());
|
|
|
92 |
}
|
| 29198 |
manish |
93 |
|
| 30217 |
tejbeer |
94 |
if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
|
| 29204 |
amit.gupta |
95 |
userIds.addAll(fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
|
|
|
96 |
.map(x -> x.getId()).collect(Collectors.toList()));
|
| 29198 |
manish |
97 |
}
|
|
|
98 |
|
|
|
99 |
LOGGER.info("userIds" + userIds);
|
|
|
100 |
|
| 25822 |
amit.gupta |
101 |
for (Integer userId : userIds) {
|
|
|
102 |
uc = new UserCampaign();
|
|
|
103 |
uc.setCampaignId(nc.getId());
|
|
|
104 |
uc.setUserId(userId);
|
|
|
105 |
uc.setPushTimestamp(LocalDateTime.now());
|
|
|
106 |
userCampaignRepository.persist(uc);
|
|
|
107 |
}
|
|
|
108 |
|
| 29204 |
amit.gupta |
109 |
List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),
|
| 25822 |
amit.gupta |
110 |
LocalDateTime.now().minusMonths(3), LocalDateTime.now());
|
|
|
111 |
|
|
|
112 |
LOGGER.info("devices" + devices);
|
| 28400 |
tejbeer |
113 |
pushNotification(nc.getId(), devices);
|
|
|
114 |
|
| 25822 |
amit.gupta |
115 |
}
|
|
|
116 |
|
| 29927 |
amit.gupta |
117 |
@Override
|
|
|
118 |
public void sendNotificationToAll(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
|
|
119 |
sendNotificationModel.setUserIds(fofoStoreRepository.selectAllDtrUserIds());
|
| 30217 |
tejbeer |
120 |
Set<AuthUser> authUsers = new HashSet<>(
|
|
|
121 |
csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
|
| 29927 |
amit.gupta |
122 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
|
|
|
123 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
|
|
|
124 |
List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
| 30256 |
tejbeer |
125 |
emailIds.add("devkinandan.lal@smartdukaan.com");
|
| 29940 |
amit.gupta |
126 |
List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
|
| 30217 |
tejbeer |
127 |
sendNotificationModel.getUserIds()
|
|
|
128 |
.addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
| 29927 |
amit.gupta |
129 |
this.sendNotification(sendNotificationModel);
|
|
|
130 |
}
|
|
|
131 |
|
| 30025 |
amit.gupta |
132 |
@Override
|
| 30217 |
tejbeer |
133 |
public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)
|
|
|
134 |
throws ProfitMandiBusinessException {
|
| 30025 |
amit.gupta |
135 |
SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
|
|
|
136 |
sendNotificationModel.setCampaignName(campaignName);
|
| 30066 |
amit.gupta |
137 |
sendNotificationModel.setMessageType(messageType);
|
| 30025 |
amit.gupta |
138 |
sendNotificationModel.setTitle(title);
|
|
|
139 |
sendNotificationModel.setMessage(message);
|
|
|
140 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
141 |
sendNotificationModel.setUserIds(Arrays.asList(userId));
|
|
|
142 |
sendNotificationModel.setMessageType(MessageType.wallet);
|
| 30066 |
amit.gupta |
143 |
this.sendNotification(sendNotificationModel);
|
| 30025 |
amit.gupta |
144 |
}
|
|
|
145 |
|
|
|
146 |
public SendNotificationModel getDefaultNotificationModel() {
|
|
|
147 |
SendNotificationModel sendNotificationModel = new SendNotificationModel();
|
|
|
148 |
sendNotificationModel.setType("url");
|
|
|
149 |
sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
|
|
|
150 |
sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
|
|
|
151 |
sendNotificationModel.setMessageType(MessageType.notification);
|
|
|
152 |
return sendNotificationModel;
|
|
|
153 |
}
|
|
|
154 |
|
| 25822 |
amit.gupta |
155 |
public void pushNotification(int cid, List<Device> devices) {
|
| 28397 |
tejbeer |
156 |
|
| 25822 |
amit.gupta |
157 |
for (Device device : devices) {
|
|
|
158 |
PushNotifications pn = new PushNotifications();
|
|
|
159 |
pn.setNotificationCampaignid(cid);
|
|
|
160 |
pn.setDeviceId(device.getId());
|
|
|
161 |
pn.setUserId(device.getUser_id());
|
|
|
162 |
pushNotificationRepository.persist(pn);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
}
|
|
|
166 |
|
| 30859 |
tejbeer |
167 |
@Override
|
|
|
168 |
public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message,
|
|
|
169 |
String url) throws ProfitMandiBusinessException {
|
|
|
170 |
SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
|
|
|
171 |
sendNotificationModel.setCampaignName(campaignName);
|
|
|
172 |
sendNotificationModel.setMessageType(messageType);
|
|
|
173 |
sendNotificationModel.setTitle(title);
|
|
|
174 |
sendNotificationModel.setMessage(message);
|
|
|
175 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
176 |
sendNotificationModel.setUserIds(Arrays.asList(userId));
|
|
|
177 |
sendNotificationModel.setMessageType(messageType);
|
|
|
178 |
sendNotificationModel.setUrl(url);
|
|
|
179 |
this.sendNotification(sendNotificationModel);
|
|
|
180 |
}
|
|
|
181 |
|
| 25822 |
amit.gupta |
182 |
}
|