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

import java.util.Set;

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

import com.dev.swms.server.model.base.BoxModel;
import com.dev.swms.server.service.GlobalParamUtils;
import com.dev.energy.server.model.EntityWithUnique;

/**
 * 包箱
 * @author  dev
 */

public class Package extends EntityWithUnique {
	private static final long serialVersionUID = -6681528093390225295L;
	/** BOL */
	private BOL bol;
	/** 箱号 */
	private String code;
	/**箱件类型***/
	private String type;
	/**包装单位属性**/
	private String unit;
	/** 箱数 */
	private double quantity;
	/** 包箱重量 */
	private double weight;
	/** 包箱体积 */
	private double volume;	
	/** 包箱说明 */
	private String description;
	/** 包箱中货物清单 */
	private Set<PackageDetail> details;
	/** 箱型 */
	private BoxModel boxModel;

	public int hashCode() {
		return new HashCodeBuilder().append(bol).append(code).append(type)
				.append(unit).append(description).append(boxModel)
				.toHashCode();
	}

	public boolean equals(final Object other) {
		if (!(other instanceof Package))
			return false;
		Package castOther = (Package) other;
		return new EqualsBuilder().append(bol, castOther.bol).append(code,
				castOther.code).append(type, castOther.type).append(unit,
				castOther.unit).append(description, castOther.description)
				.append(boxModel, castOther.boxModel).isEquals();
	}

	public BoxModel getBoxModel() {
		return boxModel;
	}

	public void setBoxModel(BoxModel boxModel) {
		this.boxModel = boxModel;
	}

	public BOL getBol() {
		return bol;
	}

	public void setBol(BOL bol) {
		this.bol = bol;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	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 String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<PackageDetail> getDetails() {
		return details;
	}

	public void setDetails(Set<PackageDetail> details) {
		this.details = details;
	}
	
	public void refresh() {
		double w = 0;
		double v = 0;
		for (PackageDetail pd : this.getDetails()) {
			w += pd.getWeight() * this.getQuantity();
			v += pd.getVolume()  * this.getQuantity();
		}
		this.weight = GlobalParamUtils.formatDecimalByType(w, "maintainTableDecimal");
		this.volume = GlobalParamUtils.formatDecimalByType(v, "maintainTableDecimal");
		
		this.bol.refreshPackageQty();
	}

	public void split(int qty) {
		this.quantity -= qty;
		
		this.refresh();
	}

}
