Subversion Repositories SmartDukaan

Rev

Rev 24467 | Rev 24500 | 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 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.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.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.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. 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;

        @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();
                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 l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
                int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
                int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
                LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
                if (l1Auth == 0) {
                        if (l2Auth == 0) {
                                this.setAssignment(ticket);
                        } else {
                                ticket.setAssigneeId(l2Auth);
                        }
                } else {
                        ticket.setAssigneeId(l1Auth);
                        ticket.setL1AuthUser(l1Auth);
                        ticket.setL2AuthUser(l2Auth);
                }
                ticket.setL3AuthUser(l3Auth);
                ticketRepository.persist(ticket);
                AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
                try {
                        Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
                                        String.format(ASSIGNED_TICKET, authUser.getFirstName(),
                                                        retailerService.getFofoRetailer(fofoId).getBusinessName()),
                                        null);
                } catch (Exception e) {
                        throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
                                        "Could not send ticket assignment mail");
                }
                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) {
                                positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
                                                region.getRegionId());
                                LOGGER.info("positions:-" + positions);
                                if (positions != null) {
                                        break;
                                }

                        }
                        if (positions.size() == 0) {
                                return 0;
                        }
                }
                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 (Integer fofoId : fofoIds) {
                        PartnerRegion partnerRegion = partnerRegionRepository.selectByRegionIdAndFofoId(regionId, fofoId);
                        if (partnerRegion == null) {
                                partnerRegion = new PartnerRegion();
                                partnerRegion.setFofoId(fofoId);
                                partnerRegion.setRegionId(regionId);
                                partnerRegionRepository.persist(partnerRegion);
                        } else {
                                continue;
                        }
                }
        }

        @Override
        public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<Ticket> tickets) {
                Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
                for (Ticket ticket : tickets) {
                        AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
                        authUserIdAndAuthUserMap.put(ticket.getAssigneeId(), authUser);
                }
                return authUserIdAndAuthUserMap;
        }

        @Override
        public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
                Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
                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<>();
                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) {
                TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
                List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
                for (Position position : positions) {
                        if (position.getEscalationType().equals(EscalationType.L1)) {
                                ticket.setAssigneeId(position.getAuthUserId());
                                ticket.setL1AuthUser(position.getAuthUserId());
                        } else {
                                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;
        }
        
}