package com.cn.dao.impl; // 数据访问对象实现包

import java.sql.*; // 导入SQL相关类
import java.util.*; // 导入集合相关类

import com.cn.dao.AdminDao; // 导入AdminDao接口
import com.cn.domain.Admin; // 导入Admin实体类
import com.cn.util.JDBCUtil; // 导入JDBC工具类

/**
 * 实现AdminDao接口，提供管理员数据访问操作。
 */
public class AdminDaoImpl implements AdminDao {
    private Connection conn = null; // 数据库连接
    private PreparedStatement pstmt = null; // 预编译语句
    private ResultSet rs = null; // 结果集

    @Override
    public int add(Admin admin) throws SQLException { // 添加管理员
        String sql = "insert into admins (username,password,creattime,flag,isuse,logintime) VALUES (?,?,?,?,?,?)";
        conn = JDBCUtil.getConnection(); // 获取连接
        pstmt = conn.prepareStatement(sql); // 准备SQL语句
        pstmt.setObject(1, admin.getUserName()); // 设置参数
        pstmt.setObject(2, admin.getPassword());
        pstmt.setObject(3, admin.getCreatTime());
        pstmt.setObject(4, admin.getFlag());
        pstmt.setObject(5, admin.getIsUse());
        pstmt.setObject(6, admin.getLoginTime());
        int status = pstmt.executeUpdate(); // 执行插入
        closeResources(); // 关闭资源
        return status;
    }

    @Override
    public int delete(int adminId) throws SQLException { // 删除管理员
        String sql = "delete from admins where adminid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, adminId);
        int status = pstmt.executeUpdate();
        closeResources();
        return status;
    }

    @Override
    public int update(Admin admin) throws SQLException { // 更新管理员信息
        String sql = "update admins set username=?,password=?,flag=?,isuse=?,logintime=? where adminid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, admin.getUserName());
        pstmt.setObject(2, admin.getPassword());
        pstmt.setObject(3, admin.getFlag());
        pstmt.setObject(4, admin.getIsUse());
        pstmt.setObject(5, admin.getLoginTime());
        pstmt.setObject(6, admin.getAdminId());
        int status = pstmt.executeUpdate();
        closeResources();
        return status;
    }

    @Override
    public Admin getById(int adminId) throws SQLException { // 根据ID获取管理员
        String sql = "select * from admins where adminid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, adminId);
        rs = pstmt.executeQuery();
        Admin admin = null;
        if (rs.next()) {
            admin = resultSetToAdmin(rs); // 将结果集转换为Admin对象
        }
        closeResources();
        return admin;
    }

    @Override
    public List<Admin> getAll() throws SQLException { // 获取所有管理员
        String sql = "select * from admins";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        List<Admin> admins = new ArrayList<>();
        while (rs.next()) {
            admins.add(resultSetToAdmin(rs)); // 将结果集转换为Admin对象并添加到列表
        }
        closeResources();
        return admins;
    }

    @Override
    public Admin getByName(String userName) throws SQLException { // 根据用户名获取管理员
        String sql = "select * from admins where username=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, userName);
        rs = pstmt.executeQuery();
        Admin admin = null;
        if (rs.next()) {
            admin = resultSetToAdmin(rs); // 将结果集转换为Admin对象
        }
        closeResources();
        return admin;
    }

    // 将结果集转换为Admin对象的方法
    private Admin resultSetToAdmin(ResultSet rs) throws SQLException {
        int adminId = rs.getInt("adminId");
        String userName = rs.getString("userName");
        String password = rs.getString("password");
        Timestamp creatTime = rs.getTimestamp("creatTime");
        int flag = rs.getInt("flag");
        int isUse = rs.getInt("isUse");
        Timestamp loginTime = rs.getTimestamp("loginTime");
        return new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime);
    }

    // 关闭数据库资源的方法
    private void closeResources() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
