Subversion Repositories SmartDukaan

Rev

Rev 23568 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21545 ashik.ali 1
package com.spice.profitmandi.dao.util;
2
import java.text.SimpleDateFormat;
3
import java.util.Calendar;
4
import java.util.GregorianCalendar;
5
import java.util.HashMap;
6
import java.util.Map;
7
 
8
import org.apache.thrift.TException;
23568 govind 9
import org.apache.logging.log4j.Logger;
10
import org.apache.logging.log4j.LogManager;
21545 ashik.ali 11
 
12
import com.spice.profitmandi.dao.model.LogisticsInfoPojo;
13
import com.spice.profitmandi.thrift.clients.LogisticsClient;
14
 
15
import in.shop2020.logistics.DeliveryType;
16
import in.shop2020.logistics.LogisticsInfo;
17
import in.shop2020.logistics.LogisticsService.Client;
18
import in.shop2020.logistics.LogisticsServiceException;
33256 amit.gupta 19
import org.springframework.stereotype.Component;
21545 ashik.ali 20
 
21
 
33256 amit.gupta 22
@Component
21545 ashik.ali 23
public class LogisticsService {
24
 
25
 
26
	private static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();
27
 
23568 govind 28
	private static final Logger log=LogManager.getLogger(LogisticsService.class);
21545 ashik.ali 29
 
30
	public static LogisticsInfoPojo getLogisticsInfo(long itemId, String pinCode){
31
		LogisticsClient logisticsServiceClient = null;
32
		long businessDays = -1;
33
		boolean isOTGAvailable = false;
34
		String days="-1";
35
		String codDays="-1";
36
		boolean isCODAvailable = false;
37
		LogisticsInfoPojo lip = new LogisticsInfoPojo();
38
 
39
		try {
40
			logisticsServiceClient = new LogisticsClient();
41
			Client logisticsClient = logisticsServiceClient.getClient();
42
			LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pinCode, DeliveryType.PREPAID);
43
 
44
			if(logistincInfo.getDeliveryTime()!=-1L){
45
				days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);
46
				if(logistincInfo.isCodAllowed()){
47
					codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);
48
				}
49
			}
50
			businessDays = logistincInfo.getDeliveryTime();
51
			isCODAvailable = logistincInfo.isCodAllowed();
52
			isOTGAvailable = logistincInfo.isOtgAvailable();
53
			lip.setBusinessDays(businessDays);
54
			lip.setCodDeliveryEstimate(codDays);
55
			lip.setDelivelyEstimate(days);
56
			lip.setIsCodAvailableForLocation(isCODAvailable);
57
			lip.setOnTimeGuarantee(isOTGAvailable);
58
		} catch (LogisticsServiceException e)	{
59
			days = "-1";
60
			isCODAvailable = false;
61
			log.error("Unable to get estimate/COD availability for " + itemId + " and pincode : " + pinCode);
62
 
63
		} catch(TException e)	{
64
 
65
		} catch (Exception e)	{
66
 
67
		}
68
		return lip;
69
 
70
	}
71
 
72
	public static void main(String[] args) throws TException {
73
		System.out.println(getDeliveryDateString(1, DeliveryType.COD));
74
		System.out.println(getDeliveryDateString(2, DeliveryType.COD));
75
		System.out.println(getDeliveryDateString(2, DeliveryType.PREPAID));
76
		System.out.println(getDeliveryDateString(3, DeliveryType.PREPAID));
77
	}
78
	public static String getDeliveryDateString(int days, DeliveryType type) throws TException {
79
		Calendar now = new GregorianCalendar();
80
		int hour = now.get(Calendar.HOUR_OF_DAY);
81
		if(type == DeliveryType.COD && hour < 15){
82
			days = days + 1;
83
		}
84
		if(businessDayToActualDateMap.containsKey(days)){
85
			if(hour != 0){	
86
				return businessDayToActualDateMap.get(days);
87
			}
88
			businessDayToActualDateMap.clear();
89
		}
90
 
91
		now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
92
 
93
		LogisticsClient logisticsServiceClient = null;
94
		logisticsServiceClient = new LogisticsClient();
95
		Client logisticsClient = logisticsServiceClient.getClient();
96
 
97
		int newdays = (int) logisticsClient.adjustDeliveryDays(now.getTimeInMillis(), days);
98
		now.add(Calendar.DAY_OF_MONTH, newdays);
99
 
100
		SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
101
		if(newdays == 1){
102
			businessDayToActualDateMap.put(days, "Tomorrow, " + dateformat.format(now.getTime()));
103
		}else{
104
			businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
105
		}
106
		return businessDayToActualDateMap.get(days);
107
	}
108
}