/*
 * Created on 2003/09/11
 *
 * 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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;

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

import org.apache.log4j.Category;

/**
 * @version $Id: SICustAddress.java,v 1.0 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>
 * arai           2003/06/25  Original
 */

public class SICustAddress extends SIBasic {

  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  //顧客コード
  private String custCode="";

  //アドレス帳番号
  private String addressCode="";

  //法人個人区分
  private String custCompanyFlg="0";

  //配送先呼称
  private String deliveryName="";

  //配送先正式名
  private String deliveryAddressee="";

  //EMAIL
  private String email="";

  //郵便番号1
  private String postCode1="";

  //郵便番号2
  private String postCode2="";

  //住所1
  private String address1="";

  //住所2
  private String address2="";

  //住所3
  private String address3="";

  //サロン名
  private String companyName="";
  
  //会社名
  private String corporationName="";

  //連絡先電話番号
  private String tel="";

  //連絡先FAX番号
  private String fax="";

  //送信フラグ
  private String sendMailFlg="0";

  //何日前
  private String daysBeforehand="0";

  //お知らせ日
  private String mailMonth="";

  //お知らせ日
  private String mailDay="";

  //最終配送日
  private String lastDeliveryDate="";

  //最終配送日の年
  private String lastDeliveryDateYear="";

  //最終配送日の月
  private String lastDeliveryDateMonth="";

  //最終配送日の日
  private String lastDeliveryDateDay="";

  //登録日時
  private String initDateTime="";

  //更新日時
  private String updateDateTime="";

  //EDBTG005-00 kamata add start
  // 登録更新フラグ
  private String insertUpdateFlg = "";
  //EDBTG005-00 kamata add end
  
  public SICustAddress(){
  }


  //EDBTG005-00 kamata add start
  public String getInsertUpdateFlg() {
    return insertUpdateFlg;
  }

  public void setInsertUpdateFlg(String insertUpdateFlg) {
    if (SIUtil.isNull(insertUpdateFlg)) insertUpdateFlg="";
    this.insertUpdateFlg = insertUpdateFlg;
  }
  //EDBTG005-00 kamata add end
  
  public SICustAddress(String lCustCode,String lAddressCode){
    setCustCode(lCustCode);
    setAddressCode(lAddressCode);
  }

  //setter of 顧客コード
  public void setCustCode(String lCustCode){
    if (SIUtil.isNull(lCustCode)) lCustCode="";
    this.custCode=SIUtil.changeTo(lCustCode.trim(),this.encode);
  }

  //setter of アドレス帳番号
  public void setAddressCode(String lAddressCode){
    if (SIUtil.isNull(lAddressCode)) lAddressCode="";
    this.addressCode=SIUtil.changeTo(lAddressCode.trim(),this.encode);
  }

  //setter of 法人個人区分
  public void setCustCompanyFlg(String lCustCompanyFlg){
    if (SIUtil.isNull(lCustCompanyFlg)) lCustCompanyFlg="0";
    this.custCompanyFlg=SIUtil.changeTo(lCustCompanyFlg.trim(),this.encode);
  }

  //setter of 配送先呼称
  public void setDeliveryName(String lDeliveryName){
    if (SIUtil.isNull(lDeliveryName)) lDeliveryName="";
    this.deliveryName=SIUtil.changeTo(lDeliveryName.trim(),this.encode);
  }

  //setter of 配送先正式名
  public void setDeliveryAddressee(String lDeliveryAddressee){
    if (SIUtil.isNull(lDeliveryAddressee)) lDeliveryAddressee="";
    this.deliveryAddressee=SIUtil.changeTo(lDeliveryAddressee.trim(),this.encode);
  }

  //setter of EMAIL
  public void setEmail(String lEmail){
    if (SIUtil.isNull(lEmail)) lEmail="";
    this.email=SIUtil.changeTo(lEmail.trim(),this.encode);
  }

