package com.ruoyi.common.utils.sql;

import com.ruoyi.common.utils.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

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

public class List2VarcharHandler implements TypeHandler<List<String>> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
        StringBuilder sb = new StringBuilder();
        for (String userId : strings) {
            sb.append(userId).append(",");
        }
        preparedStatement.setString(i, sb.toString());
    }

    @Override
    public List<String> getResult(ResultSet rs, String s) throws SQLException {
        return StringUtils.str2List(rs.getString(s), ",", true, true);
    }

    @Override
    public List<String> getResult(ResultSet rs, int i) throws SQLException {
        return StringUtils.str2List(rs.getString(i), ",", true, true);
    }

    @Override
    public List<String> getResult(CallableStatement cs, int i) throws SQLException {
        return StringUtils.str2List(cs.getString(i), ",", true, true);
    }
}
