package com.fizz.business.interceptor;

import com.fizz.business.constants.enums.OperModeEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.context.annotation.Configuration;

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


@Configuration
public class OperModeEnumTypeHandler extends BaseTypeHandler<OperModeEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, OperModeEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getCode()); // 将枚举值的 code 存储到数据库中
    }

    @Override
    public OperModeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return OperModeEnum.fromCode(code); // 从数据库中读取 code 并返回对应的枚举
    }

    @Override
    public OperModeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int code = rs.getInt(columnIndex);
        return OperModeEnum.fromCode(code);
    }

    @Override
    public OperModeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int code = cs.getInt(columnIndex);
        return OperModeEnum.fromCode(code);
    }
}
