/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */

package jp.co.sint.beans.mallmgr;

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

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIBasic;
import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SIStndrdContent;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @version $Id : UIStndrdContentListCond.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>
 * Jinwang Chen   2003/09/08  Original
 */

public class UIStndrdContentListCond extends SIBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //ショップコード
  private String shopCodeTxt="";
  
  //規格コード
  private String stndrdCodeTxt="";
  
  //規格名称
  private String stndrdNameTxt="";
  
  //SQL検索のばあいに、条件文
  private String conditionSQL="";
  
  //並び順の文
  private String orderBySQL=" ORDER BY aa.DispOrder,aa.ElementCode ";
  
  /**
   * UIStndrdContentListCond
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public UIStndrdContentListCond(){}
  
  /**
   * UIStndrdContentListCond
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public UIStndrdContentListCond(HttpServletRequest lRequest){
    //init(lRequest);
  }
  
  //setter of ショップコード
  public void setShopCodeTxt(String lShopCodeTxt){
    if (SIUtil.isNull(lShopCodeTxt)) lShopCodeTxt="";
    this.shopCodeTxt=SIUtil.changeTo(lShopCodeTxt.trim(),this.encode);
  }
  
  //setter of 規格コード
  public void setStndrdCodeTxt(String lStndrdCodeTxt){
    if (SIUtil.isNull(lStndrdCodeTxt)) lStndrdCodeTxt="";
    this.stndrdCodeTxt=SIUtil.changeTo(lStndrdCodeTxt.trim(),this.encode);
  }
  
  //setter of 規格名称
  public void setStndrdNameTxt(String lStndrdNameTxt){
    if (SIUtil.isNull(lStndrdNameTxt)) lStndrdNameTxt="";
    this.stndrdNameTxt=lStndrdNameTxt.trim();
  }
  
  //setter of 条件文
  public void setConditionSQL(String lConditionSQL){
    if (lConditionSQL==null)lConditionSQL="";
    this.conditionSQL=lConditionSQL;
  }
  
  //setter of 並び順文
  public void setOrderBySQL(String lOrderBySQL){
    if (lOrderBySQL==null)lOrderBySQL="";
    this.orderBySQL=lOrderBySQL;
  }
  
  //getter of ショップコード
  public String getShopCodeTxt(){
    return this.shopCodeTxt;
  }
  
  //getter of 規格コード
  public String getStndrdCodeTxt(){
    return this.stndrdCodeTxt;
  }
  
  //getter of 規格名称
  public String getStndrdNameTxt(){
    return this.stndrdNameTxt;
  }
  
  //getter of 条件文
  public String getCondtionSQL(){
    return this.conditionSQL;
  }
  
  //getter of 並び順文
  public String getOrderBySQL(){
    return this.orderBySQL;
  }
  
  /**
   * <b>init</b>
   * 規格名称管理画面からキーデータを取って、設定します。
   * @param request リクエスト
   * @param lConnection DBへのコネクション
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public void init(HttpServletRequest lRequest,Connection lConnection,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    
    //7.1.1 ST0236 修正 ここから
    super.init(lRequest,lUrlParam);
    if (lLogin.isShop()){
      this.setShopCodeTxt(lLogin.getMallShopCode());//ショップコード
    }else{
      this.setShopCodeTxt((String)lUrlParam.getParam("regShopCodeTxt"));//ショップコード
    }
    this.setStndrdCodeTxt((String)lUrlParam.getParam("regStndrdCodeTxt"));//規格コード
    this.setPageNumberSel((String)lUrlParam.getParam("pageNumberSel"));
    //7.1.1 ST0236 修正 ここまで
    
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT StndrdName FROM StndrdNameMTbl ");
    lSqlBuf.append("WHERE ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCodeTxt()," "));
    lSqlBuf.append("AND   StndrdCode=").append(SIDBUtil.SQL2Str(this.getStndrdCodeTxt()));
    try {
      this.setStndrdNameTxt(SIDBUtil.getFirstData(lConnection,lSqlBuf.toString()));
    } catch (SIDBAccessException e) {
      e.printStackTrace();
    }
  }
  
  /**
   * <b>getCollection</b>
   * 条件に合ったレコードを検索して、結果のコネクションを作成して、戻します。
   * @param lConnection データベースへの接続コネクション
   * @return レコードのセット
   * @throws なし
   */
  public Collection getCollection(Connection lConnection) throws SIDBAccessException{
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    SIStndrdContent lStndrdContent=new SIStndrdContent();
    StringBuffer lSqlBuf=new StringBuffer();
    Collection lStndrdContentColl=new ArrayList();
    
    //基本のSQL
    lSqlBuf.append("SELECT aa.shopcode,aa.stndrdcode,aa.elementcode,aa.disporder,aa.elementname,SUM(aa.count) AS count ");
    lSqlBuf.append("FROM (SELECT a.shopcode,a.stndrdcode,a.elementcode,a.disporder,a.elementname,");
    lSqlBuf.append("(CASE WHEN b.cmdtystndrdcode IS NULL THEN 0 ELSE 1 END) AS count ");
    lSqlBuf.append("FROM StndrdContentMTbl a ");
    lSqlBuf.append("LEFT OUTER JOIN  CmdtyStndrdMtbl b ");
    lSqlBuf.append("ON a.ShopCode=b.ShopCode ");
    lSqlBuf.append("AND ((a.StndrdCode=b.StndrdCode1 AND a.ElementCode=b.ElementCode1) ");
    lSqlBuf.append("OR (a.StndrdCode=b.StndrdCode2 AND a.ElementCode=b.ElementCode2)) ");
    lSqlBuf.append("WHERE a.ShopCode="+SIDBUtil.SQL2Str(this.getShopCodeTxt()," "));
    lSqlBuf.append("AND a.StndrdCode="+SIDBUtil.SQL2Str(this.getStndrdCodeTxt(),") aa "));
    lSqlBuf.append("GROUP BY aa.shopcode,aa.stndrdcode,aa.elementcode,aa.disporder,aa.elementname ");
    //出力順
    lSqlBuf.append(this.getOrderBySQL());
    log.debug("sqlStatement="+lSqlBuf.toString());
    //実行
    try{
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      //商品レコードのセットの作成
      while (lResultSet.next()){
        lStndrdContent=new SIStndrdContent();
        lStndrdContent.setEncode(SIConfig.SIENCODE_NONE);
        lStndrdContent.setShopCode(lResultSet.getString("ShopCode"));
        lStndrdContent.setStndrdCode(lResultSet.getString("StndrdCode"));
        lStndrdContent.setElementCode(lResultSet.getString("ElementCode"));
        lStndrdContent.setElementName(lResultSet.getString("ElementName"));
        lStndrdContent.setDispOrder(lResultSet.getString("DispOrder"));
        lStndrdContent.setCount(lResultSet.getString("Count"));
        lStndrdContentColl.add(lStndrdContent);
      }
    }catch(Exception ex){
      ex.printStackTrace();
      throw new SIDBAccessException(ex);
    }finally{
      SIDBUtil.close(lResultSet,lStatement);
    }
    return lStndrdContentColl;
  }
}