Rev 31119 | 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.io.IOException;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.temporal.ChronoUnit;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.http.conn.HttpHostConnectException;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.apache.thrift.TException;import org.json.JSONArray;import org.json.JSONObject;import org.json.XML;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.CustomOrder;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.model.TrackingModel;import com.spice.profitmandi.common.web.client.RestClient;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.logistics.Provider;import com.spice.profitmandi.dao.entity.logistics.ProviderTat;import com.spice.profitmandi.dao.entity.transaction.Order;import com.spice.profitmandi.dao.model.ProductPojo;import com.spice.profitmandi.dao.repository.logistics.ProviderRepository;import com.spice.profitmandi.dao.repository.logistics.ProviderTatRepository;import com.spice.profitmandi.dao.repository.transaction.OrderRepository;import com.spice.profitmandi.dao.util.ContentPojoPopulator;import com.spice.profitmandi.model.tracking.ScannedDetailModel;import com.spice.profitmandi.model.tracking.TrackingDetailModel;import com.spice.profitmandi.service.LogisticsService;import in.shop2020.model.v1.order.OrderStatusGroups;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;@Controller@Transactional(readOnly = true, rollbackFor = Throwable.class)public class TrackingController {@Value("${delhivery.tracking.token}")private String token;@Autowiredprivate ResponseSender<?> responseSender;@Autowiredprivate OrderRepository orderRepository;@Autowiredprivate ProviderRepository providerRepository;@Autowiredprivate ContentPojoPopulator contentPojoPopulator;@Autowiredprivate ProviderTatRepository providerTatRepository;@Autowiredprivate LogisticsService logisticsService;@Value("#{'${prod}'=='true' ? 'DEL81122' : 'DEL95932'}")private String loginId;@Value("#{'${prod}'=='true' ? '9sjnjt61hghmtjuvjio0osqkmujkphlt' : 'poehq2kiqjnllkrote9hxglo4teptfqf'}")private String licencekey;@Autowiredprivate RestClient restClient;private static final Logger LOGGER = LogManager.getLogger(TrackingController.class);@RequestMapping(value = "/order/tracking", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })public ResponseEntity<?> getDeliveryOrder(HttpServletRequest request, HttpServletResponse response,@RequestParam String airwayBillNo) throws Throwable {List<Order> orders = orderRepository.selectByAirwayBillNumber(airwayBillNo);int providerId = orders.get(0).getLogisticsProviderId();TrackingDetailModel tdm = null;if (providerId == ProfitMandiConstants.DELHIVERY_PROVIDERID) {tdm = getDelhiveryTracking(airwayBillNo);} else if (providerId == ProfitMandiConstants.BLUEDART_PROVIDERID) {tdm = getBlueDartTracking(airwayBillNo);}LOGGER.info("tdm" + tdm);return responseSender.ok(tdm);}private TrackingDetailModel getDelhiveryTracking(String airwayBillNo) throws IOException {String url = "https://track.delhivery.com/api/v1/packages/json/";OkHttpClient client = new OkHttpClient();Request request1 = new Request.Builder().url(url + "?waybill=" + String.join(",", airwayBillNo) + "&token=" + token).get().build();LOGGER.info("request1" + request1);Response response1 = client.newCall(request1).execute();LOGGER.info("jsonbj" + response1.body());JSONObject jsonObj = new JSONObject(response1.body().string());LOGGER.info("jsonObj" + jsonObj);JSONArray shipmentData = jsonObj.getJSONArray("ShipmentData");TrackingDetailModel tdm = new TrackingDetailModel();List<ScannedDetailModel> sdms = new ArrayList<>();for (int i = 0; i < shipmentData.length(); i++) {JSONObject jsonObject = shipmentData.getJSONObject(i);JSONObject shipment = jsonObject.getJSONObject("Shipment");JSONArray scans = shipment.getJSONArray("Scans");for (int j = 0; j < scans.length(); j++) {JSONObject scanObject = scans.getJSONObject(j);JSONObject scandetail = scanObject.getJSONObject("ScanDetail");ScannedDetailModel sdm = new ScannedDetailModel();sdm.setStatusDateTime(scandetail.getString("ScanDateTime"));sdm.setStatusLocation(scandetail.getString("ScannedLocation"));sdm.setInstruction(scandetail.getString("Instructions"));sdms.add(sdm);}tdm.setExpectedDelivery(shipment.getString("ExpectedDeliveryDate"));tdm.setAwb(shipment.getString("AWB"));}tdm.setSdm(sdms);return tdm;}private TrackingDetailModel getBlueDartTracking(String airwayBillNo)throws ProfitMandiBusinessException, HttpHostConnectException {String loginId = "DEL81122";String licencekey = "uhfhlg2jpmnqfhfrlsgzfr1nhu1qfvel";String url = "https://api.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery";String response = restClient.get(url + "&loginid=" + loginId + "&awb=awb&numbers="+ String.join(",", airwayBillNo) + "&format=xml&lickey=" + licencekey + "&verno=1.3&scan=1", null,null);JSONObject updateJson = XML.toJSONObject(response);TrackingDetailModel tdm = new TrackingDetailModel();List<ScannedDetailModel> sdms = new ArrayList<>();JSONObject shipmentData = updateJson.getJSONObject("ShipmentData");JSONObject shipment = shipmentData.getJSONObject("Shipment");JSONObject scan = shipment.getJSONObject("Scans");JSONArray scandetails = scan.getJSONArray("ScanDetail");for (int j = 0; j < scandetails.length(); j++) {JSONObject scanDetail = scandetails.getJSONObject(j);ScannedDetailModel sdm = new ScannedDetailModel();sdm.setStatusDateTime(scanDetail.getString("ScanDate"));sdm.setStatusLocation(scanDetail.getString("ScannedLocation"));sdm.setInstruction(scanDetail.getString("Scan"));sdms.add(sdm);}tdm.setExpectedDelivery(shipment.getString("ExpectedDeliveryDate"));tdm.setAwb(shipment.getNumber("WaybillNo").toString());tdm.setSdm(sdms);LOGGER.info("tdm" + tdm);return tdm;}@RequestMapping(value = "/order/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })public ResponseEntity<?> getOrderDetails(HttpServletRequest request, HttpServletResponse response,@RequestParam String airwayBillNo, @RequestParam int orderId) throws Throwable {LOGGER.info("airwayBillNo" + airwayBillNo);List<Order> orders = null;if (!airwayBillNo.equals("NaN")) {orders = orderRepository.selectByAirwayBillNumber(airwayBillNo);} else {orders = orderRepository.selectByOrderIds(Arrays.asList(orderId));}List<CustomOrder> customOrders = new ArrayList<CustomOrder>();for (Order order : orders) {CustomOrder co = null;try {co = toCustomOrder(order);} catch (Exception e) {continue;}customOrders.add(co);}return responseSender.ok(customOrders);}private CustomOrder toCustomOrder(Order order) throws Exception {CustomOrder co = new CustomOrder();co.setId(order.getId());co.setRetailerName(order.getRetailerName());co.setExpectedDeliveryTime(order.getExpectedDeliveryTime());co.setPromisedDeliveryTime(order.getPromisedDeliveryTime());co.setRetailerPinCode(order.getRetailerPinCode());co.setRetailerAddress1(order.getRetailerAddress1());co.setRetailerCity(order.getRetailerCity());co.setStatusDescription(order.getStatusDescription());co.setStatus(order.getStatus());co.setBrand(order.getLineItem().getBrand());co.setTransactionId(order.getTransactionId());co.setExtraInfo(order.getLineItem().getExtraInfo());co.setItemId(order.getLineItem().getItemId());co.setQuantity(order.getLineItem().getQuantity());co.setTotalAmount(order.getTotalAmount());co.setColor(order.getLineItem().getColor());co.setShippingCost(order.getShippingCost());co.setCreateTimestamp(order.getCreateTimestamp());co.setModelName(order.getLineItem().getModelName());co.setModelNumber(order.getLineItem().getModelNumber());co.setDeliveryTimestamp(order.getDeliveryTimestamp());co.setImageUrl(getImageUrl(order));co.setCancellable(getCancellableRequest(order));co.setProviderId(order.getLogisticsProviderId());co.setDelayReason(order.getDelayReasonText());Provider provider = providerRepository.selectById(order.getLogisticsProviderId());co.setProviderName(provider.getName());co.setAirwayBillNo(order.getAirwayBillNumber());co.setCancellationTimestamp(order.getRefundTimestamp());co.setBillingTimestamp(order.getBillingTimestamp());co.setShippingTimestamp(order.getShippingTimestamp());co.setInvoiceNumber(order.getInvoiceNumber());co.setExpectedShipping(order.getExpectedShippingTime());if (order.getShippingTimestamp() != null) {ProviderTat pt = providerTatRepository.selectByProviderId(order.getLogisticsProviderId(),order.getWarehouseId(), order.getRetailerPinCode());LocalDateTime shipping = order.getShippingTimestamp();LocalDate deliveryTime = logisticsService.calculateDeliveryTimeline(shipping.toLocalDate(), pt, 0);LOGGER.info("deliveryTime" + deliveryTime);LocalDateTime expected = order.getExpectedDeliveryTime();LOGGER.info("expected" + expected);long noOfDaysBetween = ChronoUnit.DAYS.between(expected, deliveryTime.atStartOfDay());co.setDelayAfterShipping((int) noOfDaysBetween);}TrackingModel tm = new TrackingModel();if (co.getBillingTimestamp() != null) {tm.setActualBilling(co.getBillingTimestamp());tm.setInTransitBilling(false);} else {tm.setInTransitBilling(true);}if (co.getShippingTimestamp() != null) {tm.setActualShipping(co.getShippingTimestamp());tm.setInTransitShipping(false);} else {if (co.getBillingTimestamp() != null) {co.setExpectedShipping(co.getBillingTimestamp());if (LocalDate.now().isAfter(co.getExpectedShipping().toLocalDate())) {long noOfDaysBetween = ChronoUnit.DAYS.between(co.getExpectedShipping(),LocalDate.now().atStartOfDay());co.setExpectedShipping(co.getBillingTimestamp().plusDays(noOfDaysBetween + 1));}}tm.setExpectedShipping(co.getExpectedShipping());if (tm.isInTransitBilling()) {tm.setInTransitShipping(false);} else {tm.setInTransitShipping(true);}}if (co.getDeliveryTimestamp() != null) {tm.setActualDelivered(co.getDeliveryTimestamp());tm.setInTransitDelivered(false);} else {tm.setExpectedDelivered(co.getExpectedDeliveryTime());if (tm.isInTransitBilling()) {tm.setInTransitDelivered(false);}else if (tm.isInTransitShipping()) {tm.setInTransitDelivered(false);} else {tm.setInTransitDelivered(true);}}co.setTrackingModel(tm);return co;}private String getImageUrl(Order order) throws TException {ProductPojo pp = contentPojoPopulator.getShortContent(order.getLineItem().getItem().getCatalogItemId());String imageUrl = "";LOGGER.info("pp {}", pp);if (pp != null) {imageUrl = pp.getImageUrl();}return imageUrl;}private Boolean getCancellableRequest(Order order) throws Exception {OrderStatusGroups orderStatusGroups = new OrderStatusGroups();if (order.isCod() == null) {throw new Exception("Invalid order");}if (order.isCod()) {if (orderStatusGroups.getCodCancellable().contains(order.getStatus())) {return true;} else {return false;}} else {if (orderStatusGroups.getPrepaidCancellableBeforeBilled().contains(order.getStatus())) {return true;} else if (orderStatusGroups.getPrepaidCancellableAfterBilled().contains(order.getStatus())) {return true;} else {return false;}}}}