www.pudn.com > restrant.zip > OrderlineDao.java, change:2014-03-08,size:6972b


package com.alex.restrantol.dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import com.alex.restrantol.base.BaseDao; 
 
import com.alex.restrantol.entity.MealEntity; 
import com.alex.restrantol.entity.OrderEntity; 
import com.alex.restrantol.entity.PageEntity; 
import com.alex.restrantol.util.MealItem; 
import com.alex.restrantol.util.ShoppingCartUtil; 
 
public class OrderlineDao{ 
	public final static int FIND_TODAY_ORDERS = 1; 
	public final static int FIND_BY_COMMAND = 2; 
	public final static int FIND_ALL_ORDERS = 3; 
	public final static int FIND_BY_COMMAND_FRONT = 4; 
	Connection con; 
	PreparedStatement ps; 
	ResultSet rs; 
	 
	public PageEntity getPageEntity(int currentPage){ 
		PageEntity pageEntity = new PageEntity(); 
		pageEntity.setCurrentPage(currentPage); 
		pageEntity.setTotalCount(this.getTotalCount()); 
		pageEntity.setCountAllPage(this.getTotalPage(pageEntity)); 
		if(currentPage >= 1){ 
			int start = currentPage * pageEntity.getCountPerPage() - (pageEntity.getCountPerPage()-1)-1; 
			int end = currentPage * pageEntity.getCountPerPage(); 
			pageEntity.setList(this.findAllOrders(start, end)); 
		} 
		return pageEntity; 
	} 
	/** 
	 * 分类pageEntity 
	 * @param command    where 的条件 
	 * @param currentPage 当前页 
	 * @param way            【1】表示status状态查询  【2】表示按照条件查询,如按照给定的orderID查找对应的一条数据 
	 * @return 
	 */ 
	public PageEntity getPageEntity(String command, int currentPage,int way){ 
		PageEntity pageEntity = new PageEntity(); 
		pageEntity.setCurrentPage(currentPage); 
		pageEntity.setTotalCount(this.getTotalCount()); 
		pageEntity.setCountAllPage(this.getTotalPage(pageEntity)); 
		if(currentPage >= 1){ 
			int start = currentPage * pageEntity.getCountPerPage() - (pageEntity.getCountPerPage()-1)-1; 
			int end = currentPage * pageEntity.getCountPerPage(); 
			if(way == 1) 
				pageEntity.setList(this.findByStatus(start, end,command)); 
			else if(way == 2) 
				pageEntity.setList(this.findById(command)); 
		} 
		return pageEntity; 
	} 
	 
	private List<OrderEntity> findByStatus(int start, int end, String status) { 
		List<OrderEntity> list = execute("select * from orderline where status=? limit ?,?",4,String.valueOf(status),String.valueOf(start),String.valueOf(end)); 
		return list; 
	} 
 
