電商專案筆記之六:訂單操作
阿新 • • 發佈:2019-02-08
手裡的專案沒有支付寶相關的程式碼 比較遺憾, 剛才網上查了一下 貌似支付寶和struts的介面也不是太難 ,有機會再試試吧
一:提交訂單
public String add() throws Exception {
order.setName(getLoginCustomer().getUsername());
order.setAddress(getLoginCustomer().getAddress());
order.setMobile(getLoginCustomer().getMobile());
return ADD;
}
付款方式前臺傳到後臺就沒處理<input type="radio" name="paymentWay" value="貨到付款">貨到付款
我看支付寶的幫助,要在action裡準備一個Map,然後put進一系列認證號 然後是總價 最後是成功後返回的url (這些應該在訂單的儲存之後執行, 成功與否都要在資料庫中再更新一下)
二:訂單的儲存
public String save() throws Exception { if(getLoginCustomer() != null){//如果使用者已登入 order.setOrderId(StringUitl.createOrderId());// 設定訂單號 order.setCustomer(getLoginCustomer());// 設定所屬使用者 Set<OrderItem> cart = getCart();// 獲取購物車 if(cart.isEmpty()){//判斷條目資訊是否為空 return ERROR;//返回訂單資訊錯誤提示頁面 } // 依次將更新訂單項中的商品的銷售數量 for(OrderItem item : cart){//遍歷購物車中的訂單條目資訊 Integer productId = item.getProductId();//獲取商品ID ProductInfo product = productDao.load(productId);//裝載商品物件 product.setSellCount(product.getSellCount() + item.getAmount());//更新商品銷售數量 productDao.update(product);//修改商品資訊 } order.setOrderItems(cart);// 設定訂單項 order.setOrderState(OrderState.DELIVERED);// 設定訂單狀態 float totalPrice = 0f;// 計算總額的變數 for (OrderItem orderItem : cart) {//遍歷購物車中的訂單條目資訊 totalPrice += orderItem.getProductPrice() * orderItem.getAmount();//商品單價*商品數量 } order.setTotalPrice(totalPrice);//設定訂單的總價格 orderDao.save(order);//儲存訂單資訊 session.remove("cart");// 清空購物車 } return findByCustomer();//返回消費者訂單查詢的方法 }
public String findByCustomer() throws Exception { if(getLoginCustomer() != null){//如果使用者已登入 String where = "where customer.id = ?";//將使用者id設定為查詢條件 Object[] queryParams = {getLoginCustomer().getId()};//建立物件陣列 Map<String, String> orderby = new HashMap<String, String>(1);//建立Map集合 orderby.put("createTime", "desc");//設定排序條件及方式 pageModel = orderDao.find(where, queryParams, orderby , pageNo, pageSize);//執行查詢方法 } return LIST;//返回訂單列表頁面 }