/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.basic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;

/**
 * @version $Id: SIFeeRule.java,v 1.0 2003/11/21 Exp $
 * @author  Jinwang Chen
 * <br>Description: 送料ルール
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 *  ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * Jinwang Chen   2003/11/21 14:06:23  Original
 */
public class SIFeeRule extends SIBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //管理コード
  private String mallShopCode="";
  
  //配送種別コード
  private String deliveryTypeCode="";
  
  //送料無料設定フラグ
  private String freeFlg="0";
  
  //購入金額
  private String price="0";
  
  //購入金額
  private String spPrice1="0";
  
  //購入金額
  private String spPrice2="0";
  
  //複数購入時設定フラグ
  private String multiBuyFlg="0";
  
  //送料単価
  private String unitPrice="0";
  
  public SIFeeRule(){
  }
  
  public SIFeeRule(String lMallShopCode,String lDeliveryTypeCode){
    setMallShopCode(lMallShopCode);
    setDeliveryTypeCode(lDeliveryTypeCode);
  }
  
  //setter of 管理コード
  public void setMallShopCode(String lMallShopCode){
    if (SIUtil.isNull(lMallShopCode)) lMallShopCode="";
    this.mallShopCode=SIUtil.changeTo(lMallShopCode.trim(),this.encode);
  }
  
  //setter of 配送種別コード
  public void setDeliveryTypeCode(String lDeliveryTypeCode){
    if (SIUtil.isNull(lDeliveryTypeCode)) lDeliveryTypeCode="";
    this.deliveryTypeCode=SIUtil.changeTo(lDeliveryTypeCode.trim(),this.encode);
  }
  
  //setter of 送料無料設定フラグ
  public void setFreeFlg(String lFreeFlg){
    if (SIUtil.isNull(lFreeFlg)) lFreeFlg="0";
    this.freeFlg=SIUtil.changeTo(lFreeFlg.trim(),this.encode);
  }
  
  //setter of 購入金額
  public void setPrice(String lPrice){
    if (SIUtil.isNull(lPrice)) lPrice="0";
    this.price=SIUtil.changeTo(lPrice.trim(),this.encode);
  }
  
  //setter of 購入金額
  public void setSpPrice1(String lPrice){
    if (SIUtil.isNull(lPrice)) lPrice="0";
    this.spPrice1=SIUtil.changeTo(lPrice.trim(),this.encode);
  }
  
  //setter of 購入金額
  public void setSpPrice2(String lPrice){
    if (SIUtil.isNull(lPrice)) lPrice="0";
    this.spPrice2=SIUtil.changeTo(lPrice.trim(),this.encode);
  }
  
  //setter of 複数購入時設定フラグ
  public void setMultiBuyFlg(String lMultiBuyFlg){
    if (SIUtil.isNull(lMultiBuyFlg)) lMultiBuyFlg="0";
    this.multiBuyFlg=SIUtil.changeTo(lMultiBuyFlg.trim(),this.encode);
  }
  
  //setter of 送料単価
  public void setUnitPrice(String lUnitPrice){
    if (SIUtil.isNull(lUnitPrice)) lUnitPrice="0";
    this.unitPrice=SIUtil.changeTo(lUnitPrice.trim(),this.encode);
  }
  
  //getter of 管理コード
  public String getMallShopCode(){
    return this.mallShopCode;
  }
  
  //getter of 配送種別コード
  public String getDeliveryTypeCode(){
    return this.deliveryTypeCode;
  }
  
  //getter of 送料無料設定フラグ
  public String getFreeFlg(){
    return this.freeFlg;
  }
  
  //getter of 購入金額
  public String getPrice(){
    return this.price;
  }
  
  //getter of 購入金額
  public String getSpPrice1(){
    return this.spPrice1;
  }
  
  //getter of 購入金額
  public String getSpPrice2(){
    return this.spPrice2;
  }
  
  //getter of 複数購入時設定フラグ
  public String getMultiBuyFlg(){
    return this.multiBuyFlg;
  }
  
  //getter of 送料単価
  public String getUnitPrice(){
    return this.unitPrice;
  }
  
  /**
   * <b>reset</b>
   * ショップコードと配送種別コードから送料ルールを取得します
   * @param lConnection
   * @return なし
   */
  public void reset(Connection lConnection){
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT * FROM FeeRuleMTbl " );
    lSqlBuf.append("WHERE MallShopCode="+SIDBUtil.SQL2Str(getMallShopCode()," "));
    lSqlBuf.append("AND DeliveryTypeCode="+SIDBUtil.SQL2Str(getDeliveryTypeCode()," "));
    
    reset(lConnection, lSqlBuf.toString());
  }

  /**
   * <b>reset</b>
   * SQLを実行し送料ルールを取得します
   * @param lConnection
   * @param lSql
   * @return boolean 情報があるかどうか
   */
  public boolean reset(Connection lConnection ,String lSql){
    Statement lStatement=null;
    ResultSet lResultSet=null;
    boolean  lRes=false;
    
    log.debug("reset:lSql="+lSql);
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSql);
      
      if (lResultSet.next()){
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setMallShopCode(lResultSet.getString("mallShopCode"));//管理コード
        this.setDeliveryTypeCode(lResultSet.getString("deliveryTypeCode"));//配送種別コード
        this.setFreeFlg(lResultSet.getString("freeFlg"));//送料無料設定フラグ
        this.setPrice(lResultSet.getString("price"));//購入金額
        this.setSpPrice1(lResultSet.getString("spprice1"));//購入金額
        this.setSpPrice2(lResultSet.getString("spprice2"));//購入金額
        this.setMultiBuyFlg(lResultSet.getString("multiBuyFlg"));//複数購入時設定フラグ
        this.setUnitPrice(lResultSet.getString("unitPrice"));//送料単価
        lRes=true;
      }else {
        log.error("not find record for deliveryTypeCode="+getDeliveryTypeCode());
      }
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    return lRes;
  }
}
