package com.lsit.RBAC.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.lsit.RBAC.domain.Department;
import com.lsit.RBAC.listener.InitConfigListener;
import com.lsit.RBAC.util.DBConnection;

public class DepartmentDAO {

	public boolean insertDeptData(Department dept) {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			int count = 0;
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(InitConfigListener.dbHashMap.get("insertDepartment"));
			
			preparedStatement.setString(++count, dept.getDeptName());
			preparedStatement.setInt(++count, getUserId(dept.getDeptManager()));
			preparedStatement.setString(++count, dept.getDeptAddr());
			preparedStatement.setString(++count, dept.getTelephone());
			preparedStatement.setString(++count, dept.getFax());
			int index = preparedStatement.executeUpdate();
			if (index > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(connection, preparedStatement, null);
		}
		return false;
	}

	public boolean updateDeptData(Department dept) {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			int count = 0;
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(InitConfigListener.dbHashMap.get("updateDepartment"));
			preparedStatement.setString(++count, dept.getDeptName());
			preparedStatement.setInt(++count, getUserId(dept.getDeptManager()));
			preparedStatement.setString(++count, dept.getDeptAddr());
			preparedStatement.setString(++count, dept.getTelephone());
			preparedStatement.setString(++count, dept.getFax());
			preparedStatement.setInt(++count, dept.getDeptId());
			int index = preparedStatement.executeUpdate();
			if (index > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(connection, preparedStatement, null);
		}
		return false;
	}

	public boolean deleteDeptData(int deptId) {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement(InitConfigListener.dbHashMap.get("deleteDepartment"));
			preparedStatement.setInt(1, deptId);
			int index = preparedStatement.executeUpdate();
			if (index > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(connection, preparedStatement, null);
		}
		return false;
	}

	public Department getDeparment(int deptId) {

		Department dept = new Department();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet=null;
		try {
			connection = DBConnection.getConnection();
			preparedStatement = connection.prepareStatement
					(InitConfigListener.dbHashMap.get("getDepartment"));
			preparedStatement.setInt(1, deptId);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				dept.setDeptId(resultSet.getInt("dept_id"));
				dept.setDeptName(resultSet.getString("dept_name"));
				dept.setDeptManager(resultSet.getString("dept_manager"));
				dept.setDeptAddr(resultSet.getString("dept_addr"));
				dept.setTelephone(resultSet.getString("tel"));
				dept.setFax(resultSet.getString("fax"));
				dept.setAddTime(resultSet.getString("add_time"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return dept;
	}

	public boolean setManager(int deptId, int userId) {
		
		Connection connection = null;
		PreparedStatement preparedStatement=null;
		try {
			connection = DBConnection.getConnection();
			 preparedStatement = connection.prepareStatement
					 (InitConfigListener.dbHashMap.get("setManager"));
			preparedStatement.setInt(1, userId);
			preparedStatement.setInt(2, deptId);
			int index = preparedStatement.executeUpdate();
			if (index > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, null);
		}
		return false;
	}

	public List<HashMap<String, String>> selectDeptNameData() {
		
		List<HashMap<String, String>> deptNameList = new ArrayList<HashMap<String, String>>();
		Connection connection=null;
		PreparedStatement preparedStatement =null;
		ResultSet resultSet=null;
		try {
			connection = DBConnection.getConnection();
			 preparedStatement = connection.prepareStatement
					 (InitConfigListener.dbHashMap.get("selectDeptNames"));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				HashMap<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("deptName", resultSet.getString("dept_name"));
				deptNameList.add(hashMap);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return deptNameList;

	}

	public int getUserId(String deptManager) {
		
		Connection connection=null;
		PreparedStatement  preparedStatement=null;
	    ResultSet resultSet=null;
		try {
		 connection = DBConnection.getConnection();
		 preparedStatement = connection.prepareStatement
				 (InitConfigListener.dbHashMap.get("getUserId"));
			preparedStatement.setString(1, deptManager);
		  resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt("user_id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return -1;
	}

	public List<Department> selectDeptData(String telephone, String deptName) {
		
		List<Department> deptList = new ArrayList<Department>();
		String sql = "select t2.* ,(select CONCAT(first_name,last_name) from tt_dept_user as t1 "
				+ "where t1.user_id=t2.dept_mgr_id)" + " as dept_manager from tt_dept as t2 where 1=1";
		if (telephone != null && !"".equals(telephone)) {
			sql += " and tel like '%" + telephone + "%'";
		}
		if (deptName != null && !"".equals(deptName)) {
			sql += " and dept_name like '%" + deptName + "%'";
		}
		Connection connection=null;
		PreparedStatement preparedStatement=null;
	   ResultSet  resultSet=null;
		try {
			 connection = DBConnection.getConnection();
			 preparedStatement = connection.prepareStatement(sql);
			 resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Department dept = new Department();
				dept.setDeptId(resultSet.getInt("dept_id"));
				dept.setDeptName(resultSet.getString("dept_name"));
				dept.setDeptAddr(resultSet.getString("dept_addr"));
				dept.setDeptManager(resultSet.getString("dept_manager"));
				dept.setTelephone(resultSet.getString("tel"));
				dept.setFax(resultSet.getString("fax"));
				dept.setAddTime(resultSet.getString("add_time"));
				deptList.add(dept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return deptList;
	}

	public boolean isDeptNameExist(String deptName) {
		
		Connection connection = null;
		PreparedStatement preparedStatement =null;
		ResultSet resultSet=null;
		try {
			connection = DBConnection.getConnection();
			 preparedStatement = connection.prepareStatement
					 (InitConfigListener.dbHashMap.get("selectDeptNames"));
			 resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				if (resultSet.getString("dept_Name").equals(deptName)) {
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return false;
	}

	public boolean isUserExist(int deptId) {
		
		Connection connection = null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection = DBConnection.getConnection();
			 preparedStatement = connection
					.prepareStatement(InitConfigListener.dbHashMap.get("isUserExistsUnderDept"));
			preparedStatement.setInt(1, deptId);
		    resultSet = preparedStatement.executeQuery();
			return  resultSet.next(); 
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return false;
	}

	public boolean isMenuExist(int deptId) {
	
		Connection connection = null;
		PreparedStatement preparedStatement=null;
	    ResultSet resultSet=null;
		try {
			connection = DBConnection.getConnection();
			 preparedStatement = connection
					.prepareStatement(InitConfigListener.dbHashMap.get("isMenuExistsUnderDept"));
			preparedStatement.setInt(1, deptId);
			resultSet = preparedStatement.executeQuery();
			return resultSet.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(connection, preparedStatement, resultSet);
		}
		return false;
	}
}
最近下载更多
xaiozhu  LV7 2021年6月28日
小安同学  LV7 2021年6月18日
zxl201377  LV2 2021年6月5日
liusha625184071  LV13 2021年6月5日
陈若愚  LV4 2021年5月14日
yuting Wang  LV9 2021年5月12日
米奇LOVE  LV4 2021年4月29日
子非鱼 淡  LV6 2020年12月23日
runningjoice  LV2 2020年12月17日
77198453  LV1 2020年11月15日
最近浏览更多
陈小灏  LV15 6月23日
15719908287  LV9 6月19日
zn2020  LV1 2023年12月19日
wxhua198  LV5 2023年11月30日
2036495585  LV9 2023年9月25日
xuweiwowzy  LV5 2023年9月21日
mumucfvbnm 2023年9月17日
暂无贡献等级
王毅麟  LV1 2023年8月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友