| 458 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
| 7791 |
rajveer |
2 |
import java.util.GregorianCalendar;
|
|
|
3 |
import java.text.SimpleDateFormat;
|
|
|
4 |
import java.util.Calendar;
|
| 3830 |
chandransh |
5 |
import java.util.HashMap;
|
|
|
6 |
import java.util.Map;
|
| 507 |
rajveer |
7 |
import java.util.StringTokenizer;
|
|
|
8 |
|
| 5072 |
amar.kumar |
9 |
import in.shop2020.datalogger.EventType;
|
| 4865 |
rajveer |
10 |
import in.shop2020.logistics.LogisticsInfo;
|
| 507 |
rajveer |
11 |
import in.shop2020.logistics.LogisticsService.Client;
|
| 4630 |
mandeep.dh |
12 |
import in.shop2020.logistics.DeliveryType;
|
| 786 |
rajveer |
13 |
import in.shop2020.logistics.LogisticsServiceException;
|
| 3126 |
rajveer |
14 |
import in.shop2020.thrift.clients.LogisticsClient;
|
| 5072 |
amar.kumar |
15 |
import in.shop2020.utils.DataLogger;
|
| 507 |
rajveer |
16 |
|
| 832 |
rajveer |
17 |
import org.apache.log4j.Logger;
|
| 458 |
rajveer |
18 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
19 |
import org.apache.struts2.rest.HttpHeaders;
|
| 786 |
rajveer |
20 |
import org.apache.thrift.TException;
|
| 458 |
rajveer |
21 |
|
| 3830 |
chandransh |
22 |
import com.google.gson.Gson;
|
|
|
23 |
|
| 458 |
rajveer |
24 |
/**
|
|
|
25 |
* @author rajveer
|
|
|
26 |
*
|
|
|
27 |
*/
|
|
|
28 |
public class EstimateController extends BaseController {
|
|
|
29 |
|
| 1044 |
chandransh |
30 |
private static final long serialVersionUID = 8023801600023970837L;
|
|
|
31 |
|
| 7791 |
rajveer |
32 |
public static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();
|
| 832 |
rajveer |
33 |
private static Logger log = Logger.getLogger(Class.class);
|
| 458 |
rajveer |
34 |
private String id;
|
| 507 |
rajveer |
35 |
private long itemId;
|
|
|
36 |
private String pincode;
|
| 7791 |
rajveer |
37 |
private String days = "-1";
|
| 7874 |
rajveer |
38 |
private long businessDays = -1;
|
| 3830 |
chandransh |
39 |
private boolean isCODAvailable;
|
| 6524 |
rajveer |
40 |
private boolean isOTGAvailable;
|
| 7809 |
rajveer |
41 |
private String codDays = "-1";
|
| 3830 |
chandransh |
42 |
private Map<String, String> response = new HashMap<String, String>();
|
|
|
43 |
|
| 458 |
rajveer |
44 |
public EstimateController() {
|
|
|
45 |
super();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// GET /logout
|
| 786 |
rajveer |
49 |
public HttpHeaders show(){
|
| 3126 |
rajveer |
50 |
LogisticsClient logisticsServiceClient = null;
|
| 507 |
rajveer |
51 |
try {
|
| 3126 |
rajveer |
52 |
logisticsServiceClient = new LogisticsClient();
|
| 507 |
rajveer |
53 |
Client logisticsClient = logisticsServiceClient.getClient();
|
| 4865 |
rajveer |
54 |
LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID);
|
| 7791 |
rajveer |
55 |
|
|
|
56 |
if(logistincInfo.getDeliveryTime()!=-1L){
|
| 7839 |
rajveer |
57 |
days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);
|
|
|
58 |
if(logistincInfo.isCodAllowed()){
|
| 7859 |
rajveer |
59 |
codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);
|
| 7809 |
rajveer |
60 |
}
|
| 7791 |
rajveer |
61 |
}
|
| 7874 |
rajveer |
62 |
businessDays = logistincInfo.getDeliveryTime();
|
| 4865 |
rajveer |
63 |
isCODAvailable = logistincInfo.isCodAllowed();
|
| 6524 |
rajveer |
64 |
isOTGAvailable = logistincInfo.isOtgAvailable();
|
| 3830 |
chandransh |
65 |
} catch (LogisticsServiceException e) {
|
| 7791 |
rajveer |
66 |
days = "-1";
|
| 3830 |
chandransh |
67 |
isCODAvailable = false;
|
|
|
68 |
log.error("Unable to get estimate/COD availability for " + itemId, e);
|
|
|
69 |
|
|
|
70 |
} catch(TException e) {
|
| 786 |
rajveer |
71 |
|
| 3830 |
chandransh |
72 |
} catch (Exception e) {
|
| 786 |
rajveer |
73 |
|
|
|
74 |
}
|
| 7791 |
rajveer |
75 |
response.put("delivery_estimate", days);
|
| 7809 |
rajveer |
76 |
response.put("cod_delivery_estimate", codDays);
|
| 3830 |
chandransh |
77 |
response.put("is_cod_available_for_location", Boolean.toString(isCODAvailable));
|
| 6524 |
rajveer |
78 |
response.put("on_time_guarantee", Boolean.toString(isOTGAvailable));
|
| 7874 |
rajveer |
79 |
response.put("business_days", businessDays + "");
|
| 3830 |
chandransh |
80 |
|
| 5072 |
amar.kumar |
81 |
try{
|
| 5340 |
amar.kumar |
82 |
if(pincode.length() == 6 && !pincode.equals("110001")){
|
|
|
83 |
String requestOrigin = request.getHeader("referer").contains("cart")?"Cart":"Product";
|
| 12616 |
anikendra |
84 |
// DataLogger.logData(EventType.DELIVERY_ESTIMATE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
|
|
|
85 |
// pincode, (new Long(days)).toString(), (new Long(itemId)).toString(), (new Boolean(isCODAvailable)).toString(),
|
|
|
86 |
// requestOrigin);
|
| 5340 |
amar.kumar |
87 |
}
|
| 5072 |
amar.kumar |
88 |
}catch(Exception e){
|
| 5191 |
amar.kumar |
89 |
log.warn(e.getMessage()+"pincode : "+ pincode + " Actual referer : " + request.getHeader("referer") + e.getStackTrace());
|
| 5072 |
amar.kumar |
90 |
}
|
| 458 |
rajveer |
91 |
return new DefaultHttpHeaders("index");
|
|
|
92 |
}
|
| 3830 |
chandransh |
93 |
|
| 7895 |
rajveer |
94 |
public static void main(String[] args) throws TException {
|
|
|
95 |
System.out.println(getDeliveryDateString(1, DeliveryType.COD));
|
|
|
96 |
System.out.println(getDeliveryDateString(2, DeliveryType.COD));
|
|
|
97 |
System.out.println(getDeliveryDateString(2, DeliveryType.PREPAID));
|
|
|
98 |
System.out.println(getDeliveryDateString(3, DeliveryType.PREPAID));
|
|
|
99 |
}
|
| 7839 |
rajveer |
100 |
public static String getDeliveryDateString(int days, DeliveryType type) throws TException {
|
| 7791 |
rajveer |
101 |
Calendar now = new GregorianCalendar();
|
|
|
102 |
int hour = now.get(Calendar.HOUR_OF_DAY);
|
| 7875 |
rajveer |
103 |
if(type == DeliveryType.COD && hour < 15){
|
| 7839 |
rajveer |
104 |
days = days + 1;
|
|
|
105 |
}
|
| 7791 |
rajveer |
106 |
if(businessDayToActualDateMap.containsKey(days)){
|
| 7798 |
rajveer |
107 |
if(hour != 0){
|
| 7791 |
rajveer |
108 |
return businessDayToActualDateMap.get(days);
|
|
|
109 |
}
|
|
|
110 |
businessDayToActualDateMap.clear();
|
|
|
111 |
}
|
|
|
112 |
|
| 7888 |
rajveer |
113 |
now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
114 |
|
| 7791 |
rajveer |
115 |
LogisticsClient logisticsServiceClient = null;
|
|
|
116 |
logisticsServiceClient = new LogisticsClient();
|
|
|
117 |
Client logisticsClient = logisticsServiceClient.getClient();
|
|
|
118 |
|
| 7895 |
rajveer |
119 |
int newdays = (int) logisticsClient.adjustDeliveryDays(now.getTimeInMillis(), days);
|
|
|
120 |
now.add(Calendar.DAY_OF_MONTH, newdays);
|
| 7874 |
rajveer |
121 |
|
| 7888 |
rajveer |
122 |
SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
|
| 7895 |
rajveer |
123 |
if(newdays == 1){
|
| 7888 |
rajveer |
124 |
businessDayToActualDateMap.put(days, "Tomorrow, " + dateformat.format(now.getTime()));
|
|
|
125 |
}else{
|
|
|
126 |
businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
|
| 7791 |
rajveer |
127 |
}
|
|
|
128 |
return businessDayToActualDateMap.get(days);
|
|
|
129 |
}
|
|
|
130 |
|
| 458 |
rajveer |
131 |
/**
|
|
|
132 |
*
|
|
|
133 |
* @param id
|
|
|
134 |
*/
|
|
|
135 |
public void setId(String id) {
|
|
|
136 |
this.id = id;
|
| 2148 |
chandransh |
137 |
StringTokenizer tokenizer = new StringTokenizer(this.id, "_");
|
| 507 |
rajveer |
138 |
this.pincode = tokenizer.nextToken();
|
|
|
139 |
this.itemId = Long.parseLong(tokenizer.nextToken());
|
| 458 |
rajveer |
140 |
}
|
|
|
141 |
|
| 3830 |
chandransh |
142 |
public String getResponseJSONString() {
|
|
|
143 |
Gson gson = new Gson();
|
|
|
144 |
return gson.toJson(response);
|
| 458 |
rajveer |
145 |
}
|
| 3830 |
chandransh |
146 |
}
|