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

import java.util.Date;
import java.util.Map;

import com.dev.swms.server.model.base.ItemKey;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.PackageUnit;
import com.dev.swms.server.model.base.Worker;
import com.dev.swms.server.model.inventory.Stock;
import com.dev.swms.server.utils.DoubleUtils;
import com.dev.energy.server.model.VersionalEntity;
import com.dev.energy.server.web.security.UserHolder;

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

public class CountRecord extends VersionalEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** 盘点计划 */
	private CountPlan countPlan;
	/** 商品属性 */
	private ItemKey itemKey;
	/** 盘点库位 */
	private Location location;
	/** 托盘 */
	private String LP;
	/** 包装单位 */
	private PackageUnit packageUnit;
	/** 库存数量 */
	private double quantity = 0;
	/** 库存数量(基本单位数量) */
	private double quantityMU = 0;
	/** 盘点数量 */
	private double countQuantity = 0;
	/** 差异数量 */
	private double deltaQuantity = 0;
	/** 盘点录入人 */
	private String operator;
	/** 盘点录入时间 */
	private Date occurTime;
	/** 唯一约束键 */
	private int uniCode;
	/** 盘点标志 **/
	private boolean needCount = false;
	/** 操作人员 **/
	private Worker worker;

	protected CountRecord() {
		
	}
	
	public CountRecord(CountPlan countPlan, ItemKey itemKey, Location location, Stock LP, PackageUnit packageUnit) {
		this.countPlan = countPlan;
		this.itemKey = itemKey;
		this.location = location;
		this.LP = (LP==null?null:LP.getLP());
		this.packageUnit = packageUnit;
		genarateUniCode();
	}
	
	public int getUniCode() {
		this.uniCode = this.hashCode();
		return uniCode;
	}
	public void setUniCode(int uniCode) {
		if(uniCode == 0) {
			this.uniCode = this.hashCode();
		}else {
			this.uniCode = uniCode;
		}
	}
	public double getQuantityMU() {
		return quantityMU;
	}
	public void setQuantityMU(double quantityMU) {
		this.quantityMU = quantityMU;
		if (this.packageUnit.getPrecision() == 0) {
			//向上取整，得到整包装数量
			this.quantity = Math.ceil(this.quantityMU / this.packageUnit.getConvertFigure());
		} else {
			//根据包装精度计算包装数量
			this.quantity = DoubleUtils.formatByPrecision(this.quantityMU / this.packageUnit.getConvertFigure(), 
					this.packageUnit.getPrecision());
		}
	}
	public void genarateUniCode() {
		this.uniCode = this.hashCode();
	}
	public CountPlan getCountPlan() {
		return countPlan;
	}
	public void setCountPlan(CountPlan countPlan) {
		this.countPlan = countPlan;
	}
	public double getCountQuantity() {
		return countQuantity;
	}
	public void setCountQuantity(double countQuantity) {
		this.countQuantity = countQuantity;
	}
	public double getDeltaQuantity() {
		return deltaQuantity;
	}
	public void setDeltaQuantity(double deltaQuantity) {
		this.deltaQuantity = deltaQuantity;
	}
	public ItemKey getItemKey() {
		return itemKey;
	}
	public void setItemKey(ItemKey itemKey) {
		this.itemKey = itemKey;
	}
	public Location getLocation() {
		return location;
	}
	public void setLocation(Location location) {
		this.location = location;
	}
	public Date getOccurTime() {
		return occurTime;
	}
	public void setOccurTime(Date occurTime) {
		this.occurTime = occurTime;
	}
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public PackageUnit getPackageUnit() {
		return packageUnit;
	}
	public void setPackageUnit(PackageUnit packageUnit) {
		this.packageUnit = packageUnit;
	}
	public double getQuantity() {
		return quantity;
	}
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}
	public String getLP() {
		return LP;
	}
	public void setLP(String lp) {
		LP = lp;
	}

	public boolean equals(final Object other) {
		if (!(other instanceof CountRecord))
			return false;
		CountRecord castOther = (CountRecord) other;
		return new EqualsBuilder().append(countPlan, castOther.getCountPlan())
				.append(itemKey, castOther.getItemKey())
				.append(location, castOther.getLocation())
				.append(LP, castOther.getLP())
				.append(packageUnit, castOther.getPackageUnit()).isEquals();
	}
	public int hashCode() {
		return new HashCodeBuilder().append(countPlan).append("|")
				.append(itemKey == null ? 0 : itemKey.getId()).append("|")
				.append(location == null ? 0 : location.getId()).append("|")
				.append(LP).append("|")
				.append(packageUnit == null ? 0 : packageUnit.getId()).append("|")
				.toHashCode();
	}

	/**
	 * 实盘数据登记
	 * 累计实盘数量，并修改差异数量和登记人、登记时间等信息
	 * @param quantity
	 */
	public void record(double quantity) {
		this.countQuantity += quantity;
		this.deltaQuantity = this.countQuantity-this.quantityMU;
		this.operator = UserHolder.getUser().getName();
		this.occurTime = new Date();
		this.needCount = Boolean.TRUE;
	}

	public boolean isNeedCount() {
		return needCount;
	}

	public void setNeedCount(boolean needCount) {
		this.needCount = needCount;
	}

	public Worker getWorker() {
		return worker;
	}

	public void setWorker(Worker worker) {
		this.worker = worker;
	}
	
	public Map<Integer,String> getLotInfoValue(){
		return itemKey.getLotInfoValue();
	}

}
