Subversion Repositories SmartDukaan

Rev

Rev 34308 | Rev 34323 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34308 Rev 34321
Line 1... Line 1...
1
package com.smartdukaan.cron.scheduled;
1
package com.smartdukaan.cron.scheduled;
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.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;
4
import com.spice.profitmandi.dao.entity.transaction.Loan;
10
import com.spice.profitmandi.dao.entity.transaction.Loan;
5
import com.spice.profitmandi.dao.entity.transaction.Transaction;
11
import com.spice.profitmandi.dao.entity.transaction.Transaction;
-
 
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;
6
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
18
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
7
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
19
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
-
 
20
import com.spice.profitmandi.dao.repository.user.UserRepository;
8
import com.spice.profitmandi.service.transaction.SDCreditService;
21
import com.spice.profitmandi.service.transaction.SDCreditService;
9
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
23
import org.apache.logging.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Value;
25
import org.springframework.beans.factory.annotation.Value;
-
 
26
import org.springframework.mail.javamail.JavaMailSender;
-
 
27
import org.springframework.mail.javamail.MimeMessageHelper;
13
import org.springframework.stereotype.Component;
28
import org.springframework.stereotype.Component;
14
import org.springframework.transaction.annotation.Transactional;
29
import org.springframework.transaction.annotation.Transactional;
15
 
30
 
-
 
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;
16
import java.time.LocalDateTime;
36
import java.time.LocalDateTime;
17
import java.time.temporal.ChronoUnit;
37
import java.time.temporal.ChronoUnit;
18
import java.util.*;
38
import java.util.*;
-
 
39
import java.util.stream.Collectors;
19
 
40
 
20
@Component
41
@Component
21
@Transactional(rollbackFor = {Throwable.class, ProfitMandiBusinessException.class})
42
@Transactional(rollbackFor = {Throwable.class, ProfitMandiBusinessException.class})
22
public class ScheduledTasksTest {
43
public class ScheduledTasksTest {
23
 
44
 
Line 30... Line 51...
30
    LoanRepository loanRepository;
51
    LoanRepository loanRepository;
31
 
52
 
32
    @Autowired
53
    @Autowired
33
    SDCreditService sdCreditService;
54
    SDCreditService sdCreditService;
34
 
55
 
-
 
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
 
35
    public void test(List<Integer> loanIds) throws Exception {
74
    public void test() throws Exception {
36
        System.out.println("test start");
75
        System.out.println("test start");
-
 
76
        this.sendRbmFeedbackSummaryEmail();
37
        this.findLoanTransactionMapingAccordingLoan(loanIds);
77
        this.sendSalesFeedbackSummaryEmail();
38
        System.out.println("test end");
78
        System.out.println("test end");
39
 
79
 
40
    }
80
    }
41
 
81
 
42
    public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
82
    public void createLoanForBillingByTransactionIdAndInvoiceNumber(int transactionId, double invoiceAmount, String invoiceNumber) throws Exception {
43
        sdCreditService.createLoanForBilling(transactionId, invoiceAmount, invoiceNumber);
83
        sdCreditService.createLoanForBilling(transactionId, invoiceAmount, invoiceNumber);
44
 
84
 
45
    }
85
    }
46
 
86
 
47
 
87
 
-
 
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);
-
 
100
 
-
 
101
        }
-
 
102
 
-
 
103
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smart Dukaan");
-
 
104
        helper.setFrom(senderAddress);
-
 
105
        mailSender.send(message);
-
 
106
    }
48
 
107
 
49
    public Map<Integer,Integer> findLoanTransactionMapingAccordingLoan(List<Integer> loanIds) throws ProfitMandiBusinessException {
108
    public Map<Integer,Integer> findLoanTransactionMapingAccordingLoan(List<Integer> loanIds) throws ProfitMandiBusinessException {
50
 
109
 
51
        Map<Integer, Integer> transactionLoanMap = new HashMap<>();
110
        Map<Integer, Integer> transactionLoanMap = new HashMap<>();
52
 
111
 
Line 70... Line 129...
70
 
129
 
71
        }
130
        }
72
        LOGGER.info("transactionLoanMap {}",transactionLoanMap);
131
        LOGGER.info("transactionLoanMap {}",transactionLoanMap);
73
        return transactionLoanMap;
132
        return transactionLoanMap;
74
    }
133
    }
-
 
134
 
-
 
135
 
-
 
136
 
-
 
137
    public void sendRbmFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
-
 
138
        LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
-
 
139
        LocalDateTime endOfMonth = LocalDateTime.now();
-
 
140
        String[] bcc = {"ranu.rajput@smartdukaan.com"};
-
 
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<>();
-
 
221
        sendTo.add("ranu.rajput@smartdukaan.com");
-
 
222
        // sendTo.add("tejus.lohani@smartdukaan.com"); //
-
 
223
 
-
 
224
        String[] emailRecipients = sendTo.toArray(new String[0]);
-
 
225
 
-
 
226
 
-
 
227
        this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
-
 
228
 
-
 
229
        LOGGER.info("Consolidated RBM feedback summary email sent.");
-
 
230
    }
-
 
231
 
-
 
232
 
-
 
233
 
-
 