  //setter of 郵便番号1
  public void setPostCode1(String lPostCode1){
    if (SIUtil.isNull(lPostCode1)) lPostCode1="";
    this.postCode1=SIUtil.changeTo(lPostCode1.trim(),this.encode);
  }

  //setter of 郵便番号2
  public void setPostCode2(String lPostCode2){
    if (SIUtil.isNull(lPostCode2)) lPostCode2="";
    this.postCode2=SIUtil.changeTo(lPostCode2.trim(),this.encode);
  }

  //setter of 住所1
  public void setAddress1(String lAddress1){
    if (SIUtil.isNull(lAddress1)) lAddress1="";
    this.address1=SIUtil.changeTo(lAddress1.trim(),this.encode);
  }

  //setter of 住所2
  public void setAddress2(String lAddress2){
    if (SIUtil.isNull(lAddress2)) lAddress2="";
    this.address2=SIUtil.changeTo(lAddress2.trim(),this.encode);
  }

  //setter of 住所3
  public void setAddress3(String lAddress3){
    if (SIUtil.isNull(lAddress3)) lAddress3="";
    this.address3=SIUtil.changeTo(lAddress3.trim(),this.encode);
  }

  //setter of サロン名
  public void setCompanyName(String lCompanyName){
    if (SIUtil.isNull(lCompanyName)) lCompanyName="";
    this.companyName=SIUtil.changeTo(lCompanyName.trim(),this.encode);
  }

  //setter of 会社名
  public void setCorporationName(String lCorporationName){
    if (SIUtil.isNull(lCorporationName)) lCorporationName="";
    this.corporationName=SIUtil.changeTo(lCorporationName.trim(),this.encode);
  }

  //setter of 連絡先電話番号
  public void setTel(String lTel){
    if (SIUtil.isNull(lTel)) lTel="";
    this.tel=SIUtil.changeTo(lTel.trim(),this.encode);
  }

  //setter of 連絡先FAX番号
  public void setFax(String lFax){
    if (SIUtil.isNull(lFax)) lFax="";
    this.fax=SIUtil.changeTo(lFax.trim(),this.encode);
  }

  //setter of 送信フラグ
  public void setSendMailFlg(String lSendMailFlg){
    if (SIUtil.isNull(lSendMailFlg)) lSendMailFlg="0";
    this.sendMailFlg=SIUtil.changeTo(lSendMailFlg.trim(),this.encode);
  }

  //setter of 何日前
  public void setDaysBeforehand(String lDaysBeforehand){
    if (SIUtil.isNull(lDaysBeforehand)) lDaysBeforehand="0";
    this.daysBeforehand=SIUtil.changeTo(lDaysBeforehand.trim(),this.encode);
  }

  //setter of お知らせ日
  public void setMailMonth(String lMailMonth){
    if (SIUtil.isNull(lMailMonth)) lMailMonth="";
    this.mailMonth=SIUtil.changeTo(lMailMonth.trim(),this.encode);
  }

  //setter of お知らせ日
  public void setMailDay(String lMailDay){
    if (SIUtil.isNull(lMailDay)) lMailDay="";
    this.mailDay=SIUtil.changeTo(lMailDay.trim(),this.encode);
  }

  //setter of 最終配送日
  public void setLastDeliveryDate(String lYear,String lMonth,String lDay){
    this.setLastDeliveryDateYear(lYear);
    this.setLastDeliveryDateMonth(lMonth);
    this.setLastDeliveryDateDay(lDay);
  }

  //setter of 最終配送日
  public void setLastDeliveryDate(String lLastDeliveryDate){
    SIDateTime lDateTime;
    if (SIUtil.isNull(lLastDeliveryDate)) lLastDeliveryDate="";
    else{
      try {
        lDateTime=new SIDateTime(lLastDeliveryDate,SIConfig.SIDATE_FORMAT);
        setLastDeliveryDate(lDateTime.getYearStr(),lDateTime.getMonthStr(),lDateTime.getDayStr());
      } catch (ParseException e) {
        setLastDeliveryDate(lLastDeliveryDate,"","");
        e.printStackTrace();
      }
    }
    this.lastDeliveryDate=lLastDeliveryDate;
  }

