Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

import com.mongodb.DBObject;
import com.spice.profitmandi.common.enumuration.ActivationType;
import com.spice.profitmandi.common.enumuration.MessageType;
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.model.SendNotificationModel;
import com.spice.profitmandi.common.util.FileUtil;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.auth.AuthUser;
import com.spice.profitmandi.dao.entity.auth.PartnerCollectionPlan;
import com.spice.profitmandi.dao.entity.auth.PartnerCollectionRemark;
import com.spice.profitmandi.dao.entity.auth.PartnerSecondaryPlan;
import com.spice.profitmandi.dao.entity.cs.Position;
import com.spice.profitmandi.dao.entity.dtr.User;
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
import com.spice.profitmandi.dao.entity.fofo.*;
import com.spice.profitmandi.dao.entity.user.*;
import com.spice.profitmandi.dao.enumuration.auth.CollectionRemark;
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
import com.spice.profitmandi.dao.enumuration.dtr.*;
import com.spice.profitmandi.dao.model.*;
import com.spice.profitmandi.dao.repository.auth.*;
import com.spice.profitmandi.dao.repository.cs.*;
import com.spice.profitmandi.dao.repository.dtr.*;
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
import com.spice.profitmandi.dao.repository.fofo.RbmRatingRepository;
import com.spice.profitmandi.dao.repository.fofo.SalesRatingRepository;
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
import com.spice.profitmandi.dao.repository.user.AddressRepository;
import com.spice.profitmandi.service.AuthService;
import com.spice.profitmandi.service.NotificationService;
import com.spice.profitmandi.service.PartnerCollectionService;
import com.spice.profitmandi.service.user.RetailerService;
import com.spice.profitmandi.service.user.StoreTimelineTatService;
import com.spice.profitmandi.web.req.CreateFranchiseeRequest;
import com.spice.profitmandi.web.req.RbmSalesRatingRequest;
import com.spice.profitmandi.web.res.Partner;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.stream.Collectors;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class LeadController {
    private static final Logger LOGGER = LogManager.getLogger(LeadController.class);
    @Autowired
    private ResponseSender<?> responseSender;

    @Autowired
    private AuthRepository authRepository;

    @Autowired
    private LeadRepository leadRepository;

    @Autowired
    DocumentRepository documentRepository;

    @Autowired
    PositionRepository positionRepository;

    @Autowired
    private ActivityAttachmentRepository activityAttachmentRepository;

    @Autowired
    private CsService csService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AddressRepository addressRepository;

    @Autowired

    private UserRoleRepository userRoleRepository;

    @Autowired
    private UserAccountRepository userAccountRepository;

    @Autowired
    private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;

    @Autowired
    private RetailerService retailerService;

    @Autowired
    private LeadActivityRepository leadActivityRepository;

    @Autowired
    private FranchiseeVisitRepository franchiseeVisitRepository;

    @Autowired
    private FranchiseeActivityRepository franchiseeActivityRepository;

    @Autowired
    private PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;

    @Autowired
    private FofoStoreRepository fofoStoreRepository;

    @Autowired
    private StoreTimelineTatService storeTimelineTatService;

    @Autowired
    private PartnerCollectionService partnerCollectionService;

    @Autowired
    private TicketRepository ticketRepository;

    @Autowired
    private TicketCategoryRepository ticketCategoryRepository;

    @Autowired
    private TicketSubCategoryRepository ticketSubCategoryRepository;

    @Autowired
    private PartnerCollectionRemarkRepository partnerCollectionRemarkRepository;

    @Autowired
    private Mongo mongoClient;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private PartnerCollectionPlanRepository partnerCollectionPlanRepository;

    @Autowired
    private PartnerSecondaryPlanRepository partnerSecondaryPlanRepository;

    @Autowired
    private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;

    @Autowired
    private AuthUserPartnerMappingRepository authUserPartnerMappingRepository;

    @Autowired
    private VisitRequestRepository visitRequestRepository;

    @Autowired
    private AuthService authService;

    @Autowired
    private NotificationService notificationService;

    @Autowired
    JavaMailSender mailSender;

    @Autowired
    private TicketAssignedRepository ticketAssignedRepository;

    @Autowired
    LeadDetailRepository leadDetailRepository;

    @Autowired
    PurchaseRepository purchaseRepository;

    @RequestMapping(value = "/lead", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})

    public ResponseEntity<?> LeadUser(@RequestBody CreateRefferalRequest createRefferalRequest) throws Exception {
        LOGGER.info("CreateReferralRequest - " + createRefferalRequest);
        if (createRefferalRequest.getOutletName() == null || createRefferalRequest.getPotential() == 0) {
            throw new Exception("Outlet should not be empty and potential should be greater > 0");
        }
        Lead lead;
        AuthUser authUser = authRepository.selectByGmailId(createRefferalRequest.getReffereeEmail());
        if (createRefferalRequest.getId() > 0) {
            lead = leadRepository.selectById(createRefferalRequest.getId());
            lead.setAssignTo(authUser.getId());
        } else {
            lead = new Lead();
            lead.setFirstName(createRefferalRequest.getFirstName());
            lead.setLastName(createRefferalRequest.getLastName());
            lead.setLeadMobile(createRefferalRequest.getMobile());
            lead.setState(createRefferalRequest.getState());
            lead.setCity(createRefferalRequest.getCity());
            lead.setAddress(createRefferalRequest.getAddress());
            lead.setCreatedTimestamp(LocalDateTime.now());
            lead.setOutLetName(createRefferalRequest.getOutletName());
            lead.setUpdatedTimestamp(LocalDateTime.now());
            lead.setStatus(createRefferalRequest.getStatus());
            lead.setSource(createRefferalRequest.getSource());
            lead.setNotinterestedReason(createRefferalRequest.getReason());
            lead.setPotential(createRefferalRequest.getPotential());
            if (createRefferalRequest.getColorCheck() == true) {
                lead.setColor("Green");
            } else {
                lead.setColor("Yellow");
            }
            String authUserName = authUser.getFullName();
            lead.setAuthId(authUser.getId());
            lead.setAssignTo(authUser.getId());
            lead.setCreatedBy(authUserName);
            leadRepository.persist(lead);

        }

        LeadActivity leadActivity = new LeadActivity();
        leadActivity.setLeadId(lead.getId());
        leadActivity.setRemark((createRefferalRequest.getId() > 0 ? "Self Assigned." : "") + createRefferalRequest.getRemark());
        leadActivity.setAuthId(authUser.getId());

        if (createRefferalRequest.getStatus().equals(LeadStatus.followUp)) {
            leadActivity.setSchelduleTimestamp(createRefferalRequest.getSchelduleTimestamp());
            leadActivity.setCommunicationType(createRefferalRequest.getCommunicationType());

            if (leadActivity.getCommunicationType().equals(CommunicationType.VISIT)) {

                visitRequestRepository.createVisitRequest(lead.getId(), "lead", lead.getAssignTo(),
                        createRefferalRequest.getSchelduleTimestamp());
            }
            //
            leadActivity.setClosureTimestamp(createRefferalRequest.getClosureTimestamp());
            lead.setClosureTimestamp(createRefferalRequest.getClosureTimestamp());
        } else {
            leadActivity.setSchelduleTimestamp(null);
            leadActivity.setClosureTimestamp(null);
            lead.setClosureTimestamp(null);
        }
        leadActivity.setCreatedTimestamp(LocalDateTime.now());
        leadActivityRepository.persist(leadActivity);

        return responseSender.ok(true);
    }

    @RequestMapping(value = "/lead-description", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> leadDescription(HttpServletRequest request, @RequestParam(name = "gmailId") String
                                                     gmailId,
                                             @RequestParam(name = "status") LeadStatus status) throws ProfitMandiBusinessException {
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
        if (authUser == null) {
            throw new ProfitMandiBusinessException("gmailId", gmailId, "Invalid gmailId");
        }
        List<Lead> leads = null;
        LOGGER.info("emails" + status);

        if (status.equals(LeadStatus.followUp)) {
            leads = leadRepository.selectLeadsScheduledBetweenDate(Arrays.asList(authUser.getId()), null, null);

            // Only fetch visitRequests when needed for followUp status
            List<VisitRequest> visitRequests = visitRequestRepository.selectByAuthIdAndDate(authUser.getId(),
                    LocalDate.now());
            if (!visitRequests.isEmpty()) {
                Map<Integer, List<VisitRequest>> visitRequestMap = visitRequests.stream()
                        .collect(Collectors.groupingBy(VisitRequest::getVisitId));

                Comparator<VisitRequest> visitComparator = Comparator.comparing(VisitRequest::getCreatedTimestamp);
                for (Lead lead : leads) {
                    List<VisitRequest> visitRequest = visitRequestMap.get(lead.getId());
                    if (visitRequest != null) {
                        if (visitRequest.size() > 1) {
                            VisitRequest youngestVisit = visitRequest.stream().max(visitComparator).get();
                            lead.setVisitStatus(youngestVisit.getStatus());
                        } else {
                            lead.setVisitStatus(visitRequest.get(0).getStatus());
                        }
                    }
                }
            }

            leads = leads.stream()
                    .sorted(Comparator.comparing(Lead::getScheduledTimestamp, Comparator.nullsFirst(Comparator.reverseOrder())))
                    .collect(Collectors.toList());
        } else {
            leads = leadRepository.selectByAssignAuthIdAndStatus(authUser.getId(), status);
        }

        return responseSender.ok(leads);
    }

    @RequestMapping(value = "/getlead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getLead(HttpServletRequest request, @RequestParam(name = "id") int id) throws
            ProfitMandiBusinessException {

        List<LeadActivity> leadActivities = leadActivityRepository.selectBYLeadId(id);
        Lead lead = leadRepository.selectById(id);
        lead.setLeadActivities(leadActivities);
        return responseSender.ok(lead);

    }

    @RequestMapping(value = "/check-existing-lead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getLeadByMobile(HttpServletRequest request, @RequestParam(name = "mobile") String
            mobile) throws ProfitMandiBusinessException {
        int userId = (int) request.getAttribute("userId");
        LOGGER.info("userId: " + userId);
        Lead lead = leadRepository.selectByMobileNumber(mobile);
        return responseSender.ok(lead);

    }

    @RequestMapping(value = "/leadUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> leadUpdate(HttpServletRequest request, @RequestParam(name = "id") int id,
                                        @RequestParam(name = "status") LeadStatus status, @RequestParam(name = "colorCheck") Boolean colorCheck,
                                        @RequestParam(name = "remark") String remark, @RequestParam(name = "reason") String reason,
                                        @RequestParam(name = "schelduleTimestamp") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime schelduleTimestamp,
                                        @RequestParam(name = "closureTimestamp") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime closureTimestamp,
                                        @RequestParam(name = "communicationType") CommunicationType communicationType) throws
            ProfitMandiBusinessException {

        Lead lead = leadRepository.selectById(id);

        LeadActivity leadActivity = new LeadActivity();
        lead.setStatus(status);
        lead.setNotinterestedReason(reason);
        leadActivity.setRemark(remark);
        leadActivity.setLeadId(id);
        leadActivity.setOutletName(lead.getOutLetName());
        leadActivity.setPotential(lead.getPotential());
        leadActivity.setCreatedTimestamp(LocalDateTime.now());
        leadActivity.setSchelduleTimestamp(null);
        leadActivity.setClosureTimestamp(null);
        leadActivity.setAuthId(lead.getAssignTo());
        lead.setUpdatedTimestamp(LocalDateTime.now());
        if (colorCheck == true) {
            lead.setColor("Green");
        } else {
            lead.setColor("Yellow");
        }
        if (status == LeadStatus.followUp) {

            if (schelduleTimestamp != null) {

                leadActivity.setCommunicationType(communicationType);

                if (leadActivity.getCommunicationType().equals(CommunicationType.VISIT)) {

                    visitRequestRepository.createVisitRequest(lead.getId(), "lead", lead.getAssignTo(), schelduleTimestamp);
                }

            }
            leadActivity.setSchelduleTimestamp(schelduleTimestamp);
            leadActivity.setClosureTimestamp(closureTimestamp);
            lead.setClosureTimestamp(closureTimestamp);

        } else {

            leadActivity.setSchelduleTimestamp(null);
            leadActivity.setClosureTimestamp(null);
            lead.setClosureTimestamp(null);

        }
        leadActivityRepository.persist(leadActivity);
        return responseSender.ok(true);

    }

    @RequestMapping(value = ProfitMandiConstants.URL_NEW_LEAD, method = RequestMethod.POST)
    public ResponseEntity<?> newLead(HttpServletRequest request, @RequestBody CreateRefferalRequest
            createRefferalRequest) throws ProfitMandiBusinessException {
        LOGGER.info("requested url : " + request.getRequestURL().toString());
        Lead lead = new Lead();
        lead.setAddress(createRefferalRequest.getCity());
        Long.parseLong(createRefferalRequest.getMobile());
        if (createRefferalRequest.getMobile().length() != 10) {
            throw new ProfitMandiBusinessException("Mobile Number", createRefferalRequest.getMobile(), "Number should be of 10 digits");
        }
        lead.setLeadMobile(createRefferalRequest.getMobile());
        lead.setCity(createRefferalRequest.getCity());
        lead.setState(createRefferalRequest.getState());
        lead.setLastName(createRefferalRequest.getLastName());
        if (lead.getState().equals("Uttar Pradesh")) {
            lead.setAssignTo(53);
        } else if (lead.getState().equals("Haryana")) {
            lead.setAssignTo(53);
        } else if (lead.getState().equals("Delhi")) {
            lead.setAssignTo(53);
        } else {
            // Assign to sm
            lead.setAssignTo(53);
            // Assign to neha
            // lead.setAssignTo(1);
        }
        lead.setAuthId(lead.getAssignTo());
        lead.setCreatedBy("daily-sync");
        lead.setSource("SD-WEB");
        lead.setFirstName(createRefferalRequest.getFirstName());
        lead.setStatus(LeadStatus.followUp);
        lead.setColor("yellow");
        lead.setCreatedTimestamp(LocalDateTime.now());
        lead.setUpdatedTimestamp(LocalDateTime.now());
        leadRepository.persist(lead);

        return responseSender.ok(true);

    }

    @RequestMapping(value = "/getPartnersList", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getPartners(HttpServletRequest request, @RequestParam(name = "gmailId") String gmailId)
            throws ProfitMandiBusinessException {

        AuthUser authUser = authRepository.selectByGmailId(gmailId);

        Map<String, Set<String>> storeGuyMap = csService.getAuthUserPartnerEmailMapping();

        Set<String> emails = storeGuyMap.get(authUser.getEmailId());
        LOGGER.info("emails" + emails);
        List<User> users = userRepository.selectAllByEmailIds(new ArrayList<>(emails));
        List<Partner> partners = new ArrayList<>();
        for (User user : users) {

            UserAccount uc = userAccountRepository.selectSaholicByUserId(user.getId());
            com.spice.profitmandi.dao.entity.user.User userInfo = userUserRepository.selectById(uc.getAccountKey());
            CustomRetailer customRetailer = retailerService.getFofoRetailer(userInfo.getId());

            Partner partner = new Partner();
            partner.setBusinessName(customRetailer.getBusinessName());
            partner.setPartnerId(customRetailer.getPartnerId());
            partner.setCartId(customRetailer.getCartId());
            partner.setEmail(customRetailer.getEmail());
            partner.setGstNumber(customRetailer.getGstNumber());
            partner.setDisplayName(customRetailer.getDisplayName());
            partner.setCity(customRetailer.getAddress().getCity());
            partner.setUserId(user.getId());
            partners.add(partner);
        }
        LOGGER.info("partners" + partners);
        return responseSender.ok(partners);
    }

    @RequestMapping(value = "/franchise-first-visit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> FranchiseFirstVisit(HttpServletRequest request, @RequestBody CreateFranchiseeRequest
            createFranchiseeRequest) throws Exception {

        FranchiseeVisit franchiseeVisit = new FranchiseeVisit();
        franchiseeVisit.setFofoId(createFranchiseeRequest.getFofoId());
        CustomRetailer customRetailer = retailerService.getFofoRetailer(createFranchiseeRequest.getFofoId());

        franchiseeVisit.setPartnerName(customRetailer.getBusinessName());
        franchiseeVisit.setAgenda(createFranchiseeRequest.getAgenda());
        franchiseeVisit.setCreatedTimestamp(LocalDateTime.now());
        franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
        franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
        franchiseeVisit.setScheduleTimestamp(createFranchiseeRequest.getFirstSchelduleTimestamp());
        // change
        AuthUser authUser = authRepository.selectByGmailId(createFranchiseeRequest.getCreatedBy());

        String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
        franchiseeVisit.setCreatedBy(authUserName);
        franchiseeVisit.setAuthId(authUser.getId());

        franchiseeVisitRepository.persist(franchiseeVisit);

        FranchiseeActivity franchiseeActivity = new FranchiseeActivity();
        franchiseeActivity.setAction(FranchiseeActivityStatus.FOLLOWUP);
        franchiseeActivity.setFranchiseeVisitd(franchiseeVisit.getId());
        franchiseeActivity.setResolution(createFranchiseeRequest.getResolution());
        franchiseeActivity.setSchelduleTimestamp(createFranchiseeRequest.getFirstSchelduleTimestamp());

        franchiseeActivity.setCreatedTimestamp(LocalDateTime.now());
        franchiseeActivityRepository.persist(franchiseeActivity);

        franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());

        visitRequestRepository.createVisitRequest(franchiseeVisit.getId(), "franchiseeVisit", authUser.getId(), createFranchiseeRequest.getFirstSchelduleTimestamp());

        return responseSender.ok(true);
    }

    @RequestMapping(value = "/franchise-visit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> FranchiseVisit(HttpServletRequest request,
                                            @RequestBody CreateFranchiseeRequest createFranchiseeRequest) throws Exception {

        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, createFranchiseeRequest.getFofoId());

        FranchiseeVisit franchiseeVisit = franchiseeVisitRepository.selectById(createFranchiseeRequest.getId());
        franchiseeVisit.setFofoId(createFranchiseeRequest.getFofoId());
        CustomRetailer customRetailer = retailerService.getFofoRetailer(createFranchiseeRequest.getFofoId());
        LOGGER.info("rbmL1 {}", rbmL1);
        franchiseeVisit.setPartnerName(customRetailer.getBusinessName());
        franchiseeVisit.setAgenda(createFranchiseeRequest.getAgenda());
        franchiseeVisit.setPartnerRemark(createFranchiseeRequest.getPartnerRemark());
        franchiseeVisit.setOutsideVisibity(createFranchiseeRequest.getOutsideVisibity());
        franchiseeVisit.setInstoreVisibility(createFranchiseeRequest.getInstoreVisibility());
        franchiseeVisit.setOutsideStock(createFranchiseeRequest.getOutsideStock());
        franchiseeVisit.setSystemKnowledge(createFranchiseeRequest.getSystemKnowledge());
        franchiseeVisit.setWorkingDevice(createFranchiseeRequest.getWorkingDevice());
        franchiseeVisit.setWorkingPrinter(createFranchiseeRequest.getWorkingPrinter());
        franchiseeVisit.setCarryBags(createFranchiseeRequest.getCarryBags());
        franchiseeVisit.setSmartdukaanTshirt(createFranchiseeRequest.getSmartdukaanTshirt());
        franchiseeVisit.setLatestDummies(createFranchiseeRequest.getLatestDummies());
        franchiseeVisit.setInvestment(createFranchiseeRequest.getInvestment());
        franchiseeVisit.setMtd(createFranchiseeRequest.getMtd());
        franchiseeVisit.setHygiene(createFranchiseeRequest.getHygiene());
        franchiseeVisit.setCreatedTimestamp(LocalDateTime.now());
        franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
        franchiseeVisit.setInformedAboutOnline(createFranchiseeRequest.getOnline());
        franchiseeVisit.setPendingBilling(createFranchiseeRequest.getPendingBilling());
        franchiseeVisit.setRbmId(rbmL1);
        franchiseeVisit.setRbmRating(createFranchiseeRequest.getRbmRating());
        if (createFranchiseeRequest.getAction().equals(FranchiseeActivityStatus.FOLLOWUP)) {
            franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
        } else {
            franchiseeVisit.setStatus(FranchiseeVisitStatus.CLOSE);
        }

        // AuthUser authUser =
        // authRepository.selectByGmailId(createFranchiseeRequest.getCreatedBy());
        // change
        AuthUser authUser = authRepository.selectByGmailId(createFranchiseeRequest.getCreatedBy());

        String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
        franchiseeVisit.setCreatedBy(authUserName);
        franchiseeVisit.setAuthId(authUser.getId());

        franchiseeVisitRepository.persist(franchiseeVisit);

        FranchiseeActivity franchiseeActivity = new FranchiseeActivity();
        franchiseeActivity.setAction(createFranchiseeRequest.getAction());
        franchiseeActivity.setFranchiseeVisitd(franchiseeVisit.getId());
        franchiseeActivity.setResolution(createFranchiseeRequest.getResolution());
        if (createFranchiseeRequest.getAction().equals(FranchiseeActivityStatus.FOLLOWUP)) {
            franchiseeActivity.setSchelduleTimestamp(createFranchiseeRequest.getSchelduleTimestamp());

            visitRequestRepository.createVisitRequest(franchiseeVisit.getId(), "franchiseeVisit",
                    franchiseeVisit.getAuthId(), createFranchiseeRequest.getSchelduleTimestamp());
        } else {
            franchiseeActivity.setSchelduleTimestamp(null);
        }
        franchiseeActivity.setCreatedTimestamp(LocalDateTime.now());
        franchiseeActivityRepository.persist(franchiseeActivity);

        franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());

        if (!createFranchiseeRequest.getOutsideVisibityReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_DESIGN, ProfitMandiConstants.TICKET_CATEGORY_DESIGNING_BRANDING_EXTERNAL_VISIBILITY, createFranchiseeRequest.getOutsideVisibityReason(), authUser.getId());
        }

        if (!createFranchiseeRequest.getInstoreVisibilityReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_DESIGN, ProfitMandiConstants.TICKET_CATEGORY_DESIGNING_BRANDING_INTERNAL_VISIBILITY, createFranchiseeRequest.getInstoreVisibilityReason(), authUser.getId());
        }

        if (!createFranchiseeRequest.getOutsideStockReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_SALES, ProfitMandiConstants.TICKET_CATEGORY_SALES_OUTSIDE_STOCK, createFranchiseeRequest.getOutsideStockReason(), authUser.getId());
        }

        if (!createFranchiseeRequest.getSystemKnowledgeReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_TRAINING, ProfitMandiConstants.TICKET_CATEGORY_TRAINING_NEED, createFranchiseeRequest.getSystemKnowledgeReason(), authUser.getId());
        }

        if (!createFranchiseeRequest.getLatestDummiesReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_MARKETING, ProfitMandiConstants.TICKET_CATEGORY_MARKETING_DUMMIES, createFranchiseeRequest.getLatestDummiesReason(), authUser.getId());
        }

        if (!createFranchiseeRequest.getInvestmentReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_SALES, ProfitMandiConstants.TICKET_CATEGORY_SALES_LOW_INVESTMENT, createFranchiseeRequest.getInvestmentReason(), authUser.getId());
        }

        if (!createFranchiseeRequest.getHygieneReason().isEmpty()) {
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_SALES, ProfitMandiConstants.TICKET_CATEGORY_SALES_LOW_DATA_ACCURACY, createFranchiseeRequest.getHygieneReason(), authUser.getId());
        }

        return responseSender.ok(true);

    }

    @RequestMapping(value = "/getFranchiseVisit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getFranchiseVisit(HttpServletRequest request,
                                               @RequestParam(name = "gmailId") String gmailId, @RequestParam(name = "status") FranchiseeVisitStatus
                                                       status,
                                               @RequestParam(name = "offset", defaultValue = "0") int offset,
                                               @RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
        List<VisitRequest> visitRequests = visitRequestRepository.selectByAuthIdAndDate(authUser.getId(), LocalDate.now());
        Map<Integer, List<VisitRequest>> visitRequestMap = null;
        if (!visitRequests.isEmpty()) {
            visitRequestMap = visitRequests.stream().collect(Collectors.groupingBy(x -> x.getVisitId()));
        }
        List<FranchiseeVisit> franchiseeVisits = franchiseeVisitRepository.selectByAuthIdAndStatus(authUser.getId(), status, offset, limit);

        // Batch fetch all FranchiseeActivity in one query instead of N+1
        List<Integer> activityIds = franchiseeVisits.stream()
                .filter(fv -> fv.getFranchiseActivityId() != 0)
                .map(FranchiseeVisit::getFranchiseActivityId)
                .collect(Collectors.toList());
        Map<Integer, FranchiseeActivity> activityMap = new HashMap<>();
        if (!activityIds.isEmpty()) {
            activityMap = franchiseeActivityRepository.selectByIds(activityIds).stream()
                    .collect(Collectors.toMap(FranchiseeActivity::getId, x -> x));
        }

        for (FranchiseeVisit fv : franchiseeVisits) {
            if (visitRequestMap != null) {

                List<VisitRequest> visitRequest = visitRequestMap.get(fv.getId());
                if (visitRequest != null) {
                    if (visitRequest.size() > 1) {

                        Comparator<VisitRequest> visitComparato = Comparator.comparing(VisitRequest::getCreatedTimestamp);

                        VisitRequest youngestVisit = visitRequest.stream().max(visitComparato).get();
                        fv.setVisitStatus(youngestVisit.getStatus());

                    } else {
                        fv.setVisitStatus(visitRequest.get(0).getStatus());

                    }
                }
            }
            if (fv.getFranchiseActivityId() != 0) {
                FranchiseeActivity fA = activityMap.get(fv.getFranchiseActivityId());
                fv.setFranchiseeActivity(fA);
            }

        }

        return responseSender.ok(franchiseeVisits);

    }

    @RequestMapping(value = "/getFranchiseActivity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getFranchiseActivity(HttpServletRequest request, @RequestParam(name = "id") int id) throws
            ProfitMandiBusinessException {

        List<FranchiseeActivity> franchiseeActivities = franchiseeActivityRepository.selectByFranchiseeVisitId(id);
        return responseSender.ok(franchiseeActivities);

    }

    @RequestMapping(value = "/getFranchiseeInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getFranchiseeInfo(HttpServletRequest request, @RequestParam(name = "id") int id) throws
            ProfitMandiBusinessException {
        FranchiseeVisit franchiseeVisit = franchiseeVisitRepository.selectById(id);

        List<DBObject> mobileBrands = mongoClient.getAllBrandsToDisplay(3);

        List<String> brands = mobileBrands.stream().filter(x -> (boolean) x.get("active"))
                .map(x -> (String) x.get("name")).collect(Collectors.toList());

        franchiseeVisit.setBrands(brands);

        return responseSender.ok(franchiseeVisit);

    }

    @RequestMapping(value = "/franchise-visit-update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> franchiseVisitUpdate(HttpServletRequest request, @RequestParam(name = "id") int id,
                                                  @RequestParam(name = "action") FranchiseeActivityStatus action,
                                                  @RequestParam(name = "resolution") String resolution,
                                                  @RequestParam(name = "schelduleTimestamp") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime schelduleTimestamp)
            throws ProfitMandiBusinessException {
        FranchiseeVisit franchiseeVisit = franchiseeVisitRepository.selectById(id);

        FranchiseeActivity franchiseeActivity = new FranchiseeActivity();

        if (action == FranchiseeActivityStatus.FOLLOWUP) {

            if (schelduleTimestamp != null) {

                visitRequestRepository.createVisitRequest(franchiseeVisit.getId(), "franchiseeVisit",
                        franchiseeVisit.getAuthId(), schelduleTimestamp);
            }

            franchiseeActivity.setSchelduleTimestamp(schelduleTimestamp);
            franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
            franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
            franchiseeVisitRepository.persist(franchiseeVisit);

        } else {

            franchiseeActivity.setSchelduleTimestamp(null);
            franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());
            franchiseeVisit.setStatus(FranchiseeVisitStatus.CLOSE);
            franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
            franchiseeVisitRepository.persist(franchiseeVisit);

        }

        franchiseeActivity.setResolution(resolution);
        franchiseeActivity.setFranchiseeVisitd(franchiseeVisit.getId());
        franchiseeActivity.setAction(action);

        franchiseeActivity.setCreatedTimestamp(LocalDateTime.now());
        franchiseeActivityRepository.persist(franchiseeActivity);

        franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());
        franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
        franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
        franchiseeVisitRepository.persist(franchiseeVisit);

        return responseSender.ok(true);

    }

    @RequestMapping(value = "/onBoardingTimelineStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> onBoardingTimelineStatus(HttpServletRequest request, Model model)
            throws ProfitMandiBusinessException {
        //This userId is of dtr.users
        int userId = (int) request.getAttribute("userId");

        //UserCart contains userId/retailerId in context of user.user as
        //uc contains retailerId and cartId of the retailerId that belongs to user schema
        UserCart uc = userAccountRepository.getUserCart(userId);
        FofoStore fs = fofoStoreRepository.selectByRetailerId(uc.getUserId());
        PartnerOnBoardingPanel partnerOnBoardingPanel = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
        Map<StoreTimeline, OnBoardingTimelineModel> timelineStatus = null;
        LOGGER.info("partnerOnBoardingPanel" + partnerOnBoardingPanel);
        if (partnerOnBoardingPanel != null) {

            timelineStatus = storeTimelineTatService.getTimeline(partnerOnBoardingPanel.getId());
        }
        List<OnBoardingTimelineModel> onBoardingModel = new ArrayList<>();
        if (timelineStatus != null) {
            onBoardingModel = timelineStatus.values().stream().skip(2).map(x -> x).collect(Collectors.toList());
        }
        return responseSender.ok(onBoardingModel);

    }

    @RequestMapping(value = "/purchaseExist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> purchaseExist(HttpServletRequest request) throws ProfitMandiBusinessException {
        int userId = (int) request.getAttribute("userId");
        UserCart uc = userAccountRepository.getUserCart(userId);
        return responseSender.ok(purchaseRepository.hasPurchaseForFofoId(uc.getUserId()));
    }

    @RequestMapping(value = "/onBoardingTimelineVisibility", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> onBoardingTimelineVisibility(HttpServletRequest request, Model model)
            throws ProfitMandiBusinessException {
        int userId = (int) request.getAttribute("userId");
        UserCart uc = userAccountRepository.getUserCart(userId);

        FofoStore fs = fofoStoreRepository.selectByRetailerId(uc.getUserId());
        PartnerOnBoardingPanel partnerOnBoardingPanel = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
        boolean status = true;
        if (partnerOnBoardingPanel != null) {

            status = storeTimelineTatService.getTimelineCompleted(partnerOnBoardingPanel.getId());
        }

        LOGGER.info("status" + status);

        return responseSender.ok(status);

    }

    @RequestMapping(value = "/getPartnerTarget", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> getPartnerTarget(HttpServletRequest request, @RequestParam String
            gmailId, @RequestParam String dayValue, @RequestParam ActivationType activationType) throws
            ProfitMandiBusinessException {

        AuthUser authUser = authRepository.selectByGmailId(gmailId);
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
        Set<Integer> fofoIds = storeGuyMap.get(authUser.getEmailId());
        List<String> brands = Arrays.asList("Vivo", "Samsung", "Oppo", "Itel", "Almost New", "Others");

        float totalPartnerTargetSecondary = 0;
        float totalPartnerTargetCollection = 0;
        float totalPartnerAchievementSecondary = 0;
        float totalPartnerAchievementCollection = 0;
        TargetModel tm = new TargetModel();
        Map<Integer, PartnerDailyInvestment> partnerDailyInvestmentMap = new HashMap<>();
        Map<Integer, Long> partnerTicketCount = ticketRepository.selectAllOpenTicketsGroupByRetailer();

        LOGGER.info("partnerTicketCount {}", partnerTicketCount);

        List<PartnerTargetAchievementModel> ptams = new ArrayList<>();

        if (fofoIds != null && fofoIds.size() > 0) {
            LOGGER.info("fofoIds {}", fofoIds);
            List<Integer> fofoIdList = fofoStoreRepository.selectByRetailerIds(new ArrayList<>(fofoIds)).stream()
                    .filter(x -> (!x.isInternal() && (activationType == null || x.getActivationType().equals(activationType))))
                    .map(x -> x.getId()).collect(Collectors.toList());
            LOGGER.info("fofoIdList {}", fofoIdList);
            LocalDateTime startDate = LocalDate.now().atStartOfDay();

            if (dayValue.equals("previous")) {
                startDate = LocalDate.now().minusDays(1).atStartOfDay();

            }

            if (fofoIdList.size() > 0) {

                List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository.selectAll(fofoIdList, startDate.toLocalDate().minusDays(1));

                if (!partnerDailyInvestments.isEmpty()) {
                    partnerDailyInvestmentMap = partnerDailyInvestments.stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
                }

                Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();

                Map<Integer, CustomRetailer> customRetailers = fofoIdList.stream().map(x -> customRetailerMap.get(x))
                        .filter(x -> x != null).collect(Collectors.toList()).stream()
                        .collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));

                List<Integer> remarkIds = partnerCollectionRemarkRepository.selectMaxRemarkId(fofoIdList);

                LOGGER.info("remarkIds {}", remarkIds);

                long todayCollectionCount = 0;
                if (!remarkIds.isEmpty()) {
                    todayCollectionCount = partnerCollectionRemarkRepository
                            .selectByAuthIdAndIds(authUser.getId(), remarkIds).stream()
                            .filter(x -> x.getCreateTimestamp().toLocalDate().equals(LocalDate.now()))
                            .collect(Collectors.counting());

                }

                Map<Integer, PartnerCollectionPlanModel> collectionMap = partnerCollectionService.getCollectionMap(fofoIdList, startDate);

                Map<Integer, List<PartnerSecondaryPlanModel>> partnerSecondaryPlans = orderRepository
                        .selectPartnerSecondaryGroupByBrand(fofoIdList, startDate.toLocalDate()).stream()
                        .collect(Collectors.groupingBy(x -> x.getFofoId()));

                // Pre-fetch all AuthUsers that might be needed (instead of N+1 queries)
                Set<Integer> allAuthIds = new HashSet<>();
                collectionMap.values().stream()
                        .map(PartnerCollectionPlanModel::getAuthId)
                        .filter(id -> id != null && id != authUser.getId())
                        .forEach(allAuthIds::add);
                partnerSecondaryPlans.values().stream()
                        .flatMap(List::stream)
                        .map(PartnerSecondaryPlanModel::getAuthId)
                        .filter(id -> id != null && id != authUser.getId())
                        .forEach(allAuthIds::add);
                Map<Integer, AuthUser> authUserMap = new HashMap<>();
                if (!allAuthIds.isEmpty()) {
                    authUserMap = authRepository.selectByIds(new ArrayList<>(allAuthIds)).stream()
                            .collect(Collectors.toMap(AuthUser::getId, x -> x));
                }

                LOGGER.info("partnerSecondayPlans {}", partnerSecondaryPlans);
                for (Entry<Integer, CustomRetailer> customRetailerEntry : customRetailers.entrySet()) {
                    int fofoId = customRetailerEntry.getKey();
                    CustomRetailer customRetailer = customRetailerEntry.getValue();
                    float totalSecondaryPlan = 0;
                    float totalSecondaryAchivement = 0;

                    PartnerTargetAchievementModel ptam = new PartnerTargetAchievementModel();
                    ptam.setFofoId(fofoId);
                    ptam.setBusinessName(customRetailer.getBusinessName());
                    ptam.setMobile(customRetailer.getMobileNumber());

                    if (partnerDailyInvestmentMap.get(fofoId) != null) {
                        ptam.setWalletAmount(partnerDailyInvestmentMap.get(fofoId).getWalletAmount());
                        ptam.setShortInvestment(partnerDailyInvestmentMap.get(fofoId).getShortInvestment());
                    }

                    if (collectionMap.get(fofoId) != null) {
                        PartnerCollectionPlanModel collectionPlan = collectionMap.get(fofoId);

                        ptam.setRemark(collectionPlan.getRemark());
                        ptam.setMessage(collectionPlan.getMessage());
                        ptam.setRemarkTimestamp(collectionPlan.getRemarkTimestamp());
                        ptam.setRank(collectionPlan.getRank());
                        Integer authId = collectionPlan.getAuthId();

                        Map<Integer, String> rankColorMap = ProfitMandiConstants.Rank_Color_Map;

                        ptam.setCollectionColor(rankColorMap.get(collectionPlan.getRank()));

                        LOGGER.info("authId" + authId);

                        if (collectionPlan.getTargetPlan() != null && collectionPlan.getCommittedDate() != null) {
                            float targetCollection = 0;

                            if (collectionPlan.getRank() == 2 && collectionPlan.getAchievementPlan() != null) {
                                targetCollection = collectionPlan.getTargetPlan() - collectionPlan.getAchievementPlan();
                            } else {
                                targetCollection = collectionPlan.getTargetPlan();
                            }

                            if (authId != null && authId == authUser.getId()) {
                                if (!collectionPlan.getCommittedDate().isAfter(startDate)) {
                                    totalPartnerTargetCollection += targetCollection;
                                }
                            }
                            ptam.setCollectionTarget(targetCollection);
                            if (collectionPlan.getRank() == 2 && collectionPlan.getAchievementPlan() != null
                                    && collectionPlan.getWalletTimestamp() != null
                                    && collectionPlan.getWalletTimestamp().toLocalDate().equals(startDate.toLocalDate())) {
                                if (authId != null && authId == authUser.getId()) {
                                    totalPartnerAchievementCollection += collectionPlan.getAchievementPlan();
                                }
                                ptam.setCollectionAchievement(collectionPlan.getAchievementPlan());
                            } else if (collectionPlan.getCommittedDate().toLocalDate().isEqual(startDate.toLocalDate())
                                    && collectionPlan.getAchievementPlan() != null) {
                                if (authId != null && authId == authUser.getId()) {
                                    totalPartnerAchievementCollection += collectionPlan.getAchievementPlan();
                                }
                                ptam.setCollectionAchievement(collectionPlan.getAchievementPlan());

                            }
                        }

                        if (authId != null && authId != authUser.getId()) {
                            ptam.setAuthUser(authUserMap.get(authId));

                        }

                        if (collectionPlan.getCommittedDate() != null) {

                            ptam.setCollectionCommitmentDate(collectionPlan.getCommittedDate().toLocalDate());
                        }

                    }

                    // Secondary

                    PartnerSecondaryPlanModel otherPartnerSecondaryPlanModel = null;
                    Map<String, PartnerSecondaryPlanModel> secondaryModelMap = new HashMap<>();
                    if (partnerSecondaryPlans.get(fofoId) != null) {
                        long otherBrandSecondary = 0;
                        for (PartnerSecondaryPlanModel pspm : partnerSecondaryPlans.get(fofoId)) {
                            Integer authId = pspm.getAuthId();
                            if (!brands.contains(pspm.getBrand())) {
                                if (pspm.getAchievementPlan() != null) {
                                    otherBrandSecondary += pspm.getAchievementPlan();
                                }
                            } else {
                                otherPartnerSecondaryPlanModel = pspm;
                            }
                            if (pspm.getTargetPlan() != null) {
                                totalSecondaryPlan += pspm.getTargetPlan();
                            }

                            if (pspm.getAchievementPlan() != null) {
                                totalSecondaryAchivement += pspm.getAchievementPlan();
                            }

                            if (pspm.getCommittedDate() != null) {

                                ptam.setSecondaryCommitmentDate(pspm.getCommittedDate().toLocalDate());
                            }
                            if (authId != null && authId == authUser.getId()) {

                                if (pspm.getTargetPlan() != null && pspm.getCommittedDate() != null) {

                                    if (pspm.getCommittedDate().isEqual(startDate)) {
                                        totalPartnerTargetSecondary += pspm.getTargetPlan();
                                    }
                                }

                                if (pspm.getAchievementPlan() != null) {
                                    totalPartnerAchievementSecondary += pspm.getAchievementPlan();
                                }

                            }
                        }
                        if (otherPartnerSecondaryPlanModel != null) {
                            otherPartnerSecondaryPlanModel.setAchievementPlan(otherBrandSecondary);
                        }
                        secondaryModelMap = partnerSecondaryPlans.get(fofoId).stream()
                                .filter(x -> brands.contains(x.getBrand()))
                                .collect(Collectors.toMap(x -> x.getBrand(), x -> x));

                        if (secondaryModelMap.containsKey("Others")) {
                            PartnerSecondaryPlanModel psp = secondaryModelMap.get("Others");
                            psp.setAchievementPlan(otherBrandSecondary);
                        } else {
                            secondaryModelMap.put("Others", new PartnerSecondaryPlanModel(fofoId, "Others", (long) 0, otherBrandSecondary, authUser.getId(), null));
                        }
                        for (String brand : brands) {
                            if (!secondaryModelMap.containsKey(brand)) {
                                secondaryModelMap.put(brand, new PartnerSecondaryPlanModel(fofoId, brand, (long) 0, (long) 0, authUser.getId(), null));
                            }
                        }

                        for (Entry<String, PartnerSecondaryPlanModel> secondaryModelEntry : secondaryModelMap.entrySet()) {
                            Integer authId = secondaryModelEntry.getValue().getAuthId();
                            if (authId != null && authId != authUser.getId()) {
                                secondaryModelEntry.getValue().setAuthUser(authUserMap.get(authId));
                                ptam.setSecondaryColor("red");
                            }

                        }

                        ptam.setPartnerSecondaryModel(secondaryModelMap);
                        ptam.setTotalSecondaryPlan(totalSecondaryPlan);
                        ptam.setTotalSecondaryAchievement(totalSecondaryAchivement);
                        // Secondary

                    } else {
                        for (String brand : brands) {
                            PartnerSecondaryPlanModel pspm = new PartnerSecondaryPlanModel();
                            pspm.setAchievementPlan((long) 0);
                            pspm.setTargetPlan((long) 0);
                            pspm.setBrand(brand);
                            pspm.setFofoId(fofoId);
                            secondaryModelMap.put(brand, pspm);
                        }
                        ptam.setPartnerSecondaryModel(secondaryModelMap);
                    }

                    if (!partnerTicketCount.isEmpty()) {
                        if (partnerTicketCount.get(fofoId) != null) {
                            ptam.setTicketCount(partnerTicketCount.get(fofoId));
                        } else {
                            ptam.setTicketCount(0);

                        }
                    }

                    ptams.add(ptam);

                }
                List<Position> positions = positionRepository.selectPositionByAuthId(authUser.getId()).stream().collect(Collectors.toList());

                LOGGER.info("positions {}", positions);

                boolean isRBMAndL1 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L1.equals(position.getEscalationType()));
                boolean isRBMAndNotL1 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && !EscalationType.L1.equals(position.getEscalationType()));

                LOGGER.info("isRBMAndL1{}", isRBMAndL1);

                boolean isSales = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_SALES == position.getCategoryId());

                tm.setTotalPartnerTargetCollection(totalPartnerTargetCollection);
                tm.setTotalPartnerTargetSecondary(totalPartnerTargetSecondary);
                tm.setTotalPartnerSecondary(totalPartnerAchievementSecondary);
                tm.setTotalPartnerCollection(totalPartnerAchievementCollection);
                tm.setTodayCollectionCount((int) todayCollectionCount);
                List<PartnerTargetAchievementModel> filteredPtams;

                if (isRBMAndL1) {
                    filteredPtams = ptams;
//                    as of now all party is showing to rbm
                    /*filteredPtams = ptams.stream()
                            .filter(ptam -> !(CollectionRemark.RBM_L2_ESCALATION.equals(ptam.getRemark()) || CollectionRemark.SALES_ESCALATION.equals(ptam.getRemark())))
                            .collect(Collectors.toList());*/
                } else if (isRBMAndNotL1) {
                    filteredPtams = ptams;
                    /*filteredPtams = ptams.stream()
                            .filter(ptam -> !(CollectionRemark.SALES_ESCALATION.equals(ptam.getRemark())))
                            .collect(Collectors.toList());*/
                } else if (isSales) {
                    filteredPtams = ptams.stream()
                            .filter(ptam -> !(CollectionRemark.RBM_L2_ESCALATION.equals(ptam.getRemark())))
                            .collect(Collectors.toList());
                } else {
                    filteredPtams = ptams;
                }
                tm.setRBMAndL1(isRBMAndL1);
                tm.setRBMAndNotL1(isRBMAndNotL1);
                tm.setSales(isSales);
                /*tm.setTargetAchievement(filteredPtams.stream().sorted(Comparator.comparing(PartnerTargetAchievementModel::getRank))
                        .collect(Collectors.toList()));*/
                tm.setTargetAchievement(filteredPtams.stream()
                        .sorted(Comparator
                                .comparing((PartnerTargetAchievementModel ptam) -> {
                                    LocalDate remarkDate = ptam.getRemarkTimestamp() != null
                                            ? ptam.getRemarkTimestamp().toLocalDate()
                                            : null;
                                    return remarkDate != null && remarkDate.equals(LocalDate.now());
                                }) // false → comes first, true (today) → goes last
                                .thenComparing(PartnerTargetAchievementModel::getRank)
                        )
                        .collect(Collectors.toList()));


            }

        }
        return responseSender.ok(tm);

    }

    //TODO:Amit
    @RequestMapping(value = "/target", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    public ResponseEntity<?> createPartnerTarget(HttpServletRequest request,
                                                 @RequestBody PartnerTargetAchievementModel ptam) throws ProfitMandiBusinessException {

        LOGGER.info("ptam" + ptam);

        AuthUser authUser = authRepository.selectByGmailId(ptam.getCreatedBy());

        if (ptam.getCollectionCommitmentDate().isAfter(LocalDate.now())
                || ptam.getCollectionCommitmentDate().isEqual(LocalDate.now())) {
            PartnerCollectionPlan partnerCollectionPlan = partnerCollectionPlanRepository
                    .selectByLocalDate(LocalDate.now(), ptam.getFofoId(), true);
            LOGGER.info("pcp" + partnerCollectionPlan);

            if (partnerCollectionPlan == null) {
                if (ptam.getCollectionTarget() > 0) {

                    partnerCollectionPlan = new PartnerCollectionPlan();
                    partnerCollectionPlan.setCreateTimestamp(LocalDateTime.now());
                    partnerCollectionPlan.setAuthId(authUser.getId());
                    partnerCollectionPlan.setFofoId(ptam.getFofoId());
                    partnerCollectionPlan.setActive(true);
                    partnerCollectionPlan.setCollectionPlan(ptam.getCollectionTarget());
                    partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
                    partnerCollectionPlan.setCommitedTimestamp(ptam.getCollectionCommitmentDate().atStartOfDay());
                    partnerCollectionPlanRepository.persist(partnerCollectionPlan);

                }

            } else {

                if (partnerCollectionPlan.getCollectionPlan() != ptam.getCollectionTarget()) {
                    float totalCollectionPlan = partnerCollectionPlan.getCollectionPlan() + 10000;
                    if (authUser.getId() == partnerCollectionPlan.getAuthId()) {

                        if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
                                || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
                                || ptam.getCollectionTarget() >= totalCollectionPlan) {
                            partnerCollectionPlan.setCollectionPlan(ptam.getCollectionTarget());
                            partnerCollectionPlan.setActive(true);
                            partnerCollectionPlan
                                    .setCommitedTimestamp(ptam.getCollectionCommitmentDate().atStartOfDay());
                            partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
                        } else {
                            throw new ProfitMandiBusinessException("collection target", "",
                                    "collection target should be more than " + totalCollectionPlan);
                        }

                    } else {

                        if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
                                || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
                                || ptam.getCollectionTarget() >= totalCollectionPlan) {

                            partnerCollectionPlan.setActive(false);
                            partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
                            partnerCollectionPlan = new PartnerCollectionPlan();
                            partnerCollectionPlan.setCreateTimestamp(LocalDateTime.now());
                            partnerCollectionPlan.setAuthId(authUser.getId());
                            partnerCollectionPlan.setFofoId(ptam.getFofoId());
                            partnerCollectionPlan.setActive(true);
                            partnerCollectionPlan
                                    .setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
                            partnerCollectionPlan.setCollectionPlan(ptam.getCollectionTarget());
                            partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
                            partnerCollectionPlanRepository.persist(partnerCollectionPlan);
                        } else {
                            throw new ProfitMandiBusinessException("collection target", "",
                                    "collection target should be more than " + totalCollectionPlan);
                        }
                    }
                }

                if ((LocalDate.now().atStartOfDay().equals(ptam.getCollectionCommitmentDate().atStartOfDay())
                        || ptam.getCollectionCommitmentDate().atStartOfDay().isAfter(LocalDate.now().atStartOfDay()))
                        && partnerCollectionPlan.getCollectionPlan() == ptam.getCollectionTarget()) {
                    partnerCollectionPlan.setCommitedTimestamp(ptam.getCollectionCommitmentDate().atStartOfDay());
                    partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
                }

            }

            PartnerCollectionRemark pcr = new PartnerCollectionRemark();
            pcr.setFofoId(ptam.getFofoId());
            pcr.setAuthId(authUser.getId());
            pcr.setMessage("Collection plan for" + ptam.getCollectionCommitmentDate());
            pcr.setRemark(CollectionRemark.COLLECTION_PLAN);
            pcr.setCreateTimestamp(LocalDateTime.now());
            partnerCollectionRemarkRepository.persist(pcr);
        } else {
            throw new ProfitMandiBusinessException("Date", "",
                    "you can't select the back date " + ptam.getCollectionCommitmentDate());
        }

        for (Entry<String, PartnerSecondaryPlanModel> pspm : ptam.getPartnerSecondaryModel().entrySet()) {

            if (ptam.getCollectionCommitmentDate().isAfter(LocalDate.now())
                    || ptam.getCollectionCommitmentDate().isEqual(LocalDate.now())) {
                PartnerSecondaryPlanModel plan = pspm.getValue();
                PartnerSecondaryPlan psp = partnerSecondaryPlanRepository.selectByLocalDateBrand(plan.getBrand(),
                        LocalDate.now(), ptam.getFofoId(), true);
                LOGGER.info("psp" + psp);

                if (psp == null) {
                    if (plan.getTargetPlan() != null && plan.getTargetPlan() > 0) {

                        psp = new PartnerSecondaryPlan();
                        psp.setAuthId(authUser.getId());
                        psp.setBrand(pspm.getKey());
                        psp.setFofoId(pspm.getValue().getFofoId());
                        psp.setSecondaryPlan(pspm.getValue().getTargetPlan());
                        psp.setCreateTimestamp(LocalDateTime.now());
                        psp.setUpdatedTimestamp(LocalDateTime.now());
                        psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
                        psp.setActive(true);
                        partnerSecondaryPlanRepository.persist(psp);
                    }

                } else {
                    if (plan.getTargetPlan() != psp.getSecondaryPlan()) {
                        float totalSecondaryPlan = psp.getSecondaryPlan() + 10000;
                        if (authUser.getId() == plan.getAuthId()) {
                            if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
                                    || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
                                    || plan.getTargetPlan() >= totalSecondaryPlan) {
                                psp.setSecondaryPlan(pspm.getValue().getTargetPlan());
                                psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
                                psp.setUpdatedTimestamp(LocalDateTime.now());
                                psp.setActive(true);
                            } else {
                                throw new ProfitMandiBusinessException("secondary target", "",
                                        "secondary target should be more than " + totalSecondaryPlan);
                            }

                        } else {

                            if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
                                    || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
                                    || plan.getTargetPlan() >= totalSecondaryPlan) {

                                psp.setUpdatedTimestamp(LocalDateTime.now());
                                psp.setActive(false);
                                psp = new PartnerSecondaryPlan();
                                psp.setAuthId(authUser.getId());
                                psp.setBrand(pspm.getKey());
                                psp.setFofoId(pspm.getValue().getFofoId());
                                psp.setSecondaryPlan(pspm.getValue().getTargetPlan());
                                psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
                                psp.setCreateTimestamp(LocalDateTime.now());
                                psp.setUpdatedTimestamp(LocalDateTime.now());
                                psp.setActive(true);
                                partnerSecondaryPlanRepository.persist(psp);
                            } else {
                                throw new ProfitMandiBusinessException("secondary target", "",
                                        "secondary target should be more than " + totalSecondaryPlan);
                            }
                        }
                    }

                    if ((LocalDate.now().atStartOfDay().equals(ptam.getSecondaryCommitmentDate().atStartOfDay())
                            || ptam.getSecondaryCommitmentDate().atStartOfDay().isAfter(LocalDate.now().atStartOfDay()))
                            && plan.getTargetPlan() == psp.getSecondaryPlan()) {
                        psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
                        psp.setUpdatedTimestamp(LocalDateTime.now());
                    }

                }
            } else {
                throw new ProfitMandiBusinessException("Date", "",
                        "you can't select the back date " + ptam.getSecondaryCommitmentDate());
            }

        }

        return responseSender.ok(true);

    }

    @RequestMapping(value = "/uploadFranchiseeVisit", method = RequestMethod.POST)
    public ResponseEntity<?> readCsvFileAndSetLead(HttpServletRequest request, @RequestPart MultipartFile
            multipartFile) throws Throwable {

        int id = (int) request.getAttribute("userId");

        LOGGER.info("id" + id);

        User user = userRepository.selectById(id);

        AuthUser authUser = authRepository.selectByEmailOrMobile(user.getEmailId());

        String fileName = multipartFile.getName();
        String fileNames = multipartFile.getOriginalFilename();

        LOGGER.info("fileName" + fileName);
        LOGGER.info("fileNames" + fileNames);

        List<CSVRecord> records = FileUtil.readFile(multipartFile);

        for (CSVRecord record : records) {
            FranchiseeVisit franchiseeVisit = new FranchiseeVisit();
            franchiseeVisit.setFofoId(Integer.parseInt(record.get(0)));
            CustomRetailer customRetailer = retailerService.getFofoRetailer(Integer.parseInt(record.get(0)));

            franchiseeVisit.setPartnerName(customRetailer.getBusinessName());
            franchiseeVisit.setAgenda(record.get(1));
            franchiseeVisit.setCreatedTimestamp(LocalDateTime.now());
            franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
            franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
            LOGGER.info(record.get(1));
            LocalDate dateTime = LocalDate.parse(record.get(2), formatter);

            franchiseeVisit.setScheduleTimestamp(dateTime.atStartOfDay());
            // change

            String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
            franchiseeVisit.setCreatedBy(authUserName);
            franchiseeVisit.setAuthId(authUser.getId());

            franchiseeVisitRepository.persist(franchiseeVisit);

            return responseSender.ok(true);
        }

        return responseSender.ok(true);

    }

    @RequestMapping(value = "/downloadFranchiseeVisitTemplate", method = RequestMethod.GET)
    public ResponseEntity<?> downloadFranchiseeVisitTemplate(HttpServletRequest request) throws Exception {

        // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        List<List<?>> rows = new ArrayList<>();

        ByteArrayOutputStream byteArrayOutputStream = FileUtil
                .getCSVByteStream(Arrays.asList("Partner Id", "Agenda", "Schedule Timestamp"), rows);

        try {
            byteArrayOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String filename = "template.csv";
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/csv");
        headers.setContentDispositionFormData(filename, filename);

        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");

        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers,
                HttpStatus.OK);
        return response;

    }

    @RequestMapping(value = "/markVisitAttendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> markVisitAttendance(HttpServletRequest request, @RequestParam(name = "id") int id)
            throws ProfitMandiBusinessException {
        FranchiseeVisit franchiseeVisit = franchiseeVisitRepository.selectById(id);

        franchiseeVisit.setVisitTimestamp(LocalDateTime.now());

        return responseSender.ok(true);

    }

    @RequestMapping(value = "/getVisitRequests", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getVisitRequests(HttpServletRequest request,
                                              @RequestParam(name = "gmailId") String gmailId, Model model) throws Exception {

        AuthUser authUser = authRepository.selectByGmailId(gmailId);

        List<Integer> authUserIds = authService.getAllReportees(authUser.getId());
        List<VisitSummaryModel> visitSummary = new ArrayList<>();

        if (!authUserIds.isEmpty()) {
            List<AuthUser> authUsers = authRepository.selectByIds(authUserIds);

            List<VisitRequest> allVisitRequests = visitRequestRepository.selectByAuthIdsAndDate(authUserIds,
                    LocalDate.now().minusDays(7), Arrays.asList(VisitStatus.approved, VisitStatus.pending));
            LOGGER.info("allVisitRequests {}", allVisitRequests);

            if (!allVisitRequests.isEmpty()) {

                List<Integer> leadIds = allVisitRequests.stream().filter(x -> x.getVisitType().equals("lead"))
                        .map(x -> x.getVisitId()).collect(Collectors.toList());
                List<Lead> leads = new ArrayList<>();
                if (!leadIds.isEmpty()) {
                    leads = leadRepository.selectAllByIds(leadIds);

                    // Batch fetch all LeadActivities instead of N+1
                    List<LeadActivity> allLeadActivities = leadActivityRepository.selectAllByleadIds(leadIds);
                    Map<Integer, List<LeadActivity>> leadActivityMap = allLeadActivities.stream()
                            .collect(Collectors.groupingBy(LeadActivity::getLeadId));

                    for (Lead lead : leads) {
                        List<LeadActivity> leadActivities = leadActivityMap.get(lead.getId());
                        if (leadActivities != null && !leadActivities.isEmpty()) {
                            lead.setScheduledTimestamp(leadActivities.get(0).getSchelduleTimestamp());
                            lead.setLeadActivity(leadActivities.get(0));
                        }
                    }
                }
                List<Integer> franchiseeIds = allVisitRequests.stream()
                        .filter(x -> x.getVisitType().equals("franchiseeVisit")).map(x -> x.getVisitId())
                        .collect(Collectors.toList());
                List<FranchiseeVisit> franchiseeVisits = new ArrayList<>();
                if (!franchiseeIds.isEmpty()) {
                    franchiseeVisits = franchiseeVisitRepository.selectAllByIds(franchiseeIds);
                    LOGGER.info("franchiseeVisits {}", franchiseeVisits);

                    // Batch fetch all FranchiseeActivities instead of N+1
                    List<FranchiseeActivity> allFranchiseeActivities = franchiseeActivityRepository
                            .selectByFranchiseeVisitIds(franchiseeIds);
                    Map<Integer, List<FranchiseeActivity>> franchiseeActivityMap = allFranchiseeActivities.stream()
                            .collect(Collectors.groupingBy(FranchiseeActivity::getFranchiseeVisitd));

                    for (FranchiseeVisit franchiseeVisit : franchiseeVisits) {
                        List<FranchiseeActivity> franchiseeActivities = franchiseeActivityMap.get(franchiseeVisit.getId());
                        LOGGER.info("franchiseeActivities {}", franchiseeActivities);

                        if (franchiseeActivities != null && !franchiseeActivities.isEmpty()) {
                            franchiseeVisit.setScheduleTimestamp(franchiseeActivities.get(0).getSchelduleTimestamp());
                            franchiseeVisit.setFranchiseeActivity(franchiseeActivities.get(0));
                        }
                    }

                }

                // Pre-fetch all retailers using cached method (instead of N+1 getFofoRetailer calls)
                Map<Integer, CustomRetailer> allRetailersMap = retailerService.getAllFofoRetailers();

                Map<LocalDate, List<VisitRequest>> dateWiseVisitRequest = allVisitRequests.stream()
                        .collect(Collectors.groupingBy(x -> x.getScheduleTimestamp().toLocalDate()));

                for (Entry<LocalDate, List<VisitRequest>> visitEntry : dateWiseVisitRequest.entrySet()) {

                    LocalDate date = visitEntry.getKey();
                    List<VisitRequest> visitRequests = visitEntry.getValue();

                    VisitSummaryModel visitSummaryModel = new VisitSummaryModel();

                    visitSummaryModel.setDate(date);
                    List<Integer> dateWiseLeadEntry = visitRequests.stream()
                            .filter(x -> x.getVisitType().equals("lead")).map(x -> x.getVisitId())
                            .collect(Collectors.toList());

                    List<Integer> dateWiseFranchiseeIds = visitRequests.stream()
                            .filter(x -> x.getVisitType().equals("franchiseeVisit")).map(x -> x.getVisitId())
                            .collect(Collectors.toList());

                    Map<Integer, List<Lead>> filteredLeadsMap = null;
                    if (!leads.isEmpty()) {
                        filteredLeadsMap = leads.stream().filter(x -> dateWiseLeadEntry.contains(x.getId()))
                                .collect(Collectors.groupingBy(x -> x.getAssignTo()));

                    }

                    Map<Integer, List<FranchiseeVisit>> filteredFranchiseeVisitsMap = null;

                    if (!franchiseeVisits.isEmpty()) {
                        filteredFranchiseeVisitsMap = franchiseeVisits.stream()
                                .filter(x -> dateWiseFranchiseeIds.contains(x.getId()))
                                .collect(Collectors.groupingBy(x -> x.getAuthId()));

                    }

                    List<UserVisitModel> userVisits = new ArrayList<>();

                    for (AuthUser auth : authUsers) {
                        UserVisitModel userVisitModel = new UserVisitModel();
                        List<VisitDescriptionModel> visitDescriptions = new ArrayList<>();

                        List<Lead> authLeads = new ArrayList<>();
                        if (filteredLeadsMap != null) {
                            authLeads = filteredLeadsMap.get(auth.getId());

                        }

                        if (authLeads != null && !authLeads.isEmpty()) {
                            userVisitModel.setAuthUser(auth.getFullName());

                            for (Lead lead : authLeads) {

                                VisitRequest visitRequest = visitRequests.stream().filter(
                                                x -> x.getVisitId() == lead.getId() && x.getCreatedBy() == lead.getAssignTo())
                                        .findAny().orElse(null);
                                VisitDescriptionModel visitDescriptionModel = new VisitDescriptionModel();
                                visitDescriptionModel.setVisitId(visitRequest.getId());
                                visitDescriptionModel.setVisitName(lead.getFirstName());
                                visitDescriptionModel.setCity(lead.getCity());
                                visitDescriptionModel.setState(lead.getState());
                                visitDescriptionModel.setScheduleTime(lead.getScheduledTimestamp());
                                visitDescriptionModel.setStatus(visitRequest.getStatus());
                                visitDescriptionModel.setRemarks(lead.getLeadActivity().getRemark());
                                visitDescriptionModel.setVisitType(visitRequest.getVisitType());
                                visitDescriptions.add(visitDescriptionModel);

                            }
                        }

                        List<FranchiseeVisit> authfranchiseeVisit = new ArrayList<>();
                        if (filteredFranchiseeVisitsMap != null) {
                            authfranchiseeVisit = filteredFranchiseeVisitsMap.get(auth.getId());

                        }

                        if (authfranchiseeVisit != null && !authfranchiseeVisit.isEmpty()) {
                            userVisitModel.setAuthUser(auth.getFullName());

                            for (FranchiseeVisit franchiseeVisit : authfranchiseeVisit) {

                                VisitRequest visitRequest = visitRequests.stream()
                                        .filter(x -> x.getVisitId() == franchiseeVisit.getId()
                                                && x.getCreatedBy() == franchiseeVisit.getAuthId())
                                        .findAny().orElse(null);

                                CustomRetailer customRetailer = allRetailersMap.get(franchiseeVisit.getFofoId());
                                VisitDescriptionModel visitDescriptionModel = new VisitDescriptionModel();
                                visitDescriptionModel.setVisitId(visitRequest.getId());
                                visitDescriptionModel.setVisitName(franchiseeVisit.getPartnerName());
                                if (customRetailer != null && customRetailer.getAddress() != null) {
                                    visitDescriptionModel.setCity(customRetailer.getAddress().getCity());
                                    visitDescriptionModel.setState(customRetailer.getAddress().getState());
                                }
                                visitDescriptionModel.setScheduleTime(
                                        franchiseeVisit.getFranchiseeActivity().getSchelduleTimestamp());
                                visitDescriptionModel.setStatus(visitRequest.getStatus());
                                visitDescriptionModel.setRemarks(franchiseeVisit.getAgenda());
                                visitDescriptionModel.setVisitType(visitRequest.getVisitType());

                                visitDescriptions.add(visitDescriptionModel);

                            }
                        }

                        userVisitModel.setVisitDescriptions(visitDescriptions);
                        LOGGER.info("userVisit {}", userVisitModel);

                        if (userVisitModel.getAuthUser() != null) {
                            userVisits.add(userVisitModel);
                        }

                    }

                    visitSummaryModel.setUserVisits(userVisits);

                    visitSummary.add(visitSummaryModel);

                }

            }

        }
        return responseSender.ok(visitSummary);

    }

    @RequestMapping(value = "/visitRequest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> visitRequest(HttpServletRequest request, @RequestParam(name = "gmailId") String
                                                  gmailId,
                                          @RequestParam(name = "visitId") int visitId, @RequestParam(name = "status") VisitStatus status, Model model)
            throws Exception {
        AuthUser authUser = authRepository.selectByGmailId(gmailId);

        VisitRequest visitRequest = visitRequestRepository.selectById(visitId);
        visitRequest.setActionedBy(authUser.getId());
        visitRequest.setStatus(status);
        visitRequest.setUpdatedTimestamp(LocalDateTime.now());
        if (status.equals(VisitStatus.approved)) {
            visitRequest.setApprovedTimestamp(LocalDateTime.now());
        }

        SendNotificationModel sendNotificationModel = new SendNotificationModel();
        sendNotificationModel.setCampaignName("Visit Request");
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
        sendNotificationModel.setTitle("Visit Request");
        String message = "Visit Request has been " + status + " for scheduled time on "
                + visitRequest.getScheduleTimestamp().toLocalDate();

        sendNotificationModel.setMessage(message);
        sendNotificationModel.setMessageType(MessageType.notification);
        User user = userRepository.selectByEmailId(gmailId);
        sendNotificationModel.setUserIds(new ArrayList<>(user.getId()));

        notificationService.sendNotificationToAll(sendNotificationModel);

        return responseSender.ok(true);

    }

    @Autowired
    RbmRatingRepository rbmRatingRepository;

    @Autowired
    SalesRatingRepository salesRatingRepository;

    @RequestMapping(value = "/rbmRating", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> rbmRating(HttpServletRequest request, @RequestBody RbmSalesRatingRequest
            rbmSalesRatingRequest, Model model) throws Exception {
        int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
        UserCart uc = userAccountRepository.getUserCart(userId);
        int fofoId = uc.getUserId();

        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);

        YearMonth currentMonth = YearMonth.now();
        LocalDateTime startOfMonth = currentMonth.atDay(1).atStartOfDay();
        LocalDateTime endOfMonth = currentMonth.atEndOfMonth().atTime(23, 59, 59);

        // Check if RBM rating already exists
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdAndRbmIdForCurrentMonth(fofoId, rbmL1, startOfMonth, endOfMonth);
        if (!existingRbmRatings.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this month already exists.");
        }

        // Check if Sales Person rating exists (optional check)
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdAndSalesL1IdForCurrentMonth(fofoId, salesL1Id, startOfMonth, endOfMonth);
        if (!existingSalesRatings.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this month already exists.");
        }

        // Save RBM rating
        RbmRating rbmRating = new RbmRating();
        rbmRating.setComment(rbmSalesRatingRequest.getRbmComment());
        rbmRating.setRating(rbmSalesRatingRequest.getRbmRating());
        rbmRating.setFofoId(fofoId);
        rbmRating.setRbmId(rbmL1);
        rbmRating.setCreateTimeStamp(LocalDateTime.now());
        rbmRatingRepository.persist(rbmRating);

        // Save Sales Person rating
        SalesRating salesRating = new SalesRating();
        salesRating.setComment(rbmSalesRatingRequest.getSalesComment());
        salesRating.setRating(rbmSalesRatingRequest.getSalesRating());
        salesRating.setFofoId(fofoId);
        salesRating.setSalesL1Id(salesL1Id);
        salesRating.setCreateTimeStamp(LocalDateTime.now());
        salesRatingRepository.persist(salesRating);

        return responseSender.ok("Rating submitted successfully.");
    }


}