package com.fizz.business.interceptor;

import com.fizz.business.constants.enums.NextUnitEnum;
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 NextUnitEnumTypeHandler extends BaseTypeHandler<NextUnitEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, NextUnitEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getCode());  // 存入数据库时存储code值
    }

    @Override
    public NextUnitEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return NextUnitEnum.fromCode(code);  // 从数据库读取时，根据code值获取枚举
    }

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

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