Subversion Repositories SmartDukaan

Rev

Rev 25835 | Rev 25850 | 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 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
 
39
	private Gson gson = new Gson();
40
 
41
	private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
42
 
43
	@Override
44
	public void sendNotification(SendNotificationModel sendNotificationModel) {
45
 
46
		SimpleCampaignParams scp = new SimpleCampaignParams();
47
		scp.setMessage(sendNotificationModel.getMessage());
48
		scp.setTitle(sendNotificationModel.getTitle());
49
		scp.setImageUrl(sendNotificationModel.getImageUrl());
50
		scp.setType(sendNotificationModel.getType());
51
		scp.setUrl(sendNotificationModel.getUrl());
52
		scp.setShowImage(sendNotificationModel.getShowImage());
53
		scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
54
		SimpleCampaign sc = new SimpleCampaign(scp);
55
		sc.setSimpleCampaignParams(scp);
56
 
57
		NotificationCampaign nc = new NotificationCampaign();
58
		nc.setName(sendNotificationModel.getCampaignName());
59
		nc.setImplementationType("SimpleCampaign");
60
		nc.setImplementationParams(gson.toJson(scp));
61
		nc.setMessageType(sendNotificationModel.getMessageType());
62
		nc.setDocumentId(sendNotificationModel.getDocumentId());
63
		nc.setCreatedTimestamp(LocalDateTime.now());
64
		notificationCampaignRepository.persist(nc);
65
 
66
		UserCampaign uc = null;
67
		List<Integer> userIds = sendNotificationModel.getUserIds();
68
		for (Integer userId : userIds) {
69
			uc = new UserCampaign();
70
			uc.setCampaignId(nc.getId());
71
			uc.setUserId(userId);
72
			uc.setPushTimestamp(LocalDateTime.now());
73
			userCampaignRepository.persist(uc);
74
		}
75
		LOGGER.info("userIds" + userIds);
76
 
77
		List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(userIds,
78
				LocalDateTime.now().minusMonths(3), LocalDateTime.now());
79
 
80
		LOGGER.info("devices" + devices);
81
		pushNotification(nc.getId(), devices);
82
	}
83
 
84
	public void pushNotification(int cid, List<Device> devices) {
85
		for (Device device : devices) {
86
			PushNotifications pn = new PushNotifications();
87
			pn.setNotificationCampaignid(cid);
88
			pn.setDeviceId(device.getId());
89
			pn.setUserId(device.getUser_id());
90
			pushNotificationRepository.persist(pn);
91
		}
92
 
93
	}
94
 
95
}