package com.skic.service;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

import com.skic.DAO.UserDAO;
import com.skic.DAO.impl.UserDAOimpl;
import com.skic.DTO.UserDTO;
import com.skic.util.ConnectionFactory;

public class CheckLoginService {
	private UserDAO userdao = new UserDAOimpl();

	public Map<String, Integer> CheckUser(UserDTO USERDTO) {
		Connection conn = null;
		Map<String ,Integer> flagTypeMap = new HashMap<String ,Integer>();
		try {
			conn = ConnectionFactory.getInstance().makeConnection();
			conn.setAutoCommit(false);
			ResultSet resultSet1 = userdao.select(conn, USERDTO);//调用数据库查询
			conn.commit();
			if (resultSet1.next()) {//查询账号是否存在
				flagTypeMap.put("login_status",1);//1表示登录成功
				flagTypeMap.put("vipflag",resultSet1.getInt("col_vipFlag"));
				flagTypeMap.put("adminflag",resultSet1.getInt("col_adminFlag"));
				flagTypeMap.put("rootflag",resultSet1.getInt("col_rootFlag"));
			}else {
				flagTypeMap.put("login_status",0);
			}
			System.out.println("login_status"+flagTypeMap.get("login_status")+"adminflag = "+flagTypeMap.get("adminflag")+"vipflag = "+flagTypeMap.get("vipflag")+"rootflag = "+flagTypeMap.get("rootflag"));
		} catch (SQLException e) {
			System.out.println("获取查询结果失败");
			e.printStackTrace();
			try {
				System.out.println("更新数据失败，数据库回滚");
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println("数据库回滚失败");
				e1.printStackTrace();
			}
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					System.out.println("数据库关闭失败");
					e.printStackTrace();
				}
			}
		}

		return flagTypeMap;
	}
}
