| 34306 |
ranu |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34321 |
ranu |
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
5 |
import com.spice.profitmandi.dao.cart.CartService;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
|
|
8 |
import com.spice.profitmandi.dao.entity.fofo.RbmRating;
|
|
|
9 |
import com.spice.profitmandi.dao.entity.fofo.SalesRating;
|
| 34306 |
ranu |
10 |
import com.spice.profitmandi.dao.entity.transaction.Loan;
|
|
|
11 |
import com.spice.profitmandi.dao.entity.transaction.Transaction;
|
| 34321 |
ranu |
12 |
import com.spice.profitmandi.dao.entity.user.User;
|
|
|
13 |
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
|
|
|
14 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.fofo.RbmRatingRepository;
|
|
|
17 |
import com.spice.profitmandi.dao.repository.fofo.SalesRatingRepository;
|
| 34306 |
ranu |
18 |
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
|
| 34321 |
ranu |
20 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 34308 |
ranu |
21 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
| 34306 |
ranu |
22 |
import org.apache.logging.log4j.LogManager;
|
|
|
23 |
import org.apache.logging.log4j.Logger;
|
|
|
24 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
25 |
import org.springframework.beans.factory.annotation.Value;
|
| 34321 |
ranu |
26 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
27 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 34306 |
ranu |
28 |
import org.springframework.stereotype.Component;
|
|
|
29 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
30 |
|
| 34321 |
ranu |
31 |
import javax.mail.MessagingException;
|
|
|
32 |
import javax.mail.internet.InternetAddress;
|
|
|
33 |
import javax.mail.internet.MimeMessage;
|
|
|
34 |
import java.io.IOException;
|
|
|
35 |
import java.time.LocalDate;
|
| 34306 |
ranu |
36 |
import java.time.LocalDateTime;
|
|
|
37 |
import java.time.temporal.ChronoUnit;
|
|
|
38 |
import java.util.*;
|
| 34321 |
ranu |
39 |
import java.util.stream.Collectors;
|
| 34306 |
ranu |
40 |
|
|
|
41 |
@Component
|
|
|
42 |
@Transactional(rollbackFor = {Throwable.class, ProfitMandiBusinessException.class})
|
|
|
43 |
public class ScheduledTasksTest {
|
|
|
44 |
|
|
|
45 |
private static final Logger LOGGER = LogManager.getLogger(ScheduledTasksTest.class);
|
|
|
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
TransactionRepository transactionRepository;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
LoanRepository loanRepository;
|
|
|
52 |
|
| 34308 |
ranu |
53 |
@Autowired
|
|
|
54 |
SDCreditService sdCreditService;
|
|
|
55 |
|
| 34321 |
ranu |
56 |
@Autowired
|
|
|
57 |
UserRepository userRepository;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
|
|
60 |
CsService csService;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
|
|
63 |
RbmRatingRepository rbmRatingRepository;
|
|
|
64 |
|
|
|
65 |
@Autowired
|
|
|
66 |
private JavaMailSender mailSender;
|
|
|
67 |
|
|
|
68 |
@Autowired
|
|
|
69 |
SalesRatingRepository salesRatingRepository;
|
|
|
70 |
|
|
|
71 |
@Autowired
|
|
|
72 |
FofoStoreRepository fofoStoreRepository;
|
|
|
73 |
|
|
|
74 |
public void test() throws Exception {
|
| 34366 |
ranu |
75 |
System.out.println("test start");
|
|
|
76 |
this.sendRbmFeedbackSummaryEmail();
|
|
|
77 |
this.sendSalesFeedbackSummaryEmail();
|
|
|
78 |
System.out.println("test end");
|
| 34306 |
ranu |
79 |
|
|
|
80 |
}
|
|
|
81 |
|
| 34308 |
ranu |
82 |
public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
|
|
|
83 |
sdCreditService.createLoanForBilling(transactionId, invoiceAmount, invoiceNumber);
|
| 34306 |
ranu |
84 |
|
| 34308 |
ranu |
85 |
}
|
| 34306 |
ranu |
86 |
|
| 34308 |
ranu |
87 |
|
| 34321 |
ranu |
88 |
private void sendMailHtmlFormat(String email[], String body, String cc[], String bcc[], String subject)
|
|
|
89 |
throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
90 |
MimeMessage message = mailSender.createMimeMessage();
|
|
|
91 |
MimeMessageHelper helper = new MimeMessageHelper(message);
|
|
|
92 |
helper.setSubject(subject);
|
|
|
93 |
helper.setText(body, true);
|
|
|
94 |
helper.setTo(email);
|
|
|
95 |
if (cc != null) {
|
|
|
96 |
helper.setCc(cc);
|
|
|
97 |
}
|
|
|
98 |
if (bcc != null) {
|
|
|
99 |
helper.setBcc(bcc);
|
| 34308 |
ranu |
100 |
|
| 34321 |
ranu |
101 |
}
|
|
|
102 |
|
|
|
103 |
InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
|
|
|
104 |
helper.setFrom(senderAddress);
|
|
|
105 |
mailSender.send(message);
|
|
|
106 |
}
|
|
|
107 |
|
| 34307 |
ranu |
108 |
public Map<Integer,Integer> findLoanTransactionMapingAccordingLoan(List<Integer> loanIds) throws ProfitMandiBusinessException {
|
| 34306 |
ranu |
109 |
|
|
|
110 |
Map<Integer, Integer> transactionLoanMap = new HashMap<>();
|
|
|
111 |
|
|
|
112 |
for(int loanId : loanIds){
|
|
|
113 |
Transaction transaction = null;
|
|
|
114 |
Loan loan = loanRepository.selectByLoanId(loanId);
|
|
|
115 |
List<Transaction> transactions = transactionRepository.selectByRetailerId(loan.getFofoId());
|
|
|
116 |
|
|
|
117 |
LocalDateTime nearestDateTime = transactions.stream().map(x -> x.getCreateTimestamp())
|
|
|
118 |
.min(Comparator.comparingLong(x -> Math.abs(ChronoUnit.MILLIS.between(x, loan.getCreatedOn()))))
|
|
|
119 |
.orElse(null);
|
|
|
120 |
|
|
|
121 |
if (nearestDateTime != null && loan.getCreatedOn().plusMinutes(2).isAfter(nearestDateTime) &&
|
|
|
122 |
loan.getCreatedOn().minusMinutes(1).isBefore(nearestDateTime)) {
|
|
|
123 |
// Here transaction is still null
|
|
|
124 |
transaction = transactions.stream()
|
|
|
125 |
.filter(x -> x.getCreateTimestamp().equals(nearestDateTime))
|
|
|
126 |
.findFirst().get();
|
|
|
127 |
transactionLoanMap.put(transaction.getId(), loanId);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
}
|
|
|
131 |
LOGGER.info("transactionLoanMap {}",transactionLoanMap);
|
|
|
132 |
return transactionLoanMap;
|
|
|
133 |
}
|
| 34321 |
ranu |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
public void sendRbmFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
138 |
LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
139 |
LocalDateTime endOfMonth = LocalDateTime.now();
|
| 34323 |
ranu |
140 |
String[] bcc = {"tarun.verma@smartdukaan.com"};
|
| 34321 |
ranu |
141 |
|
|
|
142 |
// Get all RBM users
|
|
|
143 |
List<AuthUser> authUsers = csService.getAuthUserIds(
|
|
|
144 |
ProfitMandiConstants.TICKET_CATEGORY_RBM,
|
|
|
145 |
Arrays.asList(EscalationType.L1)
|
|
|
146 |
);
|
|
|
147 |
|
|
|
148 |
if (authUsers.isEmpty()) {
|
|
|
149 |
LOGGER.info("No RBMs found.");
|
|
|
150 |
return;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
List<Integer> rbmIds = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
|
|
|
154 |
|
|
|
155 |
// Fetch ratings for all RBMs for current month
|
|
|
156 |
List<RbmRating> feedbackList = rbmRatingRepository.selectByRbmIdsAndDateRange(rbmIds, startOfMonth, endOfMonth);
|
|
|
157 |
|
|
|
158 |
if (feedbackList.isEmpty()) {
|
|
|
159 |
LOGGER.info("No feedback entries found for RBMs.");
|
|
|
160 |
return;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// Sort feedback by createTimeStamp DESC
|
|
|
164 |
feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
|
|
|
165 |
|
|
|
166 |
// Fetch and map FOFO (partner) names
|
|
|
167 |
Map<Integer, String> fofoNameMap = new HashMap<>();
|
|
|
168 |
for (RbmRating rating : feedbackList) {
|
|
|
169 |
int fofoId = rating.getFofoId();
|
|
|
170 |
if (!fofoNameMap.containsKey(fofoId)) {
|
|
|
171 |
User fofoUser = userRepository.selectById(fofoId);
|
|
|
172 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
173 |
|
|
|
174 |
String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
|
|
|
175 |
String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
|
|
|
176 |
|
|
|
177 |
String displayName = partnerName + " (" + storeCode + ")";
|
|
|
178 |
fofoNameMap.put(fofoId, displayName);
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
// Map RBM ID to name for quick lookup
|
|
|
183 |
Map<Integer, String> rbmNameMap = authUsers.stream()
|
|
|
184 |
.collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
|
|
|
185 |
|
|
|
186 |
// Generate HTML content
|
|
|
187 |
StringBuilder emailContent = new StringBuilder();
|
|
|
188 |
emailContent.append("<html><body>");
|
|
|
189 |
emailContent.append("<p>Dear Team,</p>");
|
|
|
190 |
emailContent.append("<p>Here is the <b>latest RBM Rating and Feedback Summary</b> for ")
|
|
|
191 |
.append(LocalDate.now().getMonth()).append(":</p>");
|
|
|
192 |
|
|
|
193 |
emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
|
|
|
194 |
emailContent.append("<tr>")
|
|
|
195 |
.append("<th>RBM Name</th>")
|
|
|
196 |
.append("<th>Partner Name</th>")
|
|
|
197 |
.append("<th>Rating</th>")
|
|
|
198 |
.append("<th>Comment</th>")
|
|
|
199 |
.append("<th>Date</th>")
|
|
|
200 |
.append("</tr>");
|
|
|
201 |
|
|
|
202 |
for (RbmRating rating : feedbackList) {
|
|
|
203 |
String rbmName = rbmNameMap.getOrDefault(rating.getRbmId(), "Unknown RBM");
|
|
|
204 |
String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
|
|
|
205 |
emailContent.append("<tr>")
|
|
|
206 |
.append("<td>").append(rbmName).append("</td>")
|
|
|
207 |
.append("<td>").append(partnerName).append("</td>")
|
|
|
208 |
.append("<td>").append(rating.getRating()).append("</td>")
|
|
|
209 |
.append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
|
|
|
210 |
.append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
|
|
|
211 |
.append("</tr>");
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
emailContent.append("</table>");
|
|
|
215 |
emailContent.append("<br><p>Regards,<br>Smart Dukaan Team</p>");
|
|
|
216 |
emailContent.append("</body></html>");
|
|
|
217 |
|
|
|
218 |
String subject = "Monthly RBM Feedback Summary - " + LocalDate.now().getMonth();
|
|
|
219 |
|
|
|
220 |
List<String> sendTo = new ArrayList<>();
|
| 34323 |
ranu |
221 |
sendTo.add("sm@smartdukaan.com"); //
|
|
|
222 |
sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
|
|
|
223 |
sendTo.add("kamini.sharma@smartdukaan.com"); //
|
| 34321 |
ranu |
224 |
|
|
|
225 |
String[] emailRecipients = sendTo.toArray(new String[0]);
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
|
|
|
229 |
|
|
|
230 |
LOGGER.info("Consolidated RBM feedback summary email sent.");
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
public void sendSalesFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
|
|
|
236 |
LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
|
|
|
237 |
LocalDateTime endOfMonth = LocalDateTime.now();
|
| 34323 |
ranu |
238 |
String[] bcc = {"tarun.verma@smartdukaan.com"};
|
| 34321 |
ranu |
239 |
|
|
|
240 |
// Get all RBM users
|
|
|
241 |
List<AuthUser> authUsers = csService.getAuthUserIds(
|
|
|
242 |
ProfitMandiConstants.TICKET_CATEGORY_SALES,
|
|
|
243 |
Arrays.asList(EscalationType.L1)
|
|
|
244 |
);
|
|
|
245 |
|
|
|
246 |
if (authUsers.isEmpty()) {
|
|
|
247 |
LOGGER.info("No sales person found.");
|
|
|
248 |
return;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
List<Integer> salesL1Ids = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
|
|
|
252 |
|
|
|
253 |
// Fetch ratings for all RBMs for current month
|
|
|
254 |
List<SalesRating> feedbackList = salesRatingRepository.selectBySalesL1IdsAndDateRange(salesL1Ids, startOfMonth, endOfMonth);
|
|
|
255 |
|
|
|
256 |
if (feedbackList.isEmpty()) {
|
|
|
257 |
LOGGER.info("No feedback entries found for Sales.");
|
|
|
258 |
return;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
// Sort feedback by createTimeStamp DESC
|
|
|
262 |
feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
|
|
|
263 |
|
|
|
264 |
// Fetch and map FOFO (partner) names
|
|
|
265 |
Map<Integer, String> fofoNameMap = new HashMap<>();
|
|
|
266 |
for (SalesRating rating : feedbackList) {
|
|
|
267 |
int fofoId = rating.getFofoId();
|
|
|
268 |
if (!fofoNameMap.containsKey(fofoId)) {
|
|
|
269 |
User fofoUser = userRepository.selectById(fofoId);
|
|
|
270 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
|
|
271 |
|
|
|
272 |
String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
|
|
|
273 |
String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
|
|
|
274 |
|
|
|
275 |
String displayName = partnerName + " (" + storeCode + ")";
|
|
|
276 |
fofoNameMap.put(fofoId, displayName);
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
// Map RBM ID to name for quick lookup
|
|
|
281 |
Map<Integer, String> salesL1NameMap = authUsers.stream()
|
|
|
282 |
.collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
|
|
|
283 |
|
|
|
284 |
// Generate HTML content
|
|
|
285 |
StringBuilder emailContent = new StringBuilder();
|
|
|
286 |
emailContent.append("<html><body>");
|
|
|
287 |
emailContent.append("<p>Dear Team,</p>");
|
|
|
288 |
emailContent.append("<p>Here is the <b>latest Sales L1 Rating and Feedback Summary</b> for ")
|
|
|
289 |
.append(LocalDate.now().getMonth()).append(":</p>");
|
|
|
290 |
|
|
|
291 |
emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
|
|
|
292 |
emailContent.append("<tr>")
|
|
|
293 |
.append("<th>Sales L1 Name</th>")
|
|
|
294 |
.append("<th>Partner Name</th>")
|
|
|
295 |
.append("<th>Rating</th>")
|
|
|
296 |
.append("<th>Comment</th>")
|
|
|
297 |
.append("<th>Date</th>")
|
|
|
298 |
.append("</tr>");
|
|
|
299 |
|
|
|
300 |
for (SalesRating rating : feedbackList) {
|
|
|
301 |
String salesL1 = salesL1NameMap.getOrDefault(rating.getSalesL1Id(), "Unknown Sales Person");
|
|
|
302 |
String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
|
|
|
303 |
emailContent.append("<tr>")
|
|
|
304 |
.append("<td>").append(salesL1).append("</td>")
|
|
|
305 |
.append("<td>").append(partnerName).append("</td>")
|
|
|
306 |
.append("<td>").append(rating.getRating()).append("</td>")
|
|
|
307 |
.append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
|
|
|
308 |
.append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
|
|
|
309 |
.append("</tr>");
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
emailContent.append("</table>");
|
|
|
313 |
emailContent.append("<br><p>Regards,<br>Smartdukaan Team</p>");
|
|
|
314 |
emailContent.append("</body></html>");
|
|
|
315 |
|
|
|
316 |
String subject = "Monthly Sales L1 Feedback Summary - " + LocalDate.now().getMonth();
|
|
|
317 |
|
|
|
318 |
List<String> sendTo = new ArrayList<>();
|
| 34323 |
ranu |
319 |
sendTo.add("sm@smartdukaan.com"); //
|
|
|
320 |
sendTo.add("chiranjib.sarkar@smartdukaan.com"); //
|
|
|
321 |
sendTo.add("kamini.sharma@smartdukaan.com"); //
|
| 34321 |
ranu |
322 |
|
|
|
323 |
String[] emailRecipients = sendTo.toArray(new String[0]);
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
|
|
|
327 |
|
|
|
328 |
LOGGER.info("Consolidated Sales L1 feedback summary email sent.");
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
| 34306 |
ranu |
334 |
}
|