Subversion Repositories SmartDukaan

Rev

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