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