CentreDBUtil.java 3.3 KB
package com.espeed.reading.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 
 * @项目名称: 邮件跟踪系统
 * @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
 * @技术支持: info@21gmail.com
 * @单元名称: 操作用户中心数据库的工具类
 * @开始时间: 2018-5-23
 * @开发人员: 杨志钊
 */
public class CentreDBUtil {
	public static String DBUrl = ConfigPath.getCentreUrl();
	public static String DBUser = ConfigPath.getCentreUsername();
	public static String DBPassword = ConfigPath.getCentrePassword();
	public static final String DBDriver = "com.mysql.jdbc.Driver";
	public static Connection conn;
	// 加载驱动
	static {
		try {
			Class.forName(DBDriver);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 获得连接
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(DBUrl, DBUser, DBPassword);
	}

	/** 关闭数据库连接 */
	public static void close(ResultSet rs, Statement st, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static Map<String, Object> getUserInfo(String loginId, String domain) {
		Map<String, Object> userInfo = new HashMap<String, Object>();

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			conn = CentreDBUtil.getConnection();

			String sql = "select u.user_id,u.company_id from centre_user as u,centre_company as c where u.company_id = c.company_id and u.login_id = ? and c.domain = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, loginId);
			ps.setString(2, domain);

			rs = ps.executeQuery();

			if (rs.first()) {
				userInfo.put("code", 200);
				userInfo.put("userId", rs.getObject("user_id"));
				userInfo.put("companyId", rs.getObject("company_id"));
			} else {
				userInfo.put("code", 400);
				userInfo.put("msg", "不存在的账号->" + (loginId + "@" + domain));
			}

		} catch (SQLException e) {
			userInfo.put("code", 500);
			userInfo.put(
					"msg",
					"异常:"
							+ e.getClass().getName()
							+ (e.getMessage() == null ? "" : ("->" + e
									.getMessage())));
		} finally {
			CentreDBUtil.close(rs, ps, conn);
		}

		return userInfo;
	}

	
	public static int getUserId(Integer companyId){
		int userId = 0;

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			conn = CentreDBUtil.getConnection();

			String sql = "select u.user_id from centre_user as u,centre_company as c where u.company_id = c.company_id and c.company_id = ? and u.user_role = 1 limit 1";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, companyId);

			rs = ps.executeQuery();
			if (rs.first()) {
				userId = rs.getInt("user_id");
			} 

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CentreDBUtil.close(rs, ps, conn);
		}
		
		return userId;
	}
	
}