package com.hz.pm.api.common.handler;


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

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

/**
 * <p>
 * BooleanBitTypeHandler
 * </p>
 *
 * @author WendyYang
 * @since 15:39 2023/12/15
 */
@Slf4j
@MappedJdbcTypes(value = JdbcType.BIT)
@MappedTypes(value = {Boolean.class, boolean.class})
public class BooleanBitTypeHandler extends BaseTypeHandler<Boolean> {

    public BooleanBitTypeHandler() {
        log.info(this.getClass().getSimpleName());
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null) {
            ps.setInt(i, 1);
        } else {
            ps.setInt(i, parameter ? 1 : 0);
        }
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Byte man = rs.getObject(columnName, Byte.class);
        return man == null ? null : (man == 1);
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Byte man = rs.getObject(columnIndex, Byte.class);
        return man == null ? null : (man == 1);
    }

    @Override
    public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Byte man = cs.getObject(columnIndex, Byte.class);
        return man == null ? null : (man == 1);
    }
}