Untitled

mail@pastecode.io avatar
unknown
java
a month ago
2.9 kB
1
Indexable
Never
@Scheduled(cron = "${shop.earn.approved.orders.report.scheduler}")
public void shopAndEarnApprovedOrderMailSend() {
    LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler started");

    String[] header = new String[]{"Retailer Id", "Retailer Mobile Number", "Order id", "Total Commission", "TDS", "GST", "Final amount", "Created at"};
    HashMap<Integer, String[]> approvedOrderRecords = new HashMap<>();

    try {
        processApprovedOrders(header, approvedOrderRecords);
    } catch (IOException e) {
        handleIOException(e);
    } catch (Exception e) {
        handleException(e);
    } finally {
        LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler Completed");
    }
}

private void processApprovedOrders(String[] header, HashMap<Integer, String[]> approvedOrderRecords) throws IOException {
    approvedOrderRecords.put(0, header);

    ConfigParam configParamShopAndEarnReportMailReceivers = getConfigParamShopAndEarnReportMailReceivers();

    Long currentDateTime = commonUtils.generateLongTimeStamp(LocalDateTime.now());
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime fifteenDaysAgo = now.minusDays(15);

    List<AffiliateOrdersReport> approvedOrders = getApprovedOrders(now, fifteenDaysAgo);

    sendEmailsAndDeleteFiles(configParamShopAndEarnReportMailReceivers, currentDateTime, approvedOrders);
}

private ConfigParam getConfigParamShopAndEarnReportMailReceivers() {
    return configParamRepository.findByNameIgnoreCase("paynearby_mall_approved_orders_report").orElse(null);
}

private List<AffiliateOrdersReport> getApprovedOrders(LocalDateTime now, LocalDateTime fifteenDaysAgo) {
    return affiliaOrdersReportRepository.findByStatusApproved(now, fifteenDaysAgo);
}

private void sendEmailsAndDeleteFiles(ConfigParam configParamShopAndEarnReportMailReceivers, Long currentDateTime, List<AffiliateOrdersReport> approvedOrders) throws IOException {
    if (configParamShopAndEarnReportMailReceivers != null && !StringUtils.isEmpty(configParamShopAndEarnReportMailReceivers.getValue())) {
        if (approvedOrders.isEmpty()) {
            LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler Today no entry found");
        } else {
            LOGGER.info("[ApprovedOrderReportMailProcess]ShopAndEarnApprovedOrderMailSend] Scheduler previous day entry found  ");

            int index = 1;
            for (AffiliateOrdersReport ordersReport : approvedOrders) {
                processOrdersReport(approvedOrderRecords, index, ordersReport);
                index++;
            }

            createAndWriteExcelFile(currentDateTime, approvedOrderRecords);

            sendEmailWithAttachment(configParamShopAndEarnReportMailReceivers, currentDateTime);

            deleteFiles(currentDateTime);
        }
    } else {
        LOGGER.info("[ApprovedOrderReport
Leave a Comment