Subversion Repositories SmartDukaan

Rev

Rev 26991 | Rev 27044 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.repository.cs;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.auth.AuthUser;
import com.spice.profitmandi.dao.entity.cs.Activity;
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
import com.spice.profitmandi.dao.entity.cs.Position;
import com.spice.profitmandi.dao.entity.cs.Region;
import com.spice.profitmandi.dao.entity.cs.Ticket;
import com.spice.profitmandi.dao.entity.cs.TicketAssigned;
import com.spice.profitmandi.dao.entity.cs.TicketCategory;
import com.spice.profitmandi.dao.entity.cs.TicketSubCategory;
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.dtr.LeadRepository;
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
import com.spice.profitmandi.service.PartnerInvestmentService;
import com.spice.profitmandi.service.user.RetailerService;

@Component
public class CsServiceImpl implements CsService {

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

        private static final String ASSIGNED_TICKET = "Dear %s,You have assigned a ticket by %s with ticketId#%s.Regards\nSmartdukaan";
        private static final String ASSINMENT_SUBJECT = "Assignment Ticket";

        @Autowired
        TicketRepository ticketRepository;

        @Autowired
        JavaMailSender mailSender;

        @Autowired
        TicketCategoryRepository ticketCategoryRepository;

        @Autowired
        TicketSubCategoryRepository ticketSubCategoryRepository;

        @Autowired
        ActivityRepository activityRepository;

        @Autowired
        PartnerRegionRepository partnerRegionRepository;

        @Autowired
        private PositionRepository positionRepository;

        @Autowired
        private AuthRepository authRepository;

        @Autowired
        private RetailerService retailerService;

        @Autowired
        private RegionRepository regionRepository;

        @Autowired
        private TicketAssignedRepository ticketAssignedRepository;

        @Autowired
        private PartnersPositionRepository partnersPositionRepository;

        @Autowired
        private FofoStoreRepository fofoStoreRepository;

        @Autowired
        private FofoOrderItemRepository fofoOrderItemRepository;

        @Autowired
        private LeadRepository leadRepository;

        @Autowired
        private HygieneDataRepository hygieneDataRepository;

        @Autowired
        private PartnerInvestmentService partnerInvestmentService;

        @Autowired
        private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;

        @Override
        public void createTicket(int fofoId, int categoryId, int subcategoryId, String message)
                        throws ProfitMandiBusinessException {

                ActivityType type = ActivityType.OPENED;
                EscalationType escalationTypeL1 = EscalationType.L1;
                EscalationType escalationTypeL2 = EscalationType.L2;
                EscalationType escalationTypeL3 = EscalationType.L3;
                Ticket ticket = new Ticket();
                TicketAssigned ticketAssigned = new TicketAssigned();
                ticket.setSubCategoryId(subcategoryId);
                ticket.setFofoId(fofoId);
                ticket.setCreateTimestamp(LocalDateTime.now());
                ticket.setUpdateTimestamp(LocalDateTime.now());
                ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
                ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
                ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
                ticket.setHappyCode(getRandomString());
                int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
                LOGGER.info("l1Auth" + l1Auth);
                int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
                LOGGER.info("l2Auth" + l2Auth);
                int l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
                LOGGER.info("l3Auth" + l3Auth);
                if (l1Auth == 0) {
                        if (l2Auth == 0) {
                                this.setAssignment(ticket, ticketAssigned);
                        } else {
                                ticketAssigned.setAssineeId(l2Auth);
                                ticket.setL1AuthUser(l2Auth);
                                ticket.setL2AuthUser(l2Auth);
                        }
                } else {
                        ticketAssigned.setAssineeId(l1Auth);
                        ticket.setL1AuthUser(l1Auth);
                        ticket.setL2AuthUser(l2Auth);
                }
                if (ticket.getL2AuthUser() == 0) {
                        ticket.setL2AuthUser(l3Auth);
                }
                ticket.setL3AuthUser(l3Auth);
                ticketRepository.persist(ticket);
                ticketAssigned.setTicketId(ticket.getId());
                ticketAssignedRepository.persist(ticketAssigned);
                AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
                this.sendAssignedTicketMail(authUser, ticket);
                Activity activity = this.createActivity(type, message, 0);
                this.addActivity(ticket.getId(), activity);
        }

