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

import java.text.DecimalFormat;

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

import com.dev.swms.server.model.base.ItemKey;
import com.dev.swms.server.model.base.PackageUnit;
import com.dev.swms.server.model.inventory.TaskLog;
import com.dev.swms.server.service.GlobalParamUtils;
import com.dev.energy.server.model.Entity;

/**
 * 包箱明细
 * @author  dev
 */

public class PackageDetail extends Entity {
	/** 包箱 */
	private Package pkg;
	/** 货物批次属性 */
	private ItemKey itemKey;
	/** 包装单位 */
	private PackageUnit packageUnit;
	/** 数量 */
	private double quantity;
	/** 重量 */
	private double weight;
	/** 体积 */
	private double volume;
	
	public PackageDetail(ItemKey itemKey,PackageUnit packageUnit,double quantity,double weight,double volume){
		this.itemKey = itemKey;
		this.packageUnit = packageUnit;
		this.quantity = quantity;
		this.weight = weight;
		this.volume = volume;
	}
	
	public PackageDetail(){
		
	}
	
	public Package getPkg() {
		return pkg;
	}

	public void setPkg(Package pkg) {
		this.pkg = pkg;
	}

	public ItemKey getItemKey() {
		return itemKey;
	}

	public void setItemKey(ItemKey itemKey) {
		this.itemKey = itemKey;
	}

	public PackageUnit getPackageUnit() {
		return packageUnit;
	}

	public void setPackageUnit(PackageUnit packageUnit) {
		this.packageUnit = packageUnit;
	}

	public double getQuantity() {
		return quantity;
	}

	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public double getVolume() {
		return volume;
	}

	public void setVolume(double volume) {
		this.volume = volume;
	}

	public boolean equals(final Object other) {
		if (!(other instanceof PackageDetail))
			return false;
		PackageDetail castOther = (PackageDetail) other;
		return new EqualsBuilder().append(pkg, castOther.getPkg()).append(itemKey, castOther.getItemKey())
				.append(packageUnit, castOther.getPackageUnit()).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder().append(pkg).append(itemKey).append(packageUnit).toHashCode();
	}

	public void refresh() {
		this.weight = GlobalParamUtils.formatDecimalByType(this.quantity*this.getPackageUnit().getWeight(), "maintainTableDecimal");
		this.volume = GlobalParamUtils.formatDecimalByType(this.quantity*this.getPackageUnit().getVolume(), "maintainTableDecimal");

		this.pkg.refresh();
	}
	

	/**
	 * 包装拆分，从包箱明细中扣减指定的数量
	 * @param qty
	 */
	public void split(int qty) {
		this.quantity -= qty;
		this.weight -= qty*this.getPackageUnit().getWeight();
		this.volume -= qty*this.getPackageUnit().getVolume();
	}

}
