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

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

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

import com.dev.energy.server.model.Entity;
import com.dev.swms.server.model.abstractEntity.Pinfan;
import com.dev.swms.server.model.inventory.Inventory;

public class ItemKey extends Entity {
	/** 批次号 */
	private String lot;
	/** 所属品番 */
	private Pinfan pinfan;
	/** 所属商品 */
	private Item item;
	/** 商品字符串属性 */
	private LotInfo lotInfo;
	/** 收货日期 **/
	private Date storageDate;
	/** SOI **/
	private String SOI;//备注信息
	
	/** 新增属性：批次属性 */
	private String content;
	/** 包含库存 */
	private Set<Inventory> inventories = new HashSet<Inventory>();
	
	public Set<Inventory> getInventories() {
		return inventories;
	}
	
	public Pinfan getPinfan() {
		return pinfan;
	}

	public void setPinfan(Pinfan pinfan) {
		this.pinfan = pinfan;
	}

	public void setInventories(Set<Inventory> inventories) {
		this.inventories = inventories;
	}
	public void setContent(String content) {
		if (lotInfo != null) {
			this.content = lotInfo.toString();
		}else{
			this.content = content;
		}
	}
	public String getContent(){
		return content;
	}
	
	
	public ItemKey(){
	}
	
	public ItemKey(String lot, Item item, LotInfo lotInfo, Date storageDate, String soi,Pinfan pinfan) {
		this.lot = lot;
		this.item = item;
		this.lotInfo = lotInfo;
//		this.storageDate = storageDate;
		this.SOI = soi;
		this.pinfan=pinfan;
		if(lotInfo!=null){
			this.content = lotInfo.toString();
		}
	}
	
	public ItemKey(String lot, Item item,Pinfan pinfan) {
		this.lot = lot;
		this.item = item;
		this.pinfan=pinfan;
		if(lotInfo!=null){
			this.content = lotInfo.toString();
		}
	}
	
	

	public String getSOI() {
		return SOI;
	}
	public void setSOI(String soi) {
		SOI = soi;
	}
	public Date getStorageDate() {
		return storageDate;
	}
	public void setStorageDate(Date storageDate) {
		this.storageDate = storageDate;
	}
	public String getLot() {
		return lot;
	}
	public void setLot(String lot) {
		this.lot = lot;
	}
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public LotInfo getLotInfo() {
		return lotInfo;
	}
	public void setLotInfo(LotInfo lotInfo) {
		this.lotInfo = lotInfo;
	}

	public boolean isNullProperty() {
		if (this.lotInfo.getPropC1() == null && this.lotInfo.getPropC2() == null && this.lotInfo.getPropC3() == null 
				&& this.lotInfo.getPropC4() == null && this.lotInfo.getPropC5() == null && this.lotInfo.getPropC6() == null 
				&& this.lotInfo.getPropC7() == null && this.lotInfo.getPropC8() == null && this.lotInfo.getPropC9() == null 
				&& this.lotInfo.getPropN1() == 0	  && this.lotInfo.getPropN2() == 0    && this.lotInfo.getPropN3() == 0	
				&& this.lotInfo.getPropD1() == null && this.lotInfo.getPropD2() == null && this.lotInfo.getPropD3() == null) {
			return true;
		} else {
			return false;
		}
	}

	public void setInvaidEnd(Item item){
		this.lotInfo.setPropD2(
				getDateOfDaysLater(this.lotInfo.getPropD1(),item.getValidPeriod().intValue()));
		
	}
	
	public void setInvaidStart(Item item){
		this.lotInfo.setPropD1(
				getDateOfDaysLater(this.lotInfo.getPropD2(),-item.getValidPeriod().intValue()));
		
	}
	
	private Date getDateOfDaysLater(Date start,int delta){
		Calendar cal = new GregorianCalendar();
		cal.setTime(start);
		cal.add(Calendar.DATE , delta);	
		return cal.getTime();
	}

	public boolean equals(final Object other) {
		if (!(other instanceof ItemKey))
			return false;
		ItemKey castOther = (ItemKey) other;
		return new EqualsBuilder().append(lot, castOther.getLot()).isEquals();
	}
	public int hashCode() {
		return new HashCodeBuilder().append(lot).toHashCode();
	}	
	
	public Map<Integer,String> getLotInfoMap(){
		return lotInfo.getLotInfoMap();
	}
	
	public Map<Integer,Boolean> getLotRuleMap(){
		return item.getLotRuleMap();
	}
	
