package com.ruoyi.ibs.task.handler;
/**
 * @Author 吴凯程
 * @Date 2025/6/20
 **/


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ibs.task.domain.entity.RelatePerson;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

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

@Component
public class RelatePersonTypeHandler extends BaseTypeHandler<List<RelatePerson>> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int parameterIndex, List<RelatePerson> parameter, JdbcType jdbcType)
            throws SQLException {
        String content = StringUtils.isEmpty(parameter) ? null : JSON.toJSONString(parameter);
        preparedStatement.setString(parameterIndex, content);
    }

    @Override
    public List<RelatePerson> getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        return this.getListByJsonArrayString(resultSet.getString(columnName));
    }

    @Override
    public List<RelatePerson> getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
        return this.getListByJsonArrayString(resultSet.getString(columnIndex));
    }

    @Override
    public List<RelatePerson> getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        return this.getListByJsonArrayString(callableStatement.getString(columnIndex));
    }

    private List<RelatePerson> getListByJsonArrayString(String content) {
        return StringUtils.isBlank(content) ? new ArrayList<>() : JSON.parseArray(content, RelatePerson.class);
    }


}
