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

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.Item;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.model.Entity;

public class CountPlan extends Entity { 
	/** 仓库 */
	private Warehouse warehouse;
	/** 盘点计划号 */
	private String code;
	/** 盘点计划时间 */
	private Date planDate;
	/** 盘点时间 */
	private Date countDate;
	/** 盘点人 */
	private String countPeople;
	/** 盘点类型 */
	private String countType;
	/** 盘点方式 */
	private String countMethod;
	/** 单据状态 */
	private String status;
	/** 描述 */
	private String description;
	/** 盘点货主 */
	private Structure company;
	/** 盘点明细 */
	private Set<CountDetail> details = new HashSet<CountDetail>();
	/** 盘点记录 */
	private Set<CountRecord> records = new HashSet<CountRecord>();
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Date getCountDate() {
		return countDate;
	}
	public void setCountDate(Date countDate) {
		this.countDate = countDate;
	}
	public String getCountMethod() {
		return countMethod;
	}
	public void setCountMethod(String countMethod) {
		this.countMethod = countMethod;
	}
	public String getCountPeople() {
		return countPeople;
	}
	public void setCountPeople(String countPeople) {
		this.countPeople = countPeople;
	}
	public String getCountType() {
		return countType;
	}
	public void setCountType(String countType) {
		this.countType = countType;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Date getPlanDate() {
		return planDate;
	}
	public void setPlanDate(Date planDate) {
		this.planDate = planDate;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Warehouse getWarehouse() {
		return warehouse;
	}
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}
	public Set<CountDetail> getDetails() {
		return details;
	}
	public void setDetails(Set<CountDetail> details) {
		this.details = details;
	}
	public Set<CountRecord> getRecords() {
		return records;
	}
	public void setRecords(Set<CountRecord> records) {
		this.records = records;
	}
	
	public boolean equals(final Object other) {
		if (!(other instanceof CountPlan))
			return false;
		CountPlan castOther = (CountPlan) other;
		return new EqualsBuilder().append(warehouse, castOther.getWarehouse())
				.append(code, castOther.getCode()).isEquals();
	}
	public int hashCode() {
		return new HashCodeBuilder().append(warehouse).append(code)
				.toHashCode();
	}
	
	/**
	 * 取消激活盘点计划
	 * @return true---成功 false---失败
	 */
	public boolean inactive() {
		if (CountStatus.ACTIVE.equals(this.getStatus())) {
			//库位解锁
			Set<Location> locs = new HashSet<Location>();
			for (CountRecord record : this.getRecords()) {
				locs.add(record.getLocation());
			}
			for (Location loc : locs) {
				loc.setCycleDate(null);
			}
			unlockLocations(locs);

			this.getRecords().clear();
			this.setStatus(CountStatus.OPEN);
			return true;
		}
		return false;
	}
	
	/**
	 * 激活盘点计划
	 * @return true---成功 false---失败
	 */
	public boolean active() {
		if (CountStatus.OPEN.equals(this.getStatus())) {
			this.setStatus(CountStatus.ACTIVE);
			return true;
		}
		return false;
	}
	
	/**
	 * 取消盘点计划
	 * @return true---成功 false---失败
	 */
	public boolean cancel() {
		if (CountStatus.OPEN.equals(this.getStatus())) {
			this.setStatus(CountStatus.CANCEL);
			return true;
		}
		return false;
	}

	/**
	 * 关闭盘点计划
	 * @return true---成功 false---失败
	 */
	public boolean close() {
		if (CountStatus.COUNT.equals(this.getStatus()) || CountStatus.ADJUST.equals(this.getStatus())) {
			this.setStatus(CountStatus.CLOSE);
			//库位解锁
			Set<Location> locs = new HashSet<Location>();
			for (CountRecord record : this.getRecords()) {
				locs.add(record.getLocation());
			}
			unlockLocations(locs);
			return true;
		}
		return false;
	}
	
	public void lockLocations(Set<Location> locs, String code) {
		for (Location location : locs) {
			if (location.getCountLock()==null||location.getCountLock().equals("") 
					|| location.getCountLock().equals(code)) {
				location.setCountLock(code);
			} else {
				throw new BusinessException("location.isCountLock");
			}
		}
	}

	public void unlockLocations(Set<Location> locs) {
		for (Location location : locs) {
			if(!location.getCountLock().equals("")){
				location.setCountLock("");
			}
		}
	}

	public boolean isCointainInPlan(Item item) {
		for(CountRecord record : records){
			if(record.getItemKey().getItem().equals(item)){
				return true;
			}
		}
		return false;
	}
	
	public void addRecord(CountRecord countRecord) {
		if(getRecords() == null){
			setRecords(new HashSet<CountRecord>());
		}
		getRecords().add(countRecord);
		countRecord.setCountPlan(this);
	}
	public Structure getCompany() {
		return company;
	}
	public void setCompany(Structure company) {
		this.company = company;
	}
}
