package com.skic.DAO.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.skic.DAO.UserDAO;
import com.skic.DTO.UserDTO;

public class UserDAOimpl implements UserDAO {

	@Override
	public int insert(Connection conn, UserDTO user) throws SQLException {
		PreparedStatement pre = conn.prepareStatement(""
				+ "INSERT INTO tb_skicvip (col_userName, col_realName, col_passWord, col_email, col_phoneNum, col_vipFlag, col_adminFlag, col_rootFlag) VALUES(?,?,?,?,?,?,?,?)");
		pre.setString(1, user.getUserName());
		pre.setString(2, user.getRealName());
		pre.setString(3, user.getPassword());
		pre.setString(4, user.getEmail());
		pre.setString(5, user.getPhoneNum());
		pre.setInt(6, user.getVip());
		pre.setInt(7, user.getAdmin());
		pre.setInt(8, user.getRoot());	
		int res = pre.executeUpdate();
		System.out.println("tb_skicvip"+res+"条记录");
		pre.close();
		return res;
	}

	@Override
	public boolean update(Connection conn, long PK, UserDTO user) throws SQLException {
		PreparedStatement pre = conn.prepareStatement(""
				+ "UPDATE tb_skicvip SET col_userName=?,col_realName=?,col_passWord=?,col_email=?,col_phoneNum=?,col_vipFlag=?,col_adminFlag=?,col_rootFlag=? WHERE col_id=?");
		String col_id = String.valueOf(PK);
		pre.setString(1, user.getUserName());
		pre.setString(2, user.getRealName());
		pre.setString(3, user.getPassword());
		pre.setString(4, user.getEmail());
		pre.setString(5, user.getPhoneNum());
		pre.setInt(6, user.getVip());
		pre.setInt(7, user.getAdmin());
		pre.setInt(8, user.getRoot());
		pre.setString(9, col_id);
		boolean res = pre.execute();
		pre.close();
		return res;
	}

	@Override
	public boolean delete(Connection conn, UserDTO user) throws SQLException {
		PreparedStatement pre = conn.prepareStatement("DELETE FROM tb_skicvip WHERE col_userName=?");
		pre.setString(1, user.getUserName());
		boolean res = pre.execute();
		pre.close();
		return res;
	}

	@Override
	public ResultSet select(Connection conn, UserDTO user) throws SQLException {
		PreparedStatement pre = conn
				.prepareStatement(
						"SELECT col_userName, col_realName, col_passWord, col_email, col_phoneNum, col_vipFlag, col_adminFlag, col_rootFlag FROM tb_skicvip WHERE col_userName=? AND col_passWord=?");
		pre.setString(1, user.getUserName());
		pre.setString(2, user.getPassword());
		return pre.executeQuery();
	}

	@Override
	public ResultSet showAll(Connection conn, UserDTO user) throws SQLException {
		PreparedStatement pre = conn
				.prepareStatement(
						"SELECT col_userName, col_realName, col_passWord, col_email, col_phoneNum, col_vipFlag, col_adminFlag, col_rootFlag FROM tb_skicvip");
		return pre.executeQuery();
	}

}
