Subversion Repositories SmartDukaan

Rev

Rev 29204 | Rev 29940 | 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.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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class NotificationServiceImpl implements NotificationService {

        @Autowired
        UserCampaignRepository userCampaignRepository;

        @Autowired
        UserRepository dtrUserRepository;

        @Autowired
        NotificationCampaignRepository notificationCampaignRepository;

        @Autowired
        DeviceRepository deviceRepository;

        @Autowired
        FofoStoreRepository fofoStoreRepository;

        @Autowired
        CsService csService;

        @Autowired
        RetailerService retailerService;

        @Autowired
        PushNotificationRepository pushNotificationRepository;

        @Autowired
        private Gson gson;

        private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);

        @Override
        public 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);

        }

        @Override
        public 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());
                List<User> user = dtrUserRepository.selectAllByEmailIds(emailIds);
                sendNotificationModel.getUserIds().addAll(user.stream().map(x -> x.getId()).collect(Collectors.toList()));
                this.sendNotification(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);
                }

        }

}