Rev 30256 | Rev 30989 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.service;import com.google.gson.Gson;import com.spice.profitmandi.common.enumuration.MessageType;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.SendNotificationModel;import com.spice.profitmandi.dao.entity.auth.AuthUser;import com.spice.profitmandi.dao.entity.dtr.NotificationCampaign;import com.spice.profitmandi.dao.entity.dtr.PushNotifications;import com.spice.profitmandi.dao.entity.dtr.User;import com.spice.profitmandi.dao.entity.dtr.UserCampaign;import com.spice.profitmandi.dao.entity.user.Device;import com.spice.profitmandi.dao.model.SimpleCampaign;import com.spice.profitmandi.dao.model.SimpleCampaignParams;import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;import com.spice.profitmandi.dao.repository.cs.CsService;import com.spice.profitmandi.dao.repository.dtr.*;import com.spice.profitmandi.service.user.RetailerService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.time.LocalDateTime;import java.util.*;import java.util.stream.Collectors;@Componentpublic class NotificationServiceImpl implements NotificationService {@AutowiredUserCampaignRepository userCampaignRepository;@AutowiredUserRepository dtrUserRepository;@AutowiredUserAccountRepository userAccountRepository;@AutowiredNotificationCampaignRepository notificationCampaignRepository;@AutowiredDeviceRepository deviceRepository;@AutowiredFofoStoreRepository fofoStoreRepository;@AutowiredCsService csService;@AutowiredRetailerService retailerService;@AutowiredPushNotificationRepository pushNotificationRepository;@Autowiredprivate Gson gson;private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);@Overridepublic void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {SimpleCampaignParams scp = new SimpleCampaignParams();scp.setMessage(sendNotificationModel.getMessage());scp.setTitle(sendNotificationModel.getTitle());scp.setImageUrl(sendNotificationModel.getImageUrl());scp.setType(sendNotificationModel.getType());scp.setUrl(sendNotificationModel.getUrl());scp.setShowImage(sendNotificationModel.getShowImage());scp.setExpireTimestamp(sendNotificationModel.getExpiresat());SimpleCampaign sc = new SimpleCampaign(scp);sc.setSimpleCampaignParams(scp);NotificationCampaign nc = new NotificationCampaign();nc.setName(sendNotificationModel.getCampaignName());nc.setImplementationType("SimpleCampaign");nc.setImplementationParams(gson.toJson(scp));nc.setMessageType(sendNotificationModel.getMessageType());nc.setDocumentId(sendNotificationModel.getDocumentId());nc.setCreatedTimestamp(LocalDateTime.now());notificationCampaignRepository.persist(nc);UserCampaign uc = null;Set<Integer> userIds = new HashSet<>();if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {userIds.addAll(sendNotificationModel.getUserIds());}if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {userIds.addAll(fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream().map(x -> x.getId()).collect(Collectors.toList()));}LOGGER.info("userIds" + userIds);for (Integer userId : userIds) {uc = new UserCampaign();uc.setCampaignId(nc.getId());uc.setUserId(userId);uc.setPushTimestamp(LocalDateTime.now());userCampaignRepository.persist(uc);}List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),LocalDateTime.now().minusMonths(3), LocalDateTime.now());LOGGER.info("devices" + devices);pushNotification(nc.getId(), devices);}@Overridepublic void sendNotificationToAll(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {sendNotificationModel.setUserIds(fofoStoreRepository.selectAllDtrUserIds());Set<AuthUser> authUsers = new HashSet<>(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());emailIds.add("devkinandan.lal@smartdukaan.com");List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);sendNotificationModel.getUserIds().addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));this.sendNotification(sendNotificationModel);}@Overridepublic void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)throws ProfitMandiBusinessException {SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();sendNotificationModel.setCampaignName(campaignName);sendNotificationModel.setMessageType(messageType);sendNotificationModel.setTitle(title);sendNotificationModel.setMessage(message);int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);sendNotificationModel.setUserIds(Arrays.asList(userId));sendNotificationModel.setMessageType(MessageType.wallet);this.sendNotification(sendNotificationModel);}public SendNotificationModel getDefaultNotificationModel() {SendNotificationModel sendNotificationModel = new SendNotificationModel();sendNotificationModel.setType("url");sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));sendNotificationModel.setMessageType(MessageType.notification);return sendNotificationModel;}public void pushNotification(int cid, List<Device> devices) {for (Device device : devices) {PushNotifications pn = new PushNotifications();pn.setNotificationCampaignid(cid);pn.setDeviceId(device.getId());pn.setUserId(device.getUser_id());pushNotificationRepository.persist(pn);}}@Overridepublic void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message,String url) throws ProfitMandiBusinessException {SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();sendNotificationModel.setCampaignName(campaignName);sendNotificationModel.setMessageType(messageType);sendNotificationModel.setTitle(title);sendNotificationModel.setMessage(message);int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);sendNotificationModel.setUserIds(Arrays.asList(userId));sendNotificationModel.setMessageType(messageType);sendNotificationModel.setUrl(url);this.sendNotification(sendNotificationModel);}}