/**
 * 员工档案数据持久化处理类
 * EmployeeDao.java
 * Copyright (C) 2010 SLMS
 * Simple Logistics Management System
 * 
 */
package com.zds.slms.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.zds.slms.common.BaseDao;
import com.zds.slms.domain.Employee;
import com.zds.slms.util.ChkUtil;

/**
 * Copyright (C) 2010:SLMS<br/>
 * Project:SLMS<br/>
 * Module ID:<br/>
 * Comments:Simple Logistics Management System<br/>
 * JDK version used:JDK1.6<br/>
 * Package:com.zds.slms.dao<br/>
 * Author:chenyongzheng<br/>
 * Create Date:2010-12-6<br/>
 * Modified By:<br/>
 * Modified Date:<br/>
 * Why & What is modified:<br/>
 * Version:0.1<br/>
 */
public class EmployeeDao extends BaseDao<Employee, Integer> implements
		IEmployeeDao {

	/*
	 * 员工档案查询
	 * 
	 * @see
	 * com.zds.slms.dao.IEmployeeDao#findEmployee(com.zds.slms.domain.Employee)
	 */
	public List<Employee> findEmployee(Employee employee) {
		// 对象查询条件
		DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
		if (null != employee) {
			if (!ChkUtil.isNullOrEmpty(employee.getId())) {
				criteria.add(Restrictions.eq("id", employee.getId()));
			}
			if (!ChkUtil.isNullOrEmpty(employee.getCode())) {
				criteria.add(Restrictions.eq("code", employee.getCode()));
			}
			if (!ChkUtil.isNullOrEmpty(employee.getName())) {
				criteria.add(Restrictions.like("name", employee.getName(),
						MatchMode.ANYWHERE));

			}
		}
		return findByCriteria(criteria);
	}

	/*
	 * 员工档案保存
	 * 
	 * @see
	 * com.zds.slms.dao.IEmployeeDao#saveEmployee(com.zds.slms.domain.Employee)
	 */
	public void saveEmployee(Employee employee) {
		save(employee);

	}

	/*
	 * 员工档案删除
	 * 
	 * @see com.zds.slms.dao.IEmployeeDao#deleteEmployee(java.lang.String[])
	 */
	public void deleteEmployee(String[] employeeId) {
		List<Employee> entities = new ArrayList<Employee>();
		for (String eid : employeeId) {
			entities.add(load(Integer.valueOf(eid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 员工档案更新
	 * 
	 * @see
	 * com.zds.slms.dao.IEmployeeDao#updateEmployee(com.zds.slms.domain.Employee
	 * )
	 */
	public void updateEmployee(Employee employee) {
		Integer eid = employee.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Employee perstEmployee = load(Integer.valueOf(eid));
		perstEmployee.setBirthday(employee.getBirthday());
		perstEmployee.setCode(employee.getCode());
		perstEmployee.setEmail(employee.getEmail());
		perstEmployee.setName(employee.getName());
		perstEmployee.setSex(employee.isSex());
		perstEmployee.setTelephone(employee.getTelephone());
		update(perstEmployee);

	}

}
