/**
 * Created on 2003/11/12
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
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.SIDeliveryType;
import jp.co.sint.basic.SILogin;
import jp.co.sint.config.SIConfig;
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;

/**
 * @author arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class UIDeliveryNameListCond extends SIBasic {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //ショップコード
  private String mallShopCode="";
  
  //ショップコード
  public void setMallShopCode(String string) {
    if (SIUtil.isNull(string)) string="";
    this.mallShopCode=SIUtil.changeTo(string.trim(),this.encode);
  }
  
  //ショップコード
  public String getMallShopCode(){
    return this.mallShopCode;
  }
  
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest,lUrlParam);//7.1.1 ST0236 修正
    if (lLogin.isShop()){
      this.setMallShopCode(lLogin.getMallShopCode());
    }else{
      this.setMallShopCode((String)lUrlParam.getParam("mallShopCode"));//7.1.1 ST0236 修正
    }
  }
  
  public Collection getCollection(Connection lConnection) throws SIDBAccessException {
    Statement statement = null;
    ResultSet resultSet = null;
    Collection deliColl = new ArrayList();
    StringBuffer strBuf = new StringBuffer();
    SIDeliveryType deliType = new SIDeliveryType();
    
    strBuf = new StringBuffer();
    
    strBuf.append(" SELECT a.*, cmdtyCount  FROM deliveryTypeMtbl a");
    strBuf.append(" LEFT OUTER JOIN");
    strBuf.append(" (SELECT deliveryTypeCode,ShopCode,COUNT(deliveryTypeCode) as cmdtyCount ");
    strBuf.append("  FROM cmdtyMtbl GROUP BY deliveryTypeCode,ShopCode) b");
    strBuf.append(" ON (a.deliveryTypeCode = b.deliveryTypeCode and a.mallshopcode=b.ShopCode)");
    strBuf.append(" WHERE a.mallshopcode=").append(SIDBUtil.SQL2Str(this.getMallShopCode()));
    strBuf.append(" ORDER BY a.mallShopCode ASC, a.deliveryTypeCode ASC ");
    log.debug("strBuf:"+strBuf);
    //検索の条件
    //出力順
    //実行
    try {
      statement = lConnection.createStatement();
      resultSet = statement.executeQuery(strBuf.toString());
      
      while (resultSet.next()) {
        deliType = new SIDeliveryType();
        deliType.setEncode(SIConfig.SIENCODE_NONE);
        
        deliType.setMallShopCode(resultSet.getString("mallShopCode"));
        deliType.setDeliveryTypeCode(resultSet.getString("deliveryTypeCode"));
        deliType.setDeliveryTypeName(resultSet.getString("deliveryTypeName"));
        deliType.setDeliveryTimeFlg(resultSet.getString("deliveryTimeFlg"));
        deliType.setCmdtyCount(resultSet.getString("cmdtyCount"));
        
        deliColl.add(deliType);
      }
      
    } catch (Exception ex) {
      log.debug("exception sql = " + strBuf.toString());
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(resultSet, statement);
    }
    
    return deliColl;
  }
}