234
    public void sendSalesFeedbackSummaryEmail() throws MessagingException, ProfitMandiBusinessException, IOException {
-
 
235
        LocalDateTime startOfMonth = LocalDate.now().withDayOfMonth(1).atStartOfDay();
-
 
236
        LocalDateTime endOfMonth = LocalDateTime.now();
-
 
237
        String[] bcc = {"ranu.rajput@smartdukaan.com"};
-
 
238
 
-
 
239
        // Get all RBM users
-
 
240
        List<AuthUser> authUsers = csService.getAuthUserIds(
-
 
241
                ProfitMandiConstants.TICKET_CATEGORY_SALES,
-
 
242
                Arrays.asList(EscalationType.L1)
-
 
243
        );
-
 
244
 
-
 
245
        if (authUsers.isEmpty()) {
-
 
246
            LOGGER.info("No sales person found.");
-
 
247
            return;
-
 
248
        }
-
 
249
 
-
 
250
        List<Integer> salesL1Ids = authUsers.stream().map(AuthUser::getId).collect(Collectors.toList());
-
 
251
 
-
 
252
        // Fetch ratings for all RBMs for current month
-
 
253
        List<SalesRating> feedbackList = salesRatingRepository.selectBySalesL1IdsAndDateRange(salesL1Ids, startOfMonth, endOfMonth);
-
 
254
 
-
 
255
        if (feedbackList.isEmpty()) {
-
 
256
            LOGGER.info("No feedback entries found for Sales.");
-
 
257
            return;
-
 
258
        }
-
 
259
 
-
 
260
        // Sort feedback by createTimeStamp DESC
-
 
261
        feedbackList.sort((a, b) -> b.getCreateTimeStamp().compareTo(a.getCreateTimeStamp()));
-
 
262
 
-
 
263
        // Fetch and map FOFO (partner) names
-
 
264
        Map<Integer, String> fofoNameMap = new HashMap<>();
-
 
265
        for (SalesRating rating : feedbackList) {
-
 
266
            int fofoId = rating.getFofoId();
-
 
267
            if (!fofoNameMap.containsKey(fofoId)) {
-
 
268
                User fofoUser = userRepository.selectById(fofoId);
-
 
269
                FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
-
 
270
 
-
 
271
                String partnerName = fofoUser != null ? fofoUser.getName() : "Unknown Partner";
-
 
272
                String storeCode = fofoStore != null ? fofoStore.getCode() : "Unknown Code";
-
 
273
 
-
 
274
                String displayName = partnerName + " (" + storeCode + ")";
-
 
275
                fofoNameMap.put(fofoId, displayName);
-
 
276
            }
-
 
277
        }
-
 
278
 
-
 
279
        // Map RBM ID to name for quick lookup
-
 
280
        Map<Integer, String> salesL1NameMap = authUsers.stream()
-
 
281
                .collect(Collectors.toMap(AuthUser::getId, AuthUser::getFullName));
-
 
282
 
-
 
283
        // Generate HTML content
-
 
284
        StringBuilder emailContent = new StringBuilder();
-
 
285
        emailContent.append("<html><body>");
-
 
286
        emailContent.append("<p>Dear Team,</p>");
-
 
287
        emailContent.append("<p>Here is the <b>latest Sales L1 Rating and Feedback Summary</b> for ")
-
 
288
                .append(LocalDate.now().getMonth()).append(":</p>");
-
 
289
 
-
 
290
        emailContent.append("<table border='1' cellspacing='0' cellpadding='5'>");
-
 
291
        emailContent.append("<tr>")
-
 
292
                .append("<th>Sales L1 Name</th>")
-
 
293
                .append("<th>Partner Name</th>")
-
 
294
                .append("<th>Rating</th>")
-
 
295
                .append("<th>Comment</th>")
-
 
296
                .append("<th>Date</th>")
-
 
297
                .append("</tr>");
-
 
298
 
-
 
299
        for (SalesRating rating : feedbackList) {
-
 
300
            String salesL1 = salesL1NameMap.getOrDefault(rating.getSalesL1Id(), "Unknown Sales Person");
-
 
301
            String partnerName = fofoNameMap.getOrDefault(rating.getFofoId(), "Unknown Partner");
-
 
302
            emailContent.append("<tr>")
-
 
303
                    .append("<td>").append(salesL1).append("</td>")
-
 
304
                    .append("<td>").append(partnerName).append("</td>")
-
 
305
                    .append("<td>").append(rating.getRating()).append("</td>")
-
 
306
                    .append("<td>").append(rating.getComment() != null ? rating.getComment() : "-").append("</td>")
-
 
307
                    .append("<td>").append(rating.getCreateTimeStamp().toLocalDate()).append("</td>")
-
 
308
                    .append("</tr>");
-
 
309
        }
-
 
310
 
-
 
311
        emailContent.append("</table>");
-
 
312
        emailContent.append("<br><p>Regards,<br>Smartdukaan Team</p>");
-
 
313
        emailContent.append("</body></html>");
-
 
314
 
-
 
315
        String subject = "Monthly Sales L1 Feedback Summary - " + LocalDate.now().getMonth();
-
 
316
 
-
 
317
        List<String> sendTo = new ArrayList<>();
-
 
318
        sendTo.add("ranu.rajput@smartdukaan.com");
-
 
319
        // sendTo.add("tejus.lohani@smartdukaan.com"); //
-
 
320
 
-
 
321
        String[] emailRecipients = sendTo.toArray(new String[0]);
-
 
322
 
-
 
323
 
-
 
324
        this.sendMailHtmlFormat(emailRecipients, emailContent.toString(), null, bcc, subject);
-
 
325
 
-
 
326
        LOGGER.info("Consolidated Sales L1 feedback summary email sent.");
-
 
327
    }
-
 
328
 
-
 
329
 
-
 
330
 
-
 
331
 
75
}
332
}