Subversion Repositories SmartDukaan

Rev

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