| 9327 |
amit.gupta |
1 |
package in.shop2020.mobileapi.serving.services;
|
|
|
2 |
import in.shop2020.logistics.DeliveryType;
|
|
|
3 |
import in.shop2020.logistics.LogisticsInfo;
|
|
|
4 |
import in.shop2020.logistics.LogisticsService.Client;
|
|
|
5 |
import in.shop2020.logistics.LogisticsServiceException;
|
|
|
6 |
import in.shop2020.metamodel.util.LogisticsInfoPojo;
|
|
|
7 |
import in.shop2020.mobileapi.serving.controllers.EstimateController;
|
|
|
8 |
import in.shop2020.thrift.clients.LogisticsClient;
|
|
|
9 |
|
|
|
10 |
import java.text.SimpleDateFormat;
|
|
|
11 |
import java.util.Calendar;
|
|
|
12 |
import java.util.GregorianCalendar;
|
|
|
13 |
import java.util.HashMap;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
|
|
|
16 |
import org.apache.log4j.Logger;
|
|
|
17 |
import org.apache.thrift.TException;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @author rajveer
|
|
|
21 |
*
|
|
|
22 |
*/
|
|
|
23 |
public class LogisticsService {
|
|
|
24 |
|
|
|
25 |
private static final long serialVersionUID = 8023801600023970837L;
|
|
|
26 |
|
|
|
27 |
private static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();
|
|
|
28 |
|
|
|
29 |
private static Logger log = Logger.getLogger(EstimateController.class);
|
|
|
30 |
|
|
|
31 |
// GET /logout
|
|
|
32 |
public static LogisticsInfoPojo getLogisticsInfo(long itemId, String pinCode){
|
|
|
33 |
LogisticsClient logisticsServiceClient = null;
|
|
|
34 |
long businessDays = -1;
|
|
|
35 |
boolean isOTGAvailable = false;
|
|
|
36 |
String days="-1";
|
|
|
37 |
String codDays="-1";
|
|
|
38 |
boolean isCODAvailable = false;
|
|
|
39 |
LogisticsInfoPojo lip = new LogisticsInfoPojo();
|
|
|
40 |
|
|
|
41 |
try {
|
|
|
42 |
logisticsServiceClient = new LogisticsClient();
|
|
|
43 |
Client logisticsClient = logisticsServiceClient.getClient();
|
|
|
44 |
LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pinCode, DeliveryType.PREPAID);
|
|
|
45 |
|
|
|
46 |
if(logistincInfo.getDeliveryTime()!=-1L){
|
|
|
47 |
days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);
|
|
|
48 |
if(logistincInfo.isCodAllowed()){
|
|
|
49 |
codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
businessDays = logistincInfo.getDeliveryTime();
|
|
|
53 |
isCODAvailable = logistincInfo.isCodAllowed();
|
|
|
54 |
isOTGAvailable = logistincInfo.isOtgAvailable();
|
|
|
55 |
lip.setBusinessDays(businessDays);
|
|
|
56 |
lip.setCodDeliveryEstimate(codDays);
|
|
|
57 |
lip.setDelivelyEstimate(days);
|
|
|
58 |
lip.setIsCodAvailableForLocation(isCODAvailable);
|
|
|
59 |
lip.setOnTimeGuarantee(isOTGAvailable);
|
|
|
60 |
} catch (LogisticsServiceException e) {
|
|
|
61 |
days = "-1";
|
|
|
62 |
isCODAvailable = false;
|
| 11522 |
amit.gupta |
63 |
log.error("Unable to get estimate/COD availability for " + itemId + " and pincode : " + pinCode);
|
| 9327 |
amit.gupta |
64 |
|
|
|
65 |
} catch(TException e) {
|
|
|
66 |
|
|
|
67 |
} catch (Exception e) {
|
|
|
68 |
|
|
|
69 |
}
|
|
|
70 |
return lip;
|
|
|
71 |
/*
|
|
|
72 |
try{
|
|
|
73 |
if(pincode.length() == 6 && !pincode.equals("110001")){
|
|
|
74 |
String requestOrigin = request.getHeader("referer").contains("cart")?"Cart":"Product";
|
|
|
75 |
DataLogger.logData(EventType.DELIVERY_ESTIMATE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
|
|
|
76 |
pincode, (new Long(days)).toString(), (new Long(itemId)).toString(), (new Boolean(isCODAvailable)).toString(),
|
|
|
77 |
requestOrigin);
|
|
|
78 |
}
|
|
|
79 |
}catch(Exception e){
|
|
|
80 |
log.warn(e.getMessage()+"pincode : "+ pincode + " Actual referer : " + request.getHeader("referer") + e.getStackTrace());
|
|
|
81 |
}
|
|
|
82 |
return new DefaultHttpHeaders("index");*/
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public static void main(String[] args) throws TException {
|
|
|
86 |
System.out.println(getDeliveryDateString(1, DeliveryType.COD));
|
|
|
87 |
System.out.println(getDeliveryDateString(2, DeliveryType.COD));
|
|
|
88 |
System.out.println(getDeliveryDateString(2, DeliveryType.PREPAID));
|
|
|
89 |
System.out.println(getDeliveryDateString(3, DeliveryType.PREPAID));
|
|
|
90 |
}
|
|
|
91 |
private static String getDeliveryDateString(int days, DeliveryType type) throws TException {
|
|
|
92 |
Calendar now = new GregorianCalendar();
|
|
|
93 |
int hour = now.get(Calendar.HOUR_OF_DAY);
|
|
|
94 |
if(type == DeliveryType.COD && hour < 15){
|
|
|
95 |
days = days + 1;
|
|
|
96 |
}
|
|
|
97 |
if(businessDayToActualDateMap.containsKey(days)){
|
|
|
98 |
if(hour != 0){
|
|
|
99 |
return businessDayToActualDateMap.get(days);
|
|
|
100 |
}
|
|
|
101 |
businessDayToActualDateMap.clear();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
105 |
|
|
|
106 |
LogisticsClient logisticsServiceClient = null;
|
|
|
107 |
logisticsServiceClient = new LogisticsClient();
|
|
|
108 |
Client logisticsClient = logisticsServiceClient.getClient();
|
|
|
109 |
|
|
|
110 |
int newdays = (int) logisticsClient.adjustDeliveryDays(now.getTimeInMillis(), days);
|
|
|
111 |
now.add(Calendar.DAY_OF_MONTH, newdays);
|
|
|
112 |
|
|
|
113 |
SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
|
|
|
114 |
if(newdays == 1){
|
|
|
115 |
businessDayToActualDateMap.put(days, "Tomorrow, " + dateformat.format(now.getTime()));
|
|
|
116 |
}else{
|
|
|
117 |
businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
|
|
|
118 |
}
|
|
|
119 |
return businessDayToActualDateMap.get(days);
|
|
|
120 |
}
|
|
|
121 |
}
|