package com.ruoyi.ibs.handler;

import com.ruoyi.ibs.enumertion.CustIdTypeEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

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

/**
 * @Author 吴凯程
 * @Date 2024/11/21
 **/

@MappedTypes(CustIdTypeEnum.class)
public class CustIdTypeHandler extends BaseTypeHandler<CustIdTypeEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, CustIdTypeEnum parameter, JdbcType jdbcType) throws SQLException {
        // 存入数据库时，将枚举转换为字符串
        ps.setString(i, parameter.getCode());
    }

    @Override
    public CustIdTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // 从数据库读取时，将字符串转换为枚举
        String code = rs.getString(columnName);
        return CustIdTypeEnum.getByCode(code);
    }

    @Override
    public CustIdTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String code = rs.getString(columnIndex);
        return CustIdTypeEnum.getByCode(code);
    }

    @Override
    public CustIdTypeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String code = cs.getString(columnIndex);
        return CustIdTypeEnum.getByCode(code);
    }

}
