/**
 * 商品档案数据持久化处理类
 * MerchandiseDao.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.Merchandise;
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 MerchandiseDao extends BaseDao<Merchandise, Integer> implements
		IMerchandiseDao {

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

			}
		}
		return findByCriteria(criteria);
	}

	/*
	 * 商品档案保存
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#saveMerchandise(com.zds.slms.domain.
	 * Merchandise)
	 */
	public void saveMerchandise(Merchandise merchandise) {
		save(merchandise);

	}

	/*
	 * 商品档案删除
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#deleteMerchandise(java.lang.String[])
	 */
	public void deleteMerchandise(String[] merchandiseId) {
		List<Merchandise> entities = new ArrayList<Merchandise>();
		for (String mid : merchandiseId) {
			entities.add(load(Integer.valueOf(mid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 商品档案更新
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#updateMerchandise(com.zds.slms.domain
	 * .Merchandise)
	 */
	public void updateMerchandise(Merchandise merchandise) {
		Integer mid = merchandise.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Merchandise perstMerchandise = load(Integer.valueOf(mid));
		perstMerchandise.setCode(merchandise.getCode());
		perstMerchandise.setName(merchandise.getName());
		perstMerchandise.setPrice(merchandise.getPrice());

		update(perstMerchandise);

	}

}
