2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern.customers ;
2020-04-28 22:41:29 +02:00
2020-06-10 14:51:12 +02:00
import com.fasterxml.jackson.annotation.JsonCreator ;
import com.fasterxml.jackson.annotation.JsonProperty ;
2020-06-10 17:03:59 +02:00
import org.hso.ecommerce.api.RestServiceForDelivery ;
2020-06-01 11:31:12 +02:00
import org.hso.ecommerce.entities.shop.CustomerOrder ;
import org.hso.ecommerce.repos.shop.CustomerOderRepository ;
import org.springframework.beans.factory.annotation.Autowired ;
2020-06-10 17:03:59 +02:00
import org.springframework.format.datetime.DateFormatterRegistrar ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-06-01 11:31:12 +02:00
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.GetMapping ;
2020-06-01 11:53:29 +02:00
import org.springframework.web.bind.annotation.PathVariable ;
2020-06-01 11:31:12 +02:00
import org.springframework.web.bind.annotation.RequestMapping ;
2020-06-10 17:03:59 +02:00
import javax.transaction.Transaction ;
2020-06-10 14:51:12 +02:00
import java.sql.Timestamp ;
2020-06-10 17:03:59 +02:00
import java.text.DateFormat ;
import java.text.ParseException ;
2020-06-10 14:51:12 +02:00
import java.text.SimpleDateFormat ;
import java.util.* ;
import java.util.function.Function ;
import java.util.stream.Collectors ;
2020-04-28 22:41:29 +02:00
@Controller
2020-06-01 11:31:12 +02:00
@RequestMapping ( " intern/customerOrders " )
2020-05-01 10:48:12 +02:00
public class CustomerOrderController {
2020-06-01 11:31:12 +02:00
@Autowired
private final CustomerOderRepository customerOrderRepository = null ;
2020-06-10 17:03:59 +02:00
@Autowired
private final RestServiceForDelivery restServiceForDelivery = null ;
2020-06-01 11:31:12 +02:00
@GetMapping ( " " )
public String internCustomerOrder ( Model model ) {
List < CustomerOrder > orders = customerOrderRepository . getAllOrders ( ) ;
2020-06-10 14:51:12 +02:00
Map < CustomerOrder , DeliveryData > customerOrderDeliveryDataMap = orders . stream ( ) . collect
( Collectors . toMap
( Function . identity ( ) , this : : getDeliveryDataFromCustomerOrder ) ) ;
model . addAttribute ( " orderDeliveryDataMap " , customerOrderDeliveryDataMap ) ;
2020-06-01 11:31:12 +02:00
return " intern/customerOrders/index " ;
}
@GetMapping ( " /{id} " )
2020-06-01 11:53:29 +02:00
public String internCustomerOrdersId ( Model model ,
@PathVariable ( " id " ) String id
) {
CustomerOrder order = customerOrderRepository . findById ( Long . parseLong ( id ) ) . get ( ) ;
2020-06-10 14:51:12 +02:00
DeliveryData deliveryData = getDeliveryDataFromCustomerOrder ( order ) ;
2020-06-01 11:53:29 +02:00
model . addAttribute ( " order " , order ) ;
2020-06-10 14:51:12 +02:00
model . addAttribute ( " deliveryData " , deliveryData ) ;
2020-06-01 11:53:29 +02:00
2020-06-01 11:31:12 +02:00
return " intern/customerOrders/id " ;
}
2020-06-10 14:51:12 +02:00
private DeliveryData getDeliveryDataFromCustomerOrder ( CustomerOrder customerOrder )
{
if ( customerOrder . trackingId = = null )
return new CustomerOrderController . DeliveryData ( " Bestellung wurde elektronisch angekündigt " , " " ) ;
if ( customerOrder . deliveredAt = = null )
{
2020-06-10 17:03:59 +02:00
DeliveryData deliveryData = restServiceForDelivery . getDeliveryData ( UUID . fromString ( customerOrder . trackingId ) ) ;
2020-06-10 14:51:12 +02:00
if ( deliveryData . isDelivered ( ) )
{
2020-06-10 17:03:59 +02:00
Calendar calendar = Calendar . getInstance ( ) ;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( " dd-MM-yyyy " ) ;
try {
calendar . setTime ( simpleDateFormat . parse ( deliveryData . estimatedArrival ) ) ;
} catch ( ParseException e ) {
e . printStackTrace ( ) ;
}
customerOrderRepository . updateUserDeliveredAt ( customerOrder . id , new Timestamp ( calendar . getTimeInMillis ( ) ) ) ;
2020-06-10 14:51:12 +02:00
}
return deliveryData ;
}
SimpleDateFormat formatter = new SimpleDateFormat ( " dd-MM-yyyy " ) ;
return new CustomerOrderController . DeliveryData ( " Lieferung erfolgreich " , formatter . format ( customerOrder . deliveredAt ) ) ;
}
public static class DeliveryData
{
2020-06-10 17:03:59 +02:00
private final String status ;
private final String estimatedArrival ;
2020-06-10 14:51:12 +02:00
private boolean isDelivered ;
2020-06-10 17:03:59 +02:00
@JsonCreator ( mode = JsonCreator . Mode . PROPERTIES )
2020-06-10 14:51:12 +02:00
public DeliveryData ( String status , String estimatedArrival ) {
this . status = status ;
this . estimatedArrival = estimatedArrival ;
isDelivered = status . equals ( " Lieferung erfolgreich " ) ;
}
public boolean isDelivered ( ) {
return isDelivered ;
}
public String getStatus ( ) {
return status ;
}
public String getEstimatedArrival ( ) {
return estimatedArrival ;
}
}
2020-04-28 22:41:29 +02:00
}