package com.fp.persistence.pgeneral.product.rate;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import com.fp.dto.hb.Cache;
import com.fp.dto.hb.HibernateBean;

/** Clase que implementa la entidad de Hibernate que hace referencia a la tabla TGENESUBPRODUCTRATE */
@Entity(name = "TgeneSubProductRate")
@Table(name = "TGENESUBPRODUCTRATE")
public class TgeneSubProductRate extends com.fp.dto.AbstractDataTransport implements Serializable, HibernateBean, Cloneable, Cache {
    /**
     * HashCode asociado con la Instancia
     */
    @Transient
    private int hashValue = 0;

    /**
     * Version de la Clase
     */
    private static final long serialVersionUID = 1L;

    /**
     * Clave primaria de la Entidad TgeneSubProductRate
     */
    @EmbeddedId
    @GenericGenerator(name = "seq_id", strategy = "com.fp.general.keygen.SubSequenceKey", parameters = {
            @Parameter(name = "sql", value = "select coalesce(max(RATESEQUENCE),0)+1 from TGENESUBPRODUCTRATE " + "where PRODUCTCODE=:procode "
                    + "and SUBPRODUCTCODE=:sprcode " + "and CURRENCYCODE=:curcode " + "and INTERESTBALANCETYPE=:interestbt "
                    + "and COMPANYCODE=:comcode " + "and INTERESTBALANCEGROUP=:interestbg " + "and MODULECODE=:modcode"),
            @Parameter(name = "type", value = "java.lang.Integer"),
            @Parameter(name = "param", value = "procode,pk.productcode;sprcode,pk.subproductcode;curcode,pk.currencycode;interestbt,pk.interestbalancetype;comcode,pk.companycode;interestbg,pk.interestbalancegroup;modcode,pk.modulecode"),
            @Parameter(name = "field", value = "ratesequence") })
    @GeneratedValue(generator = "seq_id")
    private TgeneSubProductRateKey pk;

    @Version
    @Column(name = "RECORDVERSION", nullable = true)
    /**
     * Manejo de optimistic locking
     */
    private Integer recordversion;

    @Column(name = "BASERATECATALOG", nullable = true)
    /**
     * Codigo de catalogo  01 Natural, 02 Juridico
     */
    private String baseratecatalog;

    @Column(name = "BASERATECATALOGCODE", nullable = true)
    /**
     * Codigo de tabla de catalogo 01 Codigo de tabla de tipos de persona
     */
    private String baseratecatalogcode;

    @Column(name = "MARGIN", nullable = true)
    /**
     * Margen a aplicar dado el operador
     */
    private BigDecimal margin;

    @Column(name = "OPERATOR", nullable = true)
    /**
     * Operador a aplicar sobre la tasa base, +, - , %  
     */
    private String operator;

    @Column(name = "MINTERM", nullable = false)
    /**
     * Plazo minnimo 
     */
    private Integer minterm;

    @Column(name = "MAXTERM", nullable = true)
    /**
     * Plazo maximo
     */
    private Integer maxterm;

    @Column(name = "MINAMOUNT", nullable = false)
    /**
     * Monto minimo 
     */
    private BigDecimal minamount;

    @Column(name = "MAXAMOUNT", nullable = true)
    /**
     * Monto Maximo
     */
    private BigDecimal maxamount;

    @Column(name = "MINRATE", nullable = true)
    /**
     * Minimo permitido en la negociacion del prestamo
     */
    private BigDecimal minrate;

    @Column(name = "MAXRATE", nullable = true)
    /**
     * Maximo permitido en la negociacion del prestamo
     */
    private BigDecimal maxrate;

    @Column(name = "READJUSTBASERATECATALOG", nullable = true)
    /**
     * Codigo de catalogo  de tasas base de reajuste, BCE tasa banco central
     */
    private String readjustbaseratecatalog;

    @Column(name = "READJUSTBASERATECATALOGCODE", nullable = true)
    /**
     * Codigo de tabla de catalogo BASERATE
     */
    private String readjustbaseratecatalogcode;

    @Column(name = "READJUSTMENTOPERATOR", nullable = true)
    /**
     * Operador que se utilizara el momento del reajuste
     */
    private String readjustmentoperator;

    @Column(name = "READJUSTMENTMARGIN", nullable = true)
    /**
     * Margen a aplicar en el reajuste
     */
    private BigDecimal readjustmentmargin;

