Subversion Repositories SmartDukaan

Rev

Rev 23568 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.TException;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.spice.profitmandi.dao.model.LogisticsInfoPojo;
import com.spice.profitmandi.thrift.clients.LogisticsClient;

import in.shop2020.logistics.DeliveryType;
import in.shop2020.logistics.LogisticsInfo;
import in.shop2020.logistics.LogisticsService.Client;
import in.shop2020.logistics.LogisticsServiceException;
import org.springframework.stereotype.Component;


@Component
public class LogisticsService {


        private static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();

        private static final Logger log=LogManager.getLogger(LogisticsService.class);

        public static LogisticsInfoPojo getLogisticsInfo(long itemId, String pinCode){
                LogisticsClient logisticsServiceClient = null;
                long businessDays = -1;
                boolean isOTGAvailable = false;
                String days="-1";
                String codDays="-1";
                boolean isCODAvailable = false;
                LogisticsInfoPojo lip = new LogisticsInfoPojo();

                try {
                        logisticsServiceClient = new LogisticsClient();
                        Client logisticsClient = logisticsServiceClient.getClient();
                        LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pinCode, DeliveryType.PREPAID);

                        if(logistincInfo.getDeliveryTime()!=-1L){
                                days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);
                                if(logistincInfo.isCodAllowed()){
                                        codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);
                                }
                        }
                        businessDays = logistincInfo.getDeliveryTime();
                        isCODAvailable = logistincInfo.isCodAllowed();
                        isOTGAvailable = logistincInfo.isOtgAvailable();
                        lip.setBusinessDays(businessDays);
                        lip.setCodDeliveryEstimate(codDays);
                        lip.setDelivelyEstimate(days);
                        lip.setIsCodAvailableForLocation(isCODAvailable);
                        lip.setOnTimeGuarantee(isOTGAvailable);
                } catch (LogisticsServiceException e)   {
                        days = "-1";
                        isCODAvailable = false;
                        log.error("Unable to get estimate/COD availability for " + itemId + " and pincode : " + pinCode);

                } catch(TException e)   {

                } catch (Exception e)   {

                }
                return lip;

        }

        public static void main(String[] args) throws TException {
                System.out.println(getDeliveryDateString(1, DeliveryType.COD));
                System.out.println(getDeliveryDateString(2, DeliveryType.COD));
                System.out.println(getDeliveryDateString(2, DeliveryType.PREPAID));
                System.out.println(getDeliveryDateString(3, DeliveryType.PREPAID));
        }
        public static String getDeliveryDateString(int days, DeliveryType type) throws TException {
                Calendar now = new GregorianCalendar();
                int hour = now.get(Calendar.HOUR_OF_DAY);
                if(type == DeliveryType.COD && hour < 15){
                        days = days + 1;
                }
                if(businessDayToActualDateMap.containsKey(days)){
                        if(hour != 0){  
                                return businessDayToActualDateMap.get(days);
                        }
                        businessDayToActualDateMap.clear();
                }

                now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

                LogisticsClient logisticsServiceClient = null;
                logisticsServiceClient = new LogisticsClient();
                Client logisticsClient = logisticsServiceClient.getClient();

                int newdays = (int) logisticsClient.adjustDeliveryDays(now.getTimeInMillis(), days);
                now.add(Calendar.DAY_OF_MONTH, newdays);

                SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
                if(newdays == 1){
                        businessDayToActualDateMap.put(days, "Tomorrow, " + dateformat.format(now.getTime()));
                }else{
                        businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
                }
                return businessDayToActualDateMap.get(days);
        }
}