/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2023 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.util;

import org.jpos.iso.ISOMsg;
import java.sql.*;

public class MySqlManager {	

    public static void insertTransaction(Connection connection, ISOMsg request, ISOMsg response) {

        String sql = "INSERT INTO transaction (pan, proc_code, amount, stan, date_time, settlement_date, mcc, terminal_id, merchant_id, reference_id, auth_code, response_code, upi_url, upi_info)" +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, request.getString(2));
            preparedStatement.setString(2, request.getString(3));
            preparedStatement.setString(3, request.getString(4));
            preparedStatement.setString(4, request.getString(11));
            preparedStatement.setString(5, request.getString(12)+request.getString(13));
            preparedStatement.setString(6, request.getString(14));
            preparedStatement.setString(7, request.getString(18));
            preparedStatement.setString(8, request.getString(41));
            preparedStatement.setString(9, request.getString(42));
            preparedStatement.setString(10, response.getString(37));
            preparedStatement.setString(11, response.getString(38));
            preparedStatement.setString(12, response.getString(39));
            preparedStatement.setString(13, response.getString(62));
            preparedStatement.setString(14, response.getString(63));
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
	
	public static void insertTransaction(ISOMsg request, ISOMsg response) {		

        String sql = "INSERT INTO transaction (pan, proc_code, amount, stan, date_time, settlement_date, mcc, terminal_id, merchant_id, reference_id, auth_code, response_code, upi_url, upi_info)" +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		
        PreparedStatement preparedStatement = null;
        try (Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/tcpay",
                    "test", "test")) {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, request.getString(2));
            preparedStatement.setString(2, request.getString(3));
            preparedStatement.setString(3, request.getString(4));
            preparedStatement.setString(4, request.getString(11));
            preparedStatement.setString(5, request.getString(12)+request.getString(13));
            preparedStatement.setString(6, request.getString(14));
            preparedStatement.setString(7, request.getString(18));
            preparedStatement.setString(8, request.getString(41));
            preparedStatement.setString(9, request.getString(42));
            preparedStatement.setString(10, response.getString(37));
            preparedStatement.setString(11, response.getString(38));
            preparedStatement.setString(12, response.getString(39));
            preparedStatement.setString(13, response.getString(62));
            preparedStatement.setString(14, response.getString(63));
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

