Subversion Repositories SmartDukaan

Rev

Rev 34554 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.scheduled.b2b;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.catalog.Item;
import com.spice.profitmandi.dao.entity.dtr.WebListing;
import com.spice.profitmandi.dao.entity.dtr.WebProductListing;
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
import com.spice.profitmandi.dao.repository.catalog.*;
import com.spice.profitmandi.dao.repository.dtr.WebListingRepository;
import com.spice.profitmandi.dao.repository.dtr.WebProductListingRepository;
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
import com.spice.profitmandi.dao.service.solr.FofoSolr;
import in.shop2020.model.v1.order.OrderStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Component
@Transactional(rollbackFor = Throwable.class)
public class Listing {
    @Autowired
    private WebListingRepository webListingRepository;

    @Autowired
    private FofoSolr fofoSolr;

    @Autowired
    OrderRepository orderRepository;

    @Autowired
    ItemRepository itemRepository;

    @Autowired
    PriceDropRepository priceDropRepository;

    @Autowired
    CustomerOfferRepository customerOfferRepository;

    @Autowired
    CustomerOfferItemRepository customerOfferItemRepository;

    @Autowired
    SchemeRepository schemeRepository;

    @Autowired
    TagListingRepository tagListingRepository;


    private static final Logger LOGGER = LogManager.getLogger(Listing.class);

    @Autowired
    private WebProductListingRepository webProductListingRepository;


    public void pushDataToSolr() throws Exception {
        fofoSolr.pushData();
    }

    public void getBestSeller() throws ProfitMandiBusinessException {
        WebListing webListing = webListingRepository.selectByUrl("partner-best-sellers");

        List<WebProductListing> webProductListings = webProductListingRepository.selectAllByWebListingId(webListing.getId());

        List<Integer> catalogItemIds = orderRepository.selectAllOrderWeight(LocalDateTime.now().minusDays(30), LocalDateTime.now(), OrderStatus.DELIVERY_SUCCESS);

        LOGGER.info("catalogItemIds {}", catalogItemIds);
        if (!catalogItemIds.isEmpty()) {
            webProductListingRepository.deleteByEqual(webListing.getId());
            int count = 0;
            for (Integer catalogItemId : catalogItemIds) {

                WebProductListing webProductListing = new WebProductListing();
                webProductListing.setEntityId(catalogItemId);
                webProductListing.setWebListingId(webListing.getId());
                count = count + 1;

                webProductListing.setRank(count + 1);
                webProductListingRepository.persist(webProductListing);

            }

        }
    }

    public void getPriceDrop() throws ProfitMandiBusinessException {
        WebListing webListing = webListingRepository.selectByUrl("partner-price-drop");

        Set<Integer> catalogItemIds = priceDropRepository.selectAllByDatesBetweenSortByDate(LocalDateTime.now().minusDays(30), LocalDateTime.now()).stream().filter(x -> x.getAmount() > 0).map(x -> x.getCatalogItemId()).collect(Collectors.toSet());

        LOGGER.info("catalogItemIds {}", catalogItemIds);
        if (!catalogItemIds.isEmpty()) {
            webProductListingRepository.deleteByEqual(webListing.getId());
            int count = 0;
            for (Integer catalogItemId : catalogItemIds) {

                WebProductListing webProductListing = new WebProductListing();
                webProductListing.setEntityId(catalogItemId);
                webProductListing.setWebListingId(webListing.getId());
                count = count + 1;
                webProductListing.setRank(count);
                webProductListingRepository.persist(webProductListing);

            }

        }
    }

    public void getNewLaunches() throws ProfitMandiBusinessException {
        WebListing webListing = webListingRepository.selectByUrl("new-launches");

        List<Integer> catalogItemIds = tagListingRepository.getRecentLanuch(LocalDate.now().minusDays(30), LocalDate.now().plusDays(1));

        if (!catalogItemIds.isEmpty()) {
            webProductListingRepository.deleteByEqual(webListing.getId());
            int count = 0;
            for (Integer catalogItemId : catalogItemIds) {

                WebProductListing webProductListing = new WebProductListing();
                webProductListing.setEntityId(catalogItemId);
                webProductListing.setWebListingId(webListing.getId());
                count = count + 1;
                webProductListing.setRank(count);
                webProductListingRepository.persist(webProductListing);

            }

        }
    }

    public void getSpecialSupport() throws ProfitMandiBusinessException {
        WebListing webListing = webListingRepository.selectByUrl("special-support");

        List<Integer> catalogItemIds = schemeRepository.getActiveScheme(SchemeType.SPECIAL_SUPPORT, LocalDate.now().plusDays(1));

        LOGGER.info("catalogItemIds {}", catalogItemIds);
        if (!catalogItemIds.isEmpty()) {
            webProductListingRepository.deleteByEqual(webListing.getId());
            int count = 0;
            for (Integer catalogItemId : catalogItemIds) {

                WebProductListing webProductListing = new WebProductListing();
                webProductListing.setEntityId(catalogItemId);
                webProductListing.setWebListingId(webListing.getId());
                count = count + 1;
                webProductListing.setRank(count);
                webProductListingRepository.persist(webProductListing);

            }

        }
    }

    public void getUpgradeOffer() throws ProfitMandiBusinessException {
        WebListing webListing = webListingRepository.selectByUrl("upgrade-offer");
        webProductListingRepository.deleteByEqual(webListing.getId());

        List<Integer> customerOfferIds = customerOfferRepository.selectOffers(LocalDate.now()).stream().map(x -> x.getId()).collect(Collectors.toList());

        List<Integer> catalogItemIds = customerOfferItemRepository.selectByOfferIdsRange(customerOfferIds, LocalDate.now()).stream().map(x -> x.getCatalogId()).distinct().collect(Collectors.toList());

        LOGGER.info("catalogItemIds {}", catalogItemIds);
        if (!catalogItemIds.isEmpty()) {
            int count = 0;
            for (Integer catalogItemId : catalogItemIds) {

                WebProductListing webProductListing = new WebProductListing();
                webProductListing.setEntityId(catalogItemId);
                webProductListing.setWebListingId(webListing.getId());
                count = count + 1;
                webProductListing.setRank(count);
                webProductListingRepository.persist(webProductListing);

            }

        }
    }

    @Scheduled(cron = "0 */30 * * * *")
    public void scheduledPushDataToSolr() throws Throwable {
        this.pushDataToSolr();
    }

    @Scheduled(cron = "0 0 8 * * *")
    public void scheduledGetBestSeller() throws Throwable {
        this.getBestSeller();
    }

    @Scheduled(cron = "0 0 8 * * *")
    public void scheduledGetPriceDrop() throws Throwable {
        this.getPriceDrop();
    }

    @Scheduled(cron = "0 0 8 * * *")
    public void scheduledGetNewLaunches() throws Throwable {
        this.getNewLaunches();
    }

    @Scheduled(cron = "0 0 8 * * *")
    public void scheduledGetSpecialSupport() throws Throwable {
        this.getSpecialSupport();
    }

    @Scheduled(cron = "0 0 8 * * *")
    public void scheduledGetUpgradeOffer() throws Throwable {
        this.getUpgradeOffer();
    }
}