자바 웹 도서 쇼핑 몰 디자인 주문 모듈(5)

18303 단어 javaweb도서 상점
이 글 은 자바 웹 도서 쇼핑 몰 에서 주문 모듈 에 대한 연구 입 니 다.
1.관련 클래스 생 성
domain:
Order
OrderItem
dao:OrderDao
service:OrderService
web.servlete:OrderServlet

/**
 *      
 */
public class OrderItem {
 private String iid;
 private int count;//   
 private double subtotal;//   
 private Order order;//     
 private Book book;//        
}

/**
 *    
 */
public class Order {
 private String oid;
 private Date ordertime;//     
 private double total;//   
 private int state;//        :1    2        3          4       
 private User owner;//      
 private String address;//     

 private List<OrderItem> orderItemList;//          
}



public class OrderDao {
 private QueryRunner qr = new TxQueryRunner();

 /**
  *     
  * @param order
  */
 public void addOrder(Order order) {
  try {
   String sql = "insert into orders values(?,?,?,?,?,?)";
   /*
    *   util Date   sql Timestamp
    */
   Timestamp timestamp = new Timestamp(order.getOrdertime().getTime());
   Object[] params = {order.getOid(), timestamp, order.getTotal(),
     order.getState(), order.getOwner().getUid(),
     order.getAddress()};
   qr.update(sql, params);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 /**
  *       
  * @param orderItemList
  */
 public void addOrderItemList(List<OrderItem> orderItemList) {
  /**
   * QueryRunner  batch(String sql, Object[][] params)
   *   params       !
   *         sql       ,           
   */
  try {
   String sql = "insert into orderitem values(?,?,?,?,?)";
   /*
    *  orderItemList       
    *    OrderItem           
    */
   Object[][] params = new Object[orderItemList.size()][];
   //     orderItemList,    orderItem   params         
   for(int i = 0; i < orderItemList.size(); i++) {
    OrderItem item = orderItemList.get(i);
    params[i] = new Object[]{item.getIid(), item.getCount(), 
      item.getSubtotal(), item.getOrder().getOid(),
      item.getBook().getBid()}; 
   }
   qr.batch(sql, params);//     
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 /**
  *  uid    
  * @param uid
  * @return
  */
 public List<Order> findByUid(String uid) {
  /*
   * 1.   uid          List<Order>
   * 2.       Order,        OrderItem
   */
  try {
   /*
    * 1.            
    */
   String sql = "select * from orders where uid=?";
   List<Order> orderList = qr.query(sql, new BeanListHandler<Order>(Order.class), uid);

   /*
    * 2.       Order,              
    */
   for(Order order : orderList) {
    loadOrderItems(order);// order            
   }

   /*
    * 3.       
    */
   return orderList;
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 /**
  *               
  * @param order
  * @throws SQLException 
  */
 private void loadOrderItems(Order order) throws SQLException {
  /*
   *      :orderitem、book
   */
  String sql = "select * from orderitem i, book b where i.bid=b.bid and oid=?";
  /*
   *                javabean,       BeanListHandler,  MapListHandler
   */
  List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler(), order.getOid());
  /*
   * mapList   map,  map       
   *   :
   * {iid=C7AD5492F27D492189105FB50E55CBB6, count=2, subtotal=60.0, oid=1AE8A70354C947F8B81B80ADA6783155, bid=7, bname=  Hibernate,price=30.0, author=   , image=book_img/8991366-1_l.jpg, cid=2}
   * ...
   * 
   *         Map      :OrderItem、Book,          ( Book   OrderItem)
   */
  /*
   *       Map,  map      ,      (      OrderItem), OrderItem    
   */
  List<OrderItem> orderItemList = toOrderItemList(mapList);
  order.setOrderItemList(orderItemList);
 }

 /**
  *  mapList   Map       ,     
  * @param mapList
  * @return
  */
 private List<OrderItem> toOrderItemList(List<Map<String, Object>> mapList) {
  List<OrderItem> orderItemList = new ArrayList<OrderItem>();
  for(Map<String,Object> map : mapList) {
   OrderItem item = toOrderItem(map);
   orderItemList.add(item);
  }
  return orderItemList;
 }

 /**
  *    Map     OrderItem  
  * @param map
  * @return
  */
 private OrderItem toOrderItem(Map<String, Object> map) {
  OrderItem orderItem = CommonUtils.toBean(map, OrderItem.class);
  Book book = CommonUtils.toBean(map, Book.class);
  orderItem.setBook(book);
  return orderItem;
 }

 /**
  *     
  * @param oid
  * @return
  */
 public Order load(String oid) {
  try {
   /*
    * 1.            
    */
   String sql = "select * from orders where oid=?";
   Order order = qr.query(sql, new BeanHandler<Order>(Order.class), oid);

   /*
    * 2.  order        
    */
   loadOrderItems(order);


   /*
    * 3.       
    */
   return order;
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 /**
  *   oid      
  * @param oid
  * @return
  */
 public int getStateByOid(String oid) {
  try {
   String sql = "select state from orders where oid=?";
   return (Integer)qr.query(sql, new ScalarHandler(), oid);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 /**
  *       
  * @param oid
  * @param state
  * @return
  */
 public void updateState(String oid, int state) {
  try {
   String sql = "update orders set state=? where oid=?";
   qr.update(sql, state, oid);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }
}


public class OrderService {
 private OrderDao orderDao = new OrderDao();

 /**
  *     
  * @param oid
  */
 public void zhiFu(String oid) {
  /*
   * 1.        
   * *      1,        
   * *       1,          
   */
  int state = orderDao.getStateByOid(oid);
  if(state == 1) {
   //        2
   orderDao.updateState(oid, 2);
  }
 }

 /**
  *     
  *       
  * @param order
  */
 public void add(Order order) {
  try {
   //     
   JdbcUtils.beginTransaction();

   orderDao.addOrder(order);//    
   orderDao.addOrderItemList(order.getOrderItemList());//          

   //     
   JdbcUtils.commitTransaction();
  } catch(Exception e) {
   //     
   try {
    JdbcUtils.rollbackTransaction();
   } catch (SQLException e1) {
   }
   throw new RuntimeException(e);
  }
 }

 /**
  *     
  * @param uid
  * @return
  */
 public List<Order> myOrders(String uid) {
  return orderDao.findByUid(uid);
 }

 /**
  *     
  * @param oid
  * @return
  */
 public Order load(String oid) {
  return orderDao.load(oid);
 }

 /**
  *     
  * @param oid
  * @throws OrderException
  */
 public void confirm(String oid) throws OrderException {
  /*
   * 1.       ,    3,    
   */
  int state = orderDao.getStateByOid(oid);//      
  if(state != 3) throw new OrderException("      ,        !");

  /*
   * 2.        4,      
   */
  orderDao.updateState(oid, 4);
 }
}


public class OrderServlet extends BaseServlet {
 private OrderService orderService = new OrderService();

 /**
  *       
  * 
  * @param request
  * @param response
  * @return
  * @throws ServletException
  * @throws IOException
  */
 public String zhiFu(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Properties props = new Properties();
  InputStream input = this.getClass().getClassLoader()
    .getResourceAsStream("merchantInfo.properties");
  props.load(input);
  /*
   *   13  
   */
  String p0_Cmd = "Buy";
  String p1_MerId = props.getProperty("p1_MerId");
  String p2_Order = request.getParameter("oid");
  String p3_Amt = "0.01";
  String p4_Cur = "CNY";
  String p5_Pid = "";
  String p6_Pcat = "";
  String p7_Pdesc = "";
  String p8_Url = props.getProperty("p8_Url");
  String p9_SAF = "";
  String pa_MP = "";
  String pd_FrpId = request.getParameter("pd_FrpId");
  String pr_NeedResponse = "1";

  /*
   *   hmac
   */
  String keyValue = props.getProperty("keyValue");
  String hmac = PaymentUtil.buildHmac(p0_Cmd, p1_MerId, p2_Order, p3_Amt,
    p4_Cur, p5_Pid, p6_Pcat, p7_Pdesc, p8_Url, p9_SAF, pa_MP,
    pd_FrpId, pr_NeedResponse, keyValue);

  /*
   *         13+1   
   */
  StringBuilder url = new StringBuilder(props.getProperty("url"));
  url.append("?p0_Cmd=").append(p0_Cmd);
  url.append("&p1_MerId=").append(p1_MerId);
  url.append("&p2_Order=").append(p2_Order);
  url.append("&p3_Amt=").append(p3_Amt);
  url.append("&p4_Cur=").append(p4_Cur);
  url.append("&p5_Pid=").append(p5_Pid);
  url.append("&p6_Pcat=").append(p6_Pcat);
  url.append("&p7_Pdesc=").append(p7_Pdesc);
  url.append("&p8_Url=").append(p8_Url);
  url.append("&p9_SAF=").append(p9_SAF);
  url.append("&pa_MP=").append(pa_MP);
  url.append("&pd_FrpId=").append(pd_FrpId);
  url.append("&pr_NeedResponse=").append(pr_NeedResponse);
  url.append("&hmac=").append(hmac);

  System.out.println(url);

  /*
   *       
   */
  response.sendRedirect(url.toString());

  return null;
 }

 /**
  *                               !
  * 
  * @param request
  * @param response
  * @return
  * @throws ServletException
  * @throws IOException
  */
 public String back(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  /*
   * 1.   11 + 1
   */
  String p1_MerId = request.getParameter("p1_MerId");
  String r0_Cmd = request.getParameter("r0_Cmd");
  String r1_Code = request.getParameter("r1_Code");
  String r2_TrxId = request.getParameter("r2_TrxId");
  String r3_Amt = request.getParameter("r3_Amt");
  String r4_Cur = request.getParameter("r4_Cur");
  String r5_Pid = request.getParameter("r5_Pid");
  String r6_Order = request.getParameter("r6_Order");
  String r7_Uid = request.getParameter("r7_Uid");
  String r8_MP = request.getParameter("r8_MP");
  String r9_BType = request.getParameter("r9_BType");

  String hmac = request.getParameter("hmac");

  /*
   * 2.           !
   */
  Properties props = new Properties();
  InputStream input = this.getClass().getClassLoader()
    .getResourceAsStream("merchantInfo.properties");
  props.load(input);
  String keyValue = props.getProperty("keyValue");

  boolean bool = PaymentUtil.verifyCallback(hmac, p1_MerId, r0_Cmd,
    r1_Code, r2_TrxId, r3_Amt, r4_Cur, r5_Pid, r6_Order, r7_Uid,
    r8_MP, r9_BType, keyValue);

  if(!bool) {//      
   request.setAttribute("msg", "        !");
   return "f:/jsps/msg.jsp";
  }

  /*
   * 3.       ,           ,           
   */
  orderService.zhiFu(r6_Order);//           ,      !

  /*
   * 4.         
   *       ,     success      
   */
  if(r9_BType.equals("2")) {
   response.getWriter().print("success");
  }

  /*
   * 5.       ,   msg.jsp
   */
  request.setAttribute("msg", "    !      !    ~");
  return "f:/jsps/msg.jsp";
 }

 /**
  *     
  * 
  * @param request
  * @param response
  * @return
  * @throws ServletException
  * @throws IOException
  */
 public String confirm(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  /*
   * 1.   oid   2.   service   >      ,      ,   msg.jsp 3.
   *       ,   msg.jsp
   */
  String oid = request.getParameter("oid");
  try {
   orderService.confirm(oid);
   request.setAttribute("msg", "  ,    !");
  } catch (OrderException e) {
   request.setAttribute("msg", e.getMessage());
  }
  return "f:/jsps/msg.jsp";
 }

 /**
  *     
  * 
  * @param request
  * @param response
  * @return
  * @throws ServletException
  * @throws IOException
  */
 public String load(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  /*
   * 1.   oid   2.   oid  service    Order 3.
   *    request ,   /jsps/order/desc.jsp
   */
  request.setAttribute("order",
    orderService.load(request.getParameter("oid")));
  return "f:/jsps/order/desc.jsp";
 }

 /**
  *     
  * 
  * @param request
  * @param response
  * @return
  * @throws ServletException
  * @throws IOException
  */
 public String myOrders(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  /*
   * 1.  session      ,    uid 2.
   *   uid  orderService#myOrders(uid)          List<Order> 3.
   *         request  ,   /jsps/order/list.jsp
   */
  User user = (User) request.getSession().getAttribute("session_user");
  List<Order> orderList = orderService.myOrders(user.getUid());
  request.setAttribute("orderList", orderList);
  return "f:/jsps/order/list.jsp";
 }

 /**
  *       session       Order  
  * 
  * @param request
  * @param response
  * @return
  * @throws ServletException
  * @throws IOException
  */
 public String add(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  /*
   * 1.  session   cart 2.   cart  Order   3.   service         4.
   *   order request  ,   /jsps/order/desc.jsp
   */
  //  session   cart
  Cart cart = (Cart) request.getSession().getAttribute("cart");
  //  cart   Order  
  /*
   *   Order  ,     
   * 
   * Cart --> Order
   */
  Order order = new Order();
  order.setOid(CommonUtils.uuid());//     
  order.setOrdertime(new Date());//       
  order.setState(1);//        1,     
  User user = (User) request.getSession().getAttribute("session_user");
  order.setOwner(user);//        
  order.setTotal(cart.getTotal());//        , cart     

  /*
   *         
   * 
   * cartItemList --> orderItemList
   */
  List<OrderItem> orderItemList = new ArrayList<OrderItem>();
  //     Cart    CartItem,     CartItem    OrderItem  ,       
  for (CartItem cartItem : cart.getCartItems()) {
   OrderItem oi = new OrderItem();//       

   oi.setIid(CommonUtils.uuid());//      id
   oi.setCount(cartItem.getCount());//        
   oi.setBook(cartItem.getBook());//        
   oi.setSubtotal(cartItem.getSubtotal());//        
   oi.setOrder(order);//       

   orderItemList.add(oi);//            
  }

  //               
  order.setOrderItemList(orderItemList);

  //      
  cart.clear();

  // ////////////////////////////////////////////

  /*
   * 3.   orderService    
   */
  orderService.add(order);
  /*
   * 4.   order request ,   /jsps/order/desc.jsp
   */
  request.setAttribute("order", order);
  return "/jsps/order/desc.jsp";
 }
}

2.주문 생 성

3.제 주문(사용자 에 따라)

4.주문 서 를 불 러 옵 니 다(id 로 확인)

5.수령 확인

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기