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

import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.frame.module.admin.entity.vo.AddressVo;
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(AddressVo.class)
public class AddressTypeHandler extends BaseTypeHandler<AddressVo> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, AddressVo parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i,parameter.forJsonAddress().toJSONString());
    }

    @Override
    public AddressVo getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return new AddressVo(JSONObject.parseObject(rs.getString(columnName)));
    }

    @Override
    public AddressVo getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return new AddressVo(JSONObject.parseObject(rs.getString(columnIndex)));
    }

    @Override
    public AddressVo getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return new AddressVo(JSONObject.parseObject(cs.getString(columnIndex)));
    }
}
