package com.cesgroup.mybatisHomework.handle;

import cn.hutool.json.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

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

public class PersonTypeHandle extends BaseTypeHandler<JSONObject> {
  @Override
  public JSONObject getNullableResult(ResultSet rs, String cloumName) throws SQLException {
    String infoJsonString = rs.getString(cloumName);
    JSONObject infObject = null;
    try {
      infObject = new JSONObject(infoJsonString);
      //System.out.println("----将json串转成json对象成功！----");
    } catch (Exception e) {
      System.err.println("将json串转成json对象失败！");
      e.printStackTrace();
    }
    return infObject;
  }

  @Override
  public JSONObject getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
    return null;
  }

  @Override
  public void setNonNullParameter(PreparedStatement statement, int i, JSONObject infoObject, JdbcType jdbcType) throws SQLException {
    try {
      String userInfoString = infoObject.toString();
      statement.setString(i, userInfoString);
    } catch (Exception e) {
      System.out.println("----将对象转化为字符串失败！----");
      e.printStackTrace();
    }
  }

  @Override
  public JSONObject getNullableResult(ResultSet arg0, int arg1) throws SQLException {
    return null;
  }

}