  //setter of 最終配送日の年
  public void setLastDeliveryDateYear(String lLastDeliveryDateYear){
    if (SIUtil.isNull(lLastDeliveryDateYear)) this.lastDeliveryDateYear="";
    else this.lastDeliveryDateYear=lLastDeliveryDateYear;
  }

  //setter of 最終配送日の月
  public void setLastDeliveryDateMonth(String lLastDeliveryDateMonth){
    if (SIUtil.isNull(lLastDeliveryDateMonth)) this.lastDeliveryDateMonth="";
    else this.lastDeliveryDateMonth=lLastDeliveryDateMonth;
  }

  //setter of 最終配送日の日
  public void setLastDeliveryDateDay(String lLastDeliveryDateDay){
    if (SIUtil.isNull(lLastDeliveryDateDay)) this.lastDeliveryDateDay="";
    else this.lastDeliveryDateDay=lLastDeliveryDateDay;
  }

  //setter of 登録日時
  public void setInitDateTime(String lInitDateTime){
    if (SIUtil.isNull(lInitDateTime)) lInitDateTime="";
    this.initDateTime=SIUtil.changeTo(lInitDateTime.trim(),this.encode);
  }

  //setter of 更新日時
  public void setUpdateDateTime(String lUpdateDateTime){
    if (SIUtil.isNull(lUpdateDateTime)) lUpdateDateTime="";
    this.updateDateTime=SIUtil.changeTo(lUpdateDateTime.trim(),this.encode);
  }

  //getter of 顧客コード
  public String getCustCode(){
    return this.custCode;
  }

  //getter of アドレス帳番号
  public String getAddressCode(){
    return this.addressCode;
  }

  //getter of 法人個人区分
  public String getCustCompanyFlg(){
    return this.custCompanyFlg;
  }

  //getter of 配送先呼称
  public String getDeliveryName(){
    return this.deliveryName;
  }

  //getter of 配送先正式名
  public String getDeliveryAddressee(){
    return this.deliveryAddressee;
  }

  //getter of EMAIL
  public String getEmail(){
    return this.email;
  }

  //getter of 郵便番号1
  public String getPostCode1(){
    return this.postCode1;
  }

  //getter of 郵便番号2
  public String getPostCode2(){
    return this.postCode2;
  }

  //getter of 住所1
  public String getAddress1(){
    return this.address1;
  }

  //getter of 住所2
  public String getAddress2(){
    return this.address2;
  }

  //getter of 住所3
  public String getAddress3(){
    return this.address3;
  }

  //getter of サロン名
  public String getCompanyName(){
    return this.companyName;
  }

  //getter of 会社名
  public String getCorporationName(){
    return this.corporationName;
  }

  //getter of 連絡先電話番号
  public String getTel(){
    return this.tel;
  }

  //getter of 連絡先FAX番号
  public String getFax(){
    return this.fax;
  }

  //getter of 送信フラグ
  public String getSendMailFlg(){
    return this.sendMailFlg;
  }

  //getter of 何日前
  public String getDaysBeforehand(){
    return this.daysBeforehand;
  }

  //getter of お知らせ日
  public String getMailMonth(){
    return this.mailMonth;
  }

  //getter of お知らせ日
  public String getMailDay(){
    return this.mailDay;
  }

  //getter of 最終配送日
  public String getLastDeliveryDate(){
    return SIDateTime.getDate(getLastDeliveryDateYear(),getLastDeliveryDateMonth(),getLastDeliveryDateDay());
  }

  //getter of 最終配送日の年
  public String getLastDeliveryDateYear(){
    return this.lastDeliveryDateYear;
  }

