订单查询

发表时间:2017-05-15 17:15:03 浏览量( 21 ) 留言数( 0 )

学习目标:

1、了解Java的历史

2、为什么要学习Java语言

3、端正学习态度


学习过程:

确认订单后就可以前台可以查看订单,后台也可以处理订单了,我们先完成前台的订单查询

一、前台订单查询

1、前台订单查询的dao实现,为OrderDao添加方法,新点击的订单默认是没有处理的状态。

  public List<Order> getOrdersByMemberId(int memeberId, Page page) {		List<Order> orders = new ArrayList<Order>();		try {			getCon();			String sqlcount = "select count(*) from orders where member_id=?";			rs = exeQuery(sqlcount, new Object[] { memeberId });			if (rs.next()) {				page.setItmeCount(rs.getInt(1));			}			String sql = "select * from orders where member_id=?  limit ?,? ";			rs = exeQuery(sql, new Object[] { memeberId, page.getItemStart(),					page.getPageSize() });			while (rs.next()) {				Order order = new Order();				order.setAddr(rs.getString("addr"));				order.setCountcash(rs.getDouble("countcash"));				order.setMemberId(rs.getInt("member_id"));				order.setOrderId(rs.getInt("order_id"));				order.setOrderTime(rs.getTimestamp("order_time"));				order.setPhone(rs.getString("phone"));				order.setRealName(rs.getString("real_name"));				order.setStatus(rs.getInt("status"));				orders.add(order);			}		} catch (SQLException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}finally{			closeAll();		}		return orders;	}

