Subversion Repositories SmartDukaan

Rev

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