Subversion Repositories SmartDukaan

Rev

Rev 23558 | Rev 23564 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
import com.spice.profitmandi.dao.entity.fofo.Purchase;
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
import com.spice.profitmandi.service.scheme.SchemeService;
import com.spice.profitmandi.web.util.MVCResponseSender;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class CronController {

        @Autowired
        PurchaseRepository purchaseRepository;

        @Autowired
        SchemeService schemeService;

        @Autowired
        FofoOrderRepository fofoOrderRepository;
        
        @Autowired
        private RestClient restClient;

        @Autowired
        private MVCResponseSender mvcResponseSender;
        
        @Value("${prod}")
        private boolean prod;
        

        private static final Logger LOGGER = LoggerFactory.getLogger(CronController.class);

        @Scheduled(cron = "0 45 6 * * *")
        public void executeJob() throws Exception {
                if(prod) {
                        String uri = "/cron/process-schemes";
                        restClient.get(SchemeType.HTTP, "localhost", 8080, uri, null);
                }
        }
        
        @RequestMapping(value = "/cron/process-schemes", method = RequestMethod.GET)
        public String createScheme(HttpServletRequest request, Model model) throws Exception {
                LocalDateTime fromDate = LocalDateTime.now().minusDays(45);
                LOGGER.info("Started execution at {}", LocalDateTime.now());
                List<Purchase> purchases = purchaseRepository.selectFromPurchaseCompleteDate(fromDate);
                for (Purchase purchase : purchases) {
                        try {
                                schemeService.processSchemeIn(purchase.getId(), purchase.getFofoId());
                        } catch (Exception e) {
                                LOGGER.error("Error while processing purchase {} for scheme In ", purchase.getId());
                                e.printStackTrace();
                                
                        }
                }
                List<FofoOrder> fofoOrders = fofoOrderRepository.selectFromSaleDate(fromDate);
                for (FofoOrder fofoOrder: fofoOrders) {
                        try {
                                schemeService.processSchemeOut(fofoOrder.getId(), fofoOrder.getFofoId());
                        } catch (Exception e) {
                                LOGGER.error("Error while processing sale order {} for scheme Out", fofoOrder.getId());
                        }
                }
                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "response";
        }

}