Subversion Repositories SmartDukaan

Rev

Rev 36860 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25899 tejbeer 1
package com.spice.profitmandi.web.controller;
2
 
32978 amit.gupta 3
import com.mongodb.DBObject;
31677 amit.gupta 4
import com.spice.profitmandi.common.enumuration.ActivationType;
31249 tejbeer 5
import com.spice.profitmandi.common.enumuration.MessageType;
25899 tejbeer 6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25952 tejbeer 7
import com.spice.profitmandi.common.model.CustomRetailer;
27117 tejbeer 8
import com.spice.profitmandi.common.model.ProfitMandiConstants;
31249 tejbeer 9
import com.spice.profitmandi.common.model.SendNotificationModel;
30433 tejbeer 10
import com.spice.profitmandi.common.util.FileUtil;
25899 tejbeer 11
import com.spice.profitmandi.common.web.util.ResponseSender;
12
import com.spice.profitmandi.dao.entity.auth.AuthUser;
30077 tejbeer 13
import com.spice.profitmandi.dao.entity.auth.PartnerCollectionPlan;
30416 tejbeer 14
import com.spice.profitmandi.dao.entity.auth.PartnerCollectionRemark;
30077 tejbeer 15
import com.spice.profitmandi.dao.entity.auth.PartnerSecondaryPlan;
36267 ranu 16
import com.spice.profitmandi.dao.entity.cs.Activity;
32978 amit.gupta 17
import com.spice.profitmandi.dao.entity.cs.Position;
36267 ranu 18
import com.spice.profitmandi.dao.entity.cs.Ticket;
25899 tejbeer 19
import com.spice.profitmandi.dao.entity.dtr.User;
25952 tejbeer 20
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
34322 ranu 21
import com.spice.profitmandi.dao.entity.fofo.*;
32978 amit.gupta 22
import com.spice.profitmandi.dao.entity.user.*;
30416 tejbeer 23
import com.spice.profitmandi.dao.enumuration.auth.CollectionRemark;
32978 amit.gupta 24
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
25
import com.spice.profitmandi.dao.enumuration.dtr.*;
26
import com.spice.profitmandi.dao.model.*;
27
import com.spice.profitmandi.dao.repository.auth.*;
28
import com.spice.profitmandi.dao.repository.cs.*;
29
import com.spice.profitmandi.dao.repository.dtr.*;
30087 tejbeer 30
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
35075 aman 31
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
36286 ranu 32
import com.spice.profitmandi.dao.repository.fofo.RatingReminderRepository;
34301 ranu 33
import com.spice.profitmandi.dao.repository.fofo.RbmRatingRepository;
34322 ranu 34
import com.spice.profitmandi.dao.repository.fofo.SalesRatingRepository;
35632 ranu 35
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
30077 tejbeer 36
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
32978 amit.gupta 37
import com.spice.profitmandi.dao.repository.user.AddressRepository;
31249 tejbeer 38
import com.spice.profitmandi.service.AuthService;
39
import com.spice.profitmandi.service.NotificationService;
30348 tejbeer 40
import com.spice.profitmandi.service.PartnerCollectionService;
35725 ranu 41
import com.spice.profitmandi.service.integrations.kommuno.RecordingService;
25952 tejbeer 42
import com.spice.profitmandi.service.user.RetailerService;
28935 tejbeer 43
import com.spice.profitmandi.service.user.StoreTimelineTatService;
25952 tejbeer 44
import com.spice.profitmandi.web.req.CreateFranchiseeRequest;
34322 ranu 45
import com.spice.profitmandi.web.req.RbmSalesRatingRequest;
25952 tejbeer 46
import com.spice.profitmandi.web.res.Partner;
25899 tejbeer 47
import io.swagger.annotations.ApiImplicitParam;
48
import io.swagger.annotations.ApiImplicitParams;
32978 amit.gupta 49
import org.apache.commons.csv.CSVRecord;
50
import org.apache.commons.io.output.ByteArrayOutputStream;
51
import org.apache.logging.log4j.LogManager;
52
import org.apache.logging.log4j.Logger;
53
import org.springframework.beans.factory.annotation.Autowired;
54
import org.springframework.format.annotation.DateTimeFormat;
55
import org.springframework.http.HttpHeaders;
56
import org.springframework.http.HttpStatus;
57
import org.springframework.http.MediaType;
58
import org.springframework.http.ResponseEntity;
59
import org.springframework.mail.javamail.JavaMailSender;
60
import org.springframework.stereotype.Controller;
61
import org.springframework.transaction.annotation.Transactional;
62
import org.springframework.ui.Model;
63
import org.springframework.web.bind.annotation.*;
64
import org.springframework.web.multipart.MultipartFile;
25899 tejbeer 65
 
32978 amit.gupta 66
import javax.servlet.http.HttpServletRequest;
67
import java.io.IOException;
68
import java.time.LocalDate;
69
import java.time.LocalDateTime;
34301 ranu 70
import java.time.YearMonth;
32978 amit.gupta 71
import java.time.format.DateTimeFormatter;
36114 ranu 72
import java.time.temporal.TemporalAdjusters;
32978 amit.gupta 73
import java.util.*;
35415 amit 74
import java.util.HashSet;
32978 amit.gupta 75
import java.util.Map.Entry;
76
import java.util.stream.Collectors;
77
 
