package com.connor.jd.erp;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class erpIntegrationOpertaion extends AbstractAIFOperation{
	private TCSession session;
	private TCComponentItemRevision rev;
//	private Connection conn;
//    private CallableStatement stat;
//    private ResultSet rs;
	private String url;
	private String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private String username;
	private String password;
//	private String sql;

	public erpIntegrationOpertaion(TCSession session, TCComponentItemRevision f) {
		// TODO Auto-generated constructor stub
		this.session = session;
		this.rev = f;
	}

	@Override
	public void executeOperation() throws Exception {
		// TODO Auto-generated method stub	
		String wlbm = rev.getStringProperty("j6_wlbm") + "-" + rev.getProperty("item_revision_id");
		System.out.println("获取的物料编码：" + wlbm);
		if(wlbm!=null && !"".equals(wlbm)) {
			//查询首选项，获取连接
			String[] value1 = session.getPreferenceService().getStringValues("Jd_Erp_Info_Connect");
			for (int i = 0; i < value1.length; i++) {
				System.out.println(value1[i]);			
			}
			url = value1[0];
			username = value1[1];
			password = value1[2];
			//查询首选项获取查询的存储过程
	        String[] value2 = session.getPreferenceService().getStringValues("Jd_Erp_Info_inventory");
			for (int i = 0; i < value2.length; i++) {
				System.out.println(value2[i]);
			}
			String[] split = value2[0].split("@");    
		    try {
		    	Class.forName(driverName); // 加载数据库驱动
		    	Connection conn = DriverManager.getConnection(url, username, password); // 获取数据库连接对象
		    	System.out.println("================开始调用存储过程=====================");
		    	CallableStatement cs = conn.prepareCall("{ CALL "+split[0]+"(?) }"); // 准备调用存储过程
		    	cs.setString(1, wlbm); // 设置存储过程的参数
		    	ResultSet rs = cs.executeQuery(); // 执行存储过程并获取结果集
		    	System.out.println("================存储过程执行结束=====================");
		    	System.out.println("================将结果写入table=====================");
		    	JTable table = new JTable(buildTableModel(rs)); // 创建一个 JTable，并将查询结果放到其中
		    	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // 设置表格的自动调整模式为关闭自动调整
		    	JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
		    			JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
		    	// 创建一个带滚动条的 JScrollPane，并将 JTable 放到其中
		    	//JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
		    	//JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		    	//scrollPane.setPreferredSize(new Dimension(500, 300)); // 设置JScrollPane的首选大小
		    	JFrame frame = new JFrame("物料库存信息"); // 创建一个 JFrame，并将 JScrollPane 放到其中
		    	frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//关闭窗口不退出
		    	frame.add(scrollPane);
		    	frame.pack();
		    	frame.setVisible(true);
		    	rs.close(); // 关闭 ResultSet
		    	cs.close(); // 关闭 CallableStatement
		    	conn.close(); // 关闭 Connection
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
		}else {
			MessageBox.post("选中物料的编码为空", "提示", MessageBox.INFORMATION);
			return;
		}
        		
	}
	public static TableModel buildTableModel(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();
        Vector<String> columnNames = new Vector<String>();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> row = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                row.add(rs.getObject(columnIndex));
            }
            data.add(row);
        }
        return new DefaultTableModel(data, columnNames);
    }
}
