package cokr.xit.ens.biz.iup.domain.repository;

import cokr.xit.ens.biz.iup.domain.TbInputDataBulkTest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Slf4j
@Repository
@RequiredArgsConstructor
public class TbInputDataBulkTestCustomRepositoryImpl implements TbInputDataBulkTestCustomRepository<TbInputDataBulkTest>{
    private final JdbcTemplate jdbcTemplate;

    @Value("${spring.jpa.properties.hibernate.jdbc.batch_size}")
    private int batchSize;


    @Override
    public void saveAll(List<TbInputDataBulkTest> list) {
        int batchCount = 0;
        List<TbInputDataBulkTest> subItems = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            subItems.add(list.get(i));
            if ((i + 1) % batchSize == 0) {
                batchCount = batchInsert(batchSize, batchCount, subItems);
            }
        }
        if (!subItems.isEmpty()) {
            batchCount = batchInsert(batchSize, batchCount, subItems);
        }
        log.info("batchCount: " + batchCount);

    }

    private int batchInsert(int batchSize, int batchCount, List<TbInputDataBulkTest> list) {

//        jdbcTemplate.batchUpdate("INSERT INTO tb_input_data_bulk_test (birthday, car_no, data_id, gender, msg_data, msg_dtl_data, name, pay_status_cd, pay_url, reg_dt, reg_id, sid, upd_dt, upd_id, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        jdbcTemplate.batchUpdate("INSERT INTO tb_input_data_bulk_test (birthday, car_no, data_id, gender, msg_data, msg_dtl_data, name, pay_status_cd, pay_url, reg_dt, reg_id, sid, upd_dt, upd_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        ps.setString(1, list.get(i).getBirthday());
                        ps.setString(2, list.get(i).getCarNo());
                        ps.setString(3, list.get(i).getDataId());
                        ps.setString(4, list.get(i).getGender());
                        ps.setString(5, list.get(i).getMsgData());
                        ps.setString(6, list.get(i).getMsgDtlData());
                        ps.setString(7, list.get(i).getName());
                        ps.setString(8, list.get(i).getPayStatusCd());
                        ps.setString(9, list.get(i).getPayUrl());
                        ps.setDate(10, list.get(i).getRegDt());
                        ps.setString(11, list.get(i).getRegId());
                        ps.setString(12, list.get(i).getSid());
                        ps.setDate(13, list.get(i).getUpdDt());
                        ps.setString(14, list.get(i).getUpdId());
//                        ps.setLong(15, list.get(i).getId());
                    }
                    @Override
                    public int getBatchSize() {
                        return list.size();
                    }
                });
        list.clear();
        batchCount++;
        return batchCount;
    }
}