2、修改订单处理的servlet代码。

  else if (ope.equals("list")) {			Member member = (Member) session.getAttribute("member");						List<GoodsTypes> goodsTypeses=goodsTypesDao.getAllTypes();						    int currentPage=1;		    String cpage=request.getParameter("cpage");		    if(cpage!=null){		    	currentPage=Integer.parseInt(cpage);		    }			            Page page=new Page();            page.setCurrentPage(currentPage);            page.setPageSize(10);            					List<Order> orders=orderDao.getOrdersByMemberId(member.getMemberId(), page);						request.setAttribute("page", page);			request.setAttribute("orders", orders);			request.setAttribute("goodsTypeses", goodsTypeses);			// 查询用户的所有的订单信息			request.getRequestDispatcher("orderlist.jsp").forward(request,					response);		}

3、修改页面

<!-- content -->
<div id="manage_content_wrapper">
<!-- left -->
<jsp:include page="left.jsp"></jsp:include>
<!-- end of left -->
<div id="manage_content_right">
<div id="manage_order_content">
<div id="order_content_top">
<span class="order_font_1">序号</span> <span class="order_font_2">收货人</span>
<span class="order_font_2">付款方式</span> <span class="order_font_2">总金额</span>
<span class="order_font_2">订单状态</span> <span class="order_font_2">送货方式</span>
<span class="order_font_3">店家信息返馈</span> <span
class="order_font_3">购买日期</span> <span class="order_font_2">相关操作</span>
</div>
<c:forEach items="${orders }" var="order" varStatus="status">
<div class="order_content_li">
<span class="order_font_4">${status.index+1 }</span> <span
class="order_font_5">${order.realName }</span> <span
class="order_font_5">货到付款</span> <span class="order_font_5">${order.countcash
}</span> <span class="order_font_5"> <c:choose>
<c:when test="${ order.status eq 1}">没有处理</c:when>
<c:when test="${ order.status eq 2}">已查看</c:when>
<c:when test="${ order.status eq 3}">已发送</c:when>
<c:when test="${ order.status eq 4}">交易成功</c:when>
<c:otherwise>交易失败</c:otherwise>
</c:choose> </span> <span class="order_font_5">申通快递</span> <span
class="order_font_6">可以送到可以送到可以</span> <span
class="order_font_6">${order.orderTime }</span> <span
class="order_font_5"><a class="show"
style="cursor: pointer;">查看</a> </span>
</div>
<div class="order_content_detil">
<div class="order_content_top2">
<span class="order_font_11">序号</span> <span
class="order_font_13">商品图片</span> <span class="order_font_12">商品名称</span>
<span class="order_font_13">单价</span> <span
class="order_font_13">数量</span> <span class="order_font_13">小计</span>
</div>
<c:forEach items="${order.orderDetails }" var="orderDetail"
varStatus="sta">
<div class="order_content_li2">
<span class="order_font_14">${sta.index+1 }</span> <span
class="order_font_17"><img src="image/pro_image.gif" />
</span> <span class="order_font_15">${orderDetail.goods.goodsName
}</span> <span class="order_font_16">${orderDetail.cash }</span> <span
class="order_font_16">${orderDetail.nums }</span> <span
class="order_font_16">${orderDetail.cash*orderDetail.nums
}</span>
</div>
</c:forEach>
</div>
</c:forEach>
<!-- page -->
<div id="pageTag">
<span class="noTopPage"><a href="orderServlet?ope=list&cpage=1">首页</a> </span> 
<span class="hasFirstPage"><a href="orderServlet?ope=list&cpage=${page.currentPage-1 }">上一页</a> </span> 
<span class="Pages"><a href="#">1</a> </span> 
<span class="currentPage"><a>2</a>
</span>
 <span class="Pages"><a href="#">3</a> </span> <span class="epi">...</span>
<span class="epiLastPage"><a href="#">10</a> </span> <span
class="hasNextPage"><a href="orderServlet?ope=list&cpage=${page.currentPage+1 }">下一页</a> </span> <span
class="hasLastPage"><a href="orderServlet?ope=list&cpage=${page.pageCount }">尾页</a> </span>
</div>
<!-- end of page -->
</div>
</div>
</div>
<!-- end of content -->

js代码实现:

  <script type="text/javascript">	$(document).ready(function() {		// 在这里写你的代码...		$(".show").toggle(function() {			//$(this).addClass("selected");			$(this).parent().parent().next("div").show("slow");			$(this).html("关闭");		}, function() {			//$(this).removeClass("selected");			$(this).parent().parent().next("div").hide("slow");			$(this).html("查看");		});	});</script>

效果如下:

attcontent/05cfe9f1-e293-4740-bf92-fd9a39308192.png

二、后台订单处理

后台需要完成订单查询,并能够根据订单状态查询,然后处理订单

attcontent/5a956cbf-cbdd-4641-b2e8-435ce9ff2367.png

点击查看详情后就可以处理订单了。

attcontent/bf5ceaa4-f94e-4a91-9a3f-166981d551d7.png

下面是代码的实现:

1、dao层的实现。修改OrderDao,添加三个方法

  	/**	 * 后台根据订单状态查询	 * 	 * @param status	 *            订单状态	 * @param page	 *            分页信息	 * @return	 */	public List<Order> getOrderByStatus(int status, Page page) {		OrderDetailDao orderDetailDao = new OrderDetailDao();		List<Order> orders = new ArrayList<Order>();		try {			getCon();			String sqlcount = "select count(*) from orders where status=?";			rs = exeQuery(sqlcount, new Object[] { status });			if (rs.next()) {				page.setItmeCount(rs.getInt(1));			}			String sql = "select * from orders where status=? order by status limit ?,? ";			rs = exeQuery(					sql,					new Object[] { status, page.getItemStart(),							page.getPageSize() });			while (rs.next()) {				Order order = new Order();				order.setAddr(rs.getString("addr"));				order.setCountcash(rs.getDouble("countcash"));				order.setMemberId(rs.getInt("member_id"));				order.setOrderId(rs.getInt("order_id"));				order.setOrderTime(rs.getTimestamp("order_time"));				order.setPhone(rs.getString("phone"));				order.setRealName(rs.getString("real_name"));				order.setStatus(rs.getInt("status"));				// 把订单详情查询出来				List<OrderDetail> orderDetails = orderDetailDao						.getDetailByOrderId(order.getOrderId());				order.setOrderDetails(orderDetails);				orders.add(order);			}		} catch (SQLException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}finally{			closeAll();		}		return orders;	}	/**	 * 根据订单Id查询,已经把该订单的详情页查询出来了。	 * 	 * @param id	 * @return	 */	public Order getOrderById(int id) {		OrderDetailDao orderDetailDao = new OrderDetailDao();		Order order = null;		try {			getCon();			String sql = "select * from orders where order_id=? ";			rs = exeQuery(sql, new Object[] { id });			if (rs.next()) {				order = new Order();				order.setAddr(rs.getString("addr"));				order.setCountcash(rs.getDouble("countcash"));				order.setMemberId(rs.getInt("member_id"));				order.setOrderId(rs.getInt("order_id"));				order.setOrderTime(rs.getTimestamp("order_time"));				order.setPhone(rs.getString("phone"));				order.setRealName(rs.getString("real_name"));				order.setStatus(rs.getInt("status"));				// 把订单详情查询出来				List<OrderDetail> orderDetails = orderDetailDao						.getDetailByOrderId(order.getOrderId());				order.setOrderDetails(orderDetails);			}		} catch (SQLException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}finally{			closeAll();		}		return order;	}	public int updateOrderStatus(int orderId, int status) {		int result = 0;		getCon();		String sql="update orders set status=? where order_id=?";		result=exeUpdate(sql, new Object[]{status,orderId});		closeAll();		return result;	}

修改OrderDetailDao类,点击一个方法

  public class OrderDetailDao extends BaseDao{			/**	 * 根据订单Id获得订单详情	 * @param orderId	 * @return	 */	public List<OrderDetail> getDetailByOrderId(int orderId){		GoodsDao goodsDao=new GoodsDao();				List<OrderDetail> orderDetails=new ArrayList<OrderDetail>();		getCon();		String sql="select * from order_detail where order_id=?";		rs=exeQuery(sql, new Object[]{orderId});				try {			while(rs.next()){				OrderDetail orderDetail=new OrderDetail();				orderDetail.setCash(rs.getDouble("cash"));				orderDetail.setDetailId(rs.getInt("detail_Id"));				orderDetail.setGoodsId(rs.getInt("goods_id"));				orderDetail.setNums(rs.getInt("nums"));								//查询对应的商品信息				Goods goods=goodsDao.getById(orderDetail.getGoodsId());				orderDetail.setGoods(goods);								orderDetails.add(orderDetail);			}		} catch (SQLException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}finally{			closeAll();		}				return orderDetails;			}	}

2、servlet实现

  /** * 后台订单处理 --订单查询,订单详情查询,订单处理 *  * @author Administrator *  */public class OrderAdminServlet extends HttpServlet {	public void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		OrderDao orderDao = new OrderDao();		OrderDetailDao orderDetailDao = new OrderDetailDao();		MemberDao memberDao=new MemberDao();				String ope = request.getParameter("ope");		if (ope == null || ope.equals("orderlist")) {			int currentPage = 1;// 当前页			int status = Order.NOT;// 订单状态			// 获得用户指定的当前页和订单状态			String cpage = request.getParameter("cpage");			String sta = request.getParameter("status");			if (cpage != null) {				currentPage = Integer.parseInt(cpage);			}			if (sta != null) {				status = Integer.parseInt(sta);			}			// 分页信息			Page page = new Page();			page.setCurrentPage(currentPage);			// 查询数据库			List<Order> orders = orderDao.getOrderByStatus(status, page);			request.setAttribute("status", status);			request.setAttribute("orders", orders);			request.setAttribute("page", page);			request.getRequestDispatcher("orderlist.jsp").forward(request,					response);		} else if (ope.equals("detaillist")) {			int orderId = Integer.parseInt(request.getParameter("oid"));			Order order = orderDao.getOrderById(orderId);			request.setAttribute("order", order);			request.getRequestDispatcher("orderdeal.jsp").forward(request,					response);		} else if (ope.equals("deal")) {						int orderId = Integer.parseInt(request.getParameter("orderId"));			int status = Integer.parseInt(request.getParameter("status"));						int result=orderDao.updateOrderStatus(orderId, status);						if(status==Order.SENDED){//发送邮件								//1、获得用户的邮箱地址				Order order=orderDao.getOrderById(orderId);				Member member=memberDao.getMemberById(order.getMemberId());								//2、发送邮件  启动一个线程				MailSenderInfo mailSenderInfo=new MailSenderInfo();				mailSenderInfo.setToAddress(member.getMemberEmail());				mailSenderInfo.setSubject("商品已经发送了");				mailSenderInfo.setContent("<b>你好</b>"+member.getRealName()+"你的邮件已经发送了。");								SimpleMailSender sender=new SimpleMailSender();				sender.sendHtmlMail(mailSenderInfo);							}						if (result>0) {			   //修改成功					response.sendRedirect("OrderAdminServlet");			}else{				response.sendRedirect("error.jsp");			}					}	}	public void doPost(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		doGet(request, response);	}}