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

import java.sql.*; // 导入SQL相关类
import com.cn.dao.PmemberDao; // 导入PmemberDao接口
import com.cn.domain.Pmember; // 导入Pmember实体类
import com.cn.util.JDBCUtil; // 导入JDBC工具类

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

    @Override
    public int add(Pmember pmember) throws SQLException { // 添加个人会员
        String sql = "insert into pmember (memberid,realname,sex,age,idcard) values (?,?,?,?,?)";
        conn = JDBCUtil.getConnection(); // 获取数据库连接
        pstmt = conn.prepareStatement(sql); // 准备SQL语句
        pstmt.setObject(1, pmember.getMemberId()); // 设置参数
        pstmt.setObject(2, pmember.getRealName());
        pstmt.setObject(3, pmember.getSex());
        pstmt.setObject(4, pmember.getAge());
        pstmt.setObject(5, pmember.getIdCard());
        int status = pstmt.executeUpdate(); // 执行插入
        closeResources(); // 关闭资源
        return status;
    }

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

    @Override
    public int update(Pmember pmember) throws SQLException { // 更新个人会员信息
        String sql = "update pmember set memberid=?,realname=?,sex=?,age=?,idcard=? where pmemberid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, pmember.getMemberId());
        pstmt.setObject(2, pmember.getRealName());
        pstmt.setObject(3, pmember.getSex());
        pstmt.setObject(4, pmember.getAge());
        pstmt.setObject(5, pmember.getIdCard());
        pstmt.setObject(6, pmember.getPmemberId());
        int status = pstmt.executeUpdate();
        closeResources();
        return status;
    }

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

    @Override
    public Pmember getById(Integer pmemberId) throws SQLException { // 根据个人会员ID获取个人会员
        String sql = "select * from pmember where pmemberid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, pmemberId);
        rs = pstmt.executeQuery();
        Pmember pmember = null;
        if (rs.next()) {
            pmember = resultSetToPmember(rs); // 将结果集转换为Pmember对象
        }
        closeResources();
        return pmember;
    }

    // 将结果集转换为Pmember对象的方法
    private Pmember resultSetToPmember(ResultSet rs) throws SQLException {
        int pmemberId = rs.getInt("pmemberId");
        int memberId = rs.getInt("memberId");
        String realName = rs.getString("realName");
        String sex = rs.getString("sex");
        int age = rs.getInt("age");
        String idCard = rs.getString("idCard");
        return new Pmember(pmemberId, memberId, realName, sex, age, idCard);
    }

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