25899 tejbeer 78
@Controller
79
@Transactional(rollbackFor = Throwable.class)
80
public class LeadController {
31677 amit.gupta 81
    private static final Logger LOGGER = LogManager.getLogger(LeadController.class);
82
    @Autowired
83
    private ResponseSender<?> responseSender;
25899 tejbeer 84
 
31677 amit.gupta 85
    @Autowired
86
    private AuthRepository authRepository;
25899 tejbeer 87
 
31677 amit.gupta 88
    @Autowired
89
    private LeadRepository leadRepository;
25899 tejbeer 90
 
31677 amit.gupta 91
    @Autowired
32923 ranu 92
    DocumentRepository documentRepository;
93
 
94
    @Autowired
95
    PositionRepository positionRepository;
96
 
97
    @Autowired
98
    private ActivityAttachmentRepository activityAttachmentRepository;
99
 
100
    @Autowired
31677 amit.gupta 101
    private CsService csService;
25952 tejbeer 102
 
31677 amit.gupta 103
    @Autowired
104
    private UserRepository userRepository;
25952 tejbeer 105
 
31677 amit.gupta 106
    @Autowired
32923 ranu 107
    private AddressRepository addressRepository;
108
 
109
    @Autowired
110
 
111
    private UserRoleRepository userRoleRepository;
112
 
113
    @Autowired
31677 amit.gupta 114
    private UserAccountRepository userAccountRepository;
25952 tejbeer 115
 
31677 amit.gupta 116
    @Autowired
117
    private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
25952 tejbeer 118
 
31677 amit.gupta 119
    @Autowired
120
    private RetailerService retailerService;
25952 tejbeer 121
 
31677 amit.gupta 122
    @Autowired
123
    private LeadActivityRepository leadActivityRepository;
25899 tejbeer 124
 
31677 amit.gupta 125
    @Autowired
126
    private FranchiseeVisitRepository franchiseeVisitRepository;
25952 tejbeer 127
 
31677 amit.gupta 128
    @Autowired
129
    private FranchiseeActivityRepository franchiseeActivityRepository;
25952 tejbeer 130
 
31677 amit.gupta 131
    @Autowired
132
    private PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
28935 tejbeer 133
 
31677 amit.gupta 134
    @Autowired
135
    private FofoStoreRepository fofoStoreRepository;
28935 tejbeer 136
 
31677 amit.gupta 137
    @Autowired
138
    private StoreTimelineTatService storeTimelineTatService;
28935 tejbeer 139
 
31677 amit.gupta 140
    @Autowired
141
    private PartnerCollectionService partnerCollectionService;
30348 tejbeer 142
 
31677 amit.gupta 143
    @Autowired
144
    private TicketRepository ticketRepository;
30416 tejbeer 145
 
31677 amit.gupta 146
    @Autowired
36267 ranu 147
    private ActivityRepository activityRepository;
148
 
149
    @Autowired
31677 amit.gupta 150
    private TicketCategoryRepository ticketCategoryRepository;
30416 tejbeer 151
 
31677 amit.gupta 152
    @Autowired
153
    private TicketSubCategoryRepository ticketSubCategoryRepository;
30416 tejbeer 154
 
31677 amit.gupta 155
    @Autowired
156
    private PartnerCollectionRemarkRepository partnerCollectionRemarkRepository;
30416 tejbeer 157
 
31677 amit.gupta 158
    @Autowired
159
    private Mongo mongoClient;
30487 tejbeer 160
 
31677 amit.gupta 161
    @Autowired
162
    private OrderRepository orderRepository;
30487 tejbeer 163
 
31677 amit.gupta 164
    @Autowired
165
    private PartnerCollectionPlanRepository partnerCollectionPlanRepository;
30487 tejbeer 166
 
31677 amit.gupta 167
    @Autowired
168
    private PartnerSecondaryPlanRepository partnerSecondaryPlanRepository;
30487 tejbeer 169
 
31677 amit.gupta 170
    @Autowired
171
    private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
30487 tejbeer 172
 
31677 amit.gupta 173
    @Autowired
174
    private VisitRequestRepository visitRequestRepository;
31249 tejbeer 175
 
31677 amit.gupta 176
    @Autowired
177
    private AuthService authService;
31249 tejbeer 178
 
31677 amit.gupta 179
    @Autowired
180
    private NotificationService notificationService;
31249 tejbeer 181
 
32923 ranu 182
    @Autowired
36400 amit 183
    JavaMailSender gmailRelaySender;
32923 ranu 184
 
185
    @Autowired
186
    private TicketAssignedRepository ticketAssignedRepository;
187
 
34124 tejus.loha 188
    @Autowired
189
    LeadDetailRepository leadDetailRepository;
190
 
35075 aman 191
    @Autowired
36771 ranu 192
    private com.spice.profitmandi.dao.repository.dtr.BeatRepository beatRepository;
193
 
194
    @Autowired
195
    private com.spice.profitmandi.dao.repository.dtr.BeatScheduleRepository beatScheduleRepository;
196
 
197
    @Autowired
198
    private com.spice.profitmandi.dao.repository.dtr.BeatRouteRepository beatRouteRepository;
199
 
200
    @Autowired
201
    private com.spice.profitmandi.dao.repository.dtr.LeadRouteRepository leadRouteRepository;
202
 
203
    @Autowired
204
    private com.spice.profitmandi.dao.repository.dtr.LeadLiveLocationRepository leadLiveLocationRepository;
205
 
206
    @Autowired
35075 aman 207
    PurchaseRepository purchaseRepository;
208
 
35632 ranu 209
    @Autowired
210
    private LoanRepository loanRepository;
211
 
35725 ranu 212
    @Autowired
213
    private RecordingService recordingService;
214
 
36771 ranu 215
    // Field-staff creates a lead from the planner — they're physically at the lead's
216
    // location, so we mark the geo APPROVED immediately (no link-and-approve cycle).
217
    @RequestMapping(value = "/lead-geo/self-approve", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
218
    @ApiImplicitParams({
219
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
220
    public ResponseEntity<?> selfApproveLeadGeo(javax.servlet.http.HttpServletRequest request,
221
                                                @RequestBody Map<String, Object> req) throws Exception {
222
        Integer leadId = req.get("leadId") instanceof Number ? ((Number) req.get("leadId")).intValue() : null;
223
        if (leadId == null || leadId <= 0) return responseSender.badRequest("leadId required");
224
        Object latObj = req.get("latitude"), lngObj = req.get("longitude");
225
        if (!(latObj instanceof Number) || !(lngObj instanceof Number)) return responseSender.badRequest("latitude/longitude required");
226
        double latitude = ((Number) latObj).doubleValue();
227
        double longitude = ((Number) lngObj).doubleValue();
228
        Lead lead = leadRepository.selectById(leadId);
229
        if (lead == null) return responseSender.badRequest("Lead not found");
230
        // Resolve approver from session, fall back to request body.
231
        int approverAuthId = 0;
232
        com.spice.profitmandi.common.model.UserInfo userInfo =
233
                (com.spice.profitmandi.common.model.UserInfo) request.getAttribute("userInfo");
234
        if (userInfo != null && userInfo.getEmail() != null) {
235
            AuthUser sessionAuth = authRepository.selectByGmailId(userInfo.getEmail());
236
            if (sessionAuth != null) approverAuthId = sessionAuth.getId();
237
        }
238
        if (approverAuthId == 0 && req.get("authUserId") instanceof Number) {
239
            approverAuthId = ((Number) req.get("authUserId")).intValue();
240
        }
241
 
242
        int imageDocumentId = 0;
243
        if (req.get("imageDocumentId") instanceof Number) {
244
            imageDocumentId = ((Number) req.get("imageDocumentId")).intValue();
245
        }
246
 
247
        LocalDateTime now = LocalDateTime.now();
248
        com.spice.profitmandi.dao.entity.user.LeadLiveLocation existing = leadLiveLocationRepository.selectByLeadId(leadId);
249
        if (existing != null) {
250
            existing.setLatitude(latitude);
251
            existing.setLongitude(longitude);
252
            existing.setStatus(com.spice.profitmandi.dao.enumuration.dtr.GeoLocationStatus.APPROVED);
253
            existing.setReviewedBy(approverAuthId);
254
            existing.setReviewedTimestamp(now);
255
            existing.setSubmissionCount(existing.getSubmissionCount() + 1);
256
            existing.setMobileNumber(lead.getLeadMobile());
257
            if (imageDocumentId > 0) existing.setImageDocumentId(imageDocumentId);
258
            existing.setUpdatedTimestamp(now);
259
            existing.setRemark("Self-approved by field creator");
260
        } else {
261
            com.spice.profitmandi.dao.entity.user.LeadLiveLocation loc = new com.spice.profitmandi.dao.entity.user.LeadLiveLocation();
262
            loc.setLeadId(leadId);
263
            loc.setLatitude(latitude);
264
            loc.setLongitude(longitude);
265
            loc.setMobileNumber(lead.getLeadMobile());
266
            loc.setImageDocumentId(imageDocumentId);
267
            loc.setStatus(com.spice.profitmandi.dao.enumuration.dtr.GeoLocationStatus.APPROVED);
268
            loc.setReviewedBy(approverAuthId);
269
            loc.setReviewedTimestamp(now);
270
            loc.setSubmissionCount(1);
271
            loc.setRemark("Self-approved by field creator");
272
            loc.setCreatedTimestamp(now);
273
            loc.setUpdatedTimestamp(now);
274
            leadLiveLocationRepository.persist(loc);
275
        }
276
 
277
        LeadActivity activity = new LeadActivity();
278
        activity.setLeadId(leadId);
279
        activity.setRemark("Geolocation self-approved by field creator");
280
        activity.setAuthId(approverAuthId);
281
        activity.setCreatedTimestamp(now);
282
        leadActivityRepository.persist(activity);
283
 
284
        Map<String, Object> result = new HashMap<>();
285
        result.put("status", "approved");
286
        result.put("leadId", leadId);
287
        return responseSender.ok(result);
288
    }
289
 
290
    // Returns the field-staff person's upcoming beat-day chips (today + next N days) so
291
    // the partner app can present "Schedule on today vs future" right after a self-create.
292
    // The auth user is resolved from the session — matches the existing
293
    // BeatTrackingController.listBeats pattern (the client `userId` is unreliable).
294
    public ResponseEntity<?> upcomingBeatsForUser(javax.servlet.http.HttpServletRequest request, int days) throws Exception {
295
        com.spice.profitmandi.common.model.UserInfo userInfo =
296
                (com.spice.profitmandi.common.model.UserInfo) request.getAttribute("userInfo");
297
        if (userInfo == null || userInfo.getEmail() == null) {
298
            return responseSender.badRequest("Auth session not found");
299
        }
300
        AuthUser authUser = authRepository.selectByGmailId(userInfo.getEmail());
301
        if (authUser == null) return responseSender.badRequest("Auth user not found");
302
        int authUserId = authUser.getId();
303
        LOGGER.info("upcomingBeatsForUser resolved authUserId={} from session email={}", authUserId, userInfo.getEmail());
304
        LocalDate today = LocalDate.now();
305
        LocalDate end = today.plusDays(days);
306
        List<com.spice.profitmandi.dao.entity.user.Beat> beats = beatRepository.selectActiveByAuthUserId(authUserId);
307
        List<Map<String, Object>> out = new ArrayList<>();
308
        for (com.spice.profitmandi.dao.entity.user.Beat b : beats) {
309
            List<com.spice.profitmandi.dao.entity.user.BeatSchedule> schedules =
310
                    beatScheduleRepository.selectByBeatIdAndDateRange(b.getId(), today, end);
311
            for (com.spice.profitmandi.dao.entity.user.BeatSchedule s : schedules) {
312
                if (s.getStartDate() == null || s.getStartDate().getYear() == 9999) continue;
313
                Map<String, Object> row = new HashMap<>();
314
                row.put("beatId", b.getId());
315
                row.put("beatName", b.getName());
316
                row.put("beatColor", b.getBeatColor());
317
                row.put("scheduleDate", s.getStartDate().toString());
318
                row.put("dayNumber", s.getDayNumber());
319
                row.put("isToday", today.equals(s.getStartDate()));
320
                out.add(row);
321
            }
322
        }
323
        out.sort(Comparator.comparing(r -> (String) r.get("scheduleDate")));
324
        return responseSender.ok(out);
325
    }
326
 
327
    // Attaches a (just-created, self-approved) lead to a sales user's beat on a given
328
    // date. Inserts a LeadRoute row with status=APPROVED; idempotent on re-call.
329
    // The auth user is resolved from the session token, not the request body.
330
    public ResponseEntity<?> scheduleLeadOnBeat(javax.servlet.http.HttpServletRequest request,
331
                                                Map<String, Object> req) throws Exception {
332
        com.spice.profitmandi.common.model.UserInfo userInfo =
333
                (com.spice.profitmandi.common.model.UserInfo) request.getAttribute("userInfo");
334
        if (userInfo == null || userInfo.getEmail() == null) {
335
            return responseSender.badRequest("Auth session not found");
336
        }
337
        AuthUser sessionAuth = authRepository.selectByGmailId(userInfo.getEmail());
338
        if (sessionAuth == null) return responseSender.badRequest("Auth user not found");
339
        int authUserId = sessionAuth.getId();
340
 
341
        Integer leadId = req.get("leadId") instanceof Number ? ((Number) req.get("leadId")).intValue() : null;
342
        Integer beatId = req.get("beatId") instanceof Number ? ((Number) req.get("beatId")).intValue() : null;
343
        Object dateObj = req.get("scheduleDate");
344
        if (leadId == null || beatId == null || dateObj == null) {
345
            return responseSender.badRequest("leadId, beatId and scheduleDate are required");
346
        }
347
        LocalDate scheduleDate;
348
        try {
349
            scheduleDate = LocalDate.parse(dateObj.toString());
350
        } catch (Exception e) {
351
            return responseSender.badRequest("scheduleDate must be yyyy-MM-dd");
352
        }
353
 
354
        Lead lead = leadRepository.selectById(leadId);
355
        if (lead == null) return responseSender.badRequest("Lead not found");
356
 
357
        // Beat must actually run on the chosen date — same guard as the panel-side flow.
358
        List<com.spice.profitmandi.dao.entity.user.BeatSchedule> match =
359
                beatScheduleRepository.selectByBeatIdAndDateRange(beatId, scheduleDate, scheduleDate);
360
        if (match.isEmpty()) return responseSender.badRequest("Beat is not scheduled on " + scheduleDate);
361
 
362
        // Idempotency: skip if the same lead is already pinned to this beat-day.
363
        boolean exists = leadRouteRepository.selectByBeatId(beatId).stream()
364
                .anyMatch(lr -> lr.getLeadId() == leadId
365
                        && scheduleDate.equals(lr.getScheduleDate())
366
                        && !"CANCELLED".equals(lr.getStatus()));
367
        if (exists) {
368
            return responseSender.ok(Collections.singletonMap("status", "already_scheduled"));
369
        }
370
 
371
        // Append at the end of the day's existing stops.
372
        int seq = beatRouteRepository.selectByBeatId(beatId).size() + 1;
373
 
374
        com.spice.profitmandi.dao.entity.user.LeadRoute lr = new com.spice.profitmandi.dao.entity.user.LeadRoute();
375
        lr.setBeatId(beatId);
376
        lr.setLeadId(leadId);
377
        lr.setScheduleDate(scheduleDate);
378
        lr.setSequenceOrder(seq);
379
        lr.setStatus("APPROVED");
380
        lr.setRequestedBy(authUserId);
381
        lr.setApprovedBy(authUserId);
382
        LocalDateTime now = LocalDateTime.now();
383
        lr.setApprovedTimestamp(now);
384
        lr.setCreatedTimestamp(now);
385
        lr.setUpdatedTimestamp(now);
386
        leadRouteRepository.persist(lr);
387
 
388
        Map<String, Object> result = new HashMap<>();
389
        result.put("status", "scheduled");
390
        result.put("scheduleDate", scheduleDate.toString());
391
        return responseSender.ok(result);
392
    }
393
 
31677 amit.gupta 394
    @RequestMapping(value = "/lead", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
395
    @ApiImplicitParams({
396
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
34118 tejus.loha 397
 
398
    public ResponseEntity<?> LeadUser(@RequestBody CreateRefferalRequest createRefferalRequest) throws Exception {
35079 vikas 399
        LOGGER.info("CreateReferralRequest - " + createRefferalRequest);
34394 tejus.loha 400
        if (createRefferalRequest.getOutletName() == null || createRefferalRequest.getPotential() == 0) {
35079 vikas 401
            throw new Exception("Outlet should not be empty and potential should be greater > 0");
34386 ranu 402
        }
35288 amit 403
        Lead lead;
404
        AuthUser authUser = authRepository.selectByGmailId(createRefferalRequest.getReffereeEmail());
405
        if (createRefferalRequest.getId() > 0) {
406
            lead = leadRepository.selectById(createRefferalRequest.getId());
407
            lead.setAssignTo(authUser.getId());
31677 amit.gupta 408
        } else {
35288 amit 409
            lead = new Lead();
410
            lead.setFirstName(createRefferalRequest.getFirstName());
411
            lead.setLastName(createRefferalRequest.getLastName());
412
            lead.setLeadMobile(createRefferalRequest.getMobile());
413
            lead.setState(createRefferalRequest.getState());
414
            lead.setCity(createRefferalRequest.getCity());
415
            lead.setAddress(createRefferalRequest.getAddress());
416
            lead.setCreatedTimestamp(LocalDateTime.now());
417
            lead.setOutLetName(createRefferalRequest.getOutletName());
418
            lead.setUpdatedTimestamp(LocalDateTime.now());
419
            lead.setStatus(createRefferalRequest.getStatus());
420
            lead.setSource(createRefferalRequest.getSource());
421
            lead.setNotinterestedReason(createRefferalRequest.getReason());
422
            lead.setPotential(createRefferalRequest.getPotential());
423
            if (createRefferalRequest.getColorCheck() == true) {
424
                lead.setColor("Green");
425
            } else {
426
                lead.setColor("Yellow");
427
            }
428
            String authUserName = authUser.getFullName();
429
            lead.setAuthId(authUser.getId());
430
            lead.setAssignTo(authUser.getId());
35079 vikas 431
            lead.setCreatedBy(authUserName);
432
            leadRepository.persist(lead);
35288 amit 433
 
35079 vikas 434
        }
34118 tejus.loha 435
 
31677 amit.gupta 436
        LeadActivity leadActivity = new LeadActivity();
437
        leadActivity.setLeadId(lead.getId());
35288 amit 438
        leadActivity.setRemark((createRefferalRequest.getId() > 0 ? "Self Assigned." : "") + createRefferalRequest.getRemark());
31677 amit.gupta 439
        leadActivity.setAuthId(authUser.getId());
31249 tejbeer 440
 
31677 amit.gupta 441
        if (createRefferalRequest.getStatus().equals(LeadStatus.followUp)) {
442
            leadActivity.setSchelduleTimestamp(createRefferalRequest.getSchelduleTimestamp());
443
            leadActivity.setCommunicationType(createRefferalRequest.getCommunicationType());
31249 tejbeer 444
 
31677 amit.gupta 445
            if (leadActivity.getCommunicationType().equals(CommunicationType.VISIT)) {
31249 tejbeer 446
 
31677 amit.gupta 447
                visitRequestRepository.createVisitRequest(lead.getId(), "lead", lead.getAssignTo(),
448
                        createRefferalRequest.getSchelduleTimestamp());
449
            }
450
            //
451
            leadActivity.setClosureTimestamp(createRefferalRequest.getClosureTimestamp());
452
            lead.setClosureTimestamp(createRefferalRequest.getClosureTimestamp());
453
        } else {
454
            leadActivity.setSchelduleTimestamp(null);
455
            leadActivity.setClosureTimestamp(null);
456
            lead.setClosureTimestamp(null);
457
        }
458
        leadActivity.setCreatedTimestamp(LocalDateTime.now());
459
        leadActivityRepository.persist(leadActivity);
25899 tejbeer 460
 
36771 ranu 461
        // Return the new lead's id alongside the legacy `success: true` flag so callers
462
        // that need to chain follow-up actions (e.g., the partner app's self-create →
463
        // self-approve geo → schedule on beat flow) can do so without an extra lookup.
464
        Map<String, Object> body = new HashMap<>();
465
        body.put("success", true);
466
        body.put("leadId", lead.getId());
467
        return responseSender.ok(body);
31677 amit.gupta 468
    }
25899 tejbeer 469
 
31677 amit.gupta 470
    @RequestMapping(value = "/lead-description", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
471
    @ApiImplicitParams({
472
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35288 amit 473
    public ResponseEntity<?> leadDescription(HttpServletRequest request, @RequestParam(name = "gmailId") String
474
                                                     gmailId,
31677 amit.gupta 475
                                             @RequestParam(name = "status") LeadStatus status) throws ProfitMandiBusinessException {
476
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
477
        List<Lead> leads = null;
478
        LOGGER.info("emails" + status);
31249 tejbeer 479
 
35469 vikas 480
        List<VisitRequest> visitRequests = visitRequestRepository.selectByAuthIdAndDate(authUser.getId(),
481
                LocalDate.now());
482
        Map<Integer, List<VisitRequest>> visitRequestMap = null;
483
        if (!visitRequests.isEmpty()) {
484
            visitRequestMap = visitRequests.stream().collect(Collectors.groupingBy(x -> x.getVisitId()));
485
        }
36827 ranu 486
 
487
        // Hierarchy expansion (Sales only):
488
        //   L1 → just self
489
        //   L2 → self + every L1 reportee
490
        //   L3 → self + L1 + L2 reportees
491
        //   L4 → self + L1 + L2 + L3 reportees
492
        // Non-Sales callers stay at "just self" — same as today.
493
        List<Integer> visibleAuthIds = visibleAuthIdsForLeads(authUser.getId());
494
 
31677 amit.gupta 495
        if (status.equals(LeadStatus.followUp)) {
35469 vikas 496
 
36853 ranu 497
            // Show every lead assigned to the caller (or downline for Sales heads)
498
            // whose status is followUp. The old call,
499
            //   selectLeadsScheduledBetweenDate(visibleAuthIds, null, null)
500
            // silently dropped any lead whose LATEST lead_activity row did not
501
            // carry a schedule_timestamp — which excludes leads that were
502
            // assigned but never scheduled, and leads whose most recent activity
503
            // was a visit-request action (create / reject / reassign / cancel)
504
            // that doesn't set schedule_timestamp. Result: most assigned leads
505
            // disappeared from the mobile "All Leads → Follow Up" tab.
506
            leads = leadRepository.selectByAssignAuthIdsAndStatus(visibleAuthIds, LeadStatus.followUp);
31249 tejbeer 507
 
36860 ranu 508
            // Hydrate Lead.scheduledTimestamp for the card date + sort order.
509
            // One batched lookup: lead_id → latest non-null schedule_timestamp.
510
            // Leads with no scheduled activity get null (still shown, just no date).
511
            if (leads != null && !leads.isEmpty()) {
512
                List<Integer> leadIds = leads.stream().map(Lead::getId).collect(Collectors.toList());
513
                Map<Integer, LocalDateTime> scheduleByLead =
514
                        leadActivityRepository.latestScheduleTimestampByLeadIds(leadIds);
515
                leads.forEach(l -> l.setScheduledTimestamp(scheduleByLead.get(l.getId())));
516
            }
517
 
35469 vikas 518
            if (visitRequestMap != null) {
31677 amit.gupta 519
                for (Lead lead : leads) {
520
                    List<VisitRequest> visitRequest = visitRequestMap.get(lead.getId());
35469 vikas 521
 
31677 amit.gupta 522
                    if (visitRequest != null) {
523
                        if (visitRequest.size() > 1) {
35469 vikas 524
 
525
                            Comparator<VisitRequest> visitComparato = Comparator
526
                                    .comparing(VisitRequest::getCreatedTimestamp);
527
 
528
                            VisitRequest youngestVisit = visitRequest.stream().max(visitComparato).get();
31677 amit.gupta 529
                            lead.setVisitStatus(youngestVisit.getStatus());
35469 vikas 530
 
31677 amit.gupta 531
                        } else {
532
                            lead.setVisitStatus(visitRequest.get(0).getStatus());
35469 vikas 533
 
31677 amit.gupta 534
                        }
535
                    }
35469 vikas 536
 
31677 amit.gupta 537
                }
538
            }
31249 tejbeer 539
 
31677 amit.gupta 540
            leads = leads.stream()
35079 vikas 541
                    .sorted(Comparator.comparing(Lead::getScheduledTimestamp, Comparator.nullsFirst(Comparator.reverseOrder())))
542
                    .collect(Collectors.toList());
35469 vikas 543
            /*
544
             * Collections.sort(leads, (o1, o2) -> { if (o1.getScheduledTimestamp() != null
545
             * && o2.getScheduledTimestamp() != null) { return
546
             * o1.getScheduledTimestamp().isBefore(o2.getScheduledTimestamp()) ? -1 : 1; }
547
             * else if (o1.getScheduledTimestamp() != null) { return 1; } else { return -1;
548
             * } });
549
             */
550
 
31677 amit.gupta 551
        } else {
36827 ranu 552
            // Same hierarchy expansion as the followUp branch — Sales heads see their
553
            // downline's leads alongside their own.
554
            leads = leadRepository.selectByAssignAuthIdsAndStatus(visibleAuthIds, status);
35469 vikas 555
 
31677 amit.gupta 556
        }
25979 tejbeer 557
 
31677 amit.gupta 558
        return responseSender.ok(leads);
35469 vikas 559
 
31677 amit.gupta 560
    }
25899 tejbeer 561
 
36827 ranu 562
    // Returns the auth-user IDs whose leads are visible to `authUserId` on the
563
    // mobile "My Leads" screen. Sales heads see their full downline; everyone
564
    // else (incl. non-Sales) sees only themselves.
565
    private List<Integer> visibleAuthIdsForLeads(int authUserId) {
566
        List<Integer> ids = new ArrayList<>();
567
        ids.add(authUserId);
568
 
569
        EscalationType highestSales = null;
570
        try {
571
            for (Position p : positionRepository.selectPositionByAuthId(authUserId)) {
572
                if (p.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_SALES) {
573
                    if (highestSales == null || p.getEscalationType().isGreaterThanEqualTo(highestSales)) {
574
                        highestSales = p.getEscalationType();
575
                    }
576
                }
577
            }
578
        } catch (Exception ignored) {
579
        }
580
 
581
        // Only sales heads (L2+) get the downline expansion. L1 / non-Sales stay at self.
582
        if (highestSales != null && highestSales.isGreaterThanEqualTo(EscalationType.L2)) {
583
            try {
584
                List<Integer> reportees = authService.getAllReportees(authUserId);
585
                if (reportees != null) {
586
                    for (Integer r : reportees) {
587
                        if (r != null && !ids.contains(r)) ids.add(r);
588
                    }
589
                }
590
            } catch (Exception ignored) {
591
            }
592
        }
593
        return ids;
594
    }
595
 
31677 amit.gupta 596
    @RequestMapping(value = "/getlead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35288 amit 597
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
598
    public ResponseEntity<?> getLead(HttpServletRequest request, @RequestParam(name = "id") int id) throws
599
            ProfitMandiBusinessException {
25899 tejbeer 600
 
31677 amit.gupta 601
        List<LeadActivity> leadActivities = leadActivityRepository.selectBYLeadId(id);
602
        Lead lead = leadRepository.selectById(id);
603
        lead.setLeadActivities(leadActivities);
604
        return responseSender.ok(lead);
25899 tejbeer 605
 
31677 amit.gupta 606
    }
25899 tejbeer 607
 
35079 vikas 608
    @RequestMapping(value = "/check-existing-lead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35288 amit 609
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
610
    public ResponseEntity<?> getLeadByMobile(HttpServletRequest request, @RequestParam(name = "mobile") String
611
            mobile) throws ProfitMandiBusinessException {
35079 vikas 612
        int userId = (int) request.getAttribute("userId");
613
        LOGGER.info("userId: " + userId);
614
        Lead lead = leadRepository.selectByMobileNumber(mobile);
615
        return responseSender.ok(lead);
616
 
617
    }
618
 
31677 amit.gupta 619
    @RequestMapping(value = "/leadUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35288 amit 620
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 621
    public ResponseEntity<?> leadUpdate(HttpServletRequest request, @RequestParam(name = "id") int id,
35288 amit 622
                                        @RequestParam(name = "status") LeadStatus status, @RequestParam(name = "colorCheck") Boolean colorCheck,
623
                                        @RequestParam(name = "remark") String remark, @RequestParam(name = "reason") String reason,
624
                                        @RequestParam(name = "schelduleTimestamp") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime schelduleTimestamp,
625
                                        @RequestParam(name = "closureTimestamp") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime closureTimestamp,
626
                                        @RequestParam(name = "communicationType") CommunicationType communicationType) throws
627
            ProfitMandiBusinessException {
25899 tejbeer 628
 
31677 amit.gupta 629
        Lead lead = leadRepository.selectById(id);
25899 tejbeer 630
 
31677 amit.gupta 631
        LeadActivity leadActivity = new LeadActivity();
632
        lead.setStatus(status);
633
        lead.setNotinterestedReason(reason);
634
        leadActivity.setRemark(remark);
635
        leadActivity.setLeadId(id);
34394 tejus.loha 636
        leadActivity.setOutletName(lead.getOutLetName());
637
        leadActivity.setPotential(lead.getPotential());
31677 amit.gupta 638
        leadActivity.setCreatedTimestamp(LocalDateTime.now());
639
        leadActivity.setSchelduleTimestamp(null);
640
        leadActivity.setClosureTimestamp(null);
641
        leadActivity.setAuthId(lead.getAssignTo());
642
        lead.setUpdatedTimestamp(LocalDateTime.now());
643
        if (colorCheck == true) {
644
            lead.setColor("Green");
645
        } else {
646
            lead.setColor("Yellow");
647
        }
648
        if (status == LeadStatus.followUp) {
25899 tejbeer 649
 
31677 amit.gupta 650
            if (schelduleTimestamp != null) {
31249 tejbeer 651
 
31677 amit.gupta 652
                leadActivity.setCommunicationType(communicationType);
31249 tejbeer 653
 
31677 amit.gupta 654
                if (leadActivity.getCommunicationType().equals(CommunicationType.VISIT)) {
31249 tejbeer 655
 
35079 vikas 656
                    visitRequestRepository.createVisitRequest(lead.getId(), "lead", lead.getAssignTo(), schelduleTimestamp);
31677 amit.gupta 657
                }
31249 tejbeer 658
 
31677 amit.gupta 659
            }
660
            leadActivity.setSchelduleTimestamp(schelduleTimestamp);
661
            leadActivity.setClosureTimestamp(closureTimestamp);
662
            lead.setClosureTimestamp(closureTimestamp);
26250 tejbeer 663
 
31677 amit.gupta 664
        } else {
26250 tejbeer 665
 
31677 amit.gupta 666
            leadActivity.setSchelduleTimestamp(null);
667
            leadActivity.setClosureTimestamp(null);
668
            lead.setClosureTimestamp(null);
26244 tejbeer 669
 
31677 amit.gupta 670
        }
671
        leadActivityRepository.persist(leadActivity);
672
        return responseSender.ok(true);
25899 tejbeer 673
 
31677 amit.gupta 674
    }
25952 tejbeer 675
 
31677 amit.gupta 676
    @RequestMapping(value = ProfitMandiConstants.URL_NEW_LEAD, method = RequestMethod.POST)
35288 amit 677
    public ResponseEntity<?> newLead(HttpServletRequest request, @RequestBody CreateRefferalRequest
678
            createRefferalRequest) throws ProfitMandiBusinessException {
31677 amit.gupta 679
        LOGGER.info("requested url : " + request.getRequestURL().toString());
680
        Lead lead = new Lead();
681
        lead.setAddress(createRefferalRequest.getCity());
682
        Long.parseLong(createRefferalRequest.getMobile());
683
        if (createRefferalRequest.getMobile().length() != 10) {
35079 vikas 684
            throw new ProfitMandiBusinessException("Mobile Number", createRefferalRequest.getMobile(), "Number should be of 10 digits");
31677 amit.gupta 685
        }
686
        lead.setLeadMobile(createRefferalRequest.getMobile());
687
        lead.setCity(createRefferalRequest.getCity());
688
        lead.setState(createRefferalRequest.getState());
689
        lead.setLastName(createRefferalRequest.getLastName());
690
        if (lead.getState().equals("Uttar Pradesh")) {
691
            lead.setAssignTo(53);
692
        } else if (lead.getState().equals("Haryana")) {
693
            lead.setAssignTo(53);
694
        } else if (lead.getState().equals("Delhi")) {
695
            lead.setAssignTo(53);
696
        } else {
697
            // Assign to sm
698
            lead.setAssignTo(53);
699
            // Assign to neha
700
            // lead.setAssignTo(1);
701
        }
702
        lead.setAuthId(lead.getAssignTo());
703
        lead.setCreatedBy("daily-sync");
704
        lead.setSource("SD-WEB");
705
        lead.setFirstName(createRefferalRequest.getFirstName());
706
        lead.setStatus(LeadStatus.followUp);
707
        lead.setColor("yellow");
708
        lead.setCreatedTimestamp(LocalDateTime.now());
709
        lead.setUpdatedTimestamp(LocalDateTime.now());
710
        leadRepository.persist(lead);
27117 tejbeer 711
 
31677 amit.gupta 712
        return responseSender.ok(true);
27117 tejbeer 713
 
31677 amit.gupta 714
    }
27117 tejbeer 715
 
31677 amit.gupta 716
    @RequestMapping(value = "/getPartnersList", method = RequestMethod.GET)
35079 vikas 717
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 718
    public ResponseEntity<?> getPartners(HttpServletRequest request, @RequestParam(name = "gmailId") String gmailId)
719
            throws ProfitMandiBusinessException {
25952 tejbeer 720
 
31677 amit.gupta 721
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
35630 aman 722
        if (authUser == null) {
723
            LOGGER.warn("No auth user found for gmailId: {}", gmailId);
724
            return responseSender.badRequest("No user found for the provided email");
725
        }
25952 tejbeer 726
 
31677 amit.gupta 727
        Map<String, Set<String>> storeGuyMap = csService.getAuthUserPartnerEmailMapping();
25952 tejbeer 728
 
31677 amit.gupta 729
        Set<String> emails = storeGuyMap.get(authUser.getEmailId());
35630 aman 730
        LOGGER.info("emails: {}", emails);
731
        if (emails == null || emails.isEmpty()) {
732
            LOGGER.info("No partner emails found for user: {}", gmailId);
733
            return responseSender.ok(new ArrayList<>());
734
        }
31677 amit.gupta 735
        List<User> users = userRepository.selectAllByEmailIds(new ArrayList<>(emails));
736
        List<Partner> partners = new ArrayList<>();
737
        for (User user : users) {
35630 aman 738
            try {
739
                UserAccount uc = userAccountRepository.selectSaholicByUserId(user.getId());
740
                if (uc == null) {
741
                    LOGGER.warn("No user account found for userId: {}", user.getId());
742
                    continue;
743
                }
744
                com.spice.profitmandi.dao.entity.user.User userInfo = userUserRepository.selectById(uc.getAccountKey());
745
                if (userInfo == null) {
746
                    LOGGER.warn("No user info found for accountKey: {}", uc.getAccountKey());
747
                    continue;
748
                }
749
                CustomRetailer customRetailer = retailerService.getFofoRetailer(userInfo.getId());
750
                if (customRetailer == null) {
751
                    LOGGER.warn("No custom retailer found for userInfoId: {}", userInfo.getId());
752
                    continue;
753
                }
25952 tejbeer 754
 
35630 aman 755
                Partner partner = new Partner();
756
                partner.setBusinessName(customRetailer.getBusinessName());
757
                partner.setPartnerId(customRetailer.getPartnerId());
758
                partner.setCartId(customRetailer.getCartId());
759
                partner.setEmail(customRetailer.getEmail());
760
                partner.setGstNumber(customRetailer.getGstNumber());
761
                partner.setDisplayName(customRetailer.getDisplayName());
762
                partner.setCity(customRetailer.getAddress() != null ? customRetailer.getAddress().getCity() : null);
763
                partner.setUserId(user.getId());
764
                partners.add(partner);
765
            } catch (Exception e) {
766
                LOGGER.error("Error processing partner for userId: {}", user.getId(), e);
767
            }
31677 amit.gupta 768
        }
35630 aman 769
        LOGGER.info("partners: {}", partners);
31677 amit.gupta 770
        return responseSender.ok(partners);
771
    }
25952 tejbeer 772
 
31677 amit.gupta 773
    @RequestMapping(value = "/franchise-first-visit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 774
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35288 amit 775
    public ResponseEntity<?> FranchiseFirstVisit(HttpServletRequest request, @RequestBody CreateFranchiseeRequest
776
            createFranchiseeRequest) throws Exception {
25952 tejbeer 777
 
31677 amit.gupta 778
        FranchiseeVisit franchiseeVisit = new FranchiseeVisit();
779
        franchiseeVisit.setFofoId(createFranchiseeRequest.getFofoId());
780
        CustomRetailer customRetailer = retailerService.getFofoRetailer(createFranchiseeRequest.getFofoId());
25952 tejbeer 781
 
31677 amit.gupta 782
        franchiseeVisit.setPartnerName(customRetailer.getBusinessName());
783
        franchiseeVisit.setAgenda(createFranchiseeRequest.getAgenda());
784
        franchiseeVisit.setCreatedTimestamp(LocalDateTime.now());
785
        franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
786
        franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
787
        franchiseeVisit.setScheduleTimestamp(createFranchiseeRequest.getFirstSchelduleTimestamp());
788
        // change
789
        AuthUser authUser = authRepository.selectByGmailId(createFranchiseeRequest.getCreatedBy());
25952 tejbeer 790
 
31677 amit.gupta 791
        String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
792
        franchiseeVisit.setCreatedBy(authUserName);
793
        franchiseeVisit.setAuthId(authUser.getId());
25952 tejbeer 794
 
31677 amit.gupta 795
        franchiseeVisitRepository.persist(franchiseeVisit);
25952 tejbeer 796
 
31677 amit.gupta 797
        FranchiseeActivity franchiseeActivity = new FranchiseeActivity();
798
        franchiseeActivity.setAction(FranchiseeActivityStatus.FOLLOWUP);
799
        franchiseeActivity.setFranchiseeVisitd(franchiseeVisit.getId());
800
        franchiseeActivity.setResolution(createFranchiseeRequest.getResolution());
801
        franchiseeActivity.setSchelduleTimestamp(createFranchiseeRequest.getFirstSchelduleTimestamp());
31249 tejbeer 802
 
31677 amit.gupta 803
        franchiseeActivity.setCreatedTimestamp(LocalDateTime.now());
804
        franchiseeActivityRepository.persist(franchiseeActivity);
31249 tejbeer 805
 
31677 amit.gupta 806
        franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());
31249 tejbeer 807
 
35079 vikas 808
        visitRequestRepository.createVisitRequest(franchiseeVisit.getId(), "franchiseeVisit", authUser.getId(), createFranchiseeRequest.getFirstSchelduleTimestamp());
31249 tejbeer 809
 
31677 amit.gupta 810
        return responseSender.ok(true);
811
    }
25952 tejbeer 812
 
31677 amit.gupta 813
    @RequestMapping(value = "/franchise-visit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 814
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 815
    public ResponseEntity<?> FranchiseVisit(HttpServletRequest request,
816
                                            @RequestBody CreateFranchiseeRequest createFranchiseeRequest) throws Exception {
25952 tejbeer 817
 
34394 tejus.loha 818
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, createFranchiseeRequest.getFofoId());
34276 ranu 819
 
36677 vikas 820
        List<FranchiseeVisit> franchiseeVisits = franchiseeVisitRepository.selectAllByFofoId(createFranchiseeRequest.getFofoId());
36723 vikas 821
        FranchiseeVisit franchiseeVisit = franchiseeVisits.isEmpty() ? new FranchiseeVisit() : franchiseeVisits.get(0);
31677 amit.gupta 822
        franchiseeVisit.setFofoId(createFranchiseeRequest.getFofoId());
823
        CustomRetailer customRetailer = retailerService.getFofoRetailer(createFranchiseeRequest.getFofoId());
34394 tejus.loha 824
        LOGGER.info("rbmL1 {}", rbmL1);
31677 amit.gupta 825
        franchiseeVisit.setPartnerName(customRetailer.getBusinessName());
826
        franchiseeVisit.setAgenda(createFranchiseeRequest.getAgenda());
827
        franchiseeVisit.setPartnerRemark(createFranchiseeRequest.getPartnerRemark());
828
        franchiseeVisit.setOutsideVisibity(createFranchiseeRequest.getOutsideVisibity());
829
        franchiseeVisit.setInstoreVisibility(createFranchiseeRequest.getInstoreVisibility());
830
        franchiseeVisit.setOutsideStock(createFranchiseeRequest.getOutsideStock());
831
        franchiseeVisit.setSystemKnowledge(createFranchiseeRequest.getSystemKnowledge());
832
        franchiseeVisit.setWorkingDevice(createFranchiseeRequest.getWorkingDevice());
833
        franchiseeVisit.setWorkingPrinter(createFranchiseeRequest.getWorkingPrinter());
834
        franchiseeVisit.setCarryBags(createFranchiseeRequest.getCarryBags());
835
        franchiseeVisit.setSmartdukaanTshirt(createFranchiseeRequest.getSmartdukaanTshirt());
836
        franchiseeVisit.setLatestDummies(createFranchiseeRequest.getLatestDummies());
837
        franchiseeVisit.setInvestment(createFranchiseeRequest.getInvestment());
838
        franchiseeVisit.setMtd(createFranchiseeRequest.getMtd());
839
        franchiseeVisit.setHygiene(createFranchiseeRequest.getHygiene());
840
        franchiseeVisit.setCreatedTimestamp(LocalDateTime.now());
841
        franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
842
        franchiseeVisit.setInformedAboutOnline(createFranchiseeRequest.getOnline());
843
        franchiseeVisit.setPendingBilling(createFranchiseeRequest.getPendingBilling());
34276 ranu 844
        franchiseeVisit.setRbmId(rbmL1);
845
        franchiseeVisit.setRbmRating(createFranchiseeRequest.getRbmRating());
31677 amit.gupta 846
        if (createFranchiseeRequest.getAction().equals(FranchiseeActivityStatus.FOLLOWUP)) {
847
            franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
848
        } else {
849
            franchiseeVisit.setStatus(FranchiseeVisitStatus.CLOSE);
850
        }
25952 tejbeer 851
 
31677 amit.gupta 852
        // AuthUser authUser =
853
        // authRepository.selectByGmailId(createFranchiseeRequest.getCreatedBy());
854
        // change
855
        AuthUser authUser = authRepository.selectByGmailId(createFranchiseeRequest.getCreatedBy());
25952 tejbeer 856
 
31677 amit.gupta 857
        String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
858
        franchiseeVisit.setCreatedBy(authUserName);
859
        franchiseeVisit.setAuthId(authUser.getId());
25952 tejbeer 860
 
31677 amit.gupta 861
        franchiseeVisitRepository.persist(franchiseeVisit);
25952 tejbeer 862
 
31677 amit.gupta 863
        FranchiseeActivity franchiseeActivity = new FranchiseeActivity();
864
        franchiseeActivity.setAction(createFranchiseeRequest.getAction());
865
        franchiseeActivity.setFranchiseeVisitd(franchiseeVisit.getId());
866
        franchiseeActivity.setResolution(createFranchiseeRequest.getResolution());
867
        if (createFranchiseeRequest.getAction().equals(FranchiseeActivityStatus.FOLLOWUP)) {
868
            franchiseeActivity.setSchelduleTimestamp(createFranchiseeRequest.getSchelduleTimestamp());
31249 tejbeer 869
 
31677 amit.gupta 870
            visitRequestRepository.createVisitRequest(franchiseeVisit.getId(), "franchiseeVisit",
871
                    franchiseeVisit.getAuthId(), createFranchiseeRequest.getSchelduleTimestamp());
872
        } else {
873
            franchiseeActivity.setSchelduleTimestamp(null);
874
        }
875
        franchiseeActivity.setCreatedTimestamp(LocalDateTime.now());
876
        franchiseeActivityRepository.persist(franchiseeActivity);
25952 tejbeer 877
 
31677 amit.gupta 878
        franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());
34276 ranu 879
 
34394 tejus.loha 880
        if (!createFranchiseeRequest.getOutsideVisibityReason().isEmpty()) {
34288 ranu 881
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_DESIGN, ProfitMandiConstants.TICKET_CATEGORY_DESIGNING_BRANDING_EXTERNAL_VISIBILITY, createFranchiseeRequest.getOutsideVisibityReason(), authUser.getId());
34276 ranu 882
        }
883
 
34394 tejus.loha 884
        if (!createFranchiseeRequest.getInstoreVisibilityReason().isEmpty()) {
34288 ranu 885
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_DESIGN, ProfitMandiConstants.TICKET_CATEGORY_DESIGNING_BRANDING_INTERNAL_VISIBILITY, createFranchiseeRequest.getInstoreVisibilityReason(), authUser.getId());
34276 ranu 886
        }
887
 
34394 tejus.loha 888
        if (!createFranchiseeRequest.getOutsideStockReason().isEmpty()) {
34288 ranu 889
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_SALES, ProfitMandiConstants.TICKET_CATEGORY_SALES_OUTSIDE_STOCK, createFranchiseeRequest.getOutsideStockReason(), authUser.getId());
34276 ranu 890
        }
891
 
34394 tejus.loha 892
        if (!createFranchiseeRequest.getSystemKnowledgeReason().isEmpty()) {
34288 ranu 893
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_TRAINING, ProfitMandiConstants.TICKET_CATEGORY_TRAINING_NEED, createFranchiseeRequest.getSystemKnowledgeReason(), authUser.getId());
34276 ranu 894
        }
895
 
34394 tejus.loha 896
        if (!createFranchiseeRequest.getLatestDummiesReason().isEmpty()) {
34288 ranu 897
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_MARKETING, ProfitMandiConstants.TICKET_CATEGORY_MARKETING_DUMMIES, createFranchiseeRequest.getLatestDummiesReason(), authUser.getId());
34276 ranu 898
        }
899
 
34394 tejus.loha 900
        if (!createFranchiseeRequest.getInvestmentReason().isEmpty()) {
34288 ranu 901
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_SALES, ProfitMandiConstants.TICKET_CATEGORY_SALES_LOW_INVESTMENT, createFranchiseeRequest.getInvestmentReason(), authUser.getId());
34276 ranu 902
        }
903
 
34394 tejus.loha 904
        if (!createFranchiseeRequest.getHygieneReason().isEmpty()) {
34288 ranu 905
            csService.createTicket(createFranchiseeRequest.getFofoId(), ProfitMandiConstants.TICKET_CATEGORY_SALES, ProfitMandiConstants.TICKET_CATEGORY_SALES_LOW_DATA_ACCURACY, createFranchiseeRequest.getHygieneReason(), authUser.getId());
906
        }
34276 ranu 907
 
31677 amit.gupta 908
        return responseSender.ok(true);
34276 ranu 909
 
31677 amit.gupta 910
    }
25952 tejbeer 911
 
31677 amit.gupta 912
    @RequestMapping(value = "/getFranchiseVisit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 913
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 914
    public ResponseEntity<?> getFranchiseVisit(HttpServletRequest request,
35288 amit 915
                                               @RequestParam(name = "gmailId") String gmailId, @RequestParam(name = "status") FranchiseeVisitStatus
916
                                                       status,
31677 amit.gupta 917
                                               @RequestParam(name = "offset", defaultValue = "0") int offset,
918
                                               @RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
919
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
35079 vikas 920
        List<VisitRequest> visitRequests = visitRequestRepository.selectByAuthIdAndDate(authUser.getId(), LocalDate.now());
31677 amit.gupta 921
        Map<Integer, List<VisitRequest>> visitRequestMap = null;
922
        if (!visitRequests.isEmpty()) {
923
            visitRequestMap = visitRequests.stream().collect(Collectors.groupingBy(x -> x.getVisitId()));
924
        }
35079 vikas 925
        List<FranchiseeVisit> franchiseeVisits = franchiseeVisitRepository.selectByAuthIdAndStatus(authUser.getId(), status, offset, limit);
25952 tejbeer 926
 
35415 amit 927
        // Batch fetch all FranchiseeActivity in one query instead of N+1
928
        List<Integer> activityIds = franchiseeVisits.stream()
929
                .filter(fv -> fv.getFranchiseActivityId() != 0)
930
                .map(FranchiseeVisit::getFranchiseActivityId)
931
                .collect(Collectors.toList());
932
        Map<Integer, FranchiseeActivity> activityMap = new HashMap<>();
933
        if (!activityIds.isEmpty()) {
934
            activityMap = franchiseeActivityRepository.selectByIds(activityIds).stream()
935
                    .collect(Collectors.toMap(FranchiseeActivity::getId, x -> x));
936
        }
937
 
31677 amit.gupta 938
        for (FranchiseeVisit fv : franchiseeVisits) {
939
            if (visitRequestMap != null) {
31249 tejbeer 940
 
31677 amit.gupta 941
                List<VisitRequest> visitRequest = visitRequestMap.get(fv.getId());
942
                if (visitRequest != null) {
943
                    if (visitRequest.size() > 1) {
31249 tejbeer 944
 
35079 vikas 945
                        Comparator<VisitRequest> visitComparato = Comparator.comparing(VisitRequest::getCreatedTimestamp);
31249 tejbeer 946
 
31677 amit.gupta 947
                        VisitRequest youngestVisit = visitRequest.stream().max(visitComparato).get();
948
                        fv.setVisitStatus(youngestVisit.getStatus());
31249 tejbeer 949
 
31677 amit.gupta 950
                    } else {
951
                        fv.setVisitStatus(visitRequest.get(0).getStatus());
31249 tejbeer 952
 
31677 amit.gupta 953
                    }
954
                }
955
            }
956
            if (fv.getFranchiseActivityId() != 0) {
35415 amit 957
                FranchiseeActivity fA = activityMap.get(fv.getFranchiseActivityId());
31677 amit.gupta 958
                fv.setFranchiseeActivity(fA);
959
            }
31249 tejbeer 960
 
31677 amit.gupta 961
        }
25952 tejbeer 962
 
31677 amit.gupta 963
        return responseSender.ok(franchiseeVisits);
25952 tejbeer 964
 
31677 amit.gupta 965
    }
25952 tejbeer 966
 
31677 amit.gupta 967
    @RequestMapping(value = "/getFranchiseActivity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 968
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35288 amit 969
    public ResponseEntity<?> getFranchiseActivity(HttpServletRequest request, @RequestParam(name = "id") int id) throws
970
            ProfitMandiBusinessException {
25952 tejbeer 971
 
31677 amit.gupta 972
        List<FranchiseeActivity> franchiseeActivities = franchiseeActivityRepository.selectByFranchiseeVisitId(id);
973
        return responseSender.ok(franchiseeActivities);
25952 tejbeer 974
 
31677 amit.gupta 975
    }
25952 tejbeer 976
 
31677 amit.gupta 977
    @RequestMapping(value = "/getFranchiseeInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 978
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35288 amit 979
    public ResponseEntity<?> getFranchiseeInfo(HttpServletRequest request, @RequestParam(name = "id") int id) throws
980
            ProfitMandiBusinessException {
36678 vikas 981
        FranchiseeVisit franchiseeVisit;
982
        if (String.valueOf(id).length() < 9) {
983
            franchiseeVisit = franchiseeVisitRepository.selectById(id);
984
        } else {
985
            List<FranchiseeVisit> franchiseeVisits = franchiseeVisitRepository.selectAllByFofoId(id);
36722 vikas 986
            if (franchiseeVisits.isEmpty()) {
987
                return responseSender.ok(null);
988
            }
36678 vikas 989
            franchiseeVisit = franchiseeVisits.get(0);
990
        }
25952 tejbeer 991
 
31677 amit.gupta 992
        List<DBObject> mobileBrands = mongoClient.getAllBrandsToDisplay(3);
30487 tejbeer 993
 
31677 amit.gupta 994
        List<String> brands = mobileBrands.stream().filter(x -> (boolean) x.get("active"))
995
                .map(x -> (String) x.get("name")).collect(Collectors.toList());
30487 tejbeer 996
 
31677 amit.gupta 997
        franchiseeVisit.setBrands(brands);
30487 tejbeer 998
 
31677 amit.gupta 999
        return responseSender.ok(franchiseeVisit);
25952 tejbeer 1000
 
31677 amit.gupta 1001
    }
25952 tejbeer 1002
 
31677 amit.gupta 1003
    @RequestMapping(value = "/franchise-visit-update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 1004
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35288 amit 1005
    public ResponseEntity<?> franchiseVisitUpdate(HttpServletRequest request, @RequestParam(name = "id") int id,
1006
                                                  @RequestParam(name = "action") FranchiseeActivityStatus action,
1007
                                                  @RequestParam(name = "resolution") String resolution,
1008
                                                  @RequestParam(name = "schelduleTimestamp") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime schelduleTimestamp)
31677 amit.gupta 1009
            throws ProfitMandiBusinessException {
1010
        FranchiseeVisit franchiseeVisit = franchiseeVisitRepository.selectById(id);
25952 tejbeer 1011
 
31677 amit.gupta 1012
        FranchiseeActivity franchiseeActivity = new FranchiseeActivity();
25952 tejbeer 1013
 
31677 amit.gupta 1014
        if (action == FranchiseeActivityStatus.FOLLOWUP) {
30487 tejbeer 1015
 
31677 amit.gupta 1016
            if (schelduleTimestamp != null) {
31249 tejbeer 1017
 
31677 amit.gupta 1018
                visitRequestRepository.createVisitRequest(franchiseeVisit.getId(), "franchiseeVisit",
1019
                        franchiseeVisit.getAuthId(), schelduleTimestamp);
1020
            }
31249 tejbeer 1021
 
31677 amit.gupta 1022
            franchiseeActivity.setSchelduleTimestamp(schelduleTimestamp);
1023
            franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
1024
            franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
1025
            franchiseeVisitRepository.persist(franchiseeVisit);
25952 tejbeer 1026
 
31677 amit.gupta 1027
        } else {
30487 tejbeer 1028
 
31677 amit.gupta 1029
            franchiseeActivity.setSchelduleTimestamp(null);
1030
            franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());
1031
            franchiseeVisit.setStatus(FranchiseeVisitStatus.CLOSE);
1032
            franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
1033
            franchiseeVisitRepository.persist(franchiseeVisit);
25952 tejbeer 1034
 
31677 amit.gupta 1035
        }
25952 tejbeer 1036
 
31677 amit.gupta 1037
        franchiseeActivity.setResolution(resolution);
1038
        franchiseeActivity.setFranchiseeVisitd(franchiseeVisit.getId());
1039
        franchiseeActivity.setAction(action);
30487 tejbeer 1040
 
31677 amit.gupta 1041
        franchiseeActivity.setCreatedTimestamp(LocalDateTime.now());
1042
        franchiseeActivityRepository.persist(franchiseeActivity);
30487 tejbeer 1043
 
31677 amit.gupta 1044
        franchiseeVisit.setFranchiseActivityId(franchiseeActivity.getId());
1045
        franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
1046
        franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
1047
        franchiseeVisitRepository.persist(franchiseeVisit);
30487 tejbeer 1048
 
31677 amit.gupta 1049
        return responseSender.ok(true);
25952 tejbeer 1050
 
31677 amit.gupta 1051
    }
28935 tejbeer 1052
 
31677 amit.gupta 1053
    @RequestMapping(value = "/onBoardingTimelineStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 1054
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 1055
    public ResponseEntity<?> onBoardingTimelineStatus(HttpServletRequest request, Model model)
1056
            throws ProfitMandiBusinessException {
35075 aman 1057
        //This userId is of dtr.users
31677 amit.gupta 1058
        int userId = (int) request.getAttribute("userId");
35075 aman 1059
 
1060
        //UserCart contains userId/retailerId in context of user.user as
1061
        //uc contains retailerId and cartId of the retailerId that belongs to user schema
31677 amit.gupta 1062
        UserCart uc = userAccountRepository.getUserCart(userId);
1063
        FofoStore fs = fofoStoreRepository.selectByRetailerId(uc.getUserId());
1064
        PartnerOnBoardingPanel partnerOnBoardingPanel = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
1065
        Map<StoreTimeline, OnBoardingTimelineModel> timelineStatus = null;
1066
        LOGGER.info("partnerOnBoardingPanel" + partnerOnBoardingPanel);
1067
        if (partnerOnBoardingPanel != null) {
28935 tejbeer 1068
 
31677 amit.gupta 1069
            timelineStatus = storeTimelineTatService.getTimeline(partnerOnBoardingPanel.getId());
1070
        }
1071
        List<OnBoardingTimelineModel> onBoardingModel = new ArrayList<>();
1072
        if (timelineStatus != null) {
1073
            onBoardingModel = timelineStatus.values().stream().skip(2).map(x -> x).collect(Collectors.toList());
1074
        }
1075
        return responseSender.ok(onBoardingModel);
28935 tejbeer 1076
 
31677 amit.gupta 1077
    }
35288 amit 1078
 
35075 aman 1079
    @RequestMapping(value = "/purchaseExist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
1080
    @ApiImplicitParams({
1081
            @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35415 amit 1082
    public ResponseEntity<?> purchaseExist(HttpServletRequest request) throws ProfitMandiBusinessException {
35075 aman 1083
        int userId = (int) request.getAttribute("userId");
1084
        UserCart uc = userAccountRepository.getUserCart(userId);
35415 amit 1085
        return responseSender.ok(purchaseRepository.hasPurchaseForFofoId(uc.getUserId()));
35075 aman 1086
    }
35288 amit 1087
 
31677 amit.gupta 1088
    @RequestMapping(value = "/onBoardingTimelineVisibility", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 1089
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 1090
    public ResponseEntity<?> onBoardingTimelineVisibility(HttpServletRequest request, Model model)
1091
            throws ProfitMandiBusinessException {
1092
        int userId = (int) request.getAttribute("userId");
1093
        UserCart uc = userAccountRepository.getUserCart(userId);
28982 tejbeer 1094
 
31677 amit.gupta 1095
        FofoStore fs = fofoStoreRepository.selectByRetailerId(uc.getUserId());
1096
        PartnerOnBoardingPanel partnerOnBoardingPanel = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
1097
        boolean status = true;
1098
        if (partnerOnBoardingPanel != null) {
28982 tejbeer 1099
 
31677 amit.gupta 1100
            status = storeTimelineTatService.getTimelineCompleted(partnerOnBoardingPanel.getId());
1101
        }
28982 tejbeer 1102
 
31677 amit.gupta 1103
        LOGGER.info("status" + status);
28983 tejbeer 1104
 
31677 amit.gupta 1105
        return responseSender.ok(status);
28982 tejbeer 1106
 
31677 amit.gupta 1107
    }
30077 tejbeer 1108
 
31677 amit.gupta 1109
    @RequestMapping(value = "/getPartnerTarget", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 1110
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
35288 amit 1111
    public ResponseEntity<?> getPartnerTarget(HttpServletRequest request, @RequestParam String
1112
            gmailId, @RequestParam String dayValue, @RequestParam ActivationType activationType) throws
1113
            ProfitMandiBusinessException {
30077 tejbeer 1114
 
35632 ranu 1115
        long startTime = System.currentTimeMillis();
1116
        long lapTime = startTime;
1117
 
31677 amit.gupta 1118
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
35632 ranu 1119
 
35649 ranu 1120
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
1121
        Set<Integer> fofoIds = storeGuyMap.get(authUser.getEmailId());
31677 amit.gupta 1122
        List<String> brands = Arrays.asList("Vivo", "Samsung", "Oppo", "Itel", "Almost New", "Others");
30078 tejbeer 1123
 
35649 ranu 1124
 
31677 amit.gupta 1125
        float totalPartnerTargetSecondary = 0;
1126
        float totalPartnerTargetCollection = 0;
1127
        float totalPartnerAchievementSecondary = 0;
1128
        float totalPartnerAchievementCollection = 0;
1129
        TargetModel tm = new TargetModel();
1130
        Map<Integer, PartnerDailyInvestment> partnerDailyInvestmentMap = new HashMap<>();
35632 ranu 1131
        Map<Integer, Long> partnerTicketCount = new HashMap<>(); // Will be populated after we have fofoIdList
30416 tejbeer 1132
 
31677 amit.gupta 1133
        List<PartnerTargetAchievementModel> ptams = new ArrayList<>();
30077 tejbeer 1134
 
31677 amit.gupta 1135
        if (fofoIds != null && fofoIds.size() > 0) {
32737 amit.gupta 1136
            LOGGER.info("fofoIds {}", fofoIds);
31727 amit.gupta 1137
            List<Integer> fofoIdList = fofoStoreRepository.selectByRetailerIds(new ArrayList<>(fofoIds)).stream()
31677 amit.gupta 1138
                    .filter(x -> (!x.isInternal() && (activationType == null || x.getActivationType().equals(activationType))))
1139
                    .map(x -> x.getId()).collect(Collectors.toList());
35632 ranu 1140
            LOGGER.info("PERF: selectByRetailerIds took {} ms", System.currentTimeMillis() - lapTime);
1141
            lapTime = System.currentTimeMillis();
1142
            LOGGER.info("fofoIdList size: {}", fofoIdList.size());
1143
 
1144
            // Optimized: fetch ticket counts only for specific fofoIds instead of all retailers
1145
            if (!fofoIdList.isEmpty()) {
1146
                partnerTicketCount = ticketRepository.selectOpenTicketsCountByFofoIds(fofoIdList);
1147
            }
1148
            LOGGER.info("PERF: selectOpenTicketsCountByFofoIds took {} ms", System.currentTimeMillis() - lapTime);
1149
            lapTime = System.currentTimeMillis();
31677 amit.gupta 1150
            LocalDateTime startDate = LocalDate.now().atStartOfDay();
30077 tejbeer 1151
 
31677 amit.gupta 1152
            if (dayValue.equals("previous")) {
1153
                startDate = LocalDate.now().minusDays(1).atStartOfDay();
30138 tejbeer 1154
 
31677 amit.gupta 1155
            }
30087 tejbeer 1156
 
31686 amit.gupta 1157
            if (fofoIdList.size() > 0) {
30453 tejbeer 1158
 
35632 ranu 1159
                // Fetch fofoIds with overdue loans (pending amount > 0 and created > 15 days ago)
1160
                // Optimized: returns only fofoIds, not full Loan objects
1161
                LocalDateTime fifteenDaysAgo = LocalDate.now().minusDays(15).atStartOfDay();
1162
                Set<Integer> fofoIdsWithOverdueLoans = loanRepository.selectFofoIdsWithOverdueLoans(fofoIdList, fifteenDaysAgo);
1163
                LOGGER.info("PERF: selectFofoIdsWithOverdueLoans took {} ms", System.currentTimeMillis() - lapTime);
1164
                lapTime = System.currentTimeMillis();
1165
 
1166
                // Fetch fofoIds with MTD billing above threshold (20000)
1167
                // Optimized: direct query on orders table instead of complex RBM query
1168
                LocalDateTime mtdStartDate = LocalDate.now().withDayOfMonth(1).atStartOfDay();
1169
                LocalDateTime mtdEndDate = LocalDate.now().plusDays(1).atStartOfDay();
35650 ranu 1170
                long billingThreshold = 1000L; // Same as RbmWeeklyBillingModel.BILLING_THRESHOLD
35632 ranu 1171
                Set<Integer> allMtdBilledFofoIds = orderRepository.selectFofoIdsWithMtdBillingAboveThreshold(
1172
                        fofoIdList, mtdStartDate, mtdEndDate, billingThreshold);
1173
                LOGGER.info("PERF: selectFofoIdsWithMtdBillingAboveThreshold took {} ms", System.currentTimeMillis() - lapTime);
1174
                lapTime = System.currentTimeMillis();
1175
 
35079 vikas 1176
                List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository.selectAll(fofoIdList, startDate.toLocalDate().minusDays(1));
35632 ranu 1177
                LOGGER.info("PERF: partnerDailyInvestmentRepository.selectAll took {} ms", System.currentTimeMillis() - lapTime);
1178
                lapTime = System.currentTimeMillis();
30087 tejbeer 1179
 
31686 amit.gupta 1180
                if (!partnerDailyInvestments.isEmpty()) {
35079 vikas 1181
                    partnerDailyInvestmentMap = partnerDailyInvestments.stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
31686 amit.gupta 1182
                }
30077 tejbeer 1183
 
35632 ranu 1184
                // Optimized: fetch only specific fofoIds instead of all retailers
1185
                Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(fofoIdList);
1186
                LOGGER.info("PERF: getFofoRetailers took {} ms", System.currentTimeMillis() - lapTime);
1187
                lapTime = System.currentTimeMillis();
30426 tejbeer 1188
 
31686 amit.gupta 1189
                List<Integer> remarkIds = partnerCollectionRemarkRepository.selectMaxRemarkId(fofoIdList);
35632 ranu 1190
                LOGGER.info("PERF: selectMaxRemarkId took {} ms", System.currentTimeMillis() - lapTime);
1191
                lapTime = System.currentTimeMillis();
30453 tejbeer 1192
 
31686 amit.gupta 1193
                long todayCollectionCount = 0;
1194
                if (!remarkIds.isEmpty()) {
1195
                    todayCollectionCount = partnerCollectionRemarkRepository
1196
                            .selectByAuthIdAndIds(authUser.getId(), remarkIds).stream()
1197
                            .filter(x -> x.getCreateTimestamp().toLocalDate().equals(LocalDate.now()))
1198
                            .collect(Collectors.counting());
30453 tejbeer 1199
 
31686 amit.gupta 1200
                }
35632 ranu 1201
                LOGGER.info("PERF: selectByAuthIdAndIds took {} ms", System.currentTimeMillis() - lapTime);
1202
                lapTime = System.currentTimeMillis();
30330 tejbeer 1203
 
35079 vikas 1204
                Map<Integer, PartnerCollectionPlanModel> collectionMap = partnerCollectionService.getCollectionMap(fofoIdList, startDate);
35632 ranu 1205
                LOGGER.info("PERF: getCollectionMap took {} ms", System.currentTimeMillis() - lapTime);
1206
                lapTime = System.currentTimeMillis();
30077 tejbeer 1207
 
32978 amit.gupta 1208
                Map<Integer, List<PartnerSecondaryPlanModel>> partnerSecondaryPlans = orderRepository
31686 amit.gupta 1209
                        .selectPartnerSecondaryGroupByBrand(fofoIdList, startDate.toLocalDate()).stream()
1210
                        .collect(Collectors.groupingBy(x -> x.getFofoId()));
35632 ranu 1211
                LOGGER.info("PERF: selectPartnerSecondaryGroupByBrand took {} ms", System.currentTimeMillis() - lapTime);
1212
                lapTime = System.currentTimeMillis();
30077 tejbeer 1213
 
35415 amit 1214
                // Pre-fetch all AuthUsers that might be needed (instead of N+1 queries)
1215
                Set<Integer> allAuthIds = new HashSet<>();
1216
                collectionMap.values().stream()
1217
                        .map(PartnerCollectionPlanModel::getAuthId)
1218
                        .filter(id -> id != null && id != authUser.getId())
1219
                        .forEach(allAuthIds::add);
1220
                partnerSecondaryPlans.values().stream()
1221
                        .flatMap(List::stream)
1222
                        .map(PartnerSecondaryPlanModel::getAuthId)
1223
                        .filter(id -> id != null && id != authUser.getId())
1224
                        .forEach(allAuthIds::add);
1225
                Map<Integer, AuthUser> authUserMap = new HashMap<>();
1226
                if (!allAuthIds.isEmpty()) {
1227
                    authUserMap = authRepository.selectByIds(new ArrayList<>(allAuthIds)).stream()
1228
                            .collect(Collectors.toMap(AuthUser::getId, x -> x));
1229
                }
1230
 
35968 ranu 1231
                // Fetch BM/ASM/RBM for all partners
1232
                Map<Integer, Map<String, Integer>> bmAsmRbmMap = csService.getBmAsmRbmAuthUserIdsByFofoIds(new HashSet<>(fofoIdList));
1233
                LOGGER.info("PERF: getBmAsmRbmAuthUserIdsByFofoIds took {} ms", System.currentTimeMillis() - lapTime);
1234
                lapTime = System.currentTimeMillis();
1235
 
1236
                // Collect all BM/ASM/RBM auth IDs to batch fetch AuthUser names
1237
                Set<Integer> bmAsmRbmAuthIds = bmAsmRbmMap.values().stream()
1238
                        .flatMap(m -> m.values().stream())
1239
                        .filter(id -> id != null && id > 0)
1240
                        .collect(Collectors.toSet());
1241
 
1242
                Map<Integer, AuthUser> bmAsmRbmAuthUserMap = new HashMap<>();
1243
                if (!bmAsmRbmAuthIds.isEmpty()) {
1244
                    bmAsmRbmAuthUserMap = authRepository.selectByIds(new ArrayList<>(bmAsmRbmAuthIds)).stream()
1245
                            .collect(Collectors.toMap(AuthUser::getId, x -> x, (a, b) -> a));
1246
                }
1247
                LOGGER.info("PERF: fetch BM/ASM/RBM AuthUsers took {} ms", System.currentTimeMillis() - lapTime);
1248
                lapTime = System.currentTimeMillis();
1249
 
32978 amit.gupta 1250
                LOGGER.info("partnerSecondayPlans {}", partnerSecondaryPlans);
35968 ranu 1251
                final Map<Integer, AuthUser> finalBmAsmRbmAuthUserMap = bmAsmRbmAuthUserMap;
31686 amit.gupta 1252
                for (Entry<Integer, CustomRetailer> customRetailerEntry : customRetailers.entrySet()) {
1253
                    int fofoId = customRetailerEntry.getKey();
1254
                    CustomRetailer customRetailer = customRetailerEntry.getValue();
1255
                    float totalSecondaryPlan = 0;
1256
                    float totalSecondaryAchivement = 0;
30077 tejbeer 1257
 
31686 amit.gupta 1258
                    PartnerTargetAchievementModel ptam = new PartnerTargetAchievementModel();
1259
                    ptam.setFofoId(fofoId);
1260
                    ptam.setBusinessName(customRetailer.getBusinessName());
1261
                    ptam.setMobile(customRetailer.getMobileNumber());
36211 ranu 1262
                    ptam.setActivationType(customRetailer.getActivationType());
30087 tejbeer 1263
 
35968 ranu 1264
                    // Set ASM/RBM/BM names
1265
                    Map<String, Integer> roleMap = bmAsmRbmMap.get(fofoId);
1266
                    if (roleMap != null) {
1267
                        Integer asmId = roleMap.get("ASM");
1268
                        if (asmId != null && asmId > 0 && finalBmAsmRbmAuthUserMap.get(asmId) != null) {
1269
                            ptam.setAsmName(finalBmAsmRbmAuthUserMap.get(asmId).getFullName());
1270
                        }
1271
                        Integer rbmId = roleMap.get("RBM");
1272
                        if (rbmId != null && rbmId > 0 && finalBmAsmRbmAuthUserMap.get(rbmId) != null) {
1273
                            ptam.setRbmName(finalBmAsmRbmAuthUserMap.get(rbmId).getFullName());
1274
                        }
1275
                        Integer bmId = roleMap.get("BM");
1276
                        if (bmId != null && bmId > 0 && finalBmAsmRbmAuthUserMap.get(bmId) != null) {
1277
                            ptam.setBmName(finalBmAsmRbmAuthUserMap.get(bmId).getFullName());
1278
                        }
1279
                    }
1280
 
31686 amit.gupta 1281
                    if (partnerDailyInvestmentMap.get(fofoId) != null) {
1282
                        ptam.setWalletAmount(partnerDailyInvestmentMap.get(fofoId).getWalletAmount());
1283
                        ptam.setShortInvestment(partnerDailyInvestmentMap.get(fofoId).getShortInvestment());
1284
                    }
30808 tejbeer 1285
 
32978 amit.gupta 1286
                    if (collectionMap.get(fofoId) != null) {
1287
                        PartnerCollectionPlanModel collectionPlan = collectionMap.get(fofoId);
30330 tejbeer 1288
 
31686 amit.gupta 1289
                        ptam.setRemark(collectionPlan.getRemark());
1290
                        ptam.setMessage(collectionPlan.getMessage());
1291
                        ptam.setRemarkTimestamp(collectionPlan.getRemarkTimestamp());
35632 ranu 1292
                        ptam.setRecordingUrl(collectionPlan.getRecordingUrl());
35835 ranu 1293
                        ptam.setScheduledCallTime(collectionPlan.getScheduledCallTime());
31686 amit.gupta 1294
                        ptam.setRank(collectionPlan.getRank());
36267 ranu 1295
 
1296
                        // If remark is SALES_ESCALATION, fetch open escalation ticket activities
1297
                        if (CollectionRemark.SALES_ESCALATION.equals(collectionPlan.getRemark())
1298
                                || CollectionRemark.PARTIAL_RESOLVE.equals(collectionPlan.getRemark())
1299
                                || CollectionRemark.NOT_RESOLVED.equals(collectionPlan.getRemark())) {
1300
                            List<Ticket> openTickets = ticketRepository.selectAllOpenTicketByRetailer(fofoId);
1301
                            for (Ticket ticket : openTickets) {
1302
                                if (ticket.getSubCategoryId() == ProfitMandiConstants.SALES_ESCALATION) {
1303
                                    List<Activity> activities = activityRepository.selectAll(ticket.getId());
1304
                                    ptam.setTicketActivities(activities);
1305
                                    break;
1306
                                }
1307
                            }
1308
                        }
1309
 
31686 amit.gupta 1310
                        Integer authId = collectionPlan.getAuthId();
30330 tejbeer 1311
 
31686 amit.gupta 1312
                        Map<Integer, String> rankColorMap = ProfitMandiConstants.Rank_Color_Map;
30416 tejbeer 1313
 
31686 amit.gupta 1314
                        ptam.setCollectionColor(rankColorMap.get(collectionPlan.getRank()));
30077 tejbeer 1315
 
31686 amit.gupta 1316
                        LOGGER.info("authId" + authId);
30137 tejbeer 1317
 
31686 amit.gupta 1318
                        if (collectionPlan.getTargetPlan() != null && collectionPlan.getCommittedDate() != null) {
1319
                            float targetCollection = 0;
30330 tejbeer 1320
 
31686 amit.gupta 1321
                            if (collectionPlan.getRank() == 2 && collectionPlan.getAchievementPlan() != null) {
1322
                                targetCollection = collectionPlan.getTargetPlan() - collectionPlan.getAchievementPlan();
1323
                            } else {
1324
                                targetCollection = collectionPlan.getTargetPlan();
31677 amit.gupta 1325
                            }
30451 tejbeer 1326
 
31677 amit.gupta 1327
                            if (authId != null && authId == authUser.getId()) {
31686 amit.gupta 1328
                                if (!collectionPlan.getCommittedDate().isAfter(startDate)) {
1329
                                    totalPartnerTargetCollection += targetCollection;
1330
                                }
31677 amit.gupta 1331
                            }
31686 amit.gupta 1332
                            ptam.setCollectionTarget(targetCollection);
1333
                            if (collectionPlan.getRank() == 2 && collectionPlan.getAchievementPlan() != null
1334
                                    && collectionPlan.getWalletTimestamp() != null
1335
                                    && collectionPlan.getWalletTimestamp().toLocalDate().equals(startDate.toLocalDate())) {
1336
                                if (authId != null && authId == authUser.getId()) {
1337
                                    totalPartnerAchievementCollection += collectionPlan.getAchievementPlan();
1338
                                }
1339
                                ptam.setCollectionAchievement(collectionPlan.getAchievementPlan());
1340
                            } else if (collectionPlan.getCommittedDate().toLocalDate().isEqual(startDate.toLocalDate())
1341
                                    && collectionPlan.getAchievementPlan() != null) {
1342
                                if (authId != null && authId == authUser.getId()) {
1343
                                    totalPartnerAchievementCollection += collectionPlan.getAchievementPlan();
1344
                                }
1345
                                ptam.setCollectionAchievement(collectionPlan.getAchievementPlan());
1346
 
31677 amit.gupta 1347
                            }
31686 amit.gupta 1348
                        }
30822 tejbeer 1349
 
31686 amit.gupta 1350
                        if (authId != null && authId != authUser.getId()) {
35415 amit 1351
                            ptam.setAuthUser(authUserMap.get(authId));
31686 amit.gupta 1352
 
31677 amit.gupta 1353
                        }
30087 tejbeer 1354
 
31686 amit.gupta 1355
                        if (collectionPlan.getCommittedDate() != null) {
30087 tejbeer 1356
 
31686 amit.gupta 1357
                            ptam.setCollectionCommitmentDate(collectionPlan.getCommittedDate().toLocalDate());
1358
                        }
30087 tejbeer 1359
 
31677 amit.gupta 1360
                    }
30821 tejbeer 1361
 
31686 amit.gupta 1362
                    // Secondary
30077 tejbeer 1363
 
31686 amit.gupta 1364
                    PartnerSecondaryPlanModel otherPartnerSecondaryPlanModel = null;
1365
                    Map<String, PartnerSecondaryPlanModel> secondaryModelMap = new HashMap<>();
32978 amit.gupta 1366
                    if (partnerSecondaryPlans.get(fofoId) != null) {
31686 amit.gupta 1367
                        long otherBrandSecondary = 0;
32978 amit.gupta 1368
                        for (PartnerSecondaryPlanModel pspm : partnerSecondaryPlans.get(fofoId)) {
31686 amit.gupta 1369
                            Integer authId = pspm.getAuthId();
1370
                            if (!brands.contains(pspm.getBrand())) {
1371
                                if (pspm.getAchievementPlan() != null) {
1372
                                    otherBrandSecondary += pspm.getAchievementPlan();
1373
                                }
1374
                            } else {
1375
                                otherPartnerSecondaryPlanModel = pspm;
1376
                            }
1377
                            if (pspm.getTargetPlan() != null) {
1378
                                totalSecondaryPlan += pspm.getTargetPlan();
1379
                            }
30330 tejbeer 1380
 
31677 amit.gupta 1381
                            if (pspm.getAchievementPlan() != null) {
31686 amit.gupta 1382
                                totalSecondaryAchivement += pspm.getAchievementPlan();
31677 amit.gupta 1383
                            }
30087 tejbeer 1384
 
31686 amit.gupta 1385
                            if (pspm.getCommittedDate() != null) {
30137 tejbeer 1386
 
31686 amit.gupta 1387
                                ptam.setSecondaryCommitmentDate(pspm.getCommittedDate().toLocalDate());
1388
                            }
1389
                            if (authId != null && authId == authUser.getId()) {
30137 tejbeer 1390
 
31686 amit.gupta 1391
                                if (pspm.getTargetPlan() != null && pspm.getCommittedDate() != null) {
30087 tejbeer 1392
 
31686 amit.gupta 1393
                                    if (pspm.getCommittedDate().isEqual(startDate)) {
1394
                                        totalPartnerTargetSecondary += pspm.getTargetPlan();
1395
                                    }
1396
                                }
30087 tejbeer 1397
 
31686 amit.gupta 1398
                                if (pspm.getAchievementPlan() != null) {
1399
                                    totalPartnerAchievementSecondary += pspm.getAchievementPlan();
31677 amit.gupta 1400
                                }
31686 amit.gupta 1401
 
31677 amit.gupta 1402
                            }
31686 amit.gupta 1403
                        }
1404
                        if (otherPartnerSecondaryPlanModel != null) {
1405
                            otherPartnerSecondaryPlanModel.setAchievementPlan(otherBrandSecondary);
1406
                        }
32978 amit.gupta 1407
                        secondaryModelMap = partnerSecondaryPlans.get(fofoId).stream()
31686 amit.gupta 1408
                                .filter(x -> brands.contains(x.getBrand()))
1409
                                .collect(Collectors.toMap(x -> x.getBrand(), x -> x));
30087 tejbeer 1410
 
31686 amit.gupta 1411
                        if (secondaryModelMap.containsKey("Others")) {
1412
                            PartnerSecondaryPlanModel psp = secondaryModelMap.get("Others");
1413
                            psp.setAchievementPlan(otherBrandSecondary);
1414
                        } else {
35079 vikas 1415
                            secondaryModelMap.put("Others", new PartnerSecondaryPlanModel(fofoId, "Others", (long) 0, otherBrandSecondary, authUser.getId(), null));
31686 amit.gupta 1416
                        }
1417
                        for (String brand : brands) {
1418
                            if (!secondaryModelMap.containsKey(brand)) {
35079 vikas 1419
                                secondaryModelMap.put(brand, new PartnerSecondaryPlanModel(fofoId, brand, (long) 0, (long) 0, authUser.getId(), null));
31677 amit.gupta 1420
                            }
31686 amit.gupta 1421
                        }
30087 tejbeer 1422
 
31686 amit.gupta 1423
                        for (Entry<String, PartnerSecondaryPlanModel> secondaryModelEntry : secondaryModelMap.entrySet()) {
1424
                            Integer authId = secondaryModelEntry.getValue().getAuthId();
1425
                            if (authId != null && authId != authUser.getId()) {
35415 amit 1426
                                secondaryModelEntry.getValue().setAuthUser(authUserMap.get(authId));
31686 amit.gupta 1427
                                ptam.setSecondaryColor("red");
1428
                            }
1429
 
31677 amit.gupta 1430
                        }
30077 tejbeer 1431
 
31686 amit.gupta 1432
                        ptam.setPartnerSecondaryModel(secondaryModelMap);
1433
                        ptam.setTotalSecondaryPlan(totalSecondaryPlan);
1434
                        ptam.setTotalSecondaryAchievement(totalSecondaryAchivement);
1435
                        // Secondary
1436
 
31677 amit.gupta 1437
                    } else {
31686 amit.gupta 1438
                        for (String brand : brands) {
1439
                            PartnerSecondaryPlanModel pspm = new PartnerSecondaryPlanModel();
1440
                            pspm.setAchievementPlan((long) 0);
1441
                            pspm.setTargetPlan((long) 0);
1442
                            pspm.setBrand(brand);
1443
                            pspm.setFofoId(fofoId);
1444
                            secondaryModelMap.put(brand, pspm);
31677 amit.gupta 1445
                        }
31686 amit.gupta 1446
                        ptam.setPartnerSecondaryModel(secondaryModelMap);
31677 amit.gupta 1447
                    }
30077 tejbeer 1448
 
31686 amit.gupta 1449
                    if (!partnerTicketCount.isEmpty()) {
1450
                        if (partnerTicketCount.get(fofoId) != null) {
1451
                            ptam.setTicketCount(partnerTicketCount.get(fofoId));
1452
                        } else {
1453
                            ptam.setTicketCount(0);
1454
 
31677 amit.gupta 1455
                        }
1456
                    }
30087 tejbeer 1457
 
35632 ranu 1458
                    // Category Assignment Logic (Priority: PLAN_TODAY > CARRY_FORWARD > ZERO_BILLED > UNTOUCHED > NORMAL)
1459
                    // Check if partner has credit due (loans > 15 days with pending amount) - just a flag, doesn't affect sorting
1460
                    boolean hasCreditDue = fofoIdsWithOverdueLoans.contains(fofoId);
1461
                    ptam.setHasOverdue(hasCreditDue);
1462
 
1463
                    // Default rank to 5 (Normal) if no collection plan exists
1464
                    int rank = ptam.getRank() > 0 ? ptam.getRank() : 5;
1465
                    boolean hasZeroBilling = !allMtdBilledFofoIds.contains(fofoId);
1466
 
1467
                    // Assign category based on rank (overdue is just a flag, doesn't affect category)
1468
                    if (rank == 1) {
1469
                        ptam.setCategory("PLAN_TODAY");
1470
                        ptam.setCategoryPriority(1);
1471
                    } else if (rank == 2) {
1472
                        ptam.setCategory("CARRY_FORWARD");
1473
                        ptam.setCategoryPriority(2);
1474
                    } else if (hasZeroBilling && rank < 5) {
1475
                        // Zero billed only if NOT in normal category (rank 5+)
1476
                        // If rank is 1,2,3,4 and has zero billing, include in ZERO_BILLED
1477
                        ptam.setCategory("ZERO_BILLED");
1478
                        ptam.setCategoryPriority(3);
1479
                    } else if (rank == 3) {
1480
                        ptam.setCategory("UNTOUCHED");
1481
                        ptam.setCategoryPriority(4);
1482
                    } else {
1483
                        // rank 4 (future plan) and rank 5+ (normal) go to NORMAL
1484
                        // Also, zero billing with rank 5+ goes to NORMAL (not ZERO_BILLED)
1485
                        ptam.setCategory("NORMAL");
1486
                        ptam.setCategoryPriority(5);
1487
                    }
1488
 
31686 amit.gupta 1489
                    ptams.add(ptam);
30077 tejbeer 1490
 
31677 amit.gupta 1491
                }
32923 ranu 1492
                List<Position> positions = positionRepository.selectPositionByAuthId(authUser.getId()).stream().collect(Collectors.toList());
30077 tejbeer 1493
 
32923 ranu 1494
                LOGGER.info("positions {}", positions);
1495
 
35079 vikas 1496
                boolean isRBMAndL1 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L1.equals(position.getEscalationType()));
36211 ranu 1497
                boolean isRBMAndL2 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L2.equals(position.getEscalationType()));
1498
                boolean isRBMAndL3 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L3.equals(position.getEscalationType()));
35079 vikas 1499
                boolean isRBMAndNotL1 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && !EscalationType.L1.equals(position.getEscalationType()));
32923 ranu 1500
 
36211 ranu 1501
                LOGGER.info("isRBMAndL1{}, isRBMAndL2{}, isRBMAndL3{}", isRBMAndL1, isRBMAndL2, isRBMAndL3);
32923 ranu 1502
 
35079 vikas 1503
                boolean isSales = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_SALES == position.getCategoryId());
32923 ranu 1504
 
31686 amit.gupta 1505
                tm.setTotalPartnerTargetCollection(totalPartnerTargetCollection);
1506
                tm.setTotalPartnerTargetSecondary(totalPartnerTargetSecondary);
1507
                tm.setTotalPartnerSecondary(totalPartnerAchievementSecondary);
1508
                tm.setTotalPartnerCollection(totalPartnerAchievementCollection);
1509
                tm.setTodayCollectionCount((int) todayCollectionCount);
32923 ranu 1510
                List<PartnerTargetAchievementModel> filteredPtams;
30416 tejbeer 1511
 
32923 ranu 1512
                if (isRBMAndL1) {
34428 ranu 1513
                    filteredPtams = ptams;
1514
//                    as of now all party is showing to rbm
1515
                    /*filteredPtams = ptams.stream()
32978 amit.gupta 1516
                            .filter(ptam -> !(CollectionRemark.RBM_L2_ESCALATION.equals(ptam.getRemark()) || CollectionRemark.SALES_ESCALATION.equals(ptam.getRemark())))
34428 ranu 1517
                            .collect(Collectors.toList());*/
32923 ranu 1518
                } else if (isRBMAndNotL1) {
34429 ranu 1519
                    filteredPtams = ptams;
1520
                    /*filteredPtams = ptams.stream()
32923 ranu 1521
                            .filter(ptam -> !(CollectionRemark.SALES_ESCALATION.equals(ptam.getRemark())))
34429 ranu 1522
                            .collect(Collectors.toList());*/
33041 ranu 1523
                } else if (isSales) {
1524
                    filteredPtams = ptams.stream()
36211 ranu 1525
                            .filter(ptam -> !(CollectionRemark.RBM_L2_ESCALATION.equals(ptam.getRemark()) || CollectionRemark.RBM_L3_ESCALATION.equals(ptam.getRemark())))
33041 ranu 1526
                            .collect(Collectors.toList());
32923 ranu 1527
                } else {
1528
                    filteredPtams = ptams;
1529
                }
34428 ranu 1530
                tm.setRBMAndL1(isRBMAndL1);
36211 ranu 1531
                tm.setRBMAndL2(isRBMAndL2);
1532
                tm.setRBMAndL3(isRBMAndL3);
34428 ranu 1533
                tm.setRBMAndNotL1(isRBMAndNotL1);
1534
                tm.setSales(isSales);
35632 ranu 1535
 
1536
                // Calculate category counts
1537
                Map<String, Long> categoryCounts = filteredPtams.stream()
1538
                        .collect(Collectors.groupingBy(
1539
                                p -> p.getCategory() != null ? p.getCategory() : "NORMAL",
1540
                                Collectors.counting()));
1541
                // Count parties with overdue flag (independent of category)
1542
                tm.setOverdueCount((int) filteredPtams.stream().filter(PartnerTargetAchievementModel::isHasOverdue).count());
1543
                tm.setPlanTodayCount(categoryCounts.getOrDefault("PLAN_TODAY", 0L).intValue());
1544
                tm.setCarryForwardCount(categoryCounts.getOrDefault("CARRY_FORWARD", 0L).intValue());
1545
                tm.setZeroBilledCount(categoryCounts.getOrDefault("ZERO_BILLED", 0L).intValue());
1546
                tm.setUntouchedCount(categoryCounts.getOrDefault("UNTOUCHED", 0L).intValue());
1547
                tm.setNormalCount(categoryCounts.getOrDefault("NORMAL", 0L).intValue());
1548
 
1549
                // Sort by: 1) Partners with today's remark go to bottom of entire list
35835 ranu 1550
                //          EXCEPT: NO_ANSWER/SCHEDULE_CALL with scheduledCallTime reached → comes back up
35853 ranu 1551
                //          EXCEPT: NO_ANSWER without scheduledCallTime older than 4 hours → comes back up
35632 ranu 1552
                //          2) Category Priority (PLAN_TODAY=1, CARRY_FORWARD=2, ZERO_BILLED=3, UNTOUCHED=4, NORMAL=5)
1553
                //          3) Then by rank
35835 ranu 1554
                LocalDateTime now = LocalDateTime.now();
35853 ranu 1555
                LocalDateTime fourHoursAgo = now.minusHours(4);
34796 ranu 1556
                tm.setTargetAchievement(filteredPtams.stream()
1557
                        .sorted(Comparator
35632 ranu 1558
                                .comparing((PartnerTargetAchievementModel p) -> {
1559
                                    if (p.getRemarkTimestamp() == null) {
1560
                                        return false; // No remark → comes first
1561
                                    }
1562
                                    LocalDate remarkDate = p.getRemarkTimestamp().toLocalDate();
1563
                                    if (!remarkDate.equals(LocalDate.now())) {
1564
                                        return false; // Not today's remark → comes first
1565
                                    }
35835 ranu 1566
                                    // Check if SCHEDULE_CALL or NO_ANSWER with scheduledCallTime
1567
                                    if (CollectionRemark.SCHEDULE_CALL.equals(p.getRemark()) || CollectionRemark.NO_ANSWER.equals(p.getRemark())) {
1568
                                        if (p.getScheduledCallTime() != null) {
1569
                                            // Has scheduled time - come up only when scheduled time is reached
1570
                                            if (!p.getScheduledCallTime().isAfter(now)) {
1571
                                                return false; // Scheduled time reached → comes back up
1572
                                            }
1573
                                            return true; // Scheduled time not reached → stays at bottom
1574
                                        }
35853 ranu 1575
                                        // NO_ANSWER without scheduledCallTime - use 4 hour logic
35835 ranu 1576
                                        if (CollectionRemark.NO_ANSWER.equals(p.getRemark())
35853 ranu 1577
                                                && p.getRemarkTimestamp().isBefore(fourHoursAgo)) {
1578
                                            return false; // NO_ANSWER older than 4 hours → comes back up
35835 ranu 1579
                                        }
35632 ranu 1580
                                    }
1581
                                    return true; // Other today's remarks → goes to bottom
1582
                                }) // false → comes first, true → goes to bottom
1583
                                .thenComparing(PartnerTargetAchievementModel::getCategoryPriority)
34796 ranu 1584
                                .thenComparing(PartnerTargetAchievementModel::getRank)
1585
                        )
31686 amit.gupta 1586
                        .collect(Collectors.toList()));
30416 tejbeer 1587
 
34796 ranu 1588
 
31677 amit.gupta 1589
            }
30077 tejbeer 1590
 
31677 amit.gupta 1591
        }
35632 ranu 1592
        LOGGER.info("PERF: Total getPartnerTarget took {} ms", System.currentTimeMillis() - startTime);
31677 amit.gupta 1593
        return responseSender.ok(tm);
30077 tejbeer 1594
 
31677 amit.gupta 1595
    }
32737 amit.gupta 1596
 
35920 ranu 1597
    @RequestMapping(value = "/getInactivePartnerTarget", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
1598
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
1599
    public ResponseEntity<?> getInactivePartnerTarget(HttpServletRequest request, @RequestParam String
1600
            gmailId, @RequestParam String dayValue, @RequestParam ActivationType activationType) throws
1601
            ProfitMandiBusinessException {
1602
 
1603
        long startTime = System.currentTimeMillis();
1604
        long lapTime = startTime;
1605
 
1606
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
1607
 
1608
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserInactivePartnerIdMapping();
1609
        Set<Integer> fofoIds = storeGuyMap.get(authUser.getEmailId());
1610
        List<String> brands = Arrays.asList("Vivo", "Samsung", "Oppo", "Itel", "Almost New", "Others");
1611
 
1612
        float totalPartnerTargetSecondary = 0;
1613
        float totalPartnerTargetCollection = 0;
1614
        float totalPartnerAchievementSecondary = 0;
1615
        float totalPartnerAchievementCollection = 0;
1616
        TargetModel tm = new TargetModel();
1617
        Map<Integer, PartnerDailyInvestment> partnerDailyInvestmentMap = new HashMap<>();
1618
        Map<Integer, Long> partnerTicketCount = new HashMap<>();
1619
 
1620
        List<PartnerTargetAchievementModel> ptams = new ArrayList<>();
1621
 
1622
        if (fofoIds != null && fofoIds.size() > 0) {
1623
            LOGGER.info("fofoIds {}", fofoIds);
1624
            // Get inactive stores for the given retailer IDs
1625
            List<Integer> fofoIdList = fofoStoreRepository.selectByRetailerIds(new ArrayList<>(fofoIds)).stream()
1626
                    .filter(x -> (!x.isInternal()))
1627
                    .map(x -> x.getId()).collect(Collectors.toList());
1628
            LOGGER.info("PERF: selectInActiveStore took {} ms", System.currentTimeMillis() - lapTime);
1629
            lapTime = System.currentTimeMillis();
1630
            LOGGER.info("inactive fofoIdList size: {}", fofoIdList.size());
1631
 
1632
            if (!fofoIdList.isEmpty()) {
1633
                partnerTicketCount = ticketRepository.selectOpenTicketsCountByFofoIds(fofoIdList);
1634
            }
1635
            LOGGER.info("PERF: selectOpenTicketsCountByFofoIds took {} ms", System.currentTimeMillis() - lapTime);
1636
            lapTime = System.currentTimeMillis();
1637
            LocalDateTime startDate = LocalDate.now().atStartOfDay();
1638
 
1639
            if (dayValue.equals("previous")) {
1640
                startDate = LocalDate.now().minusDays(1).atStartOfDay();
1641
            }
1642
 
1643
            if (fofoIdList.size() > 0) {
1644
 
1645
                LocalDateTime fifteenDaysAgo = LocalDate.now().minusDays(15).atStartOfDay();
1646
                Set<Integer> fofoIdsWithOverdueLoans = loanRepository.selectFofoIdsWithOverdueLoans(fofoIdList, fifteenDaysAgo);
1647
                LOGGER.info("PERF: selectFofoIdsWithOverdueLoans took {} ms", System.currentTimeMillis() - lapTime);
1648
                lapTime = System.currentTimeMillis();
1649
 
1650
                LocalDateTime mtdStartDate = LocalDate.now().withDayOfMonth(1).atStartOfDay();
1651
                LocalDateTime mtdEndDate = LocalDate.now().plusDays(1).atStartOfDay();
1652
                long billingThreshold = 1000L;
1653
                Set<Integer> allMtdBilledFofoIds = orderRepository.selectFofoIdsWithMtdBillingAboveThreshold(
1654
                        fofoIdList, mtdStartDate, mtdEndDate, billingThreshold);
1655
                LOGGER.info("PERF: selectFofoIdsWithMtdBillingAboveThreshold took {} ms", System.currentTimeMillis() - lapTime);
1656
                lapTime = System.currentTimeMillis();
1657
 
1658
                List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository.selectAll(fofoIdList, startDate.toLocalDate().minusDays(1));
1659
                LOGGER.info("PERF: partnerDailyInvestmentRepository.selectAll took {} ms", System.currentTimeMillis() - lapTime);
1660
                lapTime = System.currentTimeMillis();
1661
 
1662
                if (!partnerDailyInvestments.isEmpty()) {
1663
                    partnerDailyInvestmentMap = partnerDailyInvestments.stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
1664
                }
1665
 
1666
                Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(fofoIdList);
1667
                LOGGER.info("PERF: getFofoRetailers took {} ms", System.currentTimeMillis() - lapTime);
1668
                lapTime = System.currentTimeMillis();
1669
 
1670
                List<Integer> remarkIds = partnerCollectionRemarkRepository.selectMaxRemarkId(fofoIdList);
1671
                LOGGER.info("PERF: selectMaxRemarkId took {} ms", System.currentTimeMillis() - lapTime);
1672
                lapTime = System.currentTimeMillis();
1673
 
1674
                long todayCollectionCount = 0;
1675
                if (!remarkIds.isEmpty()) {
1676
                    todayCollectionCount = partnerCollectionRemarkRepository
1677
                            .selectByAuthIdAndIds(authUser.getId(), remarkIds).stream()
1678
                            .filter(x -> x.getCreateTimestamp().toLocalDate().equals(LocalDate.now()))
1679
                            .collect(Collectors.counting());
1680
                }
1681
                LOGGER.info("PERF: selectByAuthIdAndIds took {} ms", System.currentTimeMillis() - lapTime);
1682
                lapTime = System.currentTimeMillis();
1683
 
1684
                Map<Integer, PartnerCollectionPlanModel> collectionMap = partnerCollectionService.getCollectionMap(fofoIdList, startDate);
1685
                LOGGER.info("PERF: getCollectionMap took {} ms", System.currentTimeMillis() - lapTime);
1686
                lapTime = System.currentTimeMillis();
1687
 
1688
                Map<Integer, List<PartnerSecondaryPlanModel>> partnerSecondaryPlans = orderRepository
1689
                        .selectPartnerSecondaryGroupByBrand(fofoIdList, startDate.toLocalDate()).stream()
1690
                        .collect(Collectors.groupingBy(x -> x.getFofoId()));
1691
                LOGGER.info("PERF: selectPartnerSecondaryGroupByBrand took {} ms", System.currentTimeMillis() - lapTime);
1692
                lapTime = System.currentTimeMillis();
1693
 
1694
                Set<Integer> allAuthIds = new HashSet<>();
1695
                collectionMap.values().stream()
1696
                        .map(PartnerCollectionPlanModel::getAuthId)
1697
                        .filter(id -> id != null && id != authUser.getId())
1698
                        .forEach(allAuthIds::add);
1699
                partnerSecondaryPlans.values().stream()
1700
                        .flatMap(List::stream)
1701
                        .map(PartnerSecondaryPlanModel::getAuthId)
1702
                        .filter(id -> id != null && id != authUser.getId())
1703
                        .forEach(allAuthIds::add);
1704
                Map<Integer, AuthUser> authUserMap = new HashMap<>();
1705
                if (!allAuthIds.isEmpty()) {
1706
                    authUserMap = authRepository.selectByIds(new ArrayList<>(allAuthIds)).stream()
1707
                            .collect(Collectors.toMap(AuthUser::getId, x -> x));
1708
                }
1709
 
1710
                LOGGER.info("partnerSecondayPlans {}", partnerSecondaryPlans);
1711
                for (Entry<Integer, CustomRetailer> customRetailerEntry : customRetailers.entrySet()) {
1712
                    int fofoId = customRetailerEntry.getKey();
1713
                    CustomRetailer customRetailer = customRetailerEntry.getValue();
1714
                    float totalSecondaryPlan = 0;
1715
                    float totalSecondaryAchivement = 0;
1716
 
1717
                    PartnerTargetAchievementModel ptam = new PartnerTargetAchievementModel();
1718
                    ptam.setFofoId(fofoId);
1719
                    ptam.setBusinessName(customRetailer.getBusinessName());
1720
                    ptam.setMobile(customRetailer.getMobileNumber());
36211 ranu 1721
                    ptam.setActivationType(customRetailer.getActivationType());
35920 ranu 1722
 
1723
                    if (partnerDailyInvestmentMap.get(fofoId) != null) {
1724
                        ptam.setWalletAmount(partnerDailyInvestmentMap.get(fofoId).getWalletAmount());
1725
                        ptam.setShortInvestment(partnerDailyInvestmentMap.get(fofoId).getShortInvestment());
1726
                    }
1727
 
1728
                    if (collectionMap.get(fofoId) != null) {
1729
                        PartnerCollectionPlanModel collectionPlan = collectionMap.get(fofoId);
1730
 
1731
                        ptam.setRemark(collectionPlan.getRemark());
1732
                        ptam.setMessage(collectionPlan.getMessage());
1733
                        ptam.setRemarkTimestamp(collectionPlan.getRemarkTimestamp());
1734
                        ptam.setRecordingUrl(collectionPlan.getRecordingUrl());
1735
                        ptam.setScheduledCallTime(collectionPlan.getScheduledCallTime());
1736
                        ptam.setRank(collectionPlan.getRank());
1737
                        Integer authId = collectionPlan.getAuthId();
1738
 
1739
                        Map<Integer, String> rankColorMap = ProfitMandiConstants.Rank_Color_Map;
1740
                        ptam.setCollectionColor(rankColorMap.get(collectionPlan.getRank()));
1741
 
1742
                        LOGGER.info("authId" + authId);
1743
 
1744
                        if (collectionPlan.getTargetPlan() != null && collectionPlan.getCommittedDate() != null) {
1745
                            float targetCollection = 0;
1746
 
1747
                            if (collectionPlan.getRank() == 2 && collectionPlan.getAchievementPlan() != null) {
1748
                                targetCollection = collectionPlan.getTargetPlan() - collectionPlan.getAchievementPlan();
1749
                            } else {
1750
                                targetCollection = collectionPlan.getTargetPlan();
1751
                            }
1752
 
1753
                            if (authId != null && authId == authUser.getId()) {
1754
                                if (!collectionPlan.getCommittedDate().isAfter(startDate)) {
1755
                                    totalPartnerTargetCollection += targetCollection;
1756
                                }
1757
                            }
1758
                            ptam.setCollectionTarget(targetCollection);
1759
                            if (collectionPlan.getRank() == 2 && collectionPlan.getAchievementPlan() != null
1760
                                    && collectionPlan.getWalletTimestamp() != null
1761
                                    && collectionPlan.getWalletTimestamp().toLocalDate().equals(startDate.toLocalDate())) {
1762
                                if (authId != null && authId == authUser.getId()) {
1763
                                    totalPartnerAchievementCollection += collectionPlan.getAchievementPlan();
1764
                                }
1765
                                ptam.setCollectionAchievement(collectionPlan.getAchievementPlan());
1766
                            } else if (collectionPlan.getCommittedDate().toLocalDate().isEqual(startDate.toLocalDate())
1767
                                    && collectionPlan.getAchievementPlan() != null) {
1768
                                if (authId != null && authId == authUser.getId()) {
1769
                                    totalPartnerAchievementCollection += collectionPlan.getAchievementPlan();
1770
                                }
1771
                                ptam.setCollectionAchievement(collectionPlan.getAchievementPlan());
1772
                            }
1773
                        }
1774
 
1775
                        if (authId != null && authId != authUser.getId()) {
1776
                            ptam.setAuthUser(authUserMap.get(authId));
1777
                        }
1778
 
1779
                        if (collectionPlan.getCommittedDate() != null) {
1780
                            ptam.setCollectionCommitmentDate(collectionPlan.getCommittedDate().toLocalDate());
1781
                        }
1782
                    }
1783
 
1784
                    PartnerSecondaryPlanModel otherPartnerSecondaryPlanModel = null;
1785
                    Map<String, PartnerSecondaryPlanModel> secondaryModelMap = new HashMap<>();
1786
                    if (partnerSecondaryPlans.get(fofoId) != null) {
1787
                        long otherBrandSecondary = 0;
1788
                        for (PartnerSecondaryPlanModel pspm : partnerSecondaryPlans.get(fofoId)) {
1789
                            Integer authId = pspm.getAuthId();
1790
                            if (!brands.contains(pspm.getBrand())) {
1791
                                if (pspm.getAchievementPlan() != null) {
1792
                                    otherBrandSecondary += pspm.getAchievementPlan();
1793
                                }
1794
                            } else {
1795
                                otherPartnerSecondaryPlanModel = pspm;
1796
                            }
1797
                            if (pspm.getTargetPlan() != null) {
1798
                                totalSecondaryPlan += pspm.getTargetPlan();
1799
                            }
1800
 
1801
                            if (pspm.getAchievementPlan() != null) {
1802
                                totalSecondaryAchivement += pspm.getAchievementPlan();
1803
                            }
1804
 
1805
                            if (pspm.getCommittedDate() != null) {
1806
                                ptam.setSecondaryCommitmentDate(pspm.getCommittedDate().toLocalDate());
1807
                            }
1808
                            if (authId != null && authId == authUser.getId()) {
1809
                                if (pspm.getTargetPlan() != null && pspm.getCommittedDate() != null) {
1810
                                    if (pspm.getCommittedDate().isEqual(startDate)) {
1811
                                        totalPartnerTargetSecondary += pspm.getTargetPlan();
1812
                                    }
1813
                                }
1814
 
1815
                                if (pspm.getAchievementPlan() != null) {
1816
                                    totalPartnerAchievementSecondary += pspm.getAchievementPlan();
1817
                                }
1818
                            }
1819
                        }
1820
                        if (otherPartnerSecondaryPlanModel != null) {
1821
                            otherPartnerSecondaryPlanModel.setAchievementPlan(otherBrandSecondary);
1822
                        }
1823
                        secondaryModelMap = partnerSecondaryPlans.get(fofoId).stream()
1824
                                .filter(x -> brands.contains(x.getBrand()))
1825
                                .collect(Collectors.toMap(x -> x.getBrand(), x -> x));
1826
 
1827
                        if (secondaryModelMap.containsKey("Others")) {
1828
                            PartnerSecondaryPlanModel psp = secondaryModelMap.get("Others");
1829
                            psp.setAchievementPlan(otherBrandSecondary);
1830
                        } else {
1831
                            secondaryModelMap.put("Others", new PartnerSecondaryPlanModel(fofoId, "Others", (long) 0, otherBrandSecondary, authUser.getId(), null));
1832
                        }
1833
                        for (String brand : brands) {
1834
                            if (!secondaryModelMap.containsKey(brand)) {
1835
                                secondaryModelMap.put(brand, new PartnerSecondaryPlanModel(fofoId, brand, (long) 0, (long) 0, authUser.getId(), null));
1836
                            }
1837
                        }
1838
 
1839
                        for (Entry<String, PartnerSecondaryPlanModel> secondaryModelEntry : secondaryModelMap.entrySet()) {
1840
                            Integer authId = secondaryModelEntry.getValue().getAuthId();
1841
                            if (authId != null && authId != authUser.getId()) {
1842
                                secondaryModelEntry.getValue().setAuthUser(authUserMap.get(authId));
1843
                                ptam.setSecondaryColor("red");
1844
                            }
1845
                        }
1846
 
1847
                        ptam.setPartnerSecondaryModel(secondaryModelMap);
1848
                        ptam.setTotalSecondaryPlan(totalSecondaryPlan);
1849
                        ptam.setTotalSecondaryAchievement(totalSecondaryAchivement);
1850
 
1851
                    } else {
1852
                        for (String brand : brands) {
1853
                            PartnerSecondaryPlanModel pspm = new PartnerSecondaryPlanModel();
1854
                            pspm.setAchievementPlan((long) 0);
1855
                            pspm.setTargetPlan((long) 0);
1856
                            pspm.setBrand(brand);
1857
                            pspm.setFofoId(fofoId);
1858
                            secondaryModelMap.put(brand, pspm);
1859
                        }
1860
                        ptam.setPartnerSecondaryModel(secondaryModelMap);
1861
                    }
1862
 
1863
                    if (!partnerTicketCount.isEmpty()) {
1864
                        if (partnerTicketCount.get(fofoId) != null) {
1865
                            ptam.setTicketCount(partnerTicketCount.get(fofoId));
1866
                        } else {
1867
                            ptam.setTicketCount(0);
1868
                        }
1869
                    }
1870
 
1871
                    boolean hasCreditDue = fofoIdsWithOverdueLoans.contains(fofoId);
1872
                    ptam.setHasOverdue(hasCreditDue);
1873
 
1874
                    int rank = ptam.getRank() > 0 ? ptam.getRank() : 5;
1875
                    boolean hasZeroBilling = !allMtdBilledFofoIds.contains(fofoId);
1876
 
1877
                    if (rank == 1) {
1878
                        ptam.setCategory("PLAN_TODAY");
1879
                        ptam.setCategoryPriority(1);
1880
                    } else if (rank == 2) {
1881
                        ptam.setCategory("CARRY_FORWARD");
1882
                        ptam.setCategoryPriority(2);
1883
                    } else if (hasZeroBilling && rank < 5) {
1884
                        ptam.setCategory("ZERO_BILLED");
1885
                        ptam.setCategoryPriority(3);
1886
                    } else if (rank == 3) {
1887
                        ptam.setCategory("UNTOUCHED");
1888
                        ptam.setCategoryPriority(4);
1889
                    } else {
1890
                        ptam.setCategory("NORMAL");
1891
                        ptam.setCategoryPriority(5);
1892
                    }
1893
 
1894
                    ptams.add(ptam);
1895
                }
1896
 
1897
                List<Position> positions = positionRepository.selectPositionByAuthId(authUser.getId()).stream().collect(Collectors.toList());
1898
                LOGGER.info("positions {}", positions);
1899
 
1900
                boolean isRBMAndL1 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L1.equals(position.getEscalationType()));
36211 ranu 1901
                boolean isRBMAndL2 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L2.equals(position.getEscalationType()));
1902
                boolean isRBMAndL3 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && EscalationType.L3.equals(position.getEscalationType()));
35920 ranu 1903
                boolean isRBMAndNotL1 = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_RBM == position.getCategoryId() && !EscalationType.L1.equals(position.getEscalationType()));
36211 ranu 1904
                LOGGER.info("isRBMAndL1{}, isRBMAndL2{}, isRBMAndL3{}", isRBMAndL1, isRBMAndL2, isRBMAndL3);
35920 ranu 1905
 
1906
                boolean isSales = positions.stream().anyMatch(position -> ProfitMandiConstants.TICKET_CATEGORY_SALES == position.getCategoryId());
1907
 
1908
                tm.setTotalPartnerTargetCollection(totalPartnerTargetCollection);
1909
                tm.setTotalPartnerTargetSecondary(totalPartnerTargetSecondary);
1910
                tm.setTotalPartnerSecondary(totalPartnerAchievementSecondary);
1911
                tm.setTotalPartnerCollection(totalPartnerAchievementCollection);
1912
                tm.setTodayCollectionCount((int) todayCollectionCount);
1913
                List<PartnerTargetAchievementModel> filteredPtams;
1914
 
1915
                if (isRBMAndL1) {
1916
                    filteredPtams = ptams;
1917
                } else if (isRBMAndNotL1) {
1918
                    filteredPtams = ptams;
1919
                } else if (isSales) {
1920
                    filteredPtams = ptams.stream()
36211 ranu 1921
                            .filter(ptam -> !(CollectionRemark.RBM_L2_ESCALATION.equals(ptam.getRemark()) || CollectionRemark.RBM_L3_ESCALATION.equals(ptam.getRemark())))
35920 ranu 1922
                            .collect(Collectors.toList());
1923
                } else {
1924
                    filteredPtams = ptams;
1925
                }
1926
                tm.setRBMAndL1(isRBMAndL1);
36211 ranu 1927
                tm.setRBMAndL2(isRBMAndL2);
1928
                tm.setRBMAndL3(isRBMAndL3);
35920 ranu 1929
                tm.setRBMAndNotL1(isRBMAndNotL1);
1930
                tm.setSales(isSales);
1931
 
1932
                Map<String, Long> categoryCounts = filteredPtams.stream()
1933
                        .collect(Collectors.groupingBy(
1934
                                p -> p.getCategory() != null ? p.getCategory() : "NORMAL",
1935
                                Collectors.counting()));
1936
                tm.setOverdueCount((int) filteredPtams.stream().filter(PartnerTargetAchievementModel::isHasOverdue).count());
1937
                tm.setPlanTodayCount(categoryCounts.getOrDefault("PLAN_TODAY", 0L).intValue());
1938
                tm.setCarryForwardCount(categoryCounts.getOrDefault("CARRY_FORWARD", 0L).intValue());
1939
                tm.setZeroBilledCount(categoryCounts.getOrDefault("ZERO_BILLED", 0L).intValue());
1940
                tm.setUntouchedCount(categoryCounts.getOrDefault("UNTOUCHED", 0L).intValue());
1941
                tm.setNormalCount(categoryCounts.getOrDefault("NORMAL", 0L).intValue());
1942
 
1943
                LocalDateTime now = LocalDateTime.now();
1944
                LocalDateTime fourHoursAgo = now.minusHours(4);
1945
                tm.setTargetAchievement(filteredPtams.stream()
1946
                        .sorted(Comparator
1947
                                .comparing((PartnerTargetAchievementModel p) -> {
1948
                                    if (p.getRemarkTimestamp() == null) {
1949
                                        return false;
1950
                                    }
1951
                                    LocalDate remarkDate = p.getRemarkTimestamp().toLocalDate();
1952
                                    if (!remarkDate.equals(LocalDate.now())) {
1953
                                        return false;
1954
                                    }
1955
                                    if (CollectionRemark.SCHEDULE_CALL.equals(p.getRemark()) || CollectionRemark.NO_ANSWER.equals(p.getRemark())) {
1956
                                        if (p.getScheduledCallTime() != null) {
1957
                                            if (!p.getScheduledCallTime().isAfter(now)) {
1958
                                                return false;
1959
                                            }
1960
                                            return true;
1961
                                        }
1962
                                        if (CollectionRemark.NO_ANSWER.equals(p.getRemark())
1963
                                                && p.getRemarkTimestamp().isBefore(fourHoursAgo)) {
1964
                                            return false;
1965
                                        }
1966
                                    }
1967
                                    return true;
1968
                                })
1969
                                .thenComparing(PartnerTargetAchievementModel::getCategoryPriority)
1970
                                .thenComparing(PartnerTargetAchievementModel::getRank)
1971
                        )
1972
                        .collect(Collectors.toList()));
1973
            }
1974
        }
1975
        LOGGER.info("PERF: Total getInactivePartnerTarget took {} ms", System.currentTimeMillis() - startTime);
1976
        return responseSender.ok(tm);
1977
 
1978
    }
1979
 
32737 amit.gupta 1980
    //TODO:Amit
31677 amit.gupta 1981
    @RequestMapping(value = "/target", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35079 vikas 1982
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
31677 amit.gupta 1983
    public ResponseEntity<?> createPartnerTarget(HttpServletRequest request,
1984
                                                 @RequestBody PartnerTargetAchievementModel ptam) throws ProfitMandiBusinessException {
30077 tejbeer 1985
 
31677 amit.gupta 1986
        LOGGER.info("ptam" + ptam);
30089 tejbeer 1987
 
31677 amit.gupta 1988
        AuthUser authUser = authRepository.selectByGmailId(ptam.getCreatedBy());
30077 tejbeer 1989
 
31677 amit.gupta 1990
        if (ptam.getCollectionCommitmentDate().isAfter(LocalDate.now())
1991
                || ptam.getCollectionCommitmentDate().isEqual(LocalDate.now())) {
1992
            PartnerCollectionPlan partnerCollectionPlan = partnerCollectionPlanRepository
1993
                    .selectByLocalDate(LocalDate.now(), ptam.getFofoId(), true);
1994
            LOGGER.info("pcp" + partnerCollectionPlan);
30077 tejbeer 1995
 
31677 amit.gupta 1996
            if (partnerCollectionPlan == null) {
1997
                if (ptam.getCollectionTarget() > 0) {
30087 tejbeer 1998
 
31677 amit.gupta 1999
                    partnerCollectionPlan = new PartnerCollectionPlan();
2000
                    partnerCollectionPlan.setCreateTimestamp(LocalDateTime.now());
2001
                    partnerCollectionPlan.setAuthId(authUser.getId());
2002
                    partnerCollectionPlan.setFofoId(ptam.getFofoId());
2003
                    partnerCollectionPlan.setActive(true);
2004
                    partnerCollectionPlan.setCollectionPlan(ptam.getCollectionTarget());
2005
                    partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
2006
                    partnerCollectionPlan.setCommitedTimestamp(ptam.getCollectionCommitmentDate().atStartOfDay());
2007
                    partnerCollectionPlanRepository.persist(partnerCollectionPlan);
30416 tejbeer 2008
 
31677 amit.gupta 2009
                }
30087 tejbeer 2010
 
31677 amit.gupta 2011
            } else {
30087 tejbeer 2012
 
31677 amit.gupta 2013
                if (partnerCollectionPlan.getCollectionPlan() != ptam.getCollectionTarget()) {
2014
                    float totalCollectionPlan = partnerCollectionPlan.getCollectionPlan() + 10000;
2015
                    if (authUser.getId() == partnerCollectionPlan.getAuthId()) {
30087 tejbeer 2016
 
31677 amit.gupta 2017
                        if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
2018
                                || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
2019
                                || ptam.getCollectionTarget() >= totalCollectionPlan) {
2020
                            partnerCollectionPlan.setCollectionPlan(ptam.getCollectionTarget());
2021
                            partnerCollectionPlan.setActive(true);
2022
                            partnerCollectionPlan
2023
                                    .setCommitedTimestamp(ptam.getCollectionCommitmentDate().atStartOfDay());
2024
                            partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
2025
                        } else {
2026
                            throw new ProfitMandiBusinessException("collection target", "",
2027
                                    "collection target should be more than " + totalCollectionPlan);
2028
                        }
30087 tejbeer 2029
 
31677 amit.gupta 2030
                    } else {
30137 tejbeer 2031
 
31677 amit.gupta 2032
                        if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
2033
                                || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
2034
                                || ptam.getCollectionTarget() >= totalCollectionPlan) {
30137 tejbeer 2035
 
31677 amit.gupta 2036
                            partnerCollectionPlan.setActive(false);
2037
                            partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
2038
                            partnerCollectionPlan = new PartnerCollectionPlan();
2039
                            partnerCollectionPlan.setCreateTimestamp(LocalDateTime.now());
2040
                            partnerCollectionPlan.setAuthId(authUser.getId());
2041
                            partnerCollectionPlan.setFofoId(ptam.getFofoId());
2042
                            partnerCollectionPlan.setActive(true);
2043
                            partnerCollectionPlan
2044
                                    .setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
2045
                            partnerCollectionPlan.setCollectionPlan(ptam.getCollectionTarget());
2046
                            partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
2047
                            partnerCollectionPlanRepository.persist(partnerCollectionPlan);
2048
                        } else {
2049
                            throw new ProfitMandiBusinessException("collection target", "",
2050
                                    "collection target should be more than " + totalCollectionPlan);
2051
                        }
2052
                    }
2053
                }
30137 tejbeer 2054
 
31677 amit.gupta 2055
                if ((LocalDate.now().atStartOfDay().equals(ptam.getCollectionCommitmentDate().atStartOfDay())
2056
                        || ptam.getCollectionCommitmentDate().atStartOfDay().isAfter(LocalDate.now().atStartOfDay()))
2057
                        && partnerCollectionPlan.getCollectionPlan() == ptam.getCollectionTarget()) {
2058
                    partnerCollectionPlan.setCommitedTimestamp(ptam.getCollectionCommitmentDate().atStartOfDay());
2059
                    partnerCollectionPlan.setUpdatedTimestamp(LocalDateTime.now());
2060
                }
30416 tejbeer 2061
 
31677 amit.gupta 2062
            }
30433 tejbeer 2063
 
35725 ranu 2064
            long callLogId = recordingService.getLatestAgentCallLog(ptam.getFofoId(),authUser.getId());
2065
 
31677 amit.gupta 2066
            PartnerCollectionRemark pcr = new PartnerCollectionRemark();
2067
            pcr.setFofoId(ptam.getFofoId());
2068
            pcr.setAuthId(authUser.getId());
2069
            pcr.setMessage("Collection plan for" + ptam.getCollectionCommitmentDate());
2070
            pcr.setRemark(CollectionRemark.COLLECTION_PLAN);
35725 ranu 2071
            pcr.setAgentCallLogId(callLogId);
31677 amit.gupta 2072
            pcr.setCreateTimestamp(LocalDateTime.now());
2073
            partnerCollectionRemarkRepository.persist(pcr);
2074
        } else {
2075
            throw new ProfitMandiBusinessException("Date", "",
2076
                    "you can't select the back date " + ptam.getCollectionCommitmentDate());
2077
        }
30077 tejbeer 2078
 
31677 amit.gupta 2079
        for (Entry<String, PartnerSecondaryPlanModel> pspm : ptam.getPartnerSecondaryModel().entrySet()) {
30087 tejbeer 2080
 
31677 amit.gupta 2081
            if (ptam.getCollectionCommitmentDate().isAfter(LocalDate.now())
2082
                    || ptam.getCollectionCommitmentDate().isEqual(LocalDate.now())) {
2083
                PartnerSecondaryPlanModel plan = pspm.getValue();
2084
                PartnerSecondaryPlan psp = partnerSecondaryPlanRepository.selectByLocalDateBrand(plan.getBrand(),
2085
                        LocalDate.now(), ptam.getFofoId(), true);
2086
                LOGGER.info("psp" + psp);
30087 tejbeer 2087
 
31677 amit.gupta 2088
                if (psp == null) {
2089
                    if (plan.getTargetPlan() != null && plan.getTargetPlan() > 0) {
30087 tejbeer 2090
 
31677 amit.gupta 2091
                        psp = new PartnerSecondaryPlan();
2092
                        psp.setAuthId(authUser.getId());
2093
                        psp.setBrand(pspm.getKey());
2094
                        psp.setFofoId(pspm.getValue().getFofoId());
2095
                        psp.setSecondaryPlan(pspm.getValue().getTargetPlan());
2096
                        psp.setCreateTimestamp(LocalDateTime.now());
2097
                        psp.setUpdatedTimestamp(LocalDateTime.now());
2098
                        psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
2099
                        psp.setActive(true);
2100
                        partnerSecondaryPlanRepository.persist(psp);
2101
                    }
30087 tejbeer 2102
 
31677 amit.gupta 2103
                } else {
2104
                    if (plan.getTargetPlan() != psp.getSecondaryPlan()) {
2105
                        float totalSecondaryPlan = psp.getSecondaryPlan() + 10000;
2106
                        if (authUser.getId() == plan.getAuthId()) {
2107
                            if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
2108
                                    || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
2109
                                    || plan.getTargetPlan() >= totalSecondaryPlan) {
2110
                                psp.setSecondaryPlan(pspm.getValue().getTargetPlan());
2111
                                psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
2112
                                psp.setUpdatedTimestamp(LocalDateTime.now());
2113
                                psp.setActive(true);
2114
                            } else {
2115
                                throw new ProfitMandiBusinessException("secondary target", "",
2116
                                        "secondary target should be more than " + totalSecondaryPlan);
2117
                            }
30087 tejbeer 2118
 
31677 amit.gupta 2119
                        } else {
30087 tejbeer 2120
 
31677 amit.gupta 2121
                            if (authUser.getEmailId().equals("tarun.verma@smartdukaan.com")
2122
                                    || authUser.getEmailId().equals("rakesh.sonawane@smartdukaan.com")
2123
                                    || plan.getTargetPlan() >= totalSecondaryPlan) {
30087 tejbeer 2124
 
31677 amit.gupta 2125
                                psp.setUpdatedTimestamp(LocalDateTime.now());
2126
                                psp.setActive(false);
2127
                                psp = new PartnerSecondaryPlan();
2128
                                psp.setAuthId(authUser.getId());
2129
                                psp.setBrand(pspm.getKey());
2130
                                psp.setFofoId(pspm.getValue().getFofoId());
2131
                                psp.setSecondaryPlan(pspm.getValue().getTargetPlan());
2132
                                psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
2133
                                psp.setCreateTimestamp(LocalDateTime.now());
2134
                                psp.setUpdatedTimestamp(LocalDateTime.now());
2135
                                psp.setActive(true);
2136
                                partnerSecondaryPlanRepository.persist(psp);
2137
                            } else {
2138
                                throw new ProfitMandiBusinessException("secondary target", "",
2139
                                        "secondary target should be more than " + totalSecondaryPlan);
2140
                            }
2141
                        }
2142
                    }
30137 tejbeer 2143
 
31677 amit.gupta 2144
                    if ((LocalDate.now().atStartOfDay().equals(ptam.getSecondaryCommitmentDate().atStartOfDay())
2145
                            || ptam.getSecondaryCommitmentDate().atStartOfDay().isAfter(LocalDate.now().atStartOfDay()))
2146
                            && plan.getTargetPlan() == psp.getSecondaryPlan()) {
2147
                        psp.setCommitedTimestamp(ptam.getSecondaryCommitmentDate().atStartOfDay());
2148
                        psp.setUpdatedTimestamp(LocalDateTime.now());
2149
                    }
30137 tejbeer 2150
 
31677 amit.gupta 2151
                }
2152
            } else {
2153
                throw new ProfitMandiBusinessException("Date", "",
2154
                        "you can't select the back date " + ptam.getSecondaryCommitmentDate());
2155
            }
30137 tejbeer 2156
 
31677 amit.gupta 2157
        }
30077 tejbeer 2158
 
31677 amit.gupta 2159
        return responseSender.ok(true);
30137 tejbeer 2160
 
31677 amit.gupta 2161
    }
30077 tejbeer 2162
 
31677 amit.gupta 2163
    @RequestMapping(value = "/uploadFranchiseeVisit", method = RequestMethod.POST)
35288 amit 2164
    public ResponseEntity<?> readCsvFileAndSetLead(HttpServletRequest request, @RequestPart MultipartFile
2165
            multipartFile) throws Throwable {
30433 tejbeer 2166
 
31677 amit.gupta 2167
        int id = (int) request.getAttribute("userId");
30433 tejbeer 2168
 
31677 amit.gupta 2169
        LOGGER.info("id" + id);
30433 tejbeer 2170
 
31677 amit.gupta 2171
        User user = userRepository.selectById(id);
30433 tejbeer 2172
 
31677 amit.gupta 2173
        AuthUser authUser = authRepository.selectByEmailOrMobile(user.getEmailId());
30433 tejbeer 2174
 
31677 amit.gupta 2175
        String fileName = multipartFile.getName();
2176
        String fileNames = multipartFile.getOriginalFilename();
30433 tejbeer 2177
 
31677 amit.gupta 2178
        LOGGER.info("fileName" + fileName);
2179
        LOGGER.info("fileNames" + fileNames);
30433 tejbeer 2180
 
31677 amit.gupta 2181
        List<CSVRecord> records = FileUtil.readFile(multipartFile);
30433 tejbeer 2182
 
31677 amit.gupta 2183
        for (CSVRecord record : records) {
2184
            FranchiseeVisit franchiseeVisit = new FranchiseeVisit();
2185
            franchiseeVisit.setFofoId(Integer.parseInt(record.get(0)));
2186
            CustomRetailer customRetailer = retailerService.getFofoRetailer(Integer.parseInt(record.get(0)));
30433 tejbeer 2187
 
31677 amit.gupta 2188
            franchiseeVisit.setPartnerName(customRetailer.getBusinessName());
2189
            franchiseeVisit.setAgenda(record.get(1));
2190
            franchiseeVisit.setCreatedTimestamp(LocalDateTime.now());
2191
            franchiseeVisit.setUpdatedTimestamp(LocalDateTime.now());
2192
            franchiseeVisit.setStatus(FranchiseeVisitStatus.OPEN);
30433 tejbeer 2193
 
31677 amit.gupta 2194
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
2195
            LOGGER.info(record.get(1));
2196
            LocalDate dateTime = LocalDate.parse(record.get(2), formatter);
30433 tejbeer 2197
 
31677 amit.gupta 2198
            franchiseeVisit.setScheduleTimestamp(dateTime.atStartOfDay());
2199
            // change
30433 tejbeer 2200
 
31677 amit.gupta 2201
            String authUserName = authUser.getFirstName() + " " + authUser.getLastName();
2202
            franchiseeVisit.setCreatedBy(authUserName);
2203
            franchiseeVisit.setAuthId(authUser.getId());
30433 tejbeer 2204
 
31677 amit.gupta 2205
            franchiseeVisitRepository.persist(franchiseeVisit);
30433 tejbeer 2206
 
31677 amit.gupta 2207
            return responseSender.ok(true);
2208
        }
30433 tejbeer 2209
 
31677 amit.gupta 2210
        return responseSender.ok(true);
30433 tejbeer 2211
 
31677 amit.gupta 2212
    }
30487 tejbeer 2213
 
31677 amit.gupta 2214
    @RequestMapping(value = "/downloadFranchiseeVisitTemplate", method = RequestMethod.GET)
2215
    public ResponseEntity<?> downloadFranchiseeVisitTemplate(HttpServletRequest request) throws Exception {
30487 tejbeer 2216
 
31677 amit.gupta 2217
        // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
30487 tejbeer 2218
 
31677 amit.gupta 2219
        List<List<?>> rows = new ArrayList<>();
30487 tejbeer 2220
 
32923 ranu 2221
        ByteArrayOutputStream byteArrayOutputStream = FileUtil
31677 amit.gupta 2222
                .getCSVByteStream(Arrays.asList("Partner Id", "Agenda", "Schedule Timestamp"), rows);
30487 tejbeer 2223
 
31677 amit.gupta 2224
        try {
2225
            byteArrayOutputStream.close();
2226
        } catch (IOException e) {
2227
            // TODO Auto-generated catch block
2228
            e.printStackTrace();
2229
        }
30487 tejbeer 2230
 
31677 amit.gupta 2231
        String filename = "template.csv";
2232
        HttpHeaders headers = new HttpHeaders();
2233
        headers.add("Content-Type", "application/csv");
2234
        headers.setContentDispositionFormData(filename, filename);
30487 tejbeer 2235
 
31677 amit.gupta 2236
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
30487 tejbeer 2237
 
31677 amit.gupta 2238
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers,
2239
                HttpStatus.OK);
2240
        return response;
30544 tejbeer 2241
 
31677 amit.gupta 2242
    }
30542 tejbeer 2243
 
31677 amit.gupta 2244
    @RequestMapping(value = "/markVisitAttendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
2245
    public ResponseEntity<?> markVisitAttendance(HttpServletRequest request, @RequestParam(name = "id") int id)
2246
            throws ProfitMandiBusinessException {
2247
        FranchiseeVisit franchiseeVisit = franchiseeVisitRepository.selectById(id);
30542 tejbeer 2248
 
31677 amit.gupta 2249
        franchiseeVisit.setVisitTimestamp(LocalDateTime.now());
30542 tejbeer 2250
 
31677 amit.gupta 2251
        return responseSender.ok(true);
30542 tejbeer 2252
 
31677 amit.gupta 2253
    }
31249 tejbeer 2254
 
31677 amit.gupta 2255
    @RequestMapping(value = "/getVisitRequests", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
2256
    public ResponseEntity<?> getVisitRequests(HttpServletRequest request,
2257
                                              @RequestParam(name = "gmailId") String gmailId, Model model) throws Exception {
31249 tejbeer 2258
 
31677 amit.gupta 2259
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
31249 tejbeer 2260
 
31677 amit.gupta 2261
        List<Integer> authUserIds = authService.getAllReportees(authUser.getId());
2262
        List<VisitSummaryModel> visitSummary = new ArrayList<>();
31249 tejbeer 2263
 
31677 amit.gupta 2264
        if (!authUserIds.isEmpty()) {
35397 amit 2265
            List<AuthUser> authUsers = authRepository.selectByIds(authUserIds);
31249 tejbeer 2266
 
31677 amit.gupta 2267
            List<VisitRequest> allVisitRequests = visitRequestRepository.selectByAuthIdsAndDate(authUserIds,
2268
                    LocalDate.now().minusDays(7), Arrays.asList(VisitStatus.approved, VisitStatus.pending));
2269
            LOGGER.info("allVisitRequests {}", allVisitRequests);
31249 tejbeer 2270
 
31677 amit.gupta 2271
            if (!allVisitRequests.isEmpty()) {
31249 tejbeer 2272
 
31677 amit.gupta 2273
                List<Integer> leadIds = allVisitRequests.stream().filter(x -> x.getVisitType().equals("lead"))
2274
                        .map(x -> x.getVisitId()).collect(Collectors.toList());
2275
                List<Lead> leads = new ArrayList<>();
2276
                if (!leadIds.isEmpty()) {
2277
                    leads = leadRepository.selectAllByIds(leadIds);
31249 tejbeer 2278
 
35415 amit 2279
                    // Batch fetch all LeadActivities instead of N+1
2280
                    List<LeadActivity> allLeadActivities = leadActivityRepository.selectAllByleadIds(leadIds);
2281
                    Map<Integer, List<LeadActivity>> leadActivityMap = allLeadActivities.stream()
2282
                            .collect(Collectors.groupingBy(LeadActivity::getLeadId));
2283
 
31677 amit.gupta 2284
                    for (Lead lead : leads) {
35415 amit 2285
                        List<LeadActivity> leadActivities = leadActivityMap.get(lead.getId());
2286
                        if (leadActivities != null && !leadActivities.isEmpty()) {
2287
                            lead.setScheduledTimestamp(leadActivities.get(0).getSchelduleTimestamp());
2288
                            lead.setLeadActivity(leadActivities.get(0));
2289
                        }
31677 amit.gupta 2290
                    }
2291
                }
2292
                List<Integer> franchiseeIds = allVisitRequests.stream()
2293
                        .filter(x -> x.getVisitType().equals("franchiseeVisit")).map(x -> x.getVisitId())
2294
                        .collect(Collectors.toList());
2295
                List<FranchiseeVisit> franchiseeVisits = new ArrayList<>();
2296
                if (!franchiseeIds.isEmpty()) {
2297
                    franchiseeVisits = franchiseeVisitRepository.selectAllByIds(franchiseeIds);
2298
                    LOGGER.info("franchiseeVisits {}", franchiseeVisits);
31249 tejbeer 2299
 
35415 amit 2300
                    // Batch fetch all FranchiseeActivities instead of N+1
2301
                    List<FranchiseeActivity> allFranchiseeActivities = franchiseeActivityRepository
2302
                            .selectByFranchiseeVisitIds(franchiseeIds);
2303
                    Map<Integer, List<FranchiseeActivity>> franchiseeActivityMap = allFranchiseeActivities.stream()
2304
                            .collect(Collectors.groupingBy(FranchiseeActivity::getFranchiseeVisitd));
2305
 
31677 amit.gupta 2306
                    for (FranchiseeVisit franchiseeVisit : franchiseeVisits) {
35415 amit 2307
                        List<FranchiseeActivity> franchiseeActivities = franchiseeActivityMap.get(franchiseeVisit.getId());
31677 amit.gupta 2308
                        LOGGER.info("franchiseeActivities {}", franchiseeActivities);
31249 tejbeer 2309
 
35415 amit 2310
                        if (franchiseeActivities != null && !franchiseeActivities.isEmpty()) {
2311
                            franchiseeVisit.setScheduleTimestamp(franchiseeActivities.get(0).getSchelduleTimestamp());
2312
                            franchiseeVisit.setFranchiseeActivity(franchiseeActivities.get(0));
2313
                        }
31677 amit.gupta 2314
                    }
31249 tejbeer 2315
 
31677 amit.gupta 2316
                }
31249 tejbeer 2317
 
35415 amit 2318
                // Pre-fetch all retailers using cached method (instead of N+1 getFofoRetailer calls)
2319
                Map<Integer, CustomRetailer> allRetailersMap = retailerService.getAllFofoRetailers();
2320
 
31677 amit.gupta 2321
                Map<LocalDate, List<VisitRequest>> dateWiseVisitRequest = allVisitRequests.stream()
2322
                        .collect(Collectors.groupingBy(x -> x.getScheduleTimestamp().toLocalDate()));
31249 tejbeer 2323
 
31677 amit.gupta 2324
                for (Entry<LocalDate, List<VisitRequest>> visitEntry : dateWiseVisitRequest.entrySet()) {
31249 tejbeer 2325
 
31677 amit.gupta 2326
                    LocalDate date = visitEntry.getKey();
2327
                    List<VisitRequest> visitRequests = visitEntry.getValue();
31249 tejbeer 2328
 
31677 amit.gupta 2329
                    VisitSummaryModel visitSummaryModel = new VisitSummaryModel();
31249 tejbeer 2330
 
31677 amit.gupta 2331
                    visitSummaryModel.setDate(date);
2332
                    List<Integer> dateWiseLeadEntry = visitRequests.stream()
2333
                            .filter(x -> x.getVisitType().equals("lead")).map(x -> x.getVisitId())
2334
                            .collect(Collectors.toList());
31249 tejbeer 2335
 
31677 amit.gupta 2336
                    List<Integer> dateWiseFranchiseeIds = visitRequests.stream()
2337
                            .filter(x -> x.getVisitType().equals("franchiseeVisit")).map(x -> x.getVisitId())
2338
                            .collect(Collectors.toList());
31249 tejbeer 2339
 
31677 amit.gupta 2340
                    Map<Integer, List<Lead>> filteredLeadsMap = null;
2341
                    if (!leads.isEmpty()) {
2342
                        filteredLeadsMap = leads.stream().filter(x -> dateWiseLeadEntry.contains(x.getId()))
2343
                                .collect(Collectors.groupingBy(x -> x.getAssignTo()));
31249 tejbeer 2344
 
31677 amit.gupta 2345
                    }
31249 tejbeer 2346
 
31677 amit.gupta 2347
                    Map<Integer, List<FranchiseeVisit>> filteredFranchiseeVisitsMap = null;
31249 tejbeer 2348
 
31677 amit.gupta 2349
                    if (!franchiseeVisits.isEmpty()) {
2350
                        filteredFranchiseeVisitsMap = franchiseeVisits.stream()
2351
                                .filter(x -> dateWiseFranchiseeIds.contains(x.getId()))
2352
                                .collect(Collectors.groupingBy(x -> x.getAuthId()));
31249 tejbeer 2353
 
31677 amit.gupta 2354
                    }
31249 tejbeer 2355
 
31677 amit.gupta 2356
                    List<UserVisitModel> userVisits = new ArrayList<>();
31249 tejbeer 2357
 
31677 amit.gupta 2358
                    for (AuthUser auth : authUsers) {
2359
                        UserVisitModel userVisitModel = new UserVisitModel();
2360
                        List<VisitDescriptionModel> visitDescriptions = new ArrayList<>();
31249 tejbeer 2361
 
31677 amit.gupta 2362
                        List<Lead> authLeads = new ArrayList<>();
2363
                        if (filteredLeadsMap != null) {
2364
                            authLeads = filteredLeadsMap.get(auth.getId());
31249 tejbeer 2365
 
31677 amit.gupta 2366
                        }
31249 tejbeer 2367
 
31677 amit.gupta 2368
                        if (authLeads != null && !authLeads.isEmpty()) {
2369
                            userVisitModel.setAuthUser(auth.getFullName());
31249 tejbeer 2370
 
31677 amit.gupta 2371
                            for (Lead lead : authLeads) {
31249 tejbeer 2372
 
31677 amit.gupta 2373
                                VisitRequest visitRequest = visitRequests.stream().filter(
2374
                                                x -> x.getVisitId() == lead.getId() && x.getCreatedBy() == lead.getAssignTo())
2375
                                        .findAny().orElse(null);
2376
                                VisitDescriptionModel visitDescriptionModel = new VisitDescriptionModel();
2377
                                visitDescriptionModel.setVisitId(visitRequest.getId());
2378
                                visitDescriptionModel.setVisitName(lead.getFirstName());
2379
                                visitDescriptionModel.setCity(lead.getCity());
2380
                                visitDescriptionModel.setState(lead.getState());
2381
                                visitDescriptionModel.setScheduleTime(lead.getScheduledTimestamp());
2382
                                visitDescriptionModel.setStatus(visitRequest.getStatus());
2383
                                visitDescriptionModel.setRemarks(lead.getLeadActivity().getRemark());
2384
                                visitDescriptionModel.setVisitType(visitRequest.getVisitType());
2385
                                visitDescriptions.add(visitDescriptionModel);
31249 tejbeer 2386
 
31677 amit.gupta 2387
                            }
2388
                        }
31249 tejbeer 2389
 
31677 amit.gupta 2390
                        List<FranchiseeVisit> authfranchiseeVisit = new ArrayList<>();
2391
                        if (filteredFranchiseeVisitsMap != null) {
2392
                            authfranchiseeVisit = filteredFranchiseeVisitsMap.get(auth.getId());
31249 tejbeer 2393
 
31677 amit.gupta 2394
                        }
31249 tejbeer 2395
 
31677 amit.gupta 2396
                        if (authfranchiseeVisit != null && !authfranchiseeVisit.isEmpty()) {
2397
                            userVisitModel.setAuthUser(auth.getFullName());
31249 tejbeer 2398
 
31677 amit.gupta 2399
                            for (FranchiseeVisit franchiseeVisit : authfranchiseeVisit) {
31249 tejbeer 2400
 
31677 amit.gupta 2401
                                VisitRequest visitRequest = visitRequests.stream()
2402
                                        .filter(x -> x.getVisitId() == franchiseeVisit.getId()
2403
                                                && x.getCreatedBy() == franchiseeVisit.getAuthId())
2404
                                        .findAny().orElse(null);
31249 tejbeer 2405
 
35415 amit 2406
                                CustomRetailer customRetailer = allRetailersMap.get(franchiseeVisit.getFofoId());
31677 amit.gupta 2407
                                VisitDescriptionModel visitDescriptionModel = new VisitDescriptionModel();
2408
                                visitDescriptionModel.setVisitId(visitRequest.getId());
2409
                                visitDescriptionModel.setVisitName(franchiseeVisit.getPartnerName());
35415 amit 2410
                                if (customRetailer != null && customRetailer.getAddress() != null) {
2411
                                    visitDescriptionModel.setCity(customRetailer.getAddress().getCity());
2412
                                    visitDescriptionModel.setState(customRetailer.getAddress().getState());
2413
                                }
31677 amit.gupta 2414
                                visitDescriptionModel.setScheduleTime(
2415
                                        franchiseeVisit.getFranchiseeActivity().getSchelduleTimestamp());
2416
                                visitDescriptionModel.setStatus(visitRequest.getStatus());
2417
                                visitDescriptionModel.setRemarks(franchiseeVisit.getAgenda());
2418
                                visitDescriptionModel.setVisitType(visitRequest.getVisitType());
31249 tejbeer 2419
 
31677 amit.gupta 2420
                                visitDescriptions.add(visitDescriptionModel);
31249 tejbeer 2421
 
31677 amit.gupta 2422
                            }
2423
                        }
31249 tejbeer 2424
 
31677 amit.gupta 2425
                        userVisitModel.setVisitDescriptions(visitDescriptions);
2426
                        LOGGER.info("userVisit {}", userVisitModel);
31249 tejbeer 2427
 
31677 amit.gupta 2428
                        if (userVisitModel.getAuthUser() != null) {
2429
                            userVisits.add(userVisitModel);
2430
                        }
31249 tejbeer 2431
 
31677 amit.gupta 2432
                    }
31249 tejbeer 2433
 
31677 amit.gupta 2434
                    visitSummaryModel.setUserVisits(userVisits);
31249 tejbeer 2435
 
31677 amit.gupta 2436
                    visitSummary.add(visitSummaryModel);
31249 tejbeer 2437
 
31677 amit.gupta 2438
                }
31249 tejbeer 2439
 
31677 amit.gupta 2440
            }
31249 tejbeer 2441
 
31677 amit.gupta 2442
        }
2443
        return responseSender.ok(visitSummary);
31249 tejbeer 2444
 
31677 amit.gupta 2445
    }
31249 tejbeer 2446
 
31677 amit.gupta 2447
    @RequestMapping(value = "/visitRequest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35288 amit 2448
    public ResponseEntity<?> visitRequest(HttpServletRequest request, @RequestParam(name = "gmailId") String
2449
                                                  gmailId,
31677 amit.gupta 2450
                                          @RequestParam(name = "visitId") int visitId, @RequestParam(name = "status") VisitStatus status, Model model)
2451
            throws Exception {
2452
        AuthUser authUser = authRepository.selectByGmailId(gmailId);
31249 tejbeer 2453
 
31677 amit.gupta 2454
        VisitRequest visitRequest = visitRequestRepository.selectById(visitId);
2455
        visitRequest.setActionedBy(authUser.getId());
2456
        visitRequest.setStatus(status);
2457
        visitRequest.setUpdatedTimestamp(LocalDateTime.now());
2458
        if (status.equals(VisitStatus.approved)) {
2459
            visitRequest.setApprovedTimestamp(LocalDateTime.now());
2460
        }
31249 tejbeer 2461
 
31677 amit.gupta 2462
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
2463
        sendNotificationModel.setCampaignName("Visit Request");
2464
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
2465
        sendNotificationModel.setTitle("Visit Request");
2466
        String message = "Visit Request has been " + status + " for scheduled time on "
2467
                + visitRequest.getScheduleTimestamp().toLocalDate();
31249 tejbeer 2468
 
31677 amit.gupta 2469
        sendNotificationModel.setMessage(message);
2470
        sendNotificationModel.setMessageType(MessageType.notification);
2471
        User user = userRepository.selectByEmailId(gmailId);
2472
        sendNotificationModel.setUserIds(new ArrayList<>(user.getId()));
31249 tejbeer 2473
 
31677 amit.gupta 2474
        notificationService.sendNotificationToAll(sendNotificationModel);
31249 tejbeer 2475
 
31677 amit.gupta 2476
        return responseSender.ok(true);
31249 tejbeer 2477
 
31677 amit.gupta 2478
    }
31249 tejbeer 2479
 
34301 ranu 2480
    @Autowired
2481
    RbmRatingRepository rbmRatingRepository;
2482
 
34322 ranu 2483
    @Autowired
2484
    SalesRatingRepository salesRatingRepository;
2485
 
34301 ranu 2486
    @RequestMapping(value = "/rbmRating", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
35288 amit 2487
    public ResponseEntity<?> rbmRating(HttpServletRequest request, @RequestBody RbmSalesRatingRequest
2488
            rbmSalesRatingRequest, Model model) throws Exception {
34301 ranu 2489
        int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
2490
        UserCart uc = userAccountRepository.getUserCart(userId);
2491
        int fofoId = uc.getUserId();
34322 ranu 2492
 
34301 ranu 2493
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
34322 ranu 2494
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
34301 ranu 2495
 
36114 ranu 2496
        LocalDate today = LocalDate.now();
36930 ranu 2497
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
2498
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
2499
        LocalDateTime startOfMonthDateTime = startOfMonth.atStartOfDay();
2500
        LocalDateTime endOfMonthDateTime = endOfMonth.atTime(23, 59, 59);
34301 ranu 2501
 
36930 ranu 2502
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdAndRbmIdForCurrentMonth(fofoId, rbmL1, startOfMonthDateTime, endOfMonthDateTime);
34322 ranu 2503
        if (!existingRbmRatings.isEmpty()) {
36930 ranu 2504
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this month already exists.");
34301 ranu 2505
        }
2506
 
36930 ranu 2507
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdAndSalesL1IdForCurrentMonth(fofoId, salesL1Id, startOfMonthDateTime, endOfMonthDateTime);
34322 ranu 2508
        if (!existingSalesRatings.isEmpty()) {
36930 ranu 2509
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this month already exists.");
34322 ranu 2510
        }
34301 ranu 2511
 
34322 ranu 2512
        // Save RBM rating
34301 ranu 2513
        RbmRating rbmRating = new RbmRating();
34322 ranu 2514
        rbmRating.setComment(rbmSalesRatingRequest.getRbmComment());
2515
        rbmRating.setRating(rbmSalesRatingRequest.getRbmRating());
34301 ranu 2516
        rbmRating.setFofoId(fofoId);
2517
        rbmRating.setRbmId(rbmL1);
2518
        rbmRating.setCreateTimeStamp(LocalDateTime.now());
2519
        rbmRatingRepository.persist(rbmRating);
2520
 
34322 ranu 2521
        // Save Sales Person rating
2522
        SalesRating salesRating = new SalesRating();
2523
        salesRating.setComment(rbmSalesRatingRequest.getSalesComment());
2524
        salesRating.setRating(rbmSalesRatingRequest.getSalesRating());
2525
        salesRating.setFofoId(fofoId);
2526
        salesRating.setSalesL1Id(salesL1Id);
2527
        salesRating.setCreateTimeStamp(LocalDateTime.now());
2528
        salesRatingRepository.persist(salesRating);
2529
 
2530
        return responseSender.ok("Rating submitted successfully.");
34301 ranu 2531
    }
2532
 
36286 ranu 2533
    @RequestMapping(value = "/rbmRating/weekly-status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
2534
    public ResponseEntity<?> rbmRatingWeeklyStatus(HttpServletRequest request) throws Exception {
2535
        int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
2536
        UserCart uc = userAccountRepository.getUserCart(userId);
2537
        int fofoId = uc.getUserId();
34301 ranu 2538
 
36286 ranu 2539
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
2540
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
2541
 
2542
        LocalDate today = LocalDate.now();
36930 ranu 2543
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
2544
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
2545
        LocalDateTime startOfMonthDateTime = startOfMonth.atStartOfDay();
2546
        LocalDateTime endOfMonthDateTime = endOfMonth.atTime(23, 59, 59);
36286 ranu 2547
 
36930 ranu 2548
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdForCurrentWeek(fofoId, startOfMonthDateTime, endOfMonthDateTime);
2549
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdForCurrentWeek(fofoId, startOfMonthDateTime, endOfMonthDateTime);
36286 ranu 2550
 
2551
        boolean pending = existingRbmRatings.isEmpty() || existingSalesRatings.isEmpty();
2552
 
2553
        Map<String, Object> response = new HashMap<>();
2554
        response.put("pending", pending);
36930 ranu 2555
        response.put("weekStart", startOfMonth.toString());
2556
        response.put("weekEnd", endOfMonth.toString());
36286 ranu 2557
 
2558
        if (rbmL1 > 0) {
2559
            AuthUser rbmUser = authRepository.selectById(rbmL1);
2560
            if (rbmUser != null) {
2561
                response.put("rbmName", rbmUser.getFullName());
2562
            }
2563
        }
2564
        if (salesL1Id > 0) {
2565
            AuthUser salesUser = authRepository.selectById(salesL1Id);
2566
            if (salesUser != null) {
2567
                response.put("salesPersonName", salesUser.getFullName());
2568
            }
2569
        }
2570
 
2571
        return responseSender.ok(response);
2572
    }
2573
 
2574
    @Autowired
2575
    private RatingReminderRepository ratingReminderRepository;
2576
 
2577
    @RequestMapping(value = "/rbmRating/reminder", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
2578
    public ResponseEntity<?> rbmRatingReminder(HttpServletRequest request) throws Exception {
2579
        int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
2580
        UserCart uc = userAccountRepository.getUserCart(userId);
2581
        int fofoId = uc.getUserId();
2582
 
2583
        LocalDate today = LocalDate.now();
36930 ranu 2584
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
2585
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
36286 ranu 2586
 
36930 ranu 2587
        RatingReminder reminder = ratingReminderRepository.findByFofoIdAndWeek(fofoId, startOfMonth);
36286 ranu 2588
 
2589
        int attemptsRemaining;
2590
        if (reminder == null) {
2591
            reminder = new RatingReminder();
2592
            reminder.setFofoId(fofoId);
36930 ranu 2593
            reminder.setWeekStart(startOfMonth);
2594
            reminder.setWeekEnd(endOfMonth);
36286 ranu 2595
            reminder.setAttemptsRemaining(RatingReminder.MAX_ATTEMPTS_PER_WEEK);
2596
            reminder.setCreateTimestamp(LocalDateTime.now());
2597
            ratingReminderRepository.persist(reminder);
2598
            attemptsRemaining = RatingReminder.MAX_ATTEMPTS_PER_WEEK;
2599
        } else {
2600
            ratingReminderRepository.decrementRemaining(reminder.getId());
36930 ranu 2601
            reminder = ratingReminderRepository.findByFofoIdAndWeek(fofoId, startOfMonth);
36286 ranu 2602
            attemptsRemaining = reminder.getAttemptsRemaining();
2603
        }
2604
 
2605
        Map<String, Object> response = new HashMap<>();
2606
        response.put("partnerId", fofoId);
2607
        response.put("attemptsRemaining", attemptsRemaining);
2608
 
2609
        return responseSender.ok(response);
2610
    }
2611
 
2612
 
30077 tejbeer 2613
}