package com.dev.swms.server.model.shipping;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.dev.swms.server.model.base.Item;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.model.VersionalEntity;

public class PreAllocate extends VersionalEntity {
	private String code;
	private Warehouse warehouse;
	private Item item;
	private String lot;
	private double preAllocatedQuantity;

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public String getLot() {
		return lot;
	}
	public void setLot(String lot) {
		this.lot = lot;
	}
	public double getPreAllocatedQuantity() {
		return preAllocatedQuantity;
	}
	public void setPreAllocatedQuantity(double preAllocatedQuantity) {
		this.preAllocatedQuantity = preAllocatedQuantity;
	}
	public Warehouse getWarehouse() {
		return warehouse;
	}
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}
	
	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof PreAllocate))
			return false;
		PreAllocate castOther = (PreAllocate) other;
		return new EqualsBuilder().append(code, castOther.getCode()).append(
				warehouse, castOther.getWarehouse()).append(item, castOther.getItem())
				.append(lot, castOther.getLot()).isEquals();
	}
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(code).append(warehouse)
				.append(item).append(lot).toHashCode();
	}
	
	/**
	 * 预分配
	 * @param quantity
	 */
	public void preAllocate(Double quantity) {
		this.preAllocatedQuantity += quantity;
	}

	/**
	 * 取消预分配
	 * @param quantity
	 */
	public void unPreAllocate(Double quantity) {
		this.preAllocatedQuantity -= quantity;
		if (this.preAllocatedQuantity < 0) {
			throw new BusinessException("unPreAlloate.quantity.error");
		}
	}
}