        private Activity createActivity(ActivityType activityType, String message, int createdBy) {
                Activity activity = new Activity();
                activity.setMessage(message);
                activity.setCreatedBy(createdBy);
                activity.setType(activityType);
                activity.setCreateTimestamp(LocalDateTime.now());
                return activity;
        }

        private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {

                List<Position> positions = null;
                if (escalationType == EscalationType.L3) {
                        positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
                } else {
                        List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
                        LOGGER.info("regions=" + regions);
                        if (regions.size() == 0) {
                                regions = partnerRegionRepository.selectByfofoId(0);
                                LOGGER.info("regions=" + regions);
                        }
                        for (PartnerRegion region : regions) {
                                LOGGER.info(categoryId + ":" + escalationType + ":" + region.getRegionId());
                                positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
                                                region.getRegionId());
                                LOGGER.info("positions:-" + positions);
                                if (positions.size() > 0) {
                                        break;
                                }

                        }
                        if (positions.size() == 0) {
                                if (escalationType == EscalationType.L1) {
                                        regions = partnerRegionRepository.selectByfofoId(0);
                                        LOGGER.info("regions=" + regions);
                                        for (PartnerRegion region : regions) {
                                                positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
                                                                escalationType, region.getRegionId());
                                                LOGGER.info("positions:-" + positions);
                                                if (positions.size() > 0) {
                                                        break;
                                                }
                                        }
                                } else {
                                        regions = partnerRegionRepository.selectByfofoId(0);
                                        LOGGER.info("regions=" + regions);
                                        for (PartnerRegion region : regions) {
                                                positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
                                                                escalationType, region.getRegionId());
                                                LOGGER.info("positions:-" + positions);
                                                if (positions.size() > 0) {
                                                        break;
                                                }
                                        }
                                }
                        }

                }
                if (positions.size() == 0) {
                        return 0;
                }
                LOGGER.info(positions.get(0).getAuthUserId());
                return positions.get(0).getAuthUserId();
                /*
                 * if(ActivityType.escalationTypes.contains(escalationType)) {
                 * //escalationMatrix } else { throw new
                 * ProfitMandiBusinessException("SubCategory", subcategoryId,
                 * "Could not find Assignee for "); }
                 */

        }

        @Override
        public void addActivity(int ticketId, Activity activity) {
                activity.setTicketId(ticketId);
                activityRepository.persist(activity);
        }

        private String getRandomString() {
                int length = 4;
                boolean useLetters = false;
                boolean useNumbers = true;
                String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
                return generatedString;
        }

        @Override
        public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {

                for (int fofoId : fofoIds) {
                        PartnerRegion partnerRegion = new PartnerRegion();
                        partnerRegion.setFofoId(fofoId);
                        partnerRegion.setRegionId(regionId);
                        partnerRegionRepository.persist(partnerRegion);
                }
        }

        @Override
        public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
                Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();

                for (TicketAssigned ticketAssigned : ticketAssigneds) {
                        AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
                        authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
                }
                return authUserIdAndAuthUserMap;
        }

        @Override
        public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
                Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();

                for (Ticket ticket : tickets) {
                        AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
                        authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
                }
                return authUserIdAndAuthUserMap;
        }

        @Override
        public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
                Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
                if (tickets != null) {
                        for (Ticket ticket : tickets) {
                                TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
                                subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
                        }
                }
                return subCategoryIdAndSubCategoryMap;
        }

        @Override
        public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
                List<Integer> fofoIds = new ArrayList<>();
                LOGGER.info(tickets);
                if (tickets == null) {
                        return null;
                }
                for (Ticket ticket : tickets) {
                        fofoIds.add(ticket.getFofoId());
                }
                Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
                return fofoIdsAndCustomRetailer;
        }

        @Override
        public List<TicketCategory> getAllTicketCategotyFromSubCategory() {

                List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
                HashSet<Integer> categoryIds = new HashSet<>();
                for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
                        categoryIds.add(ticketSubcategory.getcategoryId());
                }
                List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
                return ticketcategories;
        }

        private Ticket setAssignment(Ticket ticket, TicketAssigned ticketAssigned) {
                TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
                List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
                for (Position position : positions) {
                        if (position.getEscalationType().equals(EscalationType.L1)) {
                                ticketAssigned.setAssineeId(position.getAuthUserId());
                                ticket.setL1AuthUser(position.getAuthUserId());
                        } else if (position.getEscalationType().equals(EscalationType.L2)) {
                                ticket.setL2AuthUser(position.getAuthUserId());
                        }
                }
                return ticket;
        }

        @Override
        public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
                Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
                for (Position position : positions) {
                        AuthUser authUser = authRepository.selectById(position.getAuthUserId());
                        authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
                }
                return authUserIdAndAuthUserMap;
        }

        @Override
        public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
                Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
                for (Position position : positions) {
                        TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
                        categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
                }
                return categoryIdAndCategoryMap;
        }

        @Override
        public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
                Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
                for (Position position : positions) {
                        Region region = regionRepository.selectById(position.getRegionId());
                        regionIdAndRegionMap.put(position.getRegionId(), region);
                }
                return regionIdAndRegionMap;
        }

        @Override
        public Map<Integer, List<AuthUser>> getAuthUserList(List<Ticket> tickets, AuthUser authUser) {
                Map<Integer, List<AuthUser>> authUserListMap = new HashMap<>();
                for (Ticket ticket : tickets) {
                        if (ticket.getL2AuthUser() == authUser.getId()) {
                                List<AuthUser> authUsers = new ArrayList<>();
                                authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
                                authUserListMap.put(ticket.getId(), authUsers);

                        } else if (ticket.getL3AuthUser() == authUser.getId()) {
                                TicketAssigned ticketAssigned = ticketAssignedRepository
                                                .selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(), ticket.getId());
                                if (ticketAssigned == null) {
                                        List<AuthUser> authUsers = new ArrayList<>();
                                        authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
                                        authUserListMap.put(ticket.getId(), authUsers);
                                } else {
                                        List<AuthUser> authUsers = new ArrayList<>();
                                        authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
                                        authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
                                        authUserListMap.put(ticket.getId(), authUsers);
                                }
                        } else {
                                TicketAssigned ticketAssigned = ticketAssignedRepository
                                                .selectByAssigneeIdAndTicketId(ticket.getL3AuthUser(), ticket.getId());
                                if (ticketAssigned == null) {
                                        ticketAssigned = ticketAssignedRepository.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(),
                                                        ticket.getId());
                                        if (ticketAssigned == null) {
                                                List<AuthUser> authUsers = new ArrayList<>();
                                                authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
                                                authUserListMap.put(ticket.getId(), authUsers);
                                        } else {
                                                List<AuthUser> authUsers = new ArrayList<>();
                                                authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
                                                authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
                                                authUserListMap.put(ticket.getId(), authUsers);
                                        }
                                } else {
                                        List<AuthUser> authUsers = new ArrayList<>();
                                        authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
                                        authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
                                        authUsers.add(authRepository.selectById(ticket.getL3AuthUser()));
                                        authUserListMap.put(ticket.getId(), authUsers);

                                }
                        }
                }
                return authUserListMap;
        }

        @Override
        public void updateTicket(int categoryId, int subCategoryId, Ticket ticket) throws ProfitMandiBusinessException {
                TicketAssigned ticketAssigned = new TicketAssigned();
                ticket.setSubCategoryId(subCategoryId);
                ticket.setUpdateTimestamp(LocalDateTime.now());
                ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
                ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
                ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
                int l3Auth = this.getAuthUserId(categoryId, EscalationType.L3, ticket.getFofoId());
                int l2Auth = this.getAuthUserId(categoryId, EscalationType.L2, ticket.getFofoId());
                int l1Auth = this.getAuthUserId(categoryId, EscalationType.L1, ticket.getFofoId());
                LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
                if (l1Auth == 0) {
                        if (l2Auth == 0) {
                                this.setAssignment(ticket, ticketAssigned);
                        } else {
                                ticketAssigned.setAssineeId(l2Auth);
                                ticket.setL1AuthUser(0);
                                ticket.setL2AuthUser(l2Auth);
                        }
                } else {
                        ticketAssigned.setAssineeId(l1Auth);
                        ticket.setL1AuthUser(l1Auth);
                        ticket.setL2AuthUser(l2Auth);
                }
                ticket.setL3AuthUser(l3Auth);
                ticketRepository.persist(ticket);
                ticketAssigned.setTicketId(ticket.getId());
                ticketAssignedRepository.persist(ticketAssigned);
                AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
                this.sendAssignedTicketMail(authUser, ticket);
        }

        @Override
        public void sendAssignedTicketMail(AuthUser authUser, Ticket ticket) throws ProfitMandiBusinessException {
                try {
                        Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
                                        String.format(ASSIGNED_TICKET, authUser.getFirstName(),
                                                        retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName(), ticket.getId()),
                                        null);
                } catch (Exception e) {
                        throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
                                        "Could not send ticket assignment mail");
                }
        }

        @Override
        public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) {
                Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
                for (Position position : positions) {
                        List<Integer> fofoIds = partnersPositionRepository.selectByPositionId(position.getId()).stream()
                                        .map(x -> x.getFofoId()).collect(Collectors.toList());

                        LOGGER.info("fofoIds - {}", fofoIds);
                        if (!fofoIds.isEmpty()) {
                                if (fofoIds.contains(0)) {
                                        fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
                                                        .map(x -> x.getFofoId()).collect(Collectors.toList());
                                        if (fofoIds.contains(0)) {
                                                fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
                                                                .collect(Collectors.toList());
                                        }

                                }
                                LOGGER.info("fofoIds - {}", fofoIds);
                                positionRetailerMap.put(position.getId(),
                                                new ArrayList<CustomRetailer>(retailerService.getFofoRetailers(fofoIds).values()));

                        }
                }
                return positionRetailerMap;
        }

        @Override
        public Map<Integer, List<CustomRetailer>> getpositionIdAndpartnerRegionMap(List<Position> positions) {
                Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
                for (Position position : positions) {
                        List<Integer> fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
                                        .map(x -> x.getFofoId()).collect(Collectors.toList());

                        if (!fofoIds.isEmpty()) {
                                if (fofoIds.contains(0)) {
                                        fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());

                                }

                                Map<Integer, CustomRetailer> fofoRetailers = retailerService.getFofoRetailers(fofoIds);
                                positionIdAndpartnerRegionMap.put(position.getRegionId(),
                                                new ArrayList<CustomRetailer>(fofoRetailers.values()));
                        }

                }

                return positionIdAndpartnerRegionMap;
        }

        @Override
        @Cacheable(value = "authUserEmailMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
        public Map<String, Set<String>> getAuthUserPartnerEmailMapping() {
                Map<String, Set<String>> storeGuyMap = new HashMap<>();
                Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
                                .collect(Collectors.toSet());
                List<Position> categoryPositions = positionRepository
                                .selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
                Map<Integer, Position> positionsMap = categoryPositions.stream()
                                .collect(Collectors.toMap(x -> x.getId(), x -> x));

                Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
                for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
                        int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
                        Set<String> partnerEmails = positionPartnerEntry.getValue().stream()
                                        .filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail())
                                        .collect(Collectors.toSet());
                        AuthUser authUser = authRepository.selectById(authUserId);
                        if (authUser.isActive()) {
                                if (!storeGuyMap.containsKey(authUser.getEmailId())) {
                                        storeGuyMap.put(authUser.getEmailId(), partnerEmails);
                                } else {
                                        storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
                                }
                        }
                }
                return storeGuyMap;
        }

        @Override
        @Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
        public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() {
                Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
                Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
                                .collect(Collectors.toSet());
                List<Position> categoryPositions = positionRepository
                                .selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
                Map<Integer, Position> positionsMap = categoryPositions.stream()
                                .collect(Collectors.toMap(x -> x.getId(), x -> x));

                Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
                for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
                        int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
                        Set<Integer> partnerEmails = positionPartnerEntry.getValue().stream()
                                        .filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
                                        .collect(Collectors.toSet());
                        AuthUser authUser = authRepository.selectById(authUserId);
                        if (authUser.isActive()) {
                                if (!storeGuyMap.containsKey(authUser.getEmailId())) {
                                        storeGuyMap.put(authUser.getEmailId(), partnerEmails);
                                } else {
                                        storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
                                }
                        }
                }
                return storeGuyMap;
        }

        @Override
        public List<String> getAuthUserByPartnerId(int fofoId) {

                List<String> emails = new ArrayList<>();
                List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
                                .collect(Collectors.toList());

                regionIds.add(5);// All partners Id;
                List<Integer> fofoIds = new ArrayList<>();
                fofoIds.add(fofoId);
                fofoIds.add(0);

                LOGGER.info("fofoIds" + fofoIds);
                List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
                                .stream().map(x -> x.getPositionId()).collect(Collectors.toList());

                LOGGER.info("partnerPositionIds" + partnerPositionIds);

                for (Integer partnerPostionId : partnerPositionIds) {
                        Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
                                        ProfitMandiConstants.TICKET_CATEGORY_SALES);
                        LOGGER.info("position" + position);
                        if (position != null) {

                                AuthUser authUser = authRepository.selectById(position.getAuthUserId());
                                LOGGER.info("authUser" + authUser);
                                emails.add(authUser.getEmailId());
                        }
                }

                return emails;
        }

        @Override
        public Map<EscalationType, String> getAuthUserAndEsclationTypeByPartnerId(int fofoId) {

                List<String> emails = new ArrayList<>();
                Map<EscalationType, String> emailEsclationTypeMap = new HashMap<>();
                List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
                                .collect(Collectors.toList());

                regionIds.add(5);// All partners Id;
                List<Integer> fofoIds = new ArrayList<>();
                fofoIds.add(fofoId);
                fofoIds.add(0);

                LOGGER.info("fofoIds" + fofoIds);
                List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
                                .stream().map(x -> x.getPositionId()).collect(Collectors.toList());

                LOGGER.info("partnerPositionIds" + partnerPositionIds);

                for (Integer partnerPostionId : partnerPositionIds) {
                        Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
                                        ProfitMandiConstants.TICKET_CATEGORY_SALES);
                        LOGGER.info("position" + position);
                        if (position != null) {

                                AuthUser authUser = authRepository.selectById(position.getAuthUserId());
                                LOGGER.info("authUser" + authUser);

                                emailEsclationTypeMap.put(position.getEscalationType(), authUser.getEmailId());
                        }
                }

                LOGGER.info("emailEsclationTypeMap" + emailEsclationTypeMap);

                return emailEsclationTypeMap;
        }

        @Override
        @Cacheable(value = "authUserIdPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
        public Map<Integer, List<Integer>> getAuthUserIdPartnerIdMapping() {
                Map<Integer, List<Integer>> storeGuyMap = new HashMap<>();
                Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
                                .collect(Collectors.toSet());
                List<Position> categoryPositions = positionRepository
                                .selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
                Map<Integer, Position> positionsMap = categoryPositions.stream()
                                .collect(Collectors.toMap(x -> x.getId(), x -> x));

                Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
                for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
                        int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
                        List<Integer> partnerIds = positionPartnerEntry.getValue().stream()
                                        .filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
                                        .collect(Collectors.toList());
                        AuthUser authUser = authRepository.selectById(authUserId);
                        if (authUser.isActive()) {
                                if (!storeGuyMap.containsKey(authUserId)) {
                                        storeGuyMap.put(authUserId, partnerIds);
                                } else {
                                        storeGuyMap.get(authUserId).addAll(partnerIds);
                                }
                        }
                }
                return storeGuyMap;
        }

        @Override
        @Cacheable(value = "L1L2Mapping", cacheManager = "thirtyMinsTimeOutCacheManager")
        public Map<Integer, List<Integer>> getL2L1Mapping() {
                List<Position> l1SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
                                ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
                List<Position> l2SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
                                ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2);

                LOGGER.info("position" + l1SalesPositions);
                Map<Integer, List<Integer>> authUserPartnerListMap = this.getAuthUserIdPartnerIdMapping();
                LOGGER.info("map" + authUserPartnerListMap);

                Map<Integer, List<Integer>> l2l1ListMap = new HashMap<>();

                for (Position l2SalePosition : l2SalesPositions) {
                        int l2AuthUserId = l2SalePosition.getAuthUserId();
                        if (authUserPartnerListMap.containsKey(l2SalePosition.getAuthUserId())) {
                                List<Integer> mappedL1AuthUsers = new ArrayList<>();
                                l2l1ListMap.put(l2AuthUserId, mappedL1AuthUsers);
                                List<Integer> l2FofoIds = authUserPartnerListMap.get(l2AuthUserId);
                                for (Position l1SalePosition : l1SalesPositions) {
                                        int l1AuthUserId = l1SalePosition.getAuthUserId();
                                        if (authUserPartnerListMap.containsKey(l1AuthUserId)) {
                                                List<Integer> l1FofoIds = authUserPartnerListMap.get(l1SalePosition.getAuthUserId());
                                                if (l2FofoIds.containsAll(l1FofoIds)) {
                                                        mappedL1AuthUsers.add(l1AuthUserId);
                                                }

                                        }

                                }
                        }

                }

                return l2l1ListMap;
        }

}