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

import java.util.Date;
import java.util.HashSet;
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.Warehouse;
import com.dev.energy.server.model.Entity;

/**
 * @author  dev
 * @description 执行计划 
 */

public class ExecutePlan extends Entity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 计划号 **/
	private String code;
	
	/** 配送计划 **/
	private DeliveryPlan deliveryPlan;
	
	/** 行号 **/
	private int lineNo;
	
	/** 仓库  **/
	private Warehouse warehouse;
	
	/** 库管组号  **/
	private String warehouseWork;
	
	/** 配送班 **/
	private String workGroup;
	
	/** 工位  **/
	private String station;
	
	private Date crateDate = new Date();
	
	/** 计划日期  **/
	private Date planDate = new Date();
	
	/** 计划数量  **/
	private double expectedQuantity = 0D;
	
	/** 实际数量  **/
	private double quantity = 0D;
	
	/** 发货单号  **/
	private String pickTicket;
	
	/** 
	 * 状态
	 *{@link ExecutePlantStatus}
	 ***/
	private String status = ExecutePlantStatus.OPEN;
	
	/** 描述  **/
	private String description;
	
	private Set<ExecutePlanDetail> details = new HashSet<ExecutePlanDetail>();
	
	public ExecutePlan(){}
	
	public boolean equals(final Object other) {
		if (!(other instanceof ExecutePlan))
			return false;
		ExecutePlan castOther = (ExecutePlan) other;
		return new EqualsBuilder()
				.append(code, castOther.getCode())
				.append(warehouse, castOther.getWarehouse())
				.isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder()
			.append(code)
			.append(warehouse)
			.toHashCode();
	}
	
	public double getExpectedQuantity() {
		return expectedQuantity;
	}

	public void setExpectedQuantity(double expectedQuantity) {
		this.expectedQuantity = expectedQuantity;
	}

	public String getCode() {
		return code;
	}

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

	public DeliveryPlan getDeliveryPlan() {
		return deliveryPlan;
	}

	public void setDeliveryPlan(DeliveryPlan deliveryPlan) {
		this.deliveryPlan = deliveryPlan;
	}

	public int getLineNo() {
		return lineNo;
	}

	public void setLineNo(int lineNo) {
		this.lineNo = lineNo;
	}

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public String getWarehouseWork() {
		return warehouseWork;
	}

	public void setWarehouseWork(String warehouseWork) {
		this.warehouseWork = warehouseWork;
	}

	public String getWorkGroup() {
		return workGroup;
	}

	public void setWorkGroup(String workGroup) {
		this.workGroup = workGroup;
	}

	public String getStation() {
		return station;
	}

	public void setStation(String station) {
		this.station = station;
	}

	public Date getCrateDate() {
		return crateDate;
	}

	public void setCrateDate(Date crateDate) {
		this.crateDate = crateDate;
	}

	public Date getPlanDate() {
		return planDate;
	}

	public void setPlanDate(Date planDate) {
		this.planDate = planDate;
	}

	public double getQuantity() {
		return quantity;
	}

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

	public String getPickTicket() {
		return pickTicket;
	}

	public void setPickTicket(String pickTicket) {
		this.pickTicket = pickTicket;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getDescription() {
		return description;
	}

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

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

	public void setDetails(Set<ExecutePlanDetail> details) {
		this.details = details;
	}
	
	public void addDetail(ExecutePlanDetail detail){
		detail.setExecutePlan(this);
		details.add(detail);
	}
	
	public void removeDetail(ExecutePlanDetail detail){
		details.remove(detail);
		detail.setExecutePlan(null);
	}
	
	public void updateDetailQuantity(){
		double expectedQuantity = 0D;
		double quantity = 0D;
		for(ExecutePlanDetail detail : details){
			expectedQuantity += detail.getExpectedQuantity();
			quantity += detail.getQuantity();
		}
		this.expectedQuantity = expectedQuantity;
		this.quantity = quantity;
	}
}
