Subversion Repositories SmartDukaan

Rev

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