  //getter of 最終配送日の月
  public String getLastDeliveryDateMonth(){
    return this.lastDeliveryDateMonth;
  }

  //getter of 最終配送日の日
  public String getLastDeliveryDateDay(){
    return this.lastDeliveryDateDay;
  }

  //getter of 登録日時
  public String getInitDateTime(){
    return this.initDateTime;
  }

  //getter of 更新日時
  public String getUpdateDateTime(){
    return this.updateDateTime;
  }


  //getter of 郵便番号
  public String getPostCode(){
    if (SIUtil.isNotNull(getPostCode1())&&SIUtil.isNotNull(getPostCode2())){
      return getPostCode1()+"-"+getPostCode2();
    }else if (SIUtil.isNotNull(getPostCode1())||SIUtil.isNotNull(getPostCode2())){
      return getPostCode1()+getPostCode2();
    }else return "";
  }

  public String getAddress(){
    return getAddress1()+getAddress2()+getAddress3();
  }

  /**
   * <b>reset</b>
   * 顧客コードに対するアドレス帳があるか判定、顧客アドレス情報を取得
   * @param  Connection
   * @return boolean 顧客コードに対するアドレス帳があるかどうか
   * @throws なし
   */
  public boolean reset(Connection lConnection){
    boolean lResult=false;
    StringBuffer lSqlBuf=new StringBuffer();

    lSqlBuf.append("SELECT * FROM CustAddressTbl WHERE CustCode=").append(SIDBUtil.SQL2Str(getCustCode()," "));
    lSqlBuf.append("AND AddressCode=").append(SIDBUtil.SQL2Str(getAddressCode()));
    log.debug("reset:lSqlBuf="+lSqlBuf.toString());

    Statement lStatement=null;
    ResultSet lResultSet=null;

    try {
       lStatement=lConnection.createStatement();
       lResultSet=lStatement.executeQuery(lSqlBuf.toString());

       if (lResultSet.next()){
         this.setEncode(SIConfig.SIENCODE_NONE);
         this.setCustCompanyFlg(lResultSet.getString("custCompanyFlg"));//法人個人区分
         this.setDeliveryName(lResultSet.getString("deliveryName"));//配送先呼称
         this.setDeliveryAddressee(lResultSet.getString("deliveryAddressee"));//配送先正式名
         this.setEmail(lResultSet.getString("email"));//EMAIL
         this.setPostCode1(lResultSet.getString("postCode1"));//郵便番号1
         this.setPostCode2(lResultSet.getString("postCode2"));//郵便番号2
         this.setAddress1(lResultSet.getString("address1"));//住所1
         this.setAddress2(lResultSet.getString("address2"));//住所2
         this.setAddress3(lResultSet.getString("address3"));//住所3
         this.setCompanyName(lResultSet.getString("companyName"));//サロン名
         this.setCorporationName(lResultSet.getString("corporationName"));//会社名
         this.setTel(lResultSet.getString("tel"));//連絡先電話番号
         this.setFax(lResultSet.getString("fax"));//連絡先FAX番号
         lResult=true;
       }else {
         log.error("not find record for custCode="+getCustCode()+",addressCode="+getAddressCode());
       }
     }catch(SQLException sqle){
       sqle.printStackTrace();
     }finally{
       SIDBUtil.close(lStatement,lResultSet);
     }
     return lResult;
  }

