| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.web.controller;
|
1 |
package com.spice.profitmandi.web.controller;
|
| 2 |
|
2 |
|
| 3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 4 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
4 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 5 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
5 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| - |
|
6 |
import com.spice.profitmandi.common.util.FileUtil;
|
| 6 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
7 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 7 |
import com.spice.profitmandi.dao.entity.cs.*;
|
8 |
import com.spice.profitmandi.dao.entity.cs.*;
|
| 8 |
import com.spice.profitmandi.dao.entity.cs.TicketReadStatus.UserType;
|
9 |
import com.spice.profitmandi.dao.entity.cs.TicketReadStatus.UserType;
|
| 9 |
import com.spice.profitmandi.dao.entity.dtr.Document;
|
10 |
import com.spice.profitmandi.dao.entity.dtr.Document;
|
| 10 |
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
|
11 |
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
|
| Line 15... |
Line 16... |
| 15 |
import com.spice.profitmandi.dao.repository.cs.*;
|
16 |
import com.spice.profitmandi.dao.repository.cs.*;
|
| 16 |
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
|
17 |
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
|
| 17 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
18 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 18 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
19 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| 19 |
import com.spice.profitmandi.service.mail.MailOutboxService;
|
20 |
import com.spice.profitmandi.service.mail.MailOutboxService;
|
| - |
|
21 |
import com.spice.profitmandi.service.order.OrderService;
|
| 20 |
import com.spice.profitmandi.service.user.RetailerService;
|
22 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 21 |
import com.spice.profitmandi.web.model.LoginDetails;
|
23 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 22 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
24 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 23 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
25 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 24 |
import org.apache.logging.log4j.LogManager;
|
26 |
import org.apache.logging.log4j.LogManager;
|
| 25 |
import org.apache.logging.log4j.Logger;
|
27 |
import org.apache.logging.log4j.Logger;
|
| 26 |
import org.springframework.beans.factory.annotation.Autowired;
|
28 |
import org.springframework.beans.factory.annotation.Autowired;
|
| - |
|
29 |
import org.springframework.http.ResponseEntity;
|
| 27 |
import org.springframework.mail.javamail.JavaMailSender;
|
30 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 28 |
import org.springframework.stereotype.Controller;
|
31 |
import org.springframework.stereotype.Controller;
|
| 29 |
import org.springframework.transaction.annotation.Transactional;
|
32 |
import org.springframework.transaction.annotation.Transactional;
|
| 30 |
import org.springframework.ui.Model;
|
33 |
import org.springframework.ui.Model;
|
| 31 |
import org.springframework.web.bind.annotation.*;
|
34 |
import org.springframework.web.bind.annotation.*;
|
| Line 88... |
Line 91... |
| 88 |
|
91 |
|
| 89 |
@Autowired
|
92 |
@Autowired
|
| 90 |
private ActivityRepository activityRepository;
|
93 |
private ActivityRepository activityRepository;
|
| 91 |
|
94 |
|
| 92 |
@Autowired
|
95 |
@Autowired
|
| - |
|
96 |
private OrderService orderService;
|
| - |
|
97 |
|
| - |
|
98 |
@Autowired
|
| 93 |
private ActivityAttachmentRepository activityAttachmentRepository;
|
99 |
private ActivityAttachmentRepository activityAttachmentRepository;
|
| 94 |
|
100 |
|
| 95 |
@Autowired
|
101 |
@Autowired
|
| 96 |
private TicketAssignedRepository ticketAssignedRepository;
|
102 |
private TicketAssignedRepository ticketAssignedRepository;
|
| 97 |
|
103 |
|
| Line 572... |
Line 578... |
| 572 |
Model model) throws ProfitMandiBusinessException {
|
578 |
Model model) throws ProfitMandiBusinessException {
|
| 573 |
populateMyTicketModel(request, sortOrder, ticketStatus, ticketSearchType, searchTerm, page, pageSize, searchText, model);
|
579 |
populateMyTicketModel(request, sortOrder, ticketStatus, ticketSearchType, searchTerm, page, pageSize, searchText, model);
|
| 574 |
return "ticket-content";
|
580 |
return "ticket-content";
|
| 575 |
}
|
581 |
}
|
| 576 |
|
582 |
|
| - |
|
583 |
private static String nvl(String s) {
|
| - |
|
584 |
return s == null ? "" : s;
|
| - |
|
585 |
}
|
| - |
|
586 |
|
| - |
|
587 |
// Download every ticket the user can see in the current filter (respects ticketStatus /
|
| - |
|
588 |
// sortOrder / search — but ignores pagination) as CSV with the same 8 columns rendered
|
| - |
|
589 |
// in ticket-content.vm: Ticket Id, Partner, Category, SubCategory, Message, Created By,
|
| - |
|
590 |
// Assigned To, Created TimeStamp.
|
| - |
|
591 |
@GetMapping(value = "/cs/downloadMyTickets")
|
| - |
|
592 |
public ResponseEntity<?> downloadMyTickets(HttpServletRequest request,
|
| - |
|
593 |
@RequestParam(name = "orderby", defaultValue = "DESCENDING") SortOrder sortOrder,
|
| - |
|
594 |
@RequestParam(name = "ticketStatus", defaultValue = "OPENED") TicketStatus ticketStatus,
|
| - |
|
595 |
@RequestParam(name = "ticketSearchType", defaultValue = "") TicketSearchType ticketSearchType,
|
| - |
|
596 |
@RequestParam(name = "searchTerm", defaultValue = "0") int searchTerm,
|
| - |
|
597 |
@RequestParam(name = "search", required = false) String searchText) throws Exception {
|
| - |
|
598 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| - |
|
599 |
boolean isAdmin = roleManager.isAdmin(new HashSet<>(loginDetails.getRoleIds()));
|
| - |
|
600 |
AuthUser currentAuthUser = isAdmin ? authRepository.selectByEmailOrMobile(loginDetails.getEmailId()) : null;
|
| - |
|
601 |
boolean isCrmUser = isAdmin && currentAuthUser != null
|
| - |
|
602 |
&& positionRepository.hasCategory(currentAuthUser.getId(), ProfitMandiConstants.TICKET_CATEGORY_CRM);
|
| - |
|
603 |
List<Integer> userCategoryIds = isAdmin && currentAuthUser != null
|
| - |
|
604 |
? positionRepository.selectCategoryIdsByAuthUserId(currentAuthUser.getId())
|
| - |
|
605 |
: Collections.emptyList();
|
| - |
|
606 |
|
| - |
|
607 |
List<Ticket> tickets;
|
| - |
|
608 |
if (isAdmin) {
|
| - |
|
609 |
int authUserId = currentAuthUser.getId();
|
| - |
|
610 |
Optional<Boolean> resolvedOpt = ticketStatus.equals(TicketStatus.RESOLVED)
|
| - |
|
611 |
? Optional.empty()
|
| - |
|
612 |
: Optional.of(TicketStatus.CLOSED.equals(ticketStatus));
|
| - |
|
613 |
long total;
|
| - |
|
614 |
if (isCrmUser) {
|
| - |
|
615 |
total = ticketRepository.selectAllTicketsPaginatedCount(
|
| - |
|
616 |
resolvedOpt, ticketSearchType, searchTerm, searchText, userCategoryIds);
|
| - |
|
617 |
tickets = ticketRepository.selectAllTicketsPaginated(
|
| - |
|
618 |
resolvedOpt, sortOrder, ticketSearchType, searchTerm, searchText,
|
| - |
|
619 |
userCategoryIds, 0, (int) Math.max(1L, total));
|
| - |
|
620 |
} else {
|
| - |
|
621 |
total = ticketRepository.selectAllByAssigneePaginatedCount(
|
| - |
|
622 |
authUserId, resolvedOpt, ticketSearchType, searchTerm, searchText);
|
| - |
|
623 |
tickets = ticketRepository.selectAllByAssigneePaginated(
|
| - |
|
624 |
authUserId, resolvedOpt, sortOrder, ticketSearchType, searchTerm, searchText,
|
| - |
|
625 |
0, (int) Math.max(1L, total));
|
| - |
|
626 |
}
|
| - |
|
627 |
} else {
|
| - |
|
628 |
tickets = ticketRepository.selectAllByCreator(
|
| - |
|
629 |
loginDetails.getFofoId(),
|
| - |
|
630 |
Optional.of(TicketStatus.OPENED.equals(ticketStatus)),
|
| - |
|
631 |
sortOrder);
|
| - |
|
632 |
}
|
| - |
|
633 |
if (tickets == null) tickets = new ArrayList<>();
|
| - |
|
634 |
|
| - |
|
635 |
List<Integer> subCategoryIds = tickets.stream().map(Ticket::getSubCategoryId).collect(Collectors.toList());
|
| - |
|
636 |
Map<Integer, TicketSubCategory> subMap = csService.getSubCategoryIdAndSubCategoryMap(subCategoryIds);
|
| - |
|
637 |
if (subMap == null) subMap = new HashMap<>();
|
| - |
|
638 |
Map<Integer, TicketCategory> catMap = csService.getSubCategoryIdAndCategoryMap(subCategoryIds);
|
| - |
|
639 |
if (catMap == null) catMap = new HashMap<>();
|
| - |
|
640 |
Map<Integer, CustomRetailer> partnerMap = tickets.isEmpty()
|
| - |
|
641 |
? new HashMap<>()
|
| - |
|
642 |
: csService.getPartnerByFofoIds(tickets);
|
| - |
|
643 |
if (partnerMap == null) partnerMap = new HashMap<>();
|
| - |
|
644 |
Map<Integer, List<AuthUser>> assigneeListMap = csService.getAssignedAuthList(tickets);
|
| - |
|
645 |
if (assigneeListMap == null) assigneeListMap = new HashMap<>();
|
| - |
|
646 |
Map<Integer, AuthUser> authUserIdMap = csService.getTicketIdAndAuthUserMapUsingTickets(tickets);
|
| - |
|
647 |
if (authUserIdMap == null) authUserIdMap = new HashMap<>();
|
| - |
|
648 |
|
| - |
|
649 |
List<Integer> ticketIds = tickets.stream().map(Ticket::getId).collect(Collectors.toList());
|
| - |
|
650 |
Map<Integer, Activity> lastMessageMap = ticketIds.isEmpty()
|
| - |
|
651 |
? new HashMap<>()
|
| - |
|
652 |
: csService.getLastMessageActivitiesForTickets(ticketIds);
|
| - |
|
653 |
if (lastMessageMap == null) lastMessageMap = new HashMap<>();
|
| - |
|
654 |
|
| - |
|
655 |
List<Activity> allActs = ticketIds.isEmpty()
|
| - |
|
656 |
? new ArrayList<>()
|
| - |
|
657 |
: activityRepository.selectAll(ticketIds);
|
| - |
|
658 |
Map<Integer, List<Activity>> activityByTicket = allActs.stream()
|
| - |
|
659 |
.collect(Collectors.groupingBy(Activity::getTicketId));
|
| - |
|
660 |
Map<Integer, Activity> activityById = allActs.stream()
|
| - |
|
661 |
.collect(Collectors.toMap(Activity::getId, x -> x, (a, b) -> a));
|
| - |
|
662 |
|
| - |
|
663 |
Set<Integer> creatorIds = new HashSet<>();
|
| - |
|
664 |
for (Activity a : allActs) if (a.getCreatedBy() > 0) creatorIds.add(a.getCreatedBy());
|
| - |
|
665 |
for (Activity a : lastMessageMap.values()) if (a.getCreatedBy() > 0) creatorIds.add(a.getCreatedBy());
|
| - |
|
666 |
Map<Integer, AuthUser> authUserMap = creatorIds.isEmpty()
|
| - |
|
667 |
? new HashMap<>()
|
| - |
|
668 |
: authRepository.selectByIds(new ArrayList<>(creatorIds)).stream()
|
| - |
|
669 |
.collect(Collectors.toMap(AuthUser::getId, x -> x));
|
| - |
|
670 |
|
| - |
|
671 |
List<List<?>> rows = new ArrayList<>();
|
| - |
|
672 |
for (Ticket t : tickets) {
|
| - |
|
673 |
int id = t.getId();
|
| - |
|
674 |
CustomRetailer cr = partnerMap.get(t.getFofoId());
|
| - |
|
675 |
String partner = cr != null ? nvl(cr.getBusinessName()) : "";
|
| - |
|
676 |
TicketCategory cat = catMap.get(t.getSubCategoryId());
|
| - |
|
677 |
String category = cat != null ? nvl(cat.getName()) : "";
|
| - |
|
678 |
TicketSubCategory sub = subMap.get(t.getSubCategoryId());
|
| - |
|
679 |
String subCategory = sub != null ? nvl(sub.getName()) : "";
|
| - |
|
680 |
|
| - |
|
681 |
String message = "-";
|
| - |
|
682 |
Activity msgAct = lastMessageMap.get(id);
|
| - |
|
683 |
if (msgAct == null) {
|
| - |
|
684 |
List<Activity> list = activityByTicket.get(id);
|
| - |
|
685 |
if (list != null && !list.isEmpty()) msgAct = list.get(0);
|
| - |
|
686 |
}
|
| - |
|
687 |
if (msgAct != null) {
|
| - |
|
688 |
String prefix = "~Partner";
|
| - |
|
689 |
if (msgAct.getCreatedBy() > 0 && authUserMap.get(msgAct.getCreatedBy()) != null) {
|
| - |
|
690 |
prefix = "~" + nvl(authUserMap.get(msgAct.getCreatedBy()).getFirstName());
|
| - |
|
691 |
}
|
| - |
|
692 |
message = prefix + " " + nvl(msgAct.getMessage());
|
| - |
|
693 |
}
|
| - |
|
694 |
|
| - |
|
695 |
String createdBy = "Partner";
|
| - |
|
696 |
Activity firstAct = activityById.get(t.getFirstActivityId());
|
| - |
|
697 |
if (firstAct != null && firstAct.getCreatedBy() > 0) {
|
| - |
|
698 |
AuthUser u = authUserMap.get(firstAct.getCreatedBy());
|
| - |
|
699 |
if (u != null) createdBy = (nvl(u.getFirstName()) + " " + nvl(u.getLastName())).trim();
|
| - |
|
700 |
}
|
| - |
|
701 |
|
| - |
|
702 |
String assignedTo = "";
|
| - |
|
703 |
List<AuthUser> assignees = assigneeListMap.get(id);
|
| - |
|
704 |
if (assignees != null && !assignees.isEmpty()) {
|
| - |
|
705 |
assignedTo = assignees.stream()
|
| - |
|
706 |
.map(u -> (nvl(u.getFirstName()) + " " + nvl(u.getLastName())).trim())
|
| - |
|
707 |
.collect(Collectors.joining(", "));
|
| - |
|
708 |
} else if (authUserIdMap.get(id) != null) {
|
| - |
|
709 |
AuthUser a = authUserIdMap.get(id);
|
| - |
|
710 |
assignedTo = (nvl(a.getFirstName()) + " " + nvl(a.getLastName())).trim();
|
| - |
|
711 |
}
|
| - |
|
712 |
|
| - |
|
713 |
String createdTs = t.getFormattedCreateTimestamp() != null ? t.getFormattedCreateTimestamp() : "";
|
| - |
|
714 |
rows.add(Arrays.asList(id, partner, category, subCategory, message, createdBy, assignedTo, createdTs));
|
| - |
|
715 |
}
|
| - |
|
716 |
|
| - |
|
717 |
org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(
|
| - |
|
718 |
Arrays.asList("Ticket Id", "Partner", "Category", "SubCategory", "Message",
|
| - |
|
719 |
"Created By", "Assigned To", "Created TimeStamp"),
|
| - |
|
720 |
rows);
|
| - |
|
721 |
return orderService.downloadReportInCsv(baos, rows, "Tickets_" + ticketStatus.name());
|
| - |
|
722 |
}
|
| - |
|
723 |
|
| 577 |
private void populateMyTicketModel(HttpServletRequest request, SortOrder sortOrder, TicketStatus ticketStatus,
|
724 |
private void populateMyTicketModel(HttpServletRequest request, SortOrder sortOrder, TicketStatus ticketStatus,
|
| 578 |
TicketSearchType ticketSearchType, int searchTerm, int page, int pageSize,
|
725 |
TicketSearchType ticketSearchType, int searchTerm, int page, int pageSize,
|
| 579 |
String searchText, Model model) throws ProfitMandiBusinessException {
|
726 |
String searchText, Model model) throws ProfitMandiBusinessException {
|
| 580 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
727 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| 581 |
List<Ticket> tickets = null;
|
728 |
List<Ticket> tickets = null;
|