package com.chengxusheji.dao;

import javax.annotation.Resource;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.chengxusheji.domain.Admin;


/**
 * 双鱼林SSH2_EasyUI图书管理系统  需要更多系统可以去我网站查询(各种开发语言都有)
 * 作者：双鱼林          QQ:254540457  网站: www.shuangyulin.com
 * Email: wangjianlin1985@126.com
 * Copyright (c) 2016-2050 All rights reserved.
 */


@Service @Transactional
public class AdminDAO {
	@Resource  SessionFactory factory;

	/*保存业务逻辑错误信息字段*/
	private String errMessage;
	public String getErrMessage() { return this.errMessage; }
	
	/*验证用户登录*/
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public boolean CheckLogin(Admin admin) { 
		Session s = factory.getCurrentSession(); 

		Admin db_admin = (Admin)s.get(Admin.class, admin.getUsername());
		if(db_admin == null) { 
			this.errMessage = " 账号不存在 ";
			System.out.print(this.errMessage);
			return false;
		} else if( !db_admin.getPassword().equals(admin.getPassword())) {
			this.errMessage = " 密码不正确! ";
			System.out.print(this.errMessage);
			return false;
		}
		
		return true;
	}
	

	/*修改用户登录密码*/
	public void ChangePassword(String username, String newPassword) {
		Session s = factory.getCurrentSession();
		
		Admin db_admin = (Admin)s.get(Admin.class, username);
		db_admin.setPassword(newPassword);
		s.save(db_admin);
		
	}
	
	/*根据用户名获取管理员对象*/
	public Admin GetAdmin(String username) {
		Session s = factory.getCurrentSession();
		Admin db_admin = null;
		db_admin = (Admin)s.get(Admin.class, username); 
		return db_admin;
	}
}
