package com.bohuikeji.frame.module.admin.handler;

import com.bohuikeji.frame.module.admin.entity.configure.PageConf;
import com.bohuikeji.frame.module.admin.utils.JsonUtil;
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;

/**
 * 页面配置转换器
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(PageConf.class)
public class PageConfTypeHandler extends BaseTypeHandler<PageConf> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, PageConf pageConf, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JsonUtil.objToJson(pageConf).toJSONString());
    }

    @Override
    public PageConf getNullableResult(ResultSet rs, String s) throws SQLException {
        return JsonUtil.strToObj(rs.getString(s),PageConf.class);
    }

    @Override
    public PageConf getNullableResult(ResultSet rs, int i) throws SQLException {
        return JsonUtil.strToObj(rs.getString(i),PageConf.class);
    }

    @Override
    public PageConf getNullableResult(CallableStatement cs, int i) throws SQLException {
        return JsonUtil.strToObj(cs.getString(i),PageConf.class);
    }
}