	public Integer getTotalCount(){ 
		int count = 0; 
		con = new BaseDao().getConn(); 
		try { 
			ps = con.prepareStatement("select count(*) from orderline where 1"); 
			rs = ps.executeQuery(); 
			while(rs.next()) 
			count = rs.getInt(1); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return count; 
	} 
	 
	public int getTotalPage(PageEntity pageEntity) { 
		int totalCount = this.getTotalCount();  
		int totalPage = 0;  
		if(totalCount%pageEntity.getCountPerPage()==0) 
			totalPage = totalCount/pageEntity.getCountPerPage();  
		else  
			totalPage = totalCount/pageEntity.getCountPerPage()+1;  
		return totalPage;  
	} 
	 
	public List<OrderEntity> findTodayOrders(){ 
		List<OrderEntity> list = execute("select * from orderline where date(overBookingDate) = curdate()",1); 
		return list; 
	} 
	 
	public List<OrderEntity> findByName(String name){ 
		List<OrderEntity> list = execute("select * from orderline where customerName=?",2,name); 
		return list; 
	} 
	 
	public List<OrderEntity> findByTel(String tel){ 
		List<OrderEntity> list = execute("select * from orderline where customerTel=?",2,tel); 
		return list; 
	} 
	 
	public List<OrderEntity> findByEmail(String email){ 
		List<OrderEntity> list = execute("select * from orderline where customerEmail=?",2,email); 
		return list; 
	} 
	 
	public List<OrderEntity> findById(String id){ 
		List<OrderEntity> list = execute("select * from orderline where orderId=?",2,id); 
		return list; 
	} 
	 
	/** 
	 * 这里是查询所有订单,应该是分页显示的,参照FeelbackMsgDao.java 
	 * @param start 开始index 
	 * @param end   结束index 
	 * @return 返回【end-start】个order 
	 */ 
	public List<OrderEntity> findAllOrders(int start,int end){ 
		List<OrderEntity> list = execute("select * from FindAllOrders limit ?,?",3, 
				String.valueOf(start),String.valueOf(end)); 
		return list; 
	} 
	 
	private List<OrderEntity> execute(String sql,int status,String...vars){ 
		List<OrderEntity> list=new ArrayList<OrderEntity>(); 
		OrderEntity order; 
		ShoppingCartUtil cartUtil; 
		try { 
			con=new BaseDao().getConn(); 
			ps=con.prepareStatement(sql); 
			if(status == 2){ 
				for(String var : vars) 
					ps.setString(1, var); 
			}else if(status == 3){ 
				int temp = 1; 
				for(String var : vars) 
					ps.setInt(temp++, Integer.parseInt(var)); 
			}else if(status == 4){ 
				int temp = 1; 
				for(String var:vars) 
					ps.setInt(temp++,Integer.parseInt(var)); 
			} 
			rs=ps.executeQuery(); 
			while (rs.next()) { 
				order = new OrderEntity(); 
				order.setOrderId(rs.getString("orderId")); 
				order.setCustomerId(rs.getInt("customerId")); 
				order.setName(rs.getString("customerName")); 
				order.setTel(rs.getString("customerTel")); 
				order.setEmail(rs.getString("customerEmail")); 
				order.setAddress(rs.getString("customerAddress")); 
				order.setTaste(rs.getString("taste")); 
				order.setRemark(rs.getString("remark")); 
				order.setTotalPrice(rs.getFloat("totalprice")); 
				order.setDate(rs.getDate("overBookingDate")); 
				order.setStatus(rs.getInt("status")); 
				cartUtil = getCartUtil(rs.getString("orderId")); 
				order.setCart(cartUtil); 
				list.add(order); 
			} 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} finally { 
			new BaseDao().closeAll(con, ps, rs); 
		} 
		return list; 
	} 
 
	private ShoppingCartUtil getCartUtil(String orderId) { 
		ShoppingCartUtil cart = new ShoppingCartUtil(); 
		Connection con = new BaseDao().getConn(); 
		Map<Integer,MealItem> map = new HashMap<Integer,MealItem>(); 
		MealItem mealItem = new MealItem(); 
		int accumulation = 0; 
		PreparedStatement ps = null; 
		ResultSet rs = null; 
		con=new BaseDao().getConn(); 
		try { 
			ps = con.prepareStatement("select * from cart where orderId=?"); 
			ps.setString(1, orderId); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				mealItem = getMealItem(rs.getInt("mealId")); 
				mealItem.setQuantity(rs.getInt("amount")); 
				map.put(rs.getInt("mealId"), mealItem); 
				accumulation+=rs.getInt("amount"); 
			} 
			cart.setTotal(accumulation); 
			cart.setMap(map); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			new BaseDao().closeAll(con, ps, rs); 
		} 
		return cart; 
	} 
 
	private MealItem getMealItem(int mealId) { 
		MealItem mealItem = new MealItem(); 
		MealEntity meal = new MealDao().findMealById(mealId); 
		mealItem.setMeal(meal); 
		return mealItem; 
	} 
		 
}