    @Column(name = "MAXBASERATECATALOG", nullable = true)
    /**
     * Codigo de catalogo  de tasas base maxima, BCE tasa banco central
     */
    private String maxbaseratecatalog;

    @Column(name = "MAXBASERATECATALOGCODE", nullable = true)
    /**
     * Codigo de tabla de catalogo BASERATE
     */
    private String maxbaseratecatalogcode;

    /** Contructor por defecto */
    public TgeneSubProductRate() {
    }

    /**
     * Contructor de TgeneSubProductRate
     * 
     * @param pPk Clave Primaria del entity
     * @param pMinterm Plazo minnimo
     * @param pMinamount Monto minimo
     */
    public TgeneSubProductRate(TgeneSubProductRateKey pPk, Integer pMinterm, BigDecimal pMinamount) {
        this();
        this.pk = pPk;
        this.minterm = pMinterm;
        this.minamount = pMinamount;
    }

    /**
     * Metodo que entrega datos de la tabla dada la clave primaria.
     * 
     * @param pEntityManager referencia de la session a obtener datos del bean.
     * @param pKey Caleve primaria del bean.
     * @return TgeneSubProductRate
     */
    public static TgeneSubProductRate find(EntityManager pEntityManager, TgeneSubProductRateKey pKey) throws Exception {
        TgeneSubProductRate obj = pEntityManager.find(TgeneSubProductRate.class, pKey);
        return obj;
    }

    /**
     * Entrega la Clave primaria de TgeneSubProductRate
     * 
     * @return El objeto que referencia a la Clave primaria de TgeneSubProductRate
     */
    public TgeneSubProductRateKey getPk() {
        return this.pk;
    }

    /**
     * Fija un nuevo valor a la Clave primaria de TgeneSubProductRate
     * 
     * @param pPk El objeto que referencia a la nueva Clave primaria de TgeneSubProductRate
     */
    public void setPk(TgeneSubProductRateKey pPk) {
        this.pk = pPk;
    }

    /**
     * Obtiene el valor de recordversion
     * 
     * @return valor de recordversion
     */
    public Integer getRecordversion() {
        return this.recordversion;
    }

    /**
     * Fija el valor de recordversion
     * 
     * @param pRecordversion nuevo Valor de recordversion
     */
    public void setRecordversion(Integer pRecordversion) {
        this.recordversion = pRecordversion;
    }

    /**
     * Obtiene el valor de baseratecatalog
     * 
     * @return valor de baseratecatalog
     */
    public String getBaseratecatalog() {
        return this.baseratecatalog;
    }

    /**
     * Fija el valor de baseratecatalog
     * 
     * @param pBaseratecatalog nuevo Valor de baseratecatalog
     */
    public void setBaseratecatalog(String pBaseratecatalog) {
        this.baseratecatalog = pBaseratecatalog;
    }

    /**
     * Obtiene el valor de baseratecatalogcode
     * 
     * @return valor de baseratecatalogcode
     */
    public String getBaseratecatalogcode() {
        return this.baseratecatalogcode;
    }

    /**
     * Fija el valor de baseratecatalogcode
     * 
     * @param pBaseratecatalogcode nuevo Valor de baseratecatalogcode
     */
    public void setBaseratecatalogcode(String pBaseratecatalogcode) {
        this.baseratecatalogcode = pBaseratecatalogcode;
    }

    /**
     * Obtiene el valor de margin
     * 
     * @return valor de margin
     */
    public BigDecimal getMargin() {
        return this.margin;
    }

    /**
     * Fija el valor de margin
     * 
     * @param pMargin nuevo Valor de margin
     */
    public void setMargin(BigDecimal pMargin) {
        this.margin = pMargin;
    }

    /**
     * Obtiene el valor de operator
     * 
     * @return valor de operator
     */
    public String getOperator() {
        return this.operator;
    }

    /**
     * Fija el valor de operator
     * 
     * @param pOperator nuevo Valor de operator
     */
    public void setOperator(String pOperator) {
        this.operator = pOperator;
    }

    /**
     * Obtiene el valor de minterm
     * 
     * @return valor de minterm
     */
    public Integer getMinterm() {
        return this.minterm;
    }

    /**
     * Fija el valor de minterm
     * 
     * @param pMinterm nuevo Valor de minterm
     */
    public void setMinterm(Integer pMinterm) {
        this.minterm = pMinterm;
    }

