/**
 * Created on 2003/08/28
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package jp.co.sint.basic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

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

import org.apache.log4j.Category;

/**
 * @author arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SIWrapping extends SIBasic {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  private String shopCode = "";
  private String wrappingCode = "";
  private String wrappingName = "";
  private String description = "";
  private String price = "0";
  private String taxRate="0";
  private String taxFlg="2";
  
  public SIWrapping(){
  }
  
  public SIWrapping(String lShopCode,String lWrappingCode){
    setShopCode(lShopCode);
    setWrappingCode(lWrappingCode);
  }
  
  /**
   * @return
   */
  public String getDescription() {
    return description;
  }
  
  /**
   * @return
   */
  public String getPrice() {
    return price;
  }
  
  public String getPriceIncTax(){
    String lTax="0";//7.1.1 ST0204 修正
    try {
      if (getTaxFlg().equals("1")){
        lTax=SIUtil.multi_LD(getPrice(),SIUtil.div_DL(getTaxRate(),"100"));
      }
    } catch (SIFatalException e) {
      e.printStackTrace();
    }
    return SIUtil.add_LL(getPrice(),lTax);
  }
  
  public String getTaxFlg(){
    return this.taxFlg;
  }
  
  public String getTaxRate(){
    return this.taxRate;
  }
  
  public String getTaxName(){
    if (getTaxFlg().equals("0")) return "("+SIFlagConf.SIFLAG_TAX_0+")";
    else if(getTaxFlg().equals("1"))return "("+SIFlagConf.SIFLAG_TAX_1+")";
    else if(getTaxFlg().equals("2"))return "("+SIFlagConf.SIFLAG_TAX_2+")";
    else return "";
  }
  
  /**
   * @return
   */
  public String getShopCode() {
    return shopCode;
  }
  
  /**
   * @return
   */
  public String getWrappingCode() {
    return wrappingCode;
  }
  
  /**
   * @return
   */
  public String getWrappingName() {
    return wrappingName;
  }
  
  public String getImageFileName(int lIndex){
    if (SIUtil.isNull(getShopCode())) return "";
    StringBuffer lImageFileNameBuf=new StringBuffer();
    lImageFileNameBuf.append(getShopCode()).append("/");
    lImageFileNameBuf.append(getWrappingCode()).append(SIConfig.SISUFFIX_SHOP_WRAPPING_IMAGE_NAME[lIndex]);
    lImageFileNameBuf.append(".jpg");
    log.debug("getImageFileName:lImageFileNameBuf="+lImageFileNameBuf.toString());
    return lImageFileNameBuf.toString();
  }
  
  public String getImageFileNameOnly(int lIndex){
    if (SIUtil.isNull(getShopCode())) return "";
    StringBuffer lImageFileNameOnlyBuf=new StringBuffer();
    lImageFileNameOnlyBuf.append(getWrappingCode()).append(SIConfig.SISUFFIX_SHOP_WRAPPING_IMAGE_NAME[lIndex]);
    lImageFileNameOnlyBuf.append(".jpg");
    log.debug("getImageFileName:lImageFileNameOnlyBuf="+lImageFileNameOnlyBuf.toString());
    return lImageFileNameOnlyBuf.toString();
  }
  
  /**
   * @param string
   */
  public void setDescription(String string) {
    if (SIUtil.isNull(string)) string = "";
    this.description = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  /**
   * @param string
   */
  public void setPrice(String string) {
    if (SIUtil.isNull(string))string="0";
    price = string;
  }
  
  public void setTaxFlg(String lTaxFlg){
    if (SIUtil.isNull(lTaxFlg))lTaxFlg="2";
    this.taxFlg =lTaxFlg;
  }
  
  public void setTaxRate(String lTaxRate){
    if (SIUtil.isNull(lTaxRate))lTaxRate="0";
    this.taxRate =lTaxRate;
  }
  
  /**
   * @param string
   */
  public void setShopCode(String string) {
    shopCode = string;
  }
  
  /**
   * @param string
   */
  public void setWrappingCode(String string) {
    wrappingCode = string;
  }
  
  /**
   * @param string
   */
  public void setWrappingName(String string) {
    if (SIUtil.isNull(string))
      string = "";
    this.wrappingName = SIUtil.changeTo(string.trim(), this.encode);
  }
  
  public void reset(Connection lConnection){
    Statement lStatement=null;
    ResultSet lResultSet=null;
    StringBuffer lSqlBuf=new StringBuffer();
    
    lSqlBuf.append("SELECT aa.*,COALESCE((SELECT TaxRate FROM TaxVW),0) AS TaxRate ");
    lSqlBuf.append("FROM WrappingMTbl").append(SIDBMultiConf.SIALIAS_CURR_NAME).append("aa WHERE aa.ShopCode=");
    lSqlBuf.append(SIDBUtil.SQL2Str(getShopCode()," "));
    lSqlBuf.append("AND aa.WrappingCode="+SIDBUtil.SQL2Str(getWrappingCode()));
    
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      if (lResultSet.next()){
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setShopCode(lResultSet.getString("shopCode"));//ショップコード
        this.setWrappingCode(lResultSet.getString("wrappingCode"));//wrappingCode
        this.setWrappingName(lResultSet.getString("wrappingName"));//wrappingName
        this.setDescription(lResultSet.getString("description"));//説明
        this.setPrice(lResultSet.getString("price"));//価格
        this.setTaxFlg(lResultSet.getString("taxFlg"));//税フラグ
        this.setTaxRate(lResultSet.getString("taxRate"));//税率
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
  }
  
  public static Collection getCollection(Connection lConnection,String lShopCode){
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    Collection lResultColl=new ArrayList();
    SIWrapping lWrapping=new SIWrapping();
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT aa.*,COALESCE((SELECT TaxRate FROM TaxVW),0) AS TaxRate ");
    lSqlBuf.append("FROM WrappingMTbl").append(SIDBMultiConf.SIALIAS_CURR_NAME).append("aa ");
    lSqlBuf.append("WHERE aa.ShopCode="+SIDBUtil.SQL2Str(lShopCode));
    
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      while(lResultSet.next()){
        lWrapping=new SIWrapping();
        lWrapping.setEncode(SIConfig.SIENCODE_NONE);
        lWrapping.setShopCode(lResultSet.getString("shopCode"));//ショップコード
        lWrapping.setWrappingCode(lResultSet.getString("wrappingCode"));//wrappingCode
        lWrapping.setWrappingName(lResultSet.getString("wrappingName"));//wrappingName
        lWrapping.setDescription(lResultSet.getString("description"));//説明
        lWrapping.setPrice(lResultSet.getString("price"));//価格
        lWrapping.setTaxFlg(lResultSet.getString("taxFlg"));//税フラグ
        lWrapping.setTaxRate(lResultSet.getString("taxRate"));//税率
        
        lResultColl.add(lWrapping);
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    
    return lResultColl;
  }
}