/**
 * 订货单数据持久化处理类
 * StockorderDao.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.Restrictions;

import com.zds.slms.common.BaseDao;
import com.zds.slms.domain.Client;
import com.zds.slms.domain.Employee;
import com.zds.slms.domain.Merchandise;
import com.zds.slms.domain.Stockorder;
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 StockorderDao extends BaseDao<Stockorder, Integer> implements
		IStockorderDao {

	// 客户档案处理接口
	IClientDao clientDao;
	// 员工档案处理接口
	IEmployeeDao employeeDao;
	// 商品档案处理接口
	IMerchandiseDao merchandiseDao;

	/*
	 * 订货单查询
	 * 
	 * @see
	 * com.zds.slms.dao.IStockorderDao#findStockorder(com.zds.slms.domain.Stockorder
	 * )
	 */
	public List<Stockorder> findStockorder(Stockorder stockorder) {
		// 对象查询条件
		DetachedCriteria criteria = DetachedCriteria.forClass(Stockorder.class);
		if (null != stockorder) {
			if (!ChkUtil.isNullOrEmpty(stockorder.getId())) {
				criteria.add(Restrictions.eq("id", stockorder.getId()));
			}
			if (!ChkUtil.isNullOrEmpty(stockorder.getCode())) {
				criteria.add(Restrictions.eq("code", stockorder.getCode()));
			}
			if ((!ChkUtil.isNullOrEmpty(stockorder.getMerchandise()))
					&& (!ChkUtil.isNullOrEmpty(stockorder.getMerchandise()
							.getId()))) {
				criteria.add(Restrictions.eq("merchandise.id", stockorder
						.getMerchandise().getId()));

			}
			if ((!ChkUtil.isNullOrEmpty(stockorder.getEmployee()))
					&& (!ChkUtil
							.isNullOrEmpty(stockorder.getEmployee().getId()))) {
				criteria.add(Restrictions.eq("employee.id", stockorder
						.getEmployee().getId()));

			}
			if (!ChkUtil.isNullOrEmpty(stockorder.getOrderdate())) {
				criteria.add(Restrictions.eq("orderdate",
						stockorder.getOrderdate()));

			}
			if ((!ChkUtil.isNullOrEmpty(stockorder.getClient()))
					&& (!ChkUtil.isNullOrEmpty(stockorder.getClient().getId()))) {
				criteria.add(Restrictions.eq("client.id", stockorder
						.getClient().getId()));

			}
		}
		return findByCriteria(criteria);
	}

	/*
	 * 订货单保存
	 * 
	 * @see
	 * com.zds.slms.dao.IStockorderDao#saveStockorder(com.zds.slms.domain.Stockorder
	 * )
	 */
	public void saveStockorder(Stockorder stockorder) {
		// 构造客户档案查询条件
		Client client = new Client();
		client.setId(stockorder.getClient().getId());
		// 构造员工档案查询条件
		Employee employee = new Employee();
		employee.setId(stockorder.getEmployee().getId());
		// 构造商品档案查询条件
		Merchandise merchandise = new Merchandise();
		merchandise.setId(stockorder.getMerchandise().getId());

		stockorder.setClient(clientDao.findClient(client).get(0));
		stockorder.setEmployee(employeeDao.findEmployee(employee).get(0));
		stockorder.setMerchandise(merchandiseDao.findMerchandise(merchandise)
				.get(0));
		save(stockorder);

	}

	/*
	 * 订货单删除
	 * 
	 * @see com.zds.slms.dao.IStockorderDao#deleteStockorder(java.lang.String[])
	 */
	public void deleteStockorder(String[] stockorderId) {
		List<Stockorder> entities = new ArrayList<Stockorder>();
		for (String cid : stockorderId) {
			entities.add(load(Integer.valueOf(cid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 订货单更新
	 * 
	 * @see
	 * com.zds.slms.dao.IStockorderDao#updateStockorder(com.zds.slms.domain.
	 * Stockorder)
	 */
	public void updateStockorder(Stockorder stockorder) {
		// 构造客户档案查询条件
		Client client = new Client();
		client.setId(stockorder.getClient().getId());
		// 构造员工档案查询条件
		Employee employee = new Employee();
		employee.setId(stockorder.getEmployee().getId());
		// 构造商品档案查询条件
		Merchandise merchandise = new Merchandise();
		merchandise.setId(stockorder.getMerchandise().getId());

		Integer cid = stockorder.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Stockorder perstStockorder = load(Integer.valueOf(cid));
		perstStockorder.setCode(stockorder.getCode());
		perstStockorder.setMerchandisenumber(stockorder.getMerchandisenumber());
		perstStockorder.setOrderdate(stockorder.getOrderdate());

		perstStockorder.setClient(clientDao.findClient(client).get(0));
		perstStockorder.setEmployee(employeeDao.findEmployee(employee).get(0));
		perstStockorder.setMerchandise(merchandiseDao.findMerchandise(
				merchandise).get(0));

		update(perstStockorder);

	}

	/**
	 * @return the clientDao
	 */
	public IClientDao getClientDao() {
		return clientDao;
	}

	/**
	 * @param clientDao
	 *            the clientDao to set
	 */
	public void setClientDao(IClientDao clientDao) {
		this.clientDao = clientDao;
	}

	/**
	 * @return the employeeDao
	 */
	public IEmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	/**
	 * @param employeeDao
	 *            the employeeDao to set
	 */
	public void setEmployeeDao(IEmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	/**
	 * @return the merchandiseDao
	 */
	public IMerchandiseDao getMerchandiseDao() {
		return merchandiseDao;
	}

	/**
	 * @param merchandiseDao
	 *            the merchandiseDao to set
	 */
	public void setMerchandiseDao(IMerchandiseDao merchandiseDao) {
		this.merchandiseDao = merchandiseDao;
	}

}
