Subversion Repositories SmartDukaan

Rev

Rev 30859 | Rev 30990 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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