package com.fizz.business.interceptor;


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

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, PlanStatusEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getStatus());  // 将枚举的status字段存储到数据库
    }

    @Override
    public PlanStatusEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String status = rs.getString(columnName);
        return PlanStatusEnum.fromStatus(status);  // 根据数据库中的status字段值，返回对应的枚举
    }

    @Override
    public PlanStatusEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String status = rs.getString(columnIndex);
        return PlanStatusEnum.fromStatus(status);
    }

    @Override
    public PlanStatusEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String status = cs.getString(columnIndex);
        return PlanStatusEnum.fromStatus(status);
    }
}
