package dao.impl;

import common.DBTool;
import dao.UserDB;
import pojo.UserInfo;

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

public class UserDBImpl implements UserDB {

    @Override
    public UserInfo getUserByName(String username) {
        UserInfo u = new UserInfo();
        ResultSet rs;
        try{
            String sql = "select * from t_user where VC_LOGIN_NAME = ?";
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if(rs.next()) {
                u.setId(rs.getInt("N_USER_ID"));
                u.setName(rs.getString("VC_LOGIN_NAME"));
                u.setPass(rs.getString("VC_PASSWORD"));
            }else{
                u = null;
            }
            DBTool.close(ps, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return u;
    }

    @Override
    public UserInfo getUserById(int id) {
        UserInfo u = new UserInfo();
        ResultSet rs;
        try{
            String sql = "select * from t_user where N_USER_ID = ?";
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if(rs.next()) {
                u.setId(rs.getInt("N_USER_ID"));
                u.setName(rs.getString("VC_LOGIN_NAME"));
                u.setPass(rs.getString("VC_PASSWORD"));
            }else{
                u = null;
            }
            DBTool.close(ps, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return u;
    }

    @Override
    public int addUser(UserInfo user) {
        int lines;
        String sql = "INSERT INTO t_user (VC_LOGIN_NAME, VC_PASSWORD) VALUES (?, ?)";
        try{
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPass());
            lines = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return lines;
    }

    @Override
    public int updateUser(UserInfo user) {
        int lines;
        String sql = "UPDATE t_user SET VC_PASSWORD = ?,VC_LOGIN_NAME = ? WHERE N_USER_ID = ?";
        try{
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setString(1, user.getPass());
            ps.setString(2, user.getName());
            ps.setInt(3, user.getId());
            lines = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return lines;
    }

    @Override
    public int deleteUser(int id) {
        int lines = 0;
        String sql = "DELETE FROM t_user WHERE N_USER_ID = ?";
        try{
           PreparedStatement ps = DBTool.getPreparedStatement(sql);
           ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
}
