/**
 * @Author: Kane Wang <wangkane@qq.com>
 * @Date: 2025-03-17 19:03:17
 * @LastEditors: Kane
 * @LastModified: 2025-03-20 10:49:49
 * @FilePath: /cpicxim-huixiabao/src/main/java/com/cpic/xim/huixiabao/db/HuixiabaoDbUtils.java
 * @Description:
 * 
 *               Copyright (c) 2025 by Kane All rights reserved
 */
package com.cpic.xim.huixiabao.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cpic.xim.huixiabao.web.data.nhs.xyx.HmbXyxInfoItem;
import com.cpic.xim.data.config.AppConfig;

public class HuixiabaoDbUtils
{
    private static Logger logger = LoggerFactory.getLogger( HuixiabaoDbUtils.class );

    public static void saveHmbXyxInfoToMysql( Vector<HmbXyxInfoItem> xyxInfo )
                                                                               throws ClassNotFoundException,
                                                                               SQLException
    {
        Connection        connection      = null;
        PreparedStatement statementDelete = null;
        PreparedStatement statementInsert = null;

        String sqlDelete = """
                delete from HmbXyxInfo where xyxNo = ?
                """;
        String sqlInsert = """
                insert into HmbXyxInfo( psnName, certno, serviceId, status, xyxNo )
                values( ?, ?, ?, ?, ? )
                                    """;
        try
        {
            Class.forName( "com.mysql.cj.jdbc.Driver" );


            connection      = DriverManager.getConnection( AppConfig.getUrltestmysql(),
                                                           AppConfig.getUsernamemysql(),
                                                           AppConfig.getPasswordmysql() );
            statementDelete = connection.prepareStatement( sqlDelete.trim() );
            statementInsert = connection.prepareStatement( sqlInsert.trim() );

            for ( HmbXyxInfoItem item : xyxInfo )
            {
                logger.info( "写入记录{<>}", item.getXyxNo() );

                statementDelete.setString( 1, item.getXyxNo() );
                statementInsert.setString( 1, item.getPsnName() );
                statementInsert.setString( 2, item.getCertno() );
                statementInsert.setString( 3, item.getServiceId() );
                statementInsert.setString( 4, item.getStatus() );
                statementInsert.setString( 5, item.getXyxNo() );

                statementDelete.execute();
                statementInsert.execute();
            }
        }
        finally
        {
            if ( statementDelete != null )
            {
                try
                {
                    statementDelete.close();
                }
                catch ( SQLException error )
                {

                }
            }

            if ( statementInsert != null )
            {
                try
                {
                    statementInsert.close();
                }
                catch ( SQLException error )
                {

                }
            }

            if ( connection != null )
            {
                try
                {
                    connection.close();
                }
                catch ( SQLException error )
                {

                }
            }
        }
    }
}
