Subversion Repositories SmartDukaan

Rev

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