package com.uas.platform.b2b.model;

import java.io.Serializable;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.uas.platform.b2b.erp.model.InquiryVendorInfo;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.alibaba.fastjson.annotation.JSONField;
import com.uas.platform.core.persistence.Logger;
import com.uas.platform.core.persistence.OtherUserUU;
import com.uas.platform.core.persistence.StatusColumn;
import com.uas.platform.core.persistence.UserUU;

@Entity
@Table(name = "purc$vendors", indexes = {@Index(name = "index_vendor_status", columnList = "ve_status"), @Index(name = "index_vendor_myen_venden", columnList = "ve_myenuu, ve_vendenuu", unique = true)})
@Logger(enUU = "ve_myenuu", userUU = "ve_myuseruu")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "com.uas.platform.b2b.model.Vendor")
public class Vendor implements Serializable {

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

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ve_id")
	private Long id;

	/**
	 * 状态
	 */
	@Column(name = "ve_status")
	@StatusColumn
	private Integer status;

	/**
     * 我的企业UU
     */
    @Column(name = "ve_myenuu")
    private Long myEnUU;
	
	/**
	 * 我的企业
	 */
	@OneToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE })
	@JoinColumn(name = "ve_myenuu", insertable = false, updatable = false, nullable = false)
	private Enterprise myEnterprise;

	/**
	 * 我的UU
	 */
	@Column(name = "ve_myuseruu")
	@UserUU
	private Long myUserUU;
	
	/**
	 * 我方联系人UU
	 */
	@OneToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE })
	@JoinColumn(name = "ve_myuseruu", insertable = false, updatable = false)
	private UserBaseInfo myUser;

	@Column(name = "ve_vendenuu")
	private Long vendEnUU;

	/**
	 * 供应商的企业UU
	 */
	@OneToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE })
	@JoinColumn(name = "ve_vendenuu", insertable = false, updatable = false, nullable = false)
	private Enterprise vendorEnterprise;

	@Column(name = "ve_venduseruu")
	private Long vendUserUU;

	/**
	 * 供应商联系人UU
	 */
	@OneToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE })
	@JoinColumn(name = "ve_venduseruu", insertable = false, updatable = false)
	@OtherUserUU
	private UserBaseInfo vendorUser;
	
	/**
	 * 客户费率
	 */
	@Column(name = "ve_rate")
	private Double rate;

	/**
	 * 判断是否被分配
	 */
	@Transient
	private boolean distribute;

	/**
	 * 是否启用b2b对账功能<br>
	 * 1. 是 <br>
	 * 0. 否
	 */
	@Column(name = "ve_apcheck")
	private Short apcheck;

	/**
	 * 供应商开关，客户操作，因为是相对的，如果一个条件不能构成约束<br>
	 * 1. 启用<br>
	 * 0. 禁用
	 */
	@Column(name = "ve_vendswitch")
	private Short vendswitch;

	/**
	 * 服务商开关，供应商操作，因为是相对的，如果一个条件不能构成约束<br>
	 * 1. 启用<br>
	 * 0. 禁用
	 */
	@Column(name = "ve_servicerswitch")
	private Short servicerswitch;

	/**
	 * 客户开关，供应商操作，因为是相对的，如果一个条件不能构成约束<br>
	 * 1. 启用<br>
	 * 0. 禁用
	 */
	@Column(name = "ve_custswitch")
	private Short custswitch;

	/**
	 * 付款方式（保存最新的，方便下次载入）
	 */
	@Column(name = "ve_paymethod")
	private String paymethod;

	/**
	 * 同步状态（是否同步到账户中心，1为已同步，0为未同步）
	 */
	@Column(name = "ve_synchstatus")
	private Short synchStatus;

	/**
	 * 同步到erp的状态
	 * <pre>
	 *     主要是针对erp邀请注册后自动添加供应商，并且同步erp的供应商uu
	 * </pre>
	 */
	@Column(name = "ve_erpstatus")
	private Integer erpstatus;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getStatus() {
		return status;
	}

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

	@JsonIgnore
	@JSONField(serialize = false)
	public Long getMyEnUU() {
		return myEnUU;
	}

	public void setMyEnUU(Long myEnUU) {
		this.myEnUU = myEnUU;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public Long getMyUserUU() {
		return myUserUU;
	}

	public void setMyUserUU(Long myUserUU) {
		this.myUserUU = myUserUU;
	}

	public Enterprise getVendorEnterprise() {
		return vendorEnterprise;
	}

	public void setVendorEnterprise(Enterprise vendorEnterprise) {
		this.vendorEnterprise = vendorEnterprise;
	}

	public UserBaseInfo getVendorUser() {
		return vendorUser;
	}

	public void setVendorUser(UserBaseInfo vendorUser) {
		this.vendorUser = vendorUser;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public Long getVendEnUU() {
		return vendEnUU;
	}

	public void setVendEnUU(Long vendEnUU) {
		this.vendEnUU = vendEnUU;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public Long getVendUserUU() {
		return vendUserUU;
	}

	public void setVendUserUU(Long vendUserUU) {
		this.vendUserUU = vendUserUU;
	}

	public Enterprise getMyEnterprise() {
		return myEnterprise;
	}

	public void setMyEnterprise(Enterprise myEnterprise) {
		this.myEnterprise = myEnterprise;
	}

	public UserBaseInfo getMyUser() {
		return myUser;
	}

	public void setMyUser(UserBaseInfo myUser) {
		this.myUser = myUser;
	}

	public Double getRate() {
		return rate;
	}

	public void setRate(Double rate) {
		this.rate = rate;
	}

	public boolean getDistribute() {
		return distribute;
	}

	public void setDistribute(boolean distribute) {
		this.distribute = distribute;
	}

	public Short getApcheck() {
		return apcheck;
	}

	public void setApcheck(Short apcheck) {
		this.apcheck = apcheck;
	}

	public Short getVendswitch() {
		return vendswitch;
	}

	public void setVendswitch(Short vendswitch) {
		this.vendswitch = vendswitch;
	}

	public Short getServicerswitch() {
		return servicerswitch;
	}

	public void setServicerswitch(Short servicerswitch) {
		this.servicerswitch = servicerswitch;
	}

	public Short getCustswitch() {
		return custswitch;
	}

	public void setCustswitch(Short custswitch) {
		this.custswitch = custswitch;
	}

	public String getPaymethod() {
		return paymethod;
	}

	public void setPaymethod(String paymethod) {
		this.paymethod = paymethod;
	}

	public Short getSynchStatus() {
		return synchStatus;
	}

	public void setSynchStatus(Short synchStatus) {
		this.synchStatus = synchStatus;
	}

	@Override
	public String toString() {
		return this.vendorEnterprise.getEnName();
	}

	public Integer getErpstatus() {
		return erpstatus;
	}

	public void setErpstatus(Integer erpstatus) {
		this.erpstatus = erpstatus;
	}


	public InquiryVendorInfo covert() {
		InquiryVendorInfo info = new InquiryVendorInfo();
		info.setB2b_id(this.id);
		info.setVe_uu(String.valueOf(this.vendEnUU));
		info.setVe_webserver(this.vendorEnterprise.getEnBussinessCode());
		return info;
	}
}