	public Map<Integer,String> getLotInfoValue(){
		Map<Integer,String> lotInfoValue = new HashMap<Integer, String>();
		Map<Integer,String> lotInfo = getLotInfoMap();
		Map<Integer,Boolean> lotRule = getLotRuleMap();
		for(int i = 1; i <= 18; i++){
			if(lotRule.get(i)){
				lotInfoValue.put(i, lotInfo.get(i));
			}
		}
		return lotInfoValue;
	}
	
	/**
	 * 检查商品库存是否存在，如不存在则返回null
	 * @param location 存货库位
	 * @param packageUnit 包装单位
	 * @return null－未找到；inventory－找到
	 */
	public Inventory getInventory(Location location, String status) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		if(soi==null){
//			soi="";
//		}
		for (Inventory inventory : this.inventories) {
			if (inventory.getLocation().getId().equals(location.getId()) 
					//&& inventory.getPackageUnit().getId().equals(packageUnit.getId()) 
//					&& sdf.format(inventory.getStorageDate()).equals(sdf.format(storageDate)) 
//					&& (soi==null||soi.equals(inventory.getSOI()))
//					&& ((LP == null && inventory.getLP() == null) || (inventory.getLP() != null && LP != null && inventory.getLP().getId().equals(LP.getId())))
//					&& ((inventory.getStatus()==null&& status==null)||(inventory.getStatus()!=null&& status!=null&&inventory.getStatus().equals(status)))
					) {
				return inventory;
			}
		}
		return null;
	}
	
	public Inventory getInventory(Location location, String status,WarehouseArea zone) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		if(soi==null){
//			soi="";
//		}
		for (Inventory inventory : this.inventories) {
			if (inventory.getLocation().getId().equals(location.getId()) 
					&&inventory.getZone().getId().equals(zone.getId())) {
				return inventory;
			}
		}
		return null;
	}
	public Inventory getInventoryNew(Location location, String status) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		if(soi==null){
//			soi="";
//		}
		for (Inventory inventory : this.inventories) {
//			if (inventory.getLocation().getId().equals(location.getId()) 
					//&& inventory.getPackageUnit().getId().equals(packageUnit.getId()) 
//					&& sdf.format(inventory.getStorageDate()).equals(sdf.format(storageDate)) 
//					&& (soi==null||soi.equals(inventory.getSOI()))
//					&& ((LP == null && inventory.getLP() == null) || (inventory.getLP() != null && LP != null && inventory.getLP().getId().equals(LP.getId())))
//					&& ((inventory.getStatus()==null&& status==null)||(inventory.getStatus()!=null&& status!=null&&inventory.getStatus().equals(status)))
//					) {
				return inventory;
//			}
		}
		return null;
	}
	public Inventory getInventoryNew1(Location location, String status) {
		Inventory dbInv = getInventory(location,  status);
		if (dbInv == null) {
			dbInv = new Inventory();//EntityFactory.getEntity(Inventory.class);
			dbInv.setLocation(location);
			dbInv.setItemKey(this);
//			location.setCurrentItem(this.getItem().getId());
//			location.setCurrentLot(this.getId());
//			dbInv.setCaCoefficient(this.getItem().getCAcoefficient()==null?0:this.getItem().getCAcoefficient());
//			dbInv.setLP(LP);
//			dbInv.setBarCode(barCode);
			dbInv.setStatus(status);
//			dbInv.setStorageDate(storageDate);
//			dbInv.setSOI(soi == null ? "" : soi);
			this.inventories.add(dbInv);
		}
		
		return dbInv;
	}
	
	/**
	 * 减少库存数
	 * @param inventory 待减少库存
	 * @param quantity 减少数量（基本单位数量）
	 */
	public void removeInventory(Inventory inventory, double quantity,double weight,double volume) {
		inventory.remove(quantity,weight,volume);
		if (inventory.getQtyOfMasterPack() == 0 && inventory.getQueuedQuantity() == 0) {
			this.getInventories().remove(inventory);
		}
	}
	
	
	
	public Inventory adjustInventoryAdd1(Location location, double quantity, 
			String status,Boolean lock,Boolean reject) {
		Inventory inventory = getInventoryNew1(location, status);
		inventory.add(quantity,0,0);
		if (inventory.isNew()) {
			this.getInventories().add(inventory);
		} else {
			if (inventory.getQtyOfMasterPack() == 0 && inventory.getQueuedQuantity() == 0) {
				this.getInventories().remove(inventory);
			}
		}
		return inventory;
	}
}
