Subversion Repositories SmartDukaan

Rev

Rev 30859 | Rev 30990 | 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.common.web.client.RestClient;
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.http.conn.HttpHostConnectException;
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;

@Component
public class NotificationServiceImpl implements NotificationService {

        @Autowired
        UserCampaignRepository userCampaignRepository;

        @Autowired
        UserRepository dtrUserRepository;

        @Autowired
        UserAccountRepository userAccountRepository;

        @Autowired
        NotificationCampaignRepository notificationCampaignRepository;

        @Autowired
        DeviceRepository deviceRepository;

        @Autowired
        FofoStoreRepository fofoStoreRepository;

        @Autowired
        CsService csService;

        @Autowired
        RetailerService retailerService;

        @Autowired
        PushNotificationRepository pushNotificationRepository;

        @Autowired
        private Gson gson;

        @Autowired
        private RestClient restClient;

        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());
                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);
        }

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

        }

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

        @Override
        public void sendWhatsappMessage(String message, String title, String mobile)
                        throws HttpHostConnectException, ProfitMandiBusinessException {
                Map<String, String> requestheaders = new HashMap<>();
                requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
                Map<String, String> requestParams = new HashMap<>();
                requestParams.put("userid", String.valueOf(2000215976));
                requestParams.put("password", "MFRd!BBL");
                requestParams.put("send_to", "919816068341");
                requestParams.put("auth_scheme", "plain");
                requestParams.put("v", "1.1");

                requestParams.put("format", "json");

                requestParams.put("msg_type", "HSM");

                requestParams.put("method", "SENDMESSAGE");

                requestParams.put("msg", message);

                requestParams.put("isTemplate", "true");
                requestParams.put("header", title);
                String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams,
                                requestheaders);
                LOGGER.info("response" + response);
        }

}