Subversion Repositories SmartDukaan

Rev

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