2011-10-25 55 views
1

在我play framework應用程序的管理頁面,我需要列出Product的兩個日期之間的銷售,顯示Product namequantity出售,總price。我已經仿照這個信息在一個CustomerOrder model.Still我有一些疑問,我應該使用哪些數據結構來保存上述數據。從CustomerOrder提取數據顯示爲總銷售額數據

管理員應該能夠列出一個表格式的銷售數據如下


Product Name | quantity | total amount received 
=================================================== 
flwr vase#12 | 13  | 1250 
--------------------------------------------------- 
lampshade#3 | 11  | 110 
----------------------------------------- 

我還以爲寫在Admin類中的方法,得到兩個日期之間的CustomerOrder S的,並被困在從CustomerOrder中提取信息。 每個產品名稱都應該是表格的主鍵,我猜是。因此,我可以創建一個產品名稱爲key的映射。然後應該是什麼值?當我迭代每個CustomerOrder時,我應該增加每個銷售的產品和收到的總金額應該更新。

總金額是order.cartitem.qty*order.cartitem.product.price + order.tax+ order.shipping ..我認爲這使得循環有點複雜...有人可以告訴我該如何做到這一點?

public void allSalesData(Date start,Date end){ 
    List<CustomerOrder> orders = CustomerOrder.find("select o from CustomerOrder o where o.orderDate between :startdate and :enddate").bind("startdate",start).bind("enddate",end).fetch(); 
    //now how to extract? should I use a map ? 
} 

import play.db.jpa.Model; 
class CustomerOrder extends Model{ 
    ... 
    public Customer customer; 
    public Date orderDate; 
    public Set<OrderItem> orderItems; 
    public BigDecimal tax; 
    public BigDecimal shipping; 
    ... 
} 

class OrderItem extends Model { 

    public Product product; 
    public int quantity; 
} 

class Product extends Model{ 
    public String name; 
    public BigDecimal price; 
} 
+0

你想要準確顯示什麼?你需要列出每個項目的數量,價格和總數? – emt14

+0

我需要列出每個產品,銷售數量和從中賺得的總金額 –

+1

使用稅和運費查詢更新的答案 – emt14

回答

1

這裏您需要的是彙總客戶訂單和相應數量列表中的所有產品。
您應該迭代CustomerOrders並訂購商品並將每個商品放入地圖中並增加相應的數量。

你可以這樣做:



public void allSalesData(Date start,Date end){ 

    List orders = CustomerOrder.find("select o from CustomerOrder o where o.orderDate between :startdate and :enddate").bind("startdate",start).bind("enddate",end).fetch(); 

    Map prodMap = new HashMap(); 
    Map priceMap = new HashMap(); 
    Integer quantity = null; 
    Long price = null; 
    for(CustomerOrder cusOrder: orders) { 
     for(OrderItem item: cusOrder.orderItems){ 
      quantity = prodMap.get(item.product); 
      prodMap.put(item.product, quantity + item.quantity); 
      price = priceMap.get(item.product); 
      priceMap.put(item.product, quantity * item.price cusOrder.tax + cusOrder.shipping + price); 
     } 
    } 

    render(prodMap, priceMap); 
} 

然後在你的模板,通過地圖迭代,並顯示產品名稱,價格,數量和數量*價格。