    /**
     * Obtiene el valor de maxterm
     * 
     * @return valor de maxterm
     */
    public Integer getMaxterm() {
        return this.maxterm;
    }

    /**
     * Fija el valor de maxterm
     * 
     * @param pMaxterm nuevo Valor de maxterm
     */
    public void setMaxterm(Integer pMaxterm) {
        this.maxterm = pMaxterm;
    }

    /**
     * Obtiene el valor de minamount
     * 
     * @return valor de minamount
     */
    public BigDecimal getMinamount() {
        return this.minamount;
    }

    /**
     * Fija el valor de minamount
     * 
     * @param pMinamount nuevo Valor de minamount
     */
    public void setMinamount(BigDecimal pMinamount) {
        this.minamount = pMinamount;
    }

    /**
     * Obtiene el valor de maxamount
     * 
     * @return valor de maxamount
     */
    public BigDecimal getMaxamount() {
        return this.maxamount;
    }

    /**
     * Fija el valor de maxamount
     * 
     * @param pMaxamount nuevo Valor de maxamount
     */
    public void setMaxamount(BigDecimal pMaxamount) {
        this.maxamount = pMaxamount;
    }

    /**
     * Obtiene el valor de minrate
     * 
     * @return valor de minrate
     */
    public BigDecimal getMinrate() {
        return this.minrate;
    }

    /**
     * Fija el valor de minrate
     * 
     * @param pMinrate nuevo Valor de minrate
     */
    public void setMinrate(BigDecimal pMinrate) {
        this.minrate = pMinrate;
    }

    /**
     * Obtiene el valor de maxrate
     * 
     * @return valor de maxrate
     */
    public BigDecimal getMaxrate() {
        return this.maxrate;
    }

    /**
     * Fija el valor de maxrate
     * 
     * @param pMaxrate nuevo Valor de maxrate
     */
    public void setMaxrate(BigDecimal pMaxrate) {
        this.maxrate = pMaxrate;
    }

    /**
     * Obtiene el valor de readjustbaseratecatalog
     * 
     * @return valor de readjustbaseratecatalog
     */
    public String getReadjustbaseratecatalog() {
        return this.readjustbaseratecatalog;
    }

    /**
     * Fija el valor de readjustbaseratecatalog
     * 
     * @param pReadjustbaseratecatalog nuevo Valor de readjustbaseratecatalog
     */
    public void setReadjustbaseratecatalog(String pReadjustbaseratecatalog) {
        this.readjustbaseratecatalog = pReadjustbaseratecatalog;
    }

    /**
     * Obtiene el valor de readjustbaseratecatalogcode
     * 
     * @return valor de readjustbaseratecatalogcode
     */
    public String getReadjustbaseratecatalogcode() {
        return this.readjustbaseratecatalogcode;
    }

    /**
     * Fija el valor de readjustbaseratecatalogcode
     * 
     * @param pReadjustbaseratecatalogcode nuevo Valor de readjustbaseratecatalogcode
     */
    public void setReadjustbaseratecatalogcode(String pReadjustbaseratecatalogcode) {
        this.readjustbaseratecatalogcode = pReadjustbaseratecatalogcode;
    }

    /**
     * Obtiene el valor de readjustmentoperator
     * 
     * @return valor de readjustmentoperator
     */
    public String getReadjustmentoperator() {
        return this.readjustmentoperator;
    }

    /**
     * Fija el valor de readjustmentoperator
     * 
     * @param pReadjustmentoperator nuevo Valor de readjustmentoperator
     */
    public void setReadjustmentoperator(String pReadjustmentoperator) {
        this.readjustmentoperator = pReadjustmentoperator;
    }

    /**
     * Obtiene el valor de readjustmentmargin
     * 
     * @return valor de readjustmentmargin
     */
    public BigDecimal getReadjustmentmargin() {
        return this.readjustmentmargin;
    }

    /**
     * Fija el valor de readjustmentmargin
     * 
     * @param pReadjustmentmargin nuevo Valor de readjustmentmargin
     */
    public void setReadjustmentmargin(BigDecimal pReadjustmentmargin) {
        this.readjustmentmargin = pReadjustmentmargin;
    }

    /**
     * Obtiene el valor de maxbaseratecatalog
     * 
     * @return valor de maxbaseratecatalog
     */
    public String getMaxbaseratecatalog() {
        return this.maxbaseratecatalog;
    }

    /**
     * Fija el valor de maxbaseratecatalog
     * 
     * @param pMaxbaseratecatalog nuevo Valor de maxbaseratecatalog
     */
    public void setMaxbaseratecatalog(String pMaxbaseratecatalog) {
        this.maxbaseratecatalog = pMaxbaseratecatalog;
    }

    /**
     * Obtiene el valor de maxbaseratecatalogcode
     * 
     * @return valor de maxbaseratecatalogcode
     */
    public String getMaxbaseratecatalogcode() {
        return this.maxbaseratecatalogcode;
    }

    /**
     * Fija el valor de maxbaseratecatalogcode
     * 
     * @param pMaxbaseratecatalogcode nuevo Valor de maxbaseratecatalogcode
     */
    public void setMaxbaseratecatalogcode(String pMaxbaseratecatalogcode) {
        this.maxbaseratecatalogcode = pMaxbaseratecatalogcode;
    }

    @Override
    public boolean equals(Object rhs) {
        if (rhs == null) {
            return false;
        }
        if (!(rhs instanceof TgeneSubProductRate)) {
            return false;
        }
        TgeneSubProductRate that = (TgeneSubProductRate) rhs;
        if ((this.getPk() == null) || (that.getPk() == null)) {
            return false;
        }
        return (this.getPk().equals(that.getPk()));
    }

    /**
     * Implementacion del metodo hashCode de la la entidad TgeneSubProductRate
     * 
     * @return el hashCode la instancia
     */
    @Override
    public int hashCode() {
        if (this.hashValue == 0) {
            int result = 17;
            if (this.getPk() == null) {
                result = super.hashCode();
            } else {
                result = this.getPk().hashCode();
            }
            this.hashValue = result;
        }
        return this.hashValue;
    }

    /** Implementacion toString */
    @Override
    public String toString() {
        Field[] fs = this.getClass().getDeclaredFields();
        String data = "";
        for (Field f : fs) {
            try {
                String name = f.getName();
                if (f.getType().getName().compareTo("java.util.Set") == 0) {
                    continue;
                }
                if ((name.compareTo("hashValue") == 0) || (name.compareTo("serialVersionUID") == 0)) {
                    continue;
                }
                data += name + "=" + f.get(this) + ";";
            } catch (Exception e) {
                continue;
            }
        }
        if (data.compareTo("") == 0) {
            data = super.toString();
        }
        return data;
    }

    /** Implementacion de la creacion de un bean en blanco TgeneSubProductRate */
    @Override
    public Object createInstance() {
        TgeneSubProductRate instance = new TgeneSubProductRate();
        instance.setPk(new TgeneSubProductRateKey());
        return instance;
    }

    /**
     * Clona la entidad TgeneSubProductRate
     * 
     * @see com.fp.dto.hb.HibernateBean#cloneMe()
     */
    @Override
    public Object cloneMe() throws CloneNotSupportedException {
        TgeneSubProductRate p = (TgeneSubProductRate) this.clone();
        p.setPk((TgeneSubProductRateKey) this.pk.cloneMe());
        return p;
    }

    // Metodos manuales.
    public boolean equalsWithoutSequence(Object rhs) {
        if (rhs == null) {
            return false;
        }
        if (!(rhs instanceof TgeneSubProductRate)) {
            return false;
        }
        TgeneSubProductRate that = (TgeneSubProductRate) rhs;
        if ((this.getPk() == null) || (that.getPk() == null)) {
            return false;
        }

        TgeneSubProductRateKey thispk = this.getPk();
        TgeneSubProductRateKey thatpk = that.getPk();

        if (thatpk == null) {
            return false;
        }
        if (!(thatpk instanceof TgeneSubProductRateKey)) {
            return false;
        }
        if ((thispk.getModulecode() == null) || (thatpk.getModulecode() == null)) {
            return false;
        }
        if (!thispk.getModulecode().equals(thatpk.getModulecode())) {
            return false;
        }
        if ((thispk.getProductcode() == null) || (thatpk.getProductcode() == null)) {
            return false;
        }
        if (!thispk.getProductcode().equals(thatpk.getProductcode())) {
            return false;
        }
        if ((thispk.getSubproductcode() == null) || (thatpk.getSubproductcode() == null)) {
            return false;
        }
        if (!thispk.getSubproductcode().equals(thatpk.getSubproductcode())) {
            return false;
        }
        if ((thispk.getCurrencycode() == null) || (thatpk.getCurrencycode() == null)) {
            return false;
        }
        if (!thispk.getCurrencycode().equals(thatpk.getCurrencycode())) {
            return false;
        }
        if ((thispk.getInterestbalancetype() == null) || (thatpk.getInterestbalancetype() == null)) {
            return false;
        }
        if (!thispk.getInterestbalancetype().equals(thatpk.getInterestbalancetype())) {
            return false;
        }
        if ((thispk.getCompanycode() == null) || (thatpk.getCompanycode() == null)) {
            return false;
        }
        if (!thispk.getCompanycode().equals(thatpk.getCompanycode())) {
            return false;
        }
        if ((thispk.getInterestbalancegroup() == null) || (thatpk.getInterestbalancegroup() == null)) {
            return false;
        }
        if (!thispk.getInterestbalancegroup().equals(thatpk.getInterestbalancegroup())) {
            return false;
        }

        return true;
    }

    /** Sentencia que devuelve una lista de TgeneSubPproductRate. */
    private static final String HQL_SUBPRODUCT_RATE = "from TgeneSubProductRate tsubprate  "
            + " where tsubprate.pk.interestbalancetype = :interestbalancetype " + " and tsubprate.pk.interestbalancegroup = :interestbalancegroup "
            + " and tsubprate.pk.modulecode = :modulecode " + " and tsubprate.pk.productcode = :productcode "
            + " and tsubprate.pk.subproductcode = :subproductcode " + " and tsubprate.pk.companycode = :companycode "
            + " and tsubprate.pk.currencycode = :currencycode ";

    /**
     * Metodo que entrega una lista de tasas definidas por subproducto compania.
     * 
     * @param pEntityManager Referencia a la session de la base de datos.
     * @param pInterestbalancetype Tipo de saldo.
     * @param pInterestbalancegroup Grupo de balance.
     * @param pModule Codigo de modulo.
     * @param pProduct Codigo de producto.
     * @param pSubProduct Codigo de subproducto.
     * @param pCompany Codigo de compania.
     * @param pCurrency Codigo de moneda.
     * @return List<TgeneSubProductRate>
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static final List<TgeneSubProductRate> find(EntityManager pEntityManager, String pInterestbalancetype, String pInterestbalancegroup,
            String pModule, String pProduct, String pSubProduct, Integer pCompany, String pCurrency) throws Exception {
        List<TgeneSubProductRate> lRates = null;
        Query qry = pEntityManager.createQuery(TgeneSubProductRate.HQL_SUBPRODUCT_RATE);
        qry.setParameter("interestbalancetype", pInterestbalancetype);
        qry.setParameter("interestbalancegroup", pInterestbalancegroup);
        qry.setParameter("modulecode", pModule);
        qry.setParameter("productcode", pProduct);
        qry.setParameter("subproductcode", pSubProduct);
        qry.setParameter("companycode", pCompany);
        qry.setParameter("currencycode", pCurrency);
        lRates = qry.getResultList();
        return lRates;
    }

    /** Sentencia que devuelve un registro de TgeneSubProductCharges. */
    private static final String JQL_RATES = "from TgeneSubProductRate tsubpchar  " + " where  tsubpchar.pk.modulecode = :modulecode "
            + " and tsubpchar.pk.productcode = :productcode " + " and tsubpchar.pk.subproductcode = :subproductcode "
            + " and tsubpchar.pk.currencycode = :currencycode " + " and tsubpchar.pk.companycode = :companycode ";

    /**
     * 
     * @param pEntityManager
     * @param pModuleCode
     * @param pProduct
     * @param pSubProduct
     * @return
     * @throws Exception
     */

    @SuppressWarnings("unchecked")
    public static final List<TgeneSubProductRate> find(EntityManager pEntityManager, String pModuleCode, String pProduct, String pSubProduct,
            String pCurrencyCode, Integer pCompanyCode) throws Exception {
        Query qry = pEntityManager.createQuery(TgeneSubProductRate.JQL_RATES);
        qry.setParameter("modulecode", pModuleCode);
        qry.setParameter("productcode", pProduct);
        qry.setParameter("subproductcode", pSubProduct);
        qry.setParameter("currencycode", pCurrencyCode);
        qry.setParameter("companycode", pCompanyCode);

        return qry.getResultList();
    }
}
