/*
 * Created on 2003/11/18
 *
 * 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.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.config.SIFlagConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.tools.SIFatalException;
import jp.co.sint.tools.SIStringUtil;
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 SIPayMethod extends SIBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  private String mallShopCode="";
  private String payMethodCode="";
  private String payMethodName="";
  private String paymentFlg="";
  private String fee="";
  private String taxRate="0";	
  
  //購入金額(代金引換を選択した時の計算用)
  private String price="";
  
  public SIPayMethod(){}
  
  public SIPayMethod(String lMallShopCode,String lPayMethodCode){
    setMallShopCode(lMallShopCode);
    setPayMethodCode(lPayMethodCode);
  }
  
  /**
   * @return
   */
  public String getFee() {
    return fee;
  }
  
  public String getFeeIncTax(){
    String lTax="0";
    try {
      lTax=SIUtil.multi_LD(getFee(),SIUtil.div_DL(getTaxRate(),"100"));
    } catch (SIFatalException e) {
      e.printStackTrace();
    }
    return SIUtil.add_LL(getFee(),lTax);
  }
  
  public String getTaxRate() {
    return this.taxRate;
  }
  
  public String getPrice() {
    return this.price;
  }
  
  /**
   * @return
   */
  public String getMallShopCode() {
    return mallShopCode;
  }
  
  /**
   * @return
   */
  public String getPaymentFlg() {
    return paymentFlg;
  }
  
  /**
   * @return
   */
  public String getPayMethodCode() {
    return payMethodCode;
  }
  
  /**
   * @return
   */
  public String getPayMethodName() {
    return payMethodName;
  }
  
  /**
   * @param string
   */
  public void setFee(String string) {
    if(string==null)string="";
    fee = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  public void setTaxRate(String lTaxRate){
    if (SIUtil.isNull(lTaxRate))lTaxRate="0";
    this.taxRate=lTaxRate;
  }
  
  public void setPrice(String lPrice){
    if (SIUtil.isNull(lPrice))lPrice="";
    this.price=lPrice;
  }
  
  /**
   * @param string
   */
  public void setMallShopCode(String string) {
    if(string==null)string="";
    mallShopCode = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  /**
   * @param string
   */
  public void setPaymentFlg(String string) {
    if(string==null)string="";
    paymentFlg = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  /**
   * @param string
   */
  public void setPayMethodCode(String string) {
    if(string==null)string="";
    payMethodCode = SIUtil.changeTo(string.trim(),this.encode);
  }
  
  /**
   * @param string
   */
  public void setPayMethodName(String string) {
    if(string==null)string="";
    string = SIUtil.changeTo(string.trim(),this.encode);
    payMethodName = SIStringUtil.rightTrim(string);
  }
  
  public boolean reset(Connection lConnection){
    boolean lResult=false;
    
    if (SIUtil.isNull(this.getMallShopCode())||SIUtil.isNull(this.getPayMethodCode())) return lResult;
    
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT * FROM PayMethodMTbl");
    lSqlBuf.append(" WHERE mallshopcode=").append(SIDBUtil.SQL2Str(this.getMallShopCode()));
    lSqlBuf.append(" AND paymethodcode=").append(SIDBUtil.SQL2Str(this.getPayMethodCode()));
    
    log.debug("reset:lSqlBuf="+lSqlBuf.toString());
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      if (lResultSet.next()){
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setFee(lResultSet.getString("Fee"));
        this.setPayMethodName(lResultSet.getString("PayMethodName"));
        this.setPaymentFlg(lResultSet.getString("PaymentFlg"));
        lResult=true;
      }else {
        log.error("not find record for PayMethodCode="+getPayMethodCode());
      } 
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    return lResult;
  }
  
  public boolean resetPayMethodName(Connection lConnection){
    if (SIUtil.isNull(getPayMethodName()))return false;//7.1.1 ST0204 追加
    boolean lResult=false;
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT aa.*,(SELECT TaxRate FROM TaxVW) AS TaxRate ");
    lSqlBuf.append("FROM PayMethodMTbl aa");
    lSqlBuf.append(" WHERE aa.MallShopCode=").append(SIDBUtil.SQL2Str(getMallShopCode()));
    lSqlBuf.append(" AND aa.PayMethodName=").append(SIDBUtil.SQL2Str(getPayMethodName()));
    
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    try {
      log.debug("resetPayMethodName:lSqlBuf="+lSqlBuf.toString());
      
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      if (lResultSet.next()){
        log.debug("lResultSet.getString(\"TaxRate\")="+lResultSet.getString("TaxRate"));
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setMallShopCode(lResultSet.getString("mallShopCode"));
        this.setPayMethodCode(lResultSet.getString("payMethodCode"));
        this.setPayMethodName(lResultSet.getString("payMethodName"));
        this.setPaymentFlg(lResultSet.getString("paymentFlg"));
        this.setTaxRate(lResultSet.getString("TaxRate"));
        
        //支払区分が代金引換の時
        if(this.getPaymentFlg().equals("2") && SIUtil.isNotNull(this.getPrice())){
          lSqlBuf = new StringBuffer("SELECT priceTo,fee FROM PayMethodFeeMTbl ");
          lSqlBuf.append(" WHERE MallShopCode=").append(SIDBUtil.SQL2Str(getMallShopCode()));
          lSqlBuf.append(" AND PayMethodCode=").append(SIDBUtil.SQL2Str(this.getPayMethodCode()));
          lSqlBuf.append(" ORDER BY priceTO ASC ");
          
          Statement lStatement2=lConnection.createStatement();
          ResultSet lResultSet2=lStatement2.executeQuery(lSqlBuf.toString());
          long priceFrom = 0;
          long priceTo = 0;
          long price = Long.parseLong(this.getPrice());
          if(price<0){
            price = 0;
          }
          String fee = "0";
          while(lResultSet2.next()){
            priceTo = lResultSet2.getInt("priceTo");
            if(priceFrom <= price && price <= priceTo){
              fee = lResultSet2.getString("fee");
            }
            priceFrom = priceTo + 1;
          }
          SIDBUtil.close(lStatement2,lResultSet2);
          this.setFee(fee);
        }else{
          this.setFee(lResultSet.getString("fee"));
        }
        lResult=true;
      }else {
        log.error("not find record for payMethodName="+getPayMethodName());
      } 
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    return lResult;
  }
  
  public Collection getCollection(Connection lConnection){
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    Collection lResultColl=new ArrayList();
    SIPayeeInfo lPayeeInfo=new SIPayeeInfo();
    
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT aa.*,bb.paymethodname FROM PayeeInfoMTbl aa,");
    lSqlBuf.append("PayMethodMTbl bb  ");
    lSqlBuf.append("WHERE aa.MallShopCode=bb.MallShopCode AND aa.PayMethodCode=bb.PayMethodCode ");
    lSqlBuf.append("AND bb.PayMethodName=").append(SIDBUtil.SQL2Str(getPayMethodName()," "));
    lSqlBuf.append("AND bb.MallShopCode=").append(SIDBUtil.SQL2Str(getMallShopCode()," "));
    lSqlBuf.append("ORDER BY aa.mallshopcode ASC ,aa.paymethodcode ASC, aa.payeecode ASC ");
    log.debug("getCollection:lSqlBuf="+lSqlBuf.toString());
    
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      while(lResultSet.next()){
        lPayeeInfo=new SIPayeeInfo();
        lPayeeInfo.setEncode(SIConfig.SIENCODE_NONE);
        lPayeeInfo.setMallShopCode(lResultSet.getString("mallshopcode"));
        lPayeeInfo.setPayMethodCode(lResultSet.getString("paymethodcode"));
        lPayeeInfo.setPayMethodName(lResultSet.getString("paymethodname"));
        lPayeeInfo.setPayeeCode(lResultSet.getString("payeecode"));
        lPayeeInfo.setBankCode(lResultSet.getString("bankcode"));
        lPayeeInfo.setBankName(lResultSet.getString("bankname"));
        lPayeeInfo.setBankNameKana(lResultSet.getString("banknamekana"));
        lPayeeInfo.setSubBankCode(lResultSet.getString("subbankcode"));
        lPayeeInfo.setSubBankName(lResultSet.getString("subbankname"));
        lPayeeInfo.setSubBankNameKana(lResultSet.getString("subbanknamekana"));
        lPayeeInfo.setAccountType(lResultSet.getString("accounttype"));
        lPayeeInfo.setAccountNo(lResultSet.getString("accountNo"));
        lPayeeInfo.setAccountName(lResultSet.getString("accountname"));
        
        lResultColl.add(lPayeeInfo);
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    
    return lResultColl;
  }
  
  public HashMap getKeyList(Connection lConnection){
    Statement lStatement = null;
    ResultSet lResultSet = null;
    HashMap key = new HashMap();
    String lSql = "SELECT payMethodCode,payMethodCode FROM paymethodmtbl WHERE MallShoCode = '0'";
    try {
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSql);
      while (lResultSet.next()) {
        key.put(lResultSet.getString("payMethodCode"),lResultSet.getString("payMethodCode"));
      }
    } catch (SQLException sqle) {
      sqle.printStackTrace();
    } finally {
      SIDBUtil.close(lStatement, lResultSet);
    }
    return key;
  }
  
  /**
    * getNameCollection
    * 支払方法リストの取得
    * @param lConnection DBへのコネクション
    * @param lMallShopCode モールまたはショップコード
    * @return 支払方法のリスト
    * @throws なし
    */
  public static Collection getNameCollection(Connection lConnection,String lMallShopCode){
    return SIPayMethod.getNameCollection(lConnection,lMallShopCode,true);
  }
  
  /**
    * getNameCollection
    * 支払方法リストの取得
    * @param lConnection DBへのコネクション
    * @param lMallShopCode モールまたはショップコード
    * @param siteFlg Frontか、Backか
    * @return 支払方法のリスト
    * @throws なし
    */
  public static Collection getNameCollection(Connection lConnection,String lMallShopCode,boolean siteFlg){
    return getNameCollection(lConnection, lMallShopCode, siteFlg, true);
  }
  
  /**
   * getNameCollection
   * 支払方法リストの取得
   * @param lConnection    DBへのコネクション
   * @param lMallShopCode  モールまたはショップコード
   * @param siteFlg        Frontか、Backか
   * @param addBlankFlg    一番上に空白行を挿入するかどうか
   * @return 支払方法のリスト
   * @throws なし
   */
  public static Collection getNameCollection(Connection lConnection,String lMallShopCode,boolean siteFlg, boolean addBlankFlg){
    if (SIUtil.isNull(lMallShopCode)) return new ArrayList();
    
    StringBuffer lSqlBuf=new StringBuffer("SELECT PayMethodName,PayMethodName FROM PayMethodMTbl WHERE MallShopCode=");
    lSqlBuf.append(SIDBUtil.SQL2Str(lMallShopCode));
    //バックからの呼び出しのときはカード決済できない。
    //バックから呼び出されたときには、コンビニ決済も選択できない。 //7.2.0 ST0300
    if(!siteFlg){
      lSqlBuf.append(" AND PaymentFlg <> ").append(SIDBUtil.SQL2Str(SIFlagConf.SIFLAG_PAYMENTFLG_CARD," "));
      //7.2.0 ST0300 追加 ここから
      lSqlBuf.append(" AND PaymentFlg <> ").append(SIDBUtil.SQL2Str(SIFlagConf.SIFLAG_PAYMENTFLG_CVS, " "));
      //7.2.0 ST0300 追加 ここまで
    }
    //フロントからの呼び出しのときは代金引換を選択できない //7.3.0 PI-NES0501
    else{
      lSqlBuf.append(" AND NOT(PaymentFlg = ").append(SIDBUtil.SQL2Str(SIFlagConf.SIFLAG_PAYMENTFLG_CASH," "));
      lSqlBuf.append(" AND PayMethodName <> ").append(SIDBUtil.SQL2Str("代金引換",") "));//「代金引換」のみOK
    }
    lSqlBuf.append(" AND PayMethodName NOT LIKE '楽天%' ");//楽天系は非表示
    
    lSqlBuf.append(" ORDER BY PaymethodCode ASC");
    
    Collection lResultColl=new ArrayList();
    
    try {
      //7.2.0 ST1030 修正 ここから
      lResultColl=SIDBUtil.getCollection(lConnection, lSqlBuf.toString(), addBlankFlg);
      //lResultColl=SIDBUtil.getCollection(lConnection,lSqlBuf.toString(),true);
      //7.2.0 ST1030 修正 ここまで
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    return lResultColl;
  }
  
  public static Collection getNameCollection2(Connection lConnection,String lMallShopCode,boolean siteFlg, boolean addBlankFlg){
    if (SIUtil.isNull(lMallShopCode)) return new ArrayList();
    
    StringBuffer lSqlBuf=new StringBuffer("SELECT PayMethodName,PayMethodName FROM PayMethodMTbl WHERE MallShopCode=");
    lSqlBuf.append(SIDBUtil.SQL2Str(lMallShopCode));
    //バックからの呼び出しのときはカード決済できない。
    //バックから呼び出されたときはコンビニ決済も選択できない。 //7.2.0 ST0300
    //バックから呼び出されたときは代金引換も選択できない。
    if(!siteFlg){
      lSqlBuf.append(" AND PaymentFlg <> ").append(SIDBUtil.SQL2Str(SIFlagConf.SIFLAG_PAYMENTFLG_CARD," "));
      //7.2.0 ST0300 追加 ここから
      lSqlBuf.append(" AND PaymentFlg <> ").append(SIDBUtil.SQL2Str(SIFlagConf.SIFLAG_PAYMENTFLG_CVS, " "));
      //7.2.0 ST0300 追加 ここまで
      lSqlBuf.append(" AND PayMethodName <> ").append(SIDBUtil.SQL2Str("代金引換"," "));//「代金引換」のみNG
    }
    //フロントからの呼び出しのときは代金引換を選択できない //7.3.0 PI-NES0501
    else{
      lSqlBuf.append(" AND PaymentFlg <> ").append(SIDBUtil.SQL2Str(SIFlagConf.SIFLAG_PAYMENTFLG_CASH," "));
    }
    lSqlBuf.append(" AND PayMethodName NOT LIKE '楽天%' ");//楽天系は非表示
    lSqlBuf.append(" ORDER BY PaymethodCode ASC");
    
    Collection lResultColl=new ArrayList();
    
    try {
      //7.2.0 ST1030 修正 ここから
      lResultColl=SIDBUtil.getCollection(lConnection, lSqlBuf.toString(), addBlankFlg);
      //lResultColl=SIDBUtil.getCollection(lConnection,lSqlBuf.toString(),true);
      //7.2.0 ST1030 修正 ここまで
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    return lResultColl;
  }
  /**
   * getNameCollection
   * 支払方法リストの取得(受注・入金一覧画面)
   * @param lConnection    DBへのコネクション
   * @param lMallShopCode  モールまたはショップコード
   * @param siteFlg        Frontか、Backか
   * @param addBlankFlg    一番上に空白行を挿入するかどうか
   * @return 支払方法のリスト
   * @throws なし
   */
  public static Collection getMainNameCollection(Connection lConnection,String lMallShopCode,boolean addBlankFlg){
    if (SIUtil.isNull(lMallShopCode)) return new ArrayList();
    
    StringBuffer lSqlBuf=new StringBuffer("SELECT PayMethodName,PayMethodName FROM PayMethodMTbl WHERE MallShopCode=");
    lSqlBuf.append(SIDBUtil.SQL2Str(lMallShopCode));
    lSqlBuf.append(" ORDER BY PaymethodCode ASC");
    Collection lResultColl=new ArrayList();
    try {
      lResultColl=SIDBUtil.getCollection(lConnection, lSqlBuf.toString(), addBlankFlg);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    return lResultColl;
  }
  
  /**
    * getNameValueCollection
    * 支払方法コードと支払方法名のセットの取得
    * @param lConnection DBへのコネクション
    * @param lMallShopCode モールまたはショップコード
    * @return 支払方法コードと支払方法名のセット
    * @throws なし
    */
  public static Collection getNameValueCollection(Connection lConnection,String lMallShopCode){
    if (SIUtil.isNull(lMallShopCode)) return new ArrayList();
    
    StringBuffer lSqlBuf=new StringBuffer("SELECT PayMethodName,paymethodcode FROM PayMethodMTbl WHERE MallShopCode=");
    lSqlBuf.append(SIDBUtil.SQL2Str(lMallShopCode));
    lSqlBuf.append(" ORDER BY PaymethodCode ASC");
    
    Collection lResultColl=new ArrayList();
    
    try {
      lResultColl=SIDBUtil.getCollection(lConnection,lSqlBuf.toString(),true);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
    
    return lResultColl;
  }
}