package com.cn.dao.impl; // 实现MemberDao接口的包

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

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

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

    @Override
    public int add(Member member) throws SQLException { // 添加会员
        String sql = "insert into member(username,password,registertime,ifuse,logintimes) values(?,?,?,?,?)";
        conn = JDBCUtil.getConnection(); // 获取数据库连接
        pstmt = conn.prepareStatement(sql); // 准备SQL语句
        pstmt.setObject(1, member.getUserName()); // 设置参数
        pstmt.setObject(2, member.getPassword());
        pstmt.setObject(3, member.getRegisterTime());
        pstmt.setObject(4, member.getIfUse());
        pstmt.setObject(5, member.getLogintimes());
        int status = pstmt.executeUpdate(); // 执行插入
        closeResources(); // 关闭资源
        return status;
    }

    @Override
    public int delete(int memberId) throws SQLException { // 根据ID删除会员
        String sql = "delete from member where memberid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, memberId);
        int status = pstmt.executeUpdate();
        closeResources();
        return status;
    }

    @Override
    public int update(Member member) throws SQLException { // 更新会员信息
        String sql = "update member set username=?,password=?,ifuse=?,logintimes=? where memberid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, member.getUserName());
        pstmt.setObject(2, member.getPassword());
        pstmt.setObject(3, member.getIfUse());
        pstmt.setObject(4, member.getLogintimes());
        pstmt.setObject(5, member.getMemberId());
        int status = pstmt.executeUpdate();
        closeResources();
        return status;
    }

    @Override
    public List<Member> getAll() throws SQLException { // 获取所有会员
        String sql = "select * from member";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        List<Member> members = new ArrayList<>();
        while (rs.next()) {
            Member member = resultSetToMember(rs); // 将结果集转换为Member对象
            members.add(member);
        }
        closeResources();
        return members;
    }

    @Override
    public Member getMemberById(int memberId) throws SQLException { // 根据ID获取会员
        String sql = "select * from member where memberid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, memberId);
        rs = pstmt.executeQuery();
        Member member = null;
        if (rs.next()) {
            member = resultSetToMember(rs); // 将结果集转换为Member对象
        }
        closeResources();
        return member;
    }

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

    // 将结果集转换为Member对象的方法
    private Member resultSetToMember(ResultSet rs) throws SQLException {
        int memberId = rs.getInt("memberId");
        String userName = rs.getString("userName");
        String password = rs.getString("password");
        Timestamp registerTime = rs.getTimestamp("registerTime");
        int ifUse = rs.getInt("ifUse");
        Timestamp logintimes = rs.getTimestamp("logintimes");
        return new Member(memberId, userName, password, registerTime, ifUse, logintimes);
    }

    // 关闭数据库资源的方法
    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();
            }
        }
    }
}
