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

import java.util.Set;

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

import com.dev.swms.server.model.rules.CompanyWorkRule;
import com.dev.swms.server.model.rules.CustomerWorkRule;
import com.dev.swms.server.model.rules.LotRule;
import com.dev.swms.server.web.filter.WarehouseHolder;
import com.dev.energy.server.model.Entity;

public class Organization extends Entity {
	private static final long serialVersionUID = 1L;
	/** 代码 */
	private String code;
	/** 名称 */
	private String name;
	/** 简称 */
	private String shortName;
	/** 地址 */
	private Address address;
	/** 联系方式 */
	private Contact contact;
	/** 描述 */
	private String description;
	
	/** 是否失效 */
	private boolean disabled = true;
	/** 是否承运商 */
	private boolean beCarrier = false;
	/** 是否客户 */
	private boolean beCustomer = false;
	/** 是否供应商 */
	private boolean beSupplier = false;
	/** 是否货主 */
	private boolean beCompany = false;
	//反向关联CompanyWorkRule，便于取得CompanyWorkRule上的属性值
	private Set<CompanyWorkRule> companyWorkRules;
	
	private Set<CustomerWorkRule> customerWorkRules;
	
	public Set<CompanyWorkRule> getCompanyWorkRules() {
		return companyWorkRules;
	}
	public void setCompanyWorkRules(Set<CompanyWorkRule> companyWorkRules) {
		this.companyWorkRules = companyWorkRules;
	}
	public Set<CustomerWorkRule> getCustomerWorkRules() {
		return customerWorkRules;
	}
	public void setCustomerWorkRules(Set<CustomerWorkRule> customerWorkRules) {
		this.customerWorkRules = customerWorkRules;
	}
	public CompanyWorkRule getCompanyWorkRule(){
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule;
			}
		}
		return null;
	}
	public CustomerWorkRule getCustomerWorkRule(){
		for(CustomerWorkRule rule:this.getCustomerWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule;
			}
		}
		return null;
	}
	public boolean isBeAllowed2ChangePutLoc(){
//		ListableBeanFactory lbf = (ListableBeanFactory) SecurityContextHolder.getCurrentSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//		OrganizationManager manager = (OrganizationManager)lbf.getBean("organizationManager");
//		CompanyWorkRule rule = (CompanyWorkRule)manager.getCommonDao().findByNamedQueryUniqueResult("getCompanyWorkRuleByWarehouseOrg",
//				new String[]{"warehouseId","companyId"},
//				new Object[]{WarehouseHolder.getWarehouse().getId(),this.getId()});
//		if(rule==null){
//			return false;
//		}
//		return rule.isBeAllowed2ChangePutLoc();
		boolean isBeAllowed2ChangePutLoc = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeAllowed2ChangePutLoc();
			}
		}
		return isBeAllowed2ChangePutLoc;
	}
	public boolean isBeAllowed2ChangePickLoc(){
		boolean isBeAllowed2ChangePickLoc = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeAllowed2ChangePickLoc();
			}
		}
		return isBeAllowed2ChangePickLoc;
	}
	
	public LotRule getLotRule(){
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.getLotRule();
			}
		}
		return null;
	}
	//货主多收
	public boolean isBeRevLimit() {
		boolean isBeRevLimit = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeRevLimit();
			}
		}
		return isBeRevLimit;
	}
	//收货人多发
	public boolean isBeShipLimit() {
		boolean isBeShipLimit = false;
		for(CustomerWorkRule rule:this.getCustomerWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeShipLimit();
			}
		}
		return isBeShipLimit;
	}
	public boolean isBePackagedFollowASN() {
		boolean isBePackagedFollowASN = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBePackagedFollowASN();
			}
		}
		return isBePackagedFollowASN;
	}
	public boolean isBeAllowedSplitLPForAllocate() {
		boolean isBeAllowedSplitLPForAllocate = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeAllowedSplitLPForAllocate();
			}
		}
		return isBeAllowedSplitLPForAllocate;
	}
	public boolean isBeAllowedSplitPUForAllocate() {
		boolean isBeAllowedSplitPUForAllocate = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeAllowedSplitPUForAllocate();
			}
		}
		return isBeAllowedSplitPUForAllocate;
	}
	public boolean isCheckReceiveCode() {
		boolean isCheckReceiveCode = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isCheckReceiveCode();
			}
		}
		return isCheckReceiveCode;
	}
	public boolean isCheckReceiveDate() {
		boolean isCheckReceiveDate = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isCheckReceiveDate();
			}
		}
		return isCheckReceiveDate;
	}
	//货主周转规则
	public String getCompanyTurnOverMode() {
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.getCompanyTurnOverMode();
			}
		}
		return "";
	}
	//收货人周转方式
	public String getCustomerTurnOverMode() {
		for(CustomerWorkRule rule:this.getCustomerWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.getCustomerTurnOverMode();
			}
		}
		return "";
	}
	public boolean isBatchPickingSupply() {
		boolean isBatchPickingSupply = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBatchPickingSupply();
			}
		}
		return isBatchPickingSupply;
	}
	public boolean isBeAsnQuality() {
		boolean isBeAsnQuality = false;
		for(CompanyWorkRule rule:this.getCompanyWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeAsnQuality();
			}
		}
		return isBeAsnQuality;
	}
	public boolean isBeAllowed2ChangePickLot() {
		boolean isBeAllowed2ChangePickLot = false;
		for(CustomerWorkRule rule:this.getCustomerWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isBeAllowed2ChangePickLot();
			}
		}
		return isBeAllowed2ChangePickLot;
	}
	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	protected Organization() {
	}

	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public String getCode() {
		return code;
	}

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

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public String getDescription() {
		return description;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isShipPart() {
		boolean isShipPart = false;
		for(CustomerWorkRule rule:this.getCustomerWorkRules()){
			if(rule.getWarehouse().getId().equals(WarehouseHolder.getWarehouse().getId())){
				return rule.isShipPart();
			}
		}
		return isShipPart;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

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

	public boolean isBeCarrier() {
		return beCarrier;
	}

	public void setBeCarrier(boolean beCarrier) {
		this.beCarrier = beCarrier;
	}

	public boolean isBeCustomer() {
		return beCustomer;
	}

	public void setBeCustomer(boolean beCustomer) {
		this.beCustomer = beCustomer;
	}

	public boolean isBeSupplier() {
		return beSupplier;
	}

	public void setBeSupplier(boolean beSupplier) {
		this.beSupplier = beSupplier;
	}

	public boolean isBeCompany() {
		return beCompany;
	}

	public void setBeCompany(boolean beCompany) {
		this.beCompany = beCompany;
	}

	public boolean equals(final Object other) {
		if (!(other instanceof Organization))
			return false;
		Organization castOther = (Organization) other;
		return new EqualsBuilder().append(code, castOther.getCode()).isEquals();
	}

}
