package com.uas.account.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.springframework.stereotype.Repository;

import com.uas.account.entity.User;
import com.uas.account.entity.UserSpaceDetail;

@Repository
public class SynchrodataToOldPlatDaoImpl implements SynchrodataToOldPlatDao {

	final static String userMethod = "user";
	final static String userSpaceMethod = "userSpace";
	final static String userPasswordMethod = "userPassword";

	@Override
	public Long SynchUserToOldPlat(User user, String spaceName) {
		String userSpaceId = "select en_id from enterprise where en_uu = '" + user.getSpaceDialectUID()
				+ "' and en_businesscode = '" + user.getSpaceUID() + "'";
		String enId = ConnectionToOldPlat(userSpaceId, userSpaceMethod);
		String sql = "insert into employees values (employees_seq.nextval,null,'111111', '" + user.getName()
				+ "',null, " + user.getSpaceDialectUID() + ",'" + spaceName + "',null,null,null,'"
				+ user.getUid() + "', '" + user.getSecondUID() + "',null,null,null,null," + user.getDialectUID()
				+ ",'管理员',null,null,'" + enId + "')";
		ConnectionToOldPlat(sql, null);
		Long userId = findUserByUserUUAndEnuu(user.getDialectUID(), user.getSpaceDialectUID());
		return userId;
	}

	@Override
	public Long SyschUserSpaceToOldPlat(UserSpaceDetail detail, User user) {
		String sql = "insert into enterprise values(enterprise_seq.nextval,'" + detail.getName() + "','"
				+ detail.getShortName() + "','" + detail.getType() + "',1,null,'" + detail.getAddress() + "','"
				+ detail.getAddress() + "','" + detail.getAdminTel() + "',null,'" + detail.getAdminName() + "','"
				+ detail.getBusinessCode() + "',null,null,null,'" + user.getSpaceDialectUID() + "',null,'"
				+ detail.getAdminEmail() + "',null,'" + detail.getAdminName() + "','" + detail.getAdminTel()
				+ "',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,'是',null,null,null,null,null)";
		ConnectionToOldPlat(sql, null);
		Long spaceId = findEnterpriseByEnuu(user.getSpaceDialectUID());
		return spaceId;
	}

	@Override
	public Long findEnterpriseByEnuu(String enuu) {
		String userSpaceIdSql = "select en_id from enterprise where en_uu = '" + enuu + "'";
		Long userSpaceId = 0L;
		String spaceId = ConnectionToOldPlat(userSpaceIdSql, userSpaceMethod);
		if (spaceId != null) {
			userSpaceId = Long.valueOf(spaceId);
		}
		return userSpaceId;
	}

	@Override
	public Long findUserByUserUUAndEnuu(String useruu, String enuu) {
		String userIdSql = "select em_id from employees where en_uu = '" + enuu + "' and em_uu = '" + useruu + "'";
		String id = ConnectionToOldPlat(userIdSql, userMethod);
		Long userId = 0L;
		if (id != null) {
			userId = Long.valueOf(id);
		}
		return userId;
	}

	/**
	 * 连接到旧平台数据库
	 */
	public String ConnectionToOldPlat(String sql, String method) {
		String OperateResult = null;
		Connection con = null;// 创建一个数据库连接
		PreparedStatement pre = null;// 创建预编译语句对象，一般都是用这个而不用Statement
		ResultSet result = null;// 创建一个结果集对象
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
			String url = "jdbc:oracle:" + "thin:@10.10.100.88:1521:orcl";
			String user = "dbusoft";// 用户名,系统默认的账户名
			String password = "dbusoft";// 你安装时选设置的密码
			con = DriverManager.getConnection(url, user, password);// 获取连接
			pre = con.prepareStatement(sql);// 实例化预编译语句
			result = pre.executeQuery();// 执行查询，注意括号中不需要再加参数
			while (result.next())
				// 当结果集不为空时
				if (method != null) {
					if (method.equals(userMethod)) {// employees表
						OperateResult = result.getString("em_id");
					} else if (method.equals(userSpaceMethod)) { // enterprise表
						OperateResult = result.getString("en_id");
					} else if (method.equals(userPasswordMethod)) {
						OperateResult = result.getString("em_password");
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 逐一将上面的几个对象关闭，因为不关闭的话会影响性能、并且占用资源
				// 注意关闭的顺序，最后使用的最先关闭
				if (result != null)
					result.close();
				if (pre != null)
					pre.close();
				if (con != null)
					con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return OperateResult;
	}

	@Override
	public String getUserPassword(String useruu, String enuu) {
		String userIdSql = "select em_password from employees where en_uu = '" + enuu + "' and em_uu = '" + useruu
				+ "'";
		return ConnectionToOldPlat(userIdSql, userPasswordMethod);
	}

}