  /**
   * <b>getNextAddressCode</b>
   * アドレス帳内にあるアドレスを全て取得する
   * @param  Connection
   * @return String 最後尾アドレス帳番号
   * @throws なし
   */
  public String getNextAddressCode(Connection lConnection)throws SIDBAccessException{
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT MAX(AddressCode::numeric)+1 FROM CustAddressTbl ");
    lSqlBuf.append("WHERE CustCode=").append(SIDBUtil.SQL2Str(getCustCode()));
    
    try {
      String code = SIDBUtil.getFirstData(lConnection, lSqlBuf.toString());
      if (Integer.parseInt(code) < 999) return code;
      log.warn("can not find the next address code for custCode="+getCustCode());
      throw new SIDBAccessException("Can not find the next address code!!!");
    } catch (Exception e) {
      e.printStackTrace();
      throw new SIDBAccessException(e);
    }
  }
  /**
   * <b>getCollection</b>
   * 顧客コードに対応する顧客が持つアドレス帳リストを取得
   * @param  Connection
   * @param  lCustCode  顧客コード
   * @return Collection アドレス帳リスト
   * @throws なし
   */
  public static Collection getCollection(Connection lConnection,String lCustCode){
    Statement lStatement=null;
    ResultSet lResultSet=null;

    Collection lResultColl=new ArrayList();
    SICustAddress lAddress=new SICustAddress();
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT * FROM CustAddressTbl WHERE CustCode="+SIDBUtil.SQL2Str(lCustCode));
    lSqlBuf.append(" ORDER BY addressCode ");//7.3.0 PI-NES0501 追加

    try {
      log.debug("getCollection:lSqlBuf="+lSqlBuf.toString());
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      while(lResultSet.next()){
        lAddress=new SICustAddress();
        lAddress.setEncode(SIConfig.SIENCODE_NONE);
        lAddress.setCustCode(lResultSet.getString("custCode"));//顧客コード
        lAddress.setAddressCode(lResultSet.getString("addressCode"));//アドレスコード
        lAddress.setCustCompanyFlg(lResultSet.getString("custCompanyFlg"));//法人個人区分
        lAddress.setDeliveryName(lResultSet.getString("deliveryName"));//配送先呼称
        lAddress.setDeliveryAddressee(lResultSet.getString("deliveryAddressee"));//配送先正式名
        lAddress.setEmail(lResultSet.getString("email"));//EMAIL
        lAddress.setPostCode1(lResultSet.getString("postCode1"));//郵便番号1
        lAddress.setPostCode2(lResultSet.getString("postCode2"));//郵便番号2
        lAddress.setAddress1(lResultSet.getString("address1"));//住所1
        lAddress.setAddress2(lResultSet.getString("address2"));//住所2
        lAddress.setAddress3(lResultSet.getString("address3"));//住所3
        lAddress.setCompanyName(lResultSet.getString("companyName"));//サロン名
        lAddress.setCorporationName(lResultSet.getString("corporationName"));//会社名
        lAddress.setLastDeliveryDate(SIDBUtil.getDate(lResultSet.getTimestamp("lastDeliveryDate")));//最後配送日付
        lAddress.setTel(lResultSet.getString("tel"));//連絡先電話番号
        lAddress.setFax(lResultSet.getString("fax"));//連絡先FAX番号
        lResultColl.add(lAddress);
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }

    return lResultColl;
  }
  /**
   * <b>getNameValueCollectoin</b>
   * 顧客コードに対応する顧客が持つアドレス帳リストを取得(iモード用）
   * @param  Connection
   * @param  lCustCode  顧客コード
   * @return Collection アドレス帳リスト
   * @throws なし
   */
  public static Collection getNameValueCollection(Connection lConnection,String lCustCode){
    if (SIUtil.isNull(lCustCode)) return new ArrayList();
    //i-mode側用
    StringBuffer lSqlBuf=new StringBuffer("SELECT deliveryAddressee||'('||deliveryName||')',addressCode FROM CustAddressTbl ");
    lSqlBuf.append("WHERE CustCode =").append(SIDBUtil.SQL2Str(lCustCode)).append(" ORDER BY AddressCode");

    Collection lResultColl=new ArrayList();

    try {
      lResultColl=SIDBUtil.getCollection(lConnection,lSqlBuf.toString(),true);
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }

    return lResultColl;
  }

}
