package cn.palmte.work.model.process;

import org.hibernate.Hibernate;
import org.hibernate.annotations.GenericGenerator;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * 采购合同流程预算采购明细的数量记录
 * <p>
 * 一个 BudgetPurchaseAmount 对应 多个 BudgetPurchaseDetail
 * <p>
 * 相同的 budgetCostId 下的 amountAlready amountLeft 分别都一样,
 * amountCurrent 代表当前的提交审核后本次采购的数目
 *
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
 * @since 2.0 2022/12/28 14:15
 */
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "procurement_contract_budget_purchase_amount")
public class BudgetPurchaseAmount implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @GenericGenerator(name = "persistenceGenerator", strategy = "increment")
  private Integer id;

  // TODO 撤回的时候 amountAlready 数据一致问题

  // 所有的
  private BigDecimal amount;

  // '本次采购数量'
  private BigDecimal amountCurrent;

  // 该流程是否提交，提交了才计算到已采购数量
  private Boolean submit;

  // 流程ID
  private Integer processId;

  // 采购合同ID
  private Integer contractId;

  // 成本ID
  private Integer budgetCostId;

  @Transient
  public boolean isSubmitted() {
    return submit != null && submit;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o))
      return false;
    BudgetPurchaseAmount that = (BudgetPurchaseAmount) o;
    return id != null && Objects.equals(id, that.id);
  }

  @Override
  public int hashCode() {
    return getClass().hashCode();
  }

}
