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.Structure;
import com.dev.energy.server.model.Entity;

public class DeliveryPlan extends Entity {

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

	/** 计划号  **/
	private String code;
	
	/** 事业部  **/
	private Structure company;
	
	/** 计划创建时间 **/
	private Date createDate = new Date();
	
	/** 计划时间 **/
	private Date planDate = new Date();
	
	/** 计划完成率 **/
	private double completionRate = 0D;
	
	/** 项目名称  **/
	private String project;
	
	/** 计划数量  **/
	private double expectedQuantity = 0D;
	
	/** 实际数量  **/
	private double quantity = 0D;
	
	/** 
	 * 状态
	 *{@link DeliveryPlanStatus}
	 ***/
	private String status = DeliveryPlanStatus.OPEN;
	
	/** 紧急程度  **/
	private String level = DeliveryPlanLevel.NORMAL;
	
	/** 补货单号  **/
	private String asnCode;
	
	/** 
	 * 补货状态  
	 * {@link ReplenishmentStatus}
	 **/
	private String replenishmentStatus = ReplenishmentStatus.NORMAL;
	
	/** 描述  **/
	private String description;
	
	/** 配送计划明细  **/
	private Set<DeliveryPlanDetail> details = new HashSet<DeliveryPlanDetail>();
	
	private Set<ExecutePlan> executePlans = new HashSet<ExecutePlan>();
	
	public DeliveryPlan(){}
	
	public boolean equals(final Object other) {
		if (!(other instanceof DeliveryPlan))
			return false;
		DeliveryPlan castOther = (DeliveryPlan) other;
		return new EqualsBuilder()
				.append(code, castOther.getCode())
				.isEquals();
	}

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

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

	public double getQuantity() {
		return quantity;
	}

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

	public Date getPlanDate() {
		return planDate;
	}

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

	public String getCode() {
		return code;
	}

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

	public Structure getCompany() {
		return company;
	}

	public void setCompany(Structure company) {
		this.company = company;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public double getCompletionRate() {
		return completionRate;
	}

	public void setCompletionRate(double completionRate) {
		this.completionRate = completionRate;
	}

	public String getProject() {
		return project;
	}

	public void setProject(String project) {
		this.project = project;
	}

	public String getStatus() {
		return status;
	}

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

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getAsnCode() {
		return asnCode;
	}

	public void setAsnCode(String asnCode) {
		this.asnCode = asnCode;
	}

	public String getReplenishmentStatus() {
		return replenishmentStatus;
	}

	public void setReplenishmentStatus(String replenishmentStatus) {
		this.replenishmentStatus = replenishmentStatus;
	}

	public String getDescription() {
		return description;
	}

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

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

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

	public Set<ExecutePlan> getExecutePlans() {
		return executePlans;
	}

	public void setExecutePlans(Set<ExecutePlan> executePlans) {
		this.executePlans = executePlans;
	}
}
