Package hotel.service.impl

Source Code of hotel.service.impl.UserServiceImpl

package hotel.service.impl;

import hotel.model.Order;
import hotel.model.OrderInfo;
import hotel.model.Room;
import hotel.model.User;
import hotel.service.RoomService;
import hotel.service.UserService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*
* 类说明
*
* @author lipeiying
* @version 创建时间:2011-12-31 下午03:41:34
*/
public class UserServiceImpl implements UserService {
  private RoomService roomService;

  public void setRoomService(RoomService roomService) {
    this.roomService = roomService;
  }
  @Override
  public void createUser(User user) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "insert into userinfo (username,password,telephone,type) values (?,?,?,?)";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, user.getUsername());
      ps.setString(2, user.getPassword());
      ps.setString(3, user.getTelephone());
      ps.setInt(4, user.getType());
      ps.executeUpdate();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
  }

  @Override
  public User login(String username, String password, int type) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    User user = null;
    String sql = "select * from userinfo where username = ? and password = ? and type = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, username);
      ps.setString(2, password);
      ps.setInt(3, type);
      rs = ps.executeQuery();
      if (rs.next()) {
        String a = rs.getString("username");
        String b = rs.getString("password");
        String c = rs.getString("telephone");
        int i = rs.getInt("type");
        user = new User(a, b, c, i);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return user;
  }

  @Override
  public boolean isExist(String username) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "select * from userinfo where username = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, username);
      rs = ps.executeQuery();
      if (rs.next())
        return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return false;
  }

  @Override
  public List<Order> listOrder(String username) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<Order> list = new ArrayList<Order>();
    String sql = "select * from orders where username = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, username);
      rs = ps.executeQuery();
      while (rs.next()) {
        int orderId = rs.getInt("orderId");
        String username2 = rs.getString("username");
        String phone = rs.getString("phone");
        Date ordertime = rs.getDate("ordertime");
        float totalprices = rs.getFloat("totalprices");
        int status = rs.getInt("status");
        Order order = new Order();
        order.setOrderId(orderId);
        order.setUsername(username2);
        order.setTelephone(phone);
        order.setTime(ordertime);
        order.setTotalPrices(totalprices);
        order.setStatus(status);
        list.add(order);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return list;
  }

  @Override
  public boolean createOrder(User user) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "insert into orders (username,phone,ordertime,totalprices,status) values (?,?,?,?,?)";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, user.getUsername());
      ps.setString(2, user.getTelephone());
      ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));
      ps.setFloat(4, 0);
      ps.setInt(5, Order.NEW);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

  @Override
  public List<OrderInfo> listOrderInfo(int orderid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<OrderInfo> list = new ArrayList<OrderInfo>();
    String sql = "select * from orderinfo where orderid = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, orderid);
      rs = ps.executeQuery();
      while (rs.next()) {
        int uid = rs.getInt("uid");
        String roomid = rs.getString("roomid");
        int orderid2 = rs.getInt("orderid");
        int days = rs.getInt("days");
        int action = rs.getInt("action");
        float totalprices = rs.getFloat("totalprices");
        OrderInfo info = new OrderInfo();
        info.setUid(uid);
        info.setRoomNumber(roomid);
        info.setDays(days);
        info.setAction(action);
        info.setOrderId(orderid2);
        info.setTotalprices(totalprices);
        list.add(info);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return list;
  }

  @Override
  public Order getOrder(int orderid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Order order = null;
    String sql = "select * from orders where orderid = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, orderid);
      rs = ps.executeQuery();
      if (rs.next()) {
        int orderId = rs.getInt("orderId");
        String username = rs.getString("username");
        String phone = rs.getString("phone");
        Date ordertime = rs.getDate("ordertime");
        float totalprices = rs.getFloat("totalprices");
        int status = rs.getInt("status");
        order = new Order();
        order.setOrderId(orderId);
        order.setUsername(username);
        order.setTelephone(phone);
        order.setTime(ordertime);
        order.setTotalPrices(totalprices);
        order.setStatus(status);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return order;
  }

  @Override
  public boolean bookRoom(int orderid, String roomid, int days) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "insert into orderinfo (roomid,orderid,days,action,totalprices) values (?,?,?,?,?)";
    Room room = roomService.getRoom(roomid);
    float totalprices = room.getPrice() * days;
    if (!roomService.bookRoom(roomid)) {
      return false;
    }
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, roomid);
      ps.setInt(2, orderid);
      ps.setInt(3, days);
      ps.setInt(4, OrderInfo.RESERVE);
      ps.setFloat(5, totalprices);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

  @Override
  public boolean cancelOrder(int orderid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "update orders set status = ? where orderid = ?";
    // 把订单里所有已经预订的房间取消
    List<OrderInfo> list = listOrderInfo(orderid);
    for (OrderInfo info : list) {
      String roomid = info.getRoomNumber();
      roomService.clearRoom(roomid);
    }
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, Order.CANCEL);
      ps.setInt(2, orderid);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

  @Override
  public boolean cancelRoom(int orderid, String roomid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "delete from orderinfo where orderid = ? and roomid = ?";
    if (!roomService.clearRoom(roomid)) {
      return false;
    }
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, orderid);
      ps.setString(2, roomid);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

  @Override
  public boolean checkIn(int orderid, String roomid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "update orderinfo set action = ? where orderid = ? and roomid = ?";
    if (!roomService.checkInRoom(roomid)) {
      return false;
    }
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, OrderInfo.CHECKIN);
      ps.setInt(2, orderid);
      ps.setString(3, roomid);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

  @Override
  public boolean checkOut(int orderid, String roomid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "update orderinfo set action = ? where orderid = ? and roomid = ?";
    if (!roomService.clearRoom(roomid)) {
      return false;
    }
    // 计算价格
    List<OrderInfo> infos = listOrderInfo(orderid);
    float totalprices = 0;
    for (OrderInfo info : infos) {
      if (info.getRoomNumber().equals(roomid))
        totalprices = info.getTotalprices();
    }
    try {
      conn = JdbcUtil.getConnection();
      conn.setAutoCommit(false);
      ps = conn.prepareStatement(sql);
      ps.setInt(1, OrderInfo.CHECKOUT);
      ps.setInt(2, orderid);
      ps.setString(3, roomid);
      ps.execute();
      sql = "update orders set totalprices = totalprices + ? where orderid = ?";
      ps = conn.prepareStatement(sql);
      ps.setFloat(1, totalprices);
      ps.setInt(2, orderid);
      ps.execute();
      conn.commit();
    } catch (Exception e) {
      try {
        conn.rollback();
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

  @Override
  public boolean submitOrder(int orderid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "update orders set status = ? where orderid = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, Order.SUBMIT);
      ps.setInt(2, orderid);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }
  @Override
  public boolean isOrderCanChange(int orderid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "select * from orders where orderid = ?";
    boolean result = false;
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, orderid);
      rs = ps.executeQuery();
      if (rs.next()) {
        int status = rs.getInt("status");
        if (Order.NEW == status)
          result = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return result;
  }
  @Override
  public List<Order> listOrder() {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<Order> list = new ArrayList<Order>();
    String sql = "select * from orders where status = ? or status = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, Order.NEW);
      ps.setInt(2, Order.SUBMIT);
      rs = ps.executeQuery();
      while (rs.next()) {
        int orderId = rs.getInt("orderId");
        String username2 = rs.getString("username");
        String phone = rs.getString("phone");
        Date ordertime = rs.getDate("ordertime");
        float totalprices = rs.getFloat("totalprices");
        int status = rs.getInt("status");
        Order order = new Order();
        order.setOrderId(orderId);
        order.setUsername(username2);
        order.setTelephone(phone);
        order.setTime(ordertime);
        order.setTotalPrices(totalprices);
        order.setStatus(status);
        list.add(order);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return list;
  }
  @Override
  public boolean directIn(int orderid, String roomid, int days) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "insert into orderinfo (roomid,orderid,days,action,totalprices) values (?,?,?,?,?)";
    Room room = roomService.getRoom(roomid);
    float totalprices = room.getPrice() * days;
    if (!roomService.checkInRoom(roomid)) {
      return false;
    }
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setString(1, roomid);
      ps.setInt(2, orderid);
      ps.setInt(3, days);
      ps.setInt(4, OrderInfo.CHECKIN);
      ps.setFloat(5, totalprices);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }
  @Override
  public boolean overOrder(int orderid) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "update orders set status = ? where orderid = ?";
    try {
      conn = JdbcUtil.getConnection();
      ps = conn.prepareStatement(sql);
      ps.setInt(1, Order.OVER);
      ps.setInt(2, orderid);
      ps.execute();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      JdbcUtil.free(rs, ps, conn);
    }
    return true;
  }

}
TOP

Related Classes of hotel.service.impl.UserServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.