package com.connor.mail;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;

public class SendMail {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SendMail s=new SendMail();
		s.getstr();
		
	}

	private  void getstr() {
		// TODO Auto-generated method stub
		System.out.println("start");
		Properties properties = new Properties();
		InputStream is = null;
		is = this.getClass().getClassLoader().getResourceAsStream(
						"com/connor/mail/DatabaseInfomation.properties");
		try {
			properties.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String JdbcDriverClass = properties.getProperty("JdbcDriverClass");//"oracle.jdbc.driver.OracleDriver";
		System.out.println(JdbcDriverClass);
		String strUrl =properties.getProperty("ConnectionString"); //"jdbc:oracle:thin:@10.201.5.203:1521:tc11";
		System.out.println(strUrl);
		String strUserName =properties.getProperty("DbUser") ;//"infodba";
		System.out.println(strUserName);
		String strPassword = properties.getProperty("DbPass");//"infodba";
		System.out.println(strPassword);
		String mail = properties.getProperty("Mail");//"zhangl@connor.net.cn,hezq@connor.net.cn";
		System.out.println(mail);
		String s="";
		try {
			Class.forName(JdbcDriverClass);
			Connection conn = DriverManager.getConnection(strUrl, strUserName,
					strPassword);// 连接数据库
			String query1 = "select JK_BOM_ID,JK_BOM_REV from JK_BOM_PARENT_LINE_TABLE where JK_STATUS='2'";
			String query2 = "select JK_ITEM_ID,JK_ITEM_NAME from JK_MATERIAL_TABLE where JK_STATUS='2'";
			Statement stmt = conn.createStatement();
			ResultSet rs_get1 = stmt.executeQuery(query1);
			
			String rsStr1="";
			String rsStr2="";
			if (rs_get1 != null) {
				ResultSetMetaData metaData = rs_get1.getMetaData();

				int i_ColumnCount = metaData.getColumnCount();
				// System.out.println("i_ColumnCount:"+i_ColumnCount);
				while (rs_get1.next()) {
					// System.out.println("【BJZJ2】"+rs_get.getString("BJZJ2"));;
					//HashMap hm_unit = new HashMap();
					String str="";
					for (int i = 1; i <= i_ColumnCount; i++) {
						if(i==1){
							str=rs_get1.getString(i);
						}else{
							str=str+"/"+rs_get1.getString(i);
						}
//						hm_unit.put(metaData.getColumnName(i),
//								rs_get1.getString(i));
						// System.out.println("metaData.getColumnName(i):"+metaData.getColumnName(i));
						// System.out.println("rs_get.getString(i):"+rs_get.getString(i));
					}
					System.out.println("str="+str);
					if(!str.equals("")){
						rsStr1=rsStr1+","+str;
					}
				}
			} 
			if(!rsStr1.equals("")){
				rsStr1=rsStr1.substring(1, rsStr1.length());
				s=s+"以下为传递失败的BOM:\r"+rsStr1;
			}
			ResultSet rs_get2 = stmt.executeQuery(query2);
			if (rs_get2 != null) {
				ResultSetMetaData metaData = rs_get2.getMetaData();

				int i_ColumnCount = metaData.getColumnCount();
				// System.out.println("i_ColumnCount:"+i_ColumnCount);
				while (rs_get2.next()) {
					// System.out.println("【BJZJ2】"+rs_get.getString("BJZJ2"));;
					//HashMap hm_unit = new HashMap();
					String str="";
					for (int i = 1; i <= i_ColumnCount; i++) {
						if(i==1){
							str=rs_get2.getString(i);
						}else{
							str=str+"-"+rs_get2.getString(i);
						}
//						hm_unit.put(metaData.getColumnName(i),
//								rs_get1.getString(i));
						// System.out.println("metaData.getColumnName(i):"+metaData.getColumnName(i));
						// System.out.println("rs_get.getString(i):"+rs_get.getString(i));
					}
					System.out.println("str="+str);
					if(!str.equals("")){
						rsStr2=rsStr2+","+str;
					}
				}
			} 
			if(!rsStr2.equals("")){
				rsStr2=rsStr2.substring(1, rsStr2.length());
				s=s+"\r\r以下为传递失败的ITEM:\r"+rsStr2;
			}
			//System.out.println("s="+s);
			if(!s.equals("")){
				Demo1 d=new Demo1();
				d.sendmail("U8传递失败提示", mail, s);
				System.out.println("mail="+mail);
				System.out.println("s="+s);
			}
			if (rs_get1 != null) {
				rs_get1.close();
			}
			if (rs_get2 != null) {
				rs_get2.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
}
