package com.hotent.base.kingbaseTypeHandler;

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.*;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @company: 广州宏天软件股份有限公司
 * @description: 描述
 * @author: czm
 * @create: 2021-10-21
 **/
@MappedTypes({LocalDate.class})
@MappedJdbcTypes(JdbcType.TIMESTAMP)
public class MyLocalDateTypeHandler extends BaseTypeHandler<LocalDate> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setObject(i, parameter);
    }

    @Override
    public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Object object = rs.getObject(columnName);
        if (null == object) {
            return null;
        }
        if (object instanceof Timestamp) {
            return ((Timestamp)object).toLocalDateTime().toLocalDate();
        }
        return rs.getObject(columnName, LocalDate.class);
    }

    @Override
    public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return rs.getObject(columnIndex, LocalDate.class);
    }

    @Override
    public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getObject(columnIndex, LocalDate.class);
    }
}
