Subversion Repositories SmartDukaan

Rev

Rev 23626 | Rev 23662 | 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.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.RechargeStatus;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.model.RechargeCredential;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
import com.spice.profitmandi.dao.entity.fofo.Purchase;
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
import com.spice.profitmandi.service.recharge.provider.OxigenRechargeProviderService;
import com.spice.profitmandi.service.recharge.provider.ThinkWalnutDigitalRechargeProviderService;
import com.spice.profitmandi.service.scheme.SchemeService;
import com.spice.profitmandi.web.util.MVCResponseSender;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class CronController {
        
        @Value("${oxigen.recharge.transaction.url}")
        private String oxigenRechargeTransactionUrl;
        
        @Value("${oxigen.recharge.enquiry.url}")
        private String oxigenRechargeEnquiryUrl;
        
        @Value("${oxigen.recharge.auth.key}")
        private String oxigenRechargeAuthKey;
        
        @Value("${oxigen.recharge.validation.url}")
        private String oxigenRechargeValidationUrl;
        
        @Value("${oxigen.recharge.validation.auth.key}")
        private String oxigenRechargeValidationAuthKey;
        
        @Value("${think.walnut.digital.recharge.transaction.mobile.url}")
        private String thinkWalnutDigitalRechargeTransactionMobileUrl;
        
        @Value("${think.walnut.digital.recharge.transaction.dth.url}")
        private String thinkWalnutDigitalRechargeTransactionDthUrl;
        
        @Value("${think.walnut.digital.recharge.enquiry.url}")
        private String thinkWalnutDigitalRechargeEnquiryUrl;
        
        @Value("${think.walnut.digital.recharge.balance.url}")
        private String thinkWalnutDigitalRechargeBalanceUrl;
        
        @Value("${think.walnut.digital.recharge.username}")
        private String thinkWalnutDigitalRechargeUserName;
        
        @Value("${think.walnut.digital.recharge.password}")
        private String thinkWalnutDigitalRechargePassword;
        
        @Value("${think.walnut.digital.recharge.auth.key}")
        private String thinkWalnutDigitalRechargeAuthKey;

        @Autowired
        PurchaseRepository purchaseRepository;

        @Autowired
        SchemeService schemeService;

        @Autowired
        RechargeTransactionRepository rechargeTransactionRepository;
        
        @Autowired
        FofoOrderRepository fofoOrderRepository;
        
        @Autowired
        ThinkWalnutDigitalRechargeProviderService thinkWalnutDigitalRechargeProviderService;

        @Autowired
        OxigenRechargeProviderService oxigenRechargeProviderService; 
        
        @Autowired
        private RestClient restClient;

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

        private static final Logger LOGGER = LogManager.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/reconcile-recharge", method = RequestMethod.GET)
        public String reconcileRecharge(Model model) throws Exception {
                LocalDateTime fromDate = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(30);
                LocalDateTime toDate = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
                List<RechargeStatus> nonSuccessRechargeStatuses = new ArrayList<>(Arrays.asList(RechargeStatus.values()));
                LOGGER.info("nonSuccessRechargeStatuses {} ", nonSuccessRechargeStatuses);
                nonSuccessRechargeStatuses.remove(RechargeStatus.SUCCESS);
                nonSuccessRechargeStatuses.remove(RechargeStatus.FAILED);
                RechargeCredential thinkWalnutDigitalRechargeEnquiryCredential = new RechargeCredential();
                thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUrl(thinkWalnutDigitalRechargeEnquiryUrl);
                thinkWalnutDigitalRechargeEnquiryCredential.setRechargeUserName(thinkWalnutDigitalRechargeUserName);
                thinkWalnutDigitalRechargeEnquiryCredential.setRechargePassword(thinkWalnutDigitalRechargePassword);
                thinkWalnutDigitalRechargeEnquiryCredential.setRechargeAuthKey(thinkWalnutDigitalRechargeAuthKey);
                Map<String, RechargeStatus> requestRechargeStatusChanged = new HashMap<>();
                List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllBetweenTimestamp(nonSuccessRechargeStatuses,fromDate, toDate);
                for(RechargeTransaction rechargeTransaction : rechargeTransactions) {
                        try {
                                int providerId = rechargeTransaction.getProviderId();
                                if(providerId==1) {
                                        oxigenRechargeProviderService.doCheckStatusRequest(oxigenRechargeEnquiryUrl, oxigenRechargeAuthKey, rechargeTransaction);
                                } else if(providerId==2) {
                                        thinkWalnutDigitalRechargeProviderService.doCheckStatusRequest(thinkWalnutDigitalRechargeEnquiryCredential, rechargeTransaction);
                                }
                                if(rechargeTransaction.getStatus().equals(RechargeStatus.SUCCESS) || rechargeTransaction.getStatus().equals(RechargeStatus.FAILED)) {
                                        requestRechargeStatusChanged.put(rechargeTransaction.getRequestId(), rechargeTransaction.getStatus());
                                }
                        } catch (Exception e) {
                                LOGGER.info("Could not check status for Request {}", rechargeTransaction.getRequestId());
                        }
                }
                model.addAttribute("response", mvcResponseSender.createResponseString(requestRechargeStatusChanged));
                return "response";
        }
        
        @RequestMapping(value = "/cron/process-schemes", method = RequestMethod.GET)
        public String createScheme(HttpServletRequest request, Model model) throws Exception {
                LocalDateTime fromDate = LocalDateTime.now().minusDays(15);
                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";
        }

}