/**
 * 客户档案数据持久化处理类
 * ClientDao.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.Client;
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-5<br/>
 * Modified By:<br/>
 * Modified Date:<br/>
 * Why & What is modified:<br/>
 * Version:0.1<br/>
 */
public class ClientDao extends BaseDao<Client, Integer> implements IClientDao {

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

			}
		}

		return findByCriteria(criteria);

	}

	/*
	 * 客户档案保存
	 * 
	 * @see com.zds.slms.dao.IClientDao#saveClient(com.zds.slms.domain.Client)
	 */
	public void saveClient(Client client) {
		save(client);

	}

	/*
	 * 客户档案删除
	 * 
	 * @see com.zds.slms.dao.IClientDao#deleteClient(java.lang.String[])
	 */
	public void deleteClient(String[] clientId) {
		List<Client> entities = new ArrayList<Client>();
		for (String cid : clientId) {
			entities.add(load(Integer.valueOf(cid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 客户档案更新
	 * 
	 * @see com.zds.slms.dao.IClientDao#updateClient(com.zds.slms.domain.Client)
	 */
	public void updateClient(Client client) {
		Integer cid = client.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Client perstClient = load(Integer.valueOf(cid));
		perstClient.setAddress(client.getAddress());
		perstClient.setCode(client.getCode());
		perstClient.setEmail(client.getEmail());
		perstClient.setName(client.getName());
		perstClient.setTelephone(client.getTelephone());
		update(perstClient);
	}

}
