/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
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.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

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.database.SITableCondition;
import jp.co.sint.database.SITableConditionManager;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckValid;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIFlagUtil;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;


/**
 * @version $Id: SICmdtyStndrd.java,v 1.0 2003/09/11 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>
 * jwchen        2003/09/11 17:11:08  Original
 */
public class SICmdtyStndrd extends SIBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //商品規格コード
  private String cmdtyStndrdCode="";
  
  //ショップコード
  private String shopCode="";
  
  //商品コード
  private String cmdtyCode="";
  
  //規格コード1
  private String stndrdCode1="";
  
  //規格コード名1
  private String stndrdName1="";
  
  //エレメント1
  private String elementCode1="";
  
  //エレメント名1
  private String elementName1="";
  
  //規格コード2
  private String stndrdCode2="";
  
  //規格コード名2
  private String stndrdName2="";
  
  //エレメント名2
  private String elementName2="";
  
  //エレメント2
  private String elementCode2="";
  
  //在庫数量
  private String amount="0";
  
  //商品数量管理フラグ
  private String amountFlg="1";
  
  //在庫コメント
  private String commentOfStock="";
  
  //在庫状況コード
  private String stockStatusCode="";
  
  private Collection elementColl1=new ArrayList();
  private Collection elementColl2=new ArrayList();
  
  //在庫管理フラグ
  private String inStockFlg="0";
  
  public SICmdtyStndrd(){}
  
  public SICmdtyStndrd(String lCmdtyStndrdCode){
    this.setCmdtyCode(lCmdtyStndrdCode);
  }
  
  public SICmdtyStndrd(String lShopCode,String lCmdtyCode){
    this.setShopCode(lShopCode);
    this.setCmdtyCode( lCmdtyCode);
  }
  
  public SICmdtyStndrd(String lShopCode,String lCmdtyCode,String lStndrdCode1,String lElementCode1,String lStndrdCode2,String lElementCode2){
    this.setShopCode(lShopCode);
    this.setCmdtyCode(lCmdtyCode);
    this.setStndrdCode1(lStndrdCode1);
    this.setElementCode1(lElementCode1);
    this.setStndrdCode2(lStndrdCode2);
    this.setElementCode2(lElementCode2);
  }
  
  //setter of 商品規格コード
  public void setCmdtyStndrdCode(String lCmdtyStndrdCode){
    if (SIUtil.isNull(lCmdtyStndrdCode)) lCmdtyStndrdCode="";
    this.cmdtyStndrdCode=SIUtil.changeTo(lCmdtyStndrdCode.trim(),this.encode);
  }
  
  //setter of ショップコード
  public void setShopCode(String lShopCode){
    if (SIUtil.isNull(lShopCode)) lShopCode="";
    this.shopCode=SIUtil.changeTo(lShopCode.trim(),this.encode);
  }
  
  //setter of 商品コード
  public void setCmdtyCode(String lCmdtyCode){
    if (SIUtil.isNull(lCmdtyCode)) lCmdtyCode="";
    this.cmdtyCode=SIUtil.changeTo(lCmdtyCode.trim(),this.encode);
  }
  
  //setter of 規格コード1
  public void setStndrdCode1(String lStndrdCode1){
    if (SIUtil.isNull(lStndrdCode1)) lStndrdCode1="";
    this.stndrdCode1=SIUtil.changeTo(lStndrdCode1.trim(),this.encode);
  }
  
  //setter of 規格名1
  public void setStndrdName1(String lStndrdName1){
    if (SIUtil.isNull(lStndrdName1)) lStndrdName1="";
    this.stndrdName1=SIUtil.changeTo(lStndrdName1.trim(),this.encode);
  }
  
  //setter of エレメント1
  public void setElementCode1(String lElementCode1){
    if (SIUtil.isNull(lElementCode1)) lElementCode1="";
    this.elementCode1=SIUtil.changeTo(lElementCode1.trim(),this.encode);
  }
  
  public void setElementColl1(Collection lCollection){
    if (lCollection!=null && !lCollection.isEmpty()) {
      lCollection=SIUtil.insertColl(lCollection,SIFlagUtil.getBlank());
    }
    this.elementColl1=lCollection;
  }
  
  public void setElementColl2(Collection lCollection){
    if (lCollection!=null && !lCollection.isEmpty()) {
      lCollection=SIUtil.insertColl(lCollection,SIFlagUtil.getBlank());
    }
    this.elementColl2=lCollection;
  }
  
  //setter of エレメント名1
  public void setElementName1(String lElementName1){
    if (SIUtil.isNull(lElementName1)) lElementName1="";
    this.elementName1=SIUtil.changeTo(lElementName1.trim(),this.encode);
  }
  
  //setter of 規格コード2
  public void setStndrdCode2(String lStndrdCode2){
    if (SIUtil.isNull(lStndrdCode2)) lStndrdCode2="";
    this.stndrdCode2=SIUtil.changeTo(lStndrdCode2.trim(),this.encode);
  }
  
  //setter of 規格名2
  public void setStndrdName2(String lStndrdName2){
    if (SIUtil.isNull(lStndrdName2)) lStndrdName2="";
    this.stndrdName2=SIUtil.changeTo(lStndrdName2.trim(),this.encode);
  }
  
  //setter of エレメント2
  public void setElementCode2(String lElementCode2){
    if (SIUtil.isNull(lElementCode2)) lElementCode2="";
    this.elementCode2=SIUtil.changeTo(lElementCode2.trim(),this.encode);
  }
  
  //setter of エレメント名2
  public void setElementName2(String lElementName2){
    if (SIUtil.isNull(lElementName2)) lElementName2="";
    this.elementName2=SIUtil.changeTo(lElementName2.trim(),this.encode);
  }
  
  //setter of 在庫数量
  public void setAmount(String lAmount){
    if (SIUtil.isNull(lAmount)) lAmount="0";
    this.amount=SIUtil.changeTo(lAmount.trim(),this.encode);
  }
  
  //setter of 数量管理設定フラグ
  public void setAmountFlg(String lAmountFlg){
    if (SIUtil.isNull(lAmountFlg)) lAmountFlg="1";
    this.amountFlg=SIUtil.changeTo(lAmountFlg.trim(),this.encode);
  }
  
  //setter of 在庫状況コード
  public void setStockStatusCode(String lStockStatusCode){
    if (SIUtil.isNull(lStockStatusCode)) lStockStatusCode="";
    this.stockStatusCode=SIUtil.changeTo(lStockStatusCode.trim(),this.encode);
  }
  
  //種類別に画像規定ファイル名取得
  public String getImageFileName(int lIndex){
    return getImageFileName(getShopCode(),getCmdtyCode(),lIndex);
  }
  
  //表示用画像規定ファイル名取得（shopcode/画像名のshopcode/なし）
  public String getImageFileNameOnly(int lIndex){
    return getImageFileNameOnly(getShopCode(),getCmdtyCode(),lIndex);
  }
  
  /**
   * <b>getImageFileName</b>
   * 画像規定ファイル名取得
   * @param shopCode   ショップコード
   * @param cmdtyCode  商品コード
   * @param lIndex     インデックス
   * @return String    画像規定ファイル名
   */
  public String getImageFileName(String shopCode, String cmdtyCode,int lIndex){
    if (SIUtil.isNull(shopCode)) return "";
    
    StringBuffer lImageFileNameBuf=new StringBuffer();
    lImageFileNameBuf.append(getShopCode()).append("/");
    lImageFileNameBuf.append(cmdtyCode);
    if(SIUtil.isNotNull(getStndrdCode1())&&SIUtil.isNotNull(getElementCode1())){
      lImageFileNameBuf.append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getStndrdCode1()).append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getElementCode1());
    }
    if(SIUtil.isNotNull(getStndrdCode2())&&SIUtil.isNotNull(getElementCode2())){
      lImageFileNameBuf.append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getStndrdCode2()).append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getElementCode2());
    }
    if (lIndex==SIConfig.SISUFFIX_IMAGE_TYPE_PC){
      lImageFileNameBuf.append(SIConfig.SISUFFIX_CMDTY_STNDRD_IMAGE_NAME[lIndex]).append(".jpg");
    }else{
      lImageFileNameBuf.append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(SIConfig.SISUFFIX_CMDTY_STNDRD_IMAGE_NAME[lIndex]).append(".jpg");
    }
    
    return lImageFileNameBuf.toString();
  }
  
  /**
   * <b>getImageFileName</b>
   * 表示用画像ファイル名取得
   * @param shopCode   ショップコード
   * @param cmdtyCode  商品コード
   * @param lIndex     インデックス
   * @return String    画像規定ファイル名
   */
  public String getImageFileNameOnly(String shopCode, String cmdtyCode,int lIndex){
    if (SIUtil.isNull(shopCode)) return "";
    
    StringBuffer lImageFileNameOnlyBuf=new StringBuffer();
    lImageFileNameOnlyBuf.append(cmdtyCode);
    if(SIUtil.isNotNull(getStndrdCode1())&&SIUtil.isNotNull(getElementCode1())){
      lImageFileNameOnlyBuf.append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getStndrdCode1()).append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getElementCode1());
    }
    if(SIUtil.isNotNull(getStndrdCode2())&&SIUtil.isNotNull(getElementCode2())){
      lImageFileNameOnlyBuf.append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getStndrdCode2()).append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(getElementCode2());
    }
    if (lIndex==SIConfig.SISUFFIX_IMAGE_TYPE_PC){
      lImageFileNameOnlyBuf.append(SIConfig.SISUFFIX_CMDTY_STNDRD_IMAGE_NAME[lIndex]).append(".jpg");
    }else{
      lImageFileNameOnlyBuf.append(SIConfig.SIWEBSHOPPING_IMAGE_TOKEN).append(SIConfig.SISUFFIX_CMDTY_STNDRD_IMAGE_NAME[lIndex]).append(".jpg");
    }
    return lImageFileNameOnlyBuf.toString();
  }
  
  public void setInStockFlg(String lInStockFlg){
    if (SIUtil.isNull(lInStockFlg))lInStockFlg="0";
    this.inStockFlg =lInStockFlg;
  }
  
  //在庫コメント
  public void setCommentOfStock(String lCommentOfStock){
    if (SIUtil.isNull(lCommentOfStock)) lCommentOfStock="";
    this.commentOfStock=SIUtil.changeTo(lCommentOfStock.trim(),this.encode);
  }
  
  //getter of 商品規格コード
  public String getCmdtyStndrdCode(){
    return this.cmdtyStndrdCode;
  }
  
  //getter of ショップコード
  public String getShopCode(){
    return this.shopCode;
  }
  
  //getter of 商品コード
  public String getCmdtyCode(){
    return this.cmdtyCode;
  }
  
  //getter of 規格コード1
  public String getStndrdCode1(){
    return this.stndrdCode1;
  }
  
  //getter of 規格名1
  public String getStndrdName1(){
    return this.stndrdName1;
  }
  
  //getter of エレメント1
  public String getElementCode1(){
    return this.elementCode1;
  }
  
  //getter of エレメント名１
  public String getElementName1(){
    return this.elementName1;
  }
  
  //getter of 規格コード2
  public String getStndrdCode2(){
    return this.stndrdCode2;
  }
  
  //getter of 規格名2
  public String getStndrdName2(){
    return this.stndrdName2;
  }
  
  //getter of エレメント名2
  public String getElementName2(){
    return this.elementName2;
  }
  
  //getter of エレメント2
  public String getElementCode2(){
    return this.elementCode2;
  }
  
  public Collection getElementColl1(){
    return this.elementColl1;
  }
  
  public boolean hasStndrd1(){
    return (this.elementColl1!=null&&!this.elementColl1.isEmpty());
  }
  
  public Collection getElementColl2(){
    return this.elementColl2;
  }
  
  public boolean hasStndrd2(){
    return (this.elementColl2 !=null&&!this.elementColl2.isEmpty());
  }
  
  public boolean hasStndrd(){
    return (this.hasStndrd1()||this.hasStndrd2());
  }
  
  //getter of 在庫数量
  public String getAmount(){
    return this.amount;
  }
  
  //getter of 商品数量管理フラグ
  public String getAmountFlg(){
    return this.amountFlg;
  }
  
  //getter of 在庫数量
  public String getStockStatusCode(){
    return this.stockStatusCode;
  }
  
  public String getCommentOfStock(){
    return this.commentOfStock;
  }
  
  public String getInStockFlg(){
    return this.inStockFlg;
  }
  
  /**
   * <b>getFrontInStock</b>
   * 在庫管理フラグに応じてFrontで表示する在庫量を取得する
   * @param  なし
   * @return String 在庫量or在庫コメント
   * @throws なし
   */
  public String getFrontInStock(){
    //コメント選択時のみ表示
    if(SIUtil.isNotNull(this.getStockStatusCode())){
      if (this.getAmountFlg().equals("0")) return "";
      else if(this.getInStockFlg().equals("0") && SIUtil.isNotNull(this.getCommentOfStock())){
        return "在庫： "+this.getCommentOfStock();
      }else if(this.getInStockFlg().equals("1")){
        return "在庫数： "+this.getAmount();
      }
    }
    return "";
  }
  
  public void reset(Connection lConnection)throws SIDBAccessException{
    reset(lConnection,getShopCode(),getCmdtyCode(),false);
  }
  
  public void reset(Connection lConnection,boolean lElementEnable)throws SIDBAccessException{
    reset(lConnection,getShopCode(),getCmdtyCode(),lElementEnable);
  }
  
  /**
   * <b>reset</b>
   * 規格コードと、エレメントコードからエレメント名を取得します。
   * @param lConnection DBへのコネクション
   * @param idx 規格番号
   * @param lStndrdCode 規格コード
   * @param lElement エレメントコード
   */
  public void reset(Connection lConnection,String lShopCode,String lCmdtyCode,boolean lElementEnable)throws SIDBAccessException{
    HashMap lResMap=new HashMap();
    lResMap=SIDBUtil.getOneRowData(lConnection,SICmdtyStndrdUtil.makeResetSQL(getShopCode(),getCmdtyCode(),1));
    
    setEncode(SIConfig.SIENCODE_NONE);
    setStndrdCode1((String)lResMap.get("stndrdcode"));
    setStndrdName1((String)lResMap.get("stndrdname"));
    
    lResMap=SIDBUtil.getOneRowData(lConnection,SICmdtyStndrdUtil.makeResetSQL(getShopCode(),getCmdtyCode(),2));
    setStndrdCode2((String)lResMap.get("stndrdcode"));
    setStndrdName2((String)lResMap.get("stndrdname"));
    
    if (lElementEnable){
      this.setElementColl1(SIDBUtil.getCollection(lConnection,SICmdtyStndrdUtil.makeElementSQL(getShopCode(),getCmdtyCode(),1)));
      this.setElementColl2(SIDBUtil.getCollection(lConnection,SICmdtyStndrdUtil.makeElementSQL(getShopCode(),getCmdtyCode(),2,getElementCode1())));
    }
  }
  
  /**
   * <b>setElementName</b>
   * 規格コードと、エレメントコードからエレメント名を取得します。
   * @param lConnection DBへのコネクション
   * @param idx 規格番号
   * @param lStndrdCode 規格コード
   * @param lElement エレメントコード
   */
  public void setElementName(Connection lConnection, String idx, String lStndrdCode, String lElementCode){
    Statement lStatement=null;
    ResultSet lResultSet=null;
    SITableConditionManager manCondition=new SITableConditionManager();
    SITableCondition tableCondition=new SITableCondition();
    
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT elementName FROM StndrdContentMTbl " );
    lSqlBuf.append(" WHERE 1=1");
    if(!shopCode.equals("")) lSqlBuf.append("and ShopCode="+SIDBUtil.SQL2Str(this.getShopCode()," "));
    if(!lStndrdCode.equals("")) lSqlBuf.append("and StndrdCode="+SIDBUtil.SQL2Str(lStndrdCode," "));
    if(!lElementCode.equals("")) lSqlBuf.append("and ElementCode="+SIDBUtil.SQL2Str(lElementCode," "));
    
    lSqlBuf.append(manCondition.getCondtionSQL());
    log.debug("lSqlBuf="+lSqlBuf);
    
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      if (lResultSet.next()){
        this.setEncode(SIConfig.SIENCODE_NONE);
        if(idx=="1") this.setElementName1(lResultSet.getString("elementName"));
        if(idx=="2") this.setElementName2(lResultSet.getString("elementName"));
      }else {
        log.error("not find record for cmdtyCode="+getCmdtyCode());
      }
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
  }
  
  /**
   * <b>check</b>
   * 商品に規格があるか判定し、規格あり商品の数量等を取得します
   * @param lConnection
   * @param lElementCode1 規格コード１
   * @param lElementCode2 規格コード２
   * @return boolean 該当規格商品がある場合
   * @throws SIDBAccessException
   */
  public boolean check(Connection lConnection,String lElementCode1,String lElementCode2)throws SIDBAccessException{
    boolean lResult=false;
    Statement lStatement=null;
    ResultSet lResultSet=null;
    
    SITableConditionManager manCondition=new SITableConditionManager();
    SITableCondition tableCondition=new SITableCondition();
    
    StringBuffer lSqlBuf=new StringBuffer("SELECT aa.shopCode,aa.cmdtyCode,aa.stndrdCode1,aa.elementCode1,");
    lSqlBuf.append("aa.stndrdCode2,aa.elementCode2,aa.amount,cc.StockStatusCode,cc.AmountFlg, ");
    lSqlBuf.append("CASE WHEN (aa.Amount<=0) THEN bb.CommentOfNoStock ");
    lSqlBuf.append("WHEN aa.Amount<=bb.AmountOfLessStock THEN bb.CommentOfLessStock ");
    lSqlBuf.append("ELSE CommentOfMoreStock END AS CommentOfStock ");
    lSqlBuf.append(" ,(SELECT InStockFlg FROM MallVW ) AS InStockFlg ");
    lSqlBuf.append("FROM CmdtyStndrdMTbl").append(SIDBMultiConf.SIALIAS_CURR_NAME).append("aa,");
    lSqlBuf.append("CmdtyMTbl").append(SIDBMultiConf.SIALIAS_CURR_NAME).append("cc ");
    lSqlBuf.append("LEFT OUTER JOIN StockStatusMTbl").append(SIDBMultiConf.SIALIAS_CURR_NAME).append("bb ON bb.StockStatusCode=cc.StockStatusCode ");
    lSqlBuf.append("WHERE aa.ShopCode=cc.ShopCode AND aa.CmdtyCode=cc.CmdtyCode ");
    
    tableCondition=new SITableCondition("aa","ShopCode",getShopCode(),SIConfig.SICONDITION_TYPE_EQUAL,SIConfig.SICONDITION_TYPE_AND);
    tableCondition.setBlankEnable(true);
    manCondition.add(tableCondition);
    
    tableCondition=new SITableCondition("aa","cmdtyCode",getCmdtyCode(),SIConfig.SICONDITION_TYPE_EQUAL,SIConfig.SICONDITION_TYPE_AND);
    tableCondition.setBlankEnable(true);
    manCondition.add(tableCondition);
    
    tableCondition=new SITableCondition("aa","ElementCode1",lElementCode1,SIConfig.SICONDITION_TYPE_EQUAL,SIConfig.SICONDITION_TYPE_AND);
    tableCondition.setBlankEnable(true);
    manCondition.add(tableCondition);
    
    tableCondition=new SITableCondition("aa","ElementCode2",lElementCode2,SIConfig.SICONDITION_TYPE_EQUAL,SIConfig.SICONDITION_TYPE_AND);
    tableCondition.setBlankEnable(true);
    manCondition.add(tableCondition);
    
    lSqlBuf.append(manCondition.getCondtionSQL());
    
    log.debug("lSqlBuf="+lSqlBuf);
    
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      
      if (lResultSet.next()){
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setShopCode(lResultSet.getString("shopCode"));//ショップコード
        this.setCmdtyCode(lResultSet.getString("cmdtyCode"));//商品コード
        this.setStndrdCode1(lResultSet.getString("stndrdCode1"));//規格コード1
        this.setElementCode1(lResultSet.getString("elementCode1"));//エレメントコード1
        this.setStndrdCode2(lResultSet.getString("stndrdCode2"));//規格コード2
        this.setElementCode2(lResultSet.getString("elementCode2"));//エレメントコード2
        this.setAmount(lResultSet.getString("amount"));//数量
        this.setAmountFlg(lResultSet.getString("amountFlg"));//商品数量管理
        this.setCommentOfStock(lResultSet.getString("CommentOfStock"));//在庫コメント
        this.setInStockFlg(lResultSet.getString("InStockFlg"));//在庫管理フラグ
        this.setStockStatusCode(lResultSet.getString("StockStatusCode"));//在庫状況コード
        lResult=true;
      }else {
        log.error("not find record for cmdtyCode="+getCmdtyCode());
      }
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    return lResult;
  }
  
  /**
   * <b>validate</b>
   * 商品規格のデータをチェックします。
   * @param lRequest リクエスト
   * @param lConnection DBへのコネクション
   * @return チェックの結果
   * @throws SIDuplicateKeyException
   */
  public boolean validate(HttpServletRequest lRequest,Connection lConnection){
    return validate(lRequest,lConnection,false);
  }
  
  public boolean validate(HttpServletRequest lRequest,Connection lConnection,boolean lCsvCheck){
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors=new SICustomErrors();
    StringBuffer lSqlBuf=new StringBuffer("");
    
    SICheckValid.checkValid(errors,"ショップコード",this.getShopCode(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE+SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors,"ショップコード",this.getShopCode(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,16);
    
    SICheckValid.checkValid(errors,"商品コード",this.getCmdtyCode(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE+SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors,"商品コード",this.getCmdtyCode(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,16);
    
    //商品存在のチェック
    if (SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getCmdtyCode())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM CmdtyMTbl ");
      lSqlBuf.append("WHERE ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND CmdtyCode=").append(SIDBUtil.SQL2Str(this.getCmdtyCode()));
      SICheckValid.checkExist(errors,lConnection,"商品コード",lSqlBuf.toString());
    }
    
    if (lCsvCheck){
      SICheckValid.checkValid(errors,"規格分類コード1",this.getStndrdCode1(),SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
      SICheckValid.checkValid(errors,"規格分類コード1",this.getStndrdCode1(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,4);
    }else{
      SICheckValid.checkValid(errors,"規格分類コード1",this.getStndrdCode1(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE+SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
      SICheckValid.checkValid(errors,"規格分類コード1",this.getStndrdCode1(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,4);
    }
    
    //規格存在のチェック
    if (lCsvCheck&&SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getStndrdCode1())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM StndrdNameMTbl ");
      lSqlBuf.append("WHERE ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND StndrdCode=").append(SIDBUtil.SQL2Str(this.getStndrdCode1()));
      SICheckValid.checkExist(errors,lConnection,"規格分類コード1",lSqlBuf.toString());
    }
    
    //既存の規格に合うかどうか
    if (lCsvCheck&&getEditModeTxt().equals(SIConfig.SIEDIT_MODE_INSERT)&&SIUtil.isNotNull(getShopCode())
       &&SIUtil.isNotNull(getCmdtyCode())){//新規
      lSqlBuf=new StringBuffer("SELECT Distinct StndrdCode1 AS stndrdcode  FROM CmdtyStndrdMTbl WHERE ShopCode=");
      lSqlBuf.append(SIDBUtil.SQL2Str(getShopCode())).append(" AND CmdtyCode=").append(SIDBUtil.SQL2Str(getCmdtyCode()));
      HashMap lResMap=new HashMap();
      String lStndrdCode1="";
      try{
        lResMap=SIDBUtil.getOneRowData(lConnection,lSqlBuf.toString());
        lStndrdCode1=(String)lResMap.get("stndrdcode");
        if (SIUtil.isNotNull(lStndrdCode1)&&!lStndrdCode1.equals(getStndrdCode1())){
          errors.addError(new SICustomError("input.data.match.1","既存規格分類1"));
        }else if (SIUtil.isNull(lStndrdCode1)&&lResMap.containsKey("stndrdcode")&&SIUtil.isNotNull(getStndrdCode1())){
          errors.addError(new SICustomError("input.data.match.1","既存規格分類1"));
        }
      }catch(Exception e){e.printStackTrace();}
    }
    
    if (lCsvCheck){
      SICheckValid.checkValid(errors,"規格コード1",this.getElementCode1(),SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
      SICheckValid.checkValid(errors,"規格コード1",this.getElementCode1(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,4);
    }else{
      SICheckValid.checkValid(errors,"規格コード1",this.getElementCode1(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE+SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
      SICheckValid.checkValid(errors,"規格コード1",this.getElementCode1(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,4);
    }
    
    //エレメントコード１存在のチェック
    if (lCsvCheck&&SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getStndrdCode1())&&SIUtil.isNotNull(getElementCode1())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM StndrdContentMTbl ");
      lSqlBuf.append("WHERE ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND StndrdCode=").append(SIDBUtil.SQL2Str(this.getStndrdCode1()," "));
      lSqlBuf.append("AND ElementCode=").append(SIDBUtil.SQL2Str(this.getElementCode1()));
      SICheckValid.checkExist(errors,lConnection,"規格コード1",lSqlBuf.toString());
    }
    
    SICheckValid.checkValid(errors,"規格分類コード2",this.getStndrdCode2(),SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors,"規格分類コード2",this.getStndrdCode2(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,4);
    
    //規格コード２の存在のチェック
    if (lCsvCheck&&SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getStndrdCode2())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM StndrdNameMTbl ");
      lSqlBuf.append("WHERE ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND StndrdCode=").append(SIDBUtil.SQL2Str(this.getStndrdCode2()));
      SICheckValid.checkExist(errors,lConnection,"規格分類コード2",lSqlBuf.toString());
    }
    
    //既存の規格に合うかどうか
    if (lCsvCheck&&getEditModeTxt().equals(SIConfig.SIEDIT_MODE_INSERT)&&SIUtil.isNotNull(getShopCode())
       &&SIUtil.isNotNull(getCmdtyCode())){//新規
      lSqlBuf=new StringBuffer("SELECT Distinct StndrdCode2 AS stndrdcode FROM CmdtyStndrdMTbl WHERE ShopCode=");
      lSqlBuf.append(SIDBUtil.SQL2Str(getShopCode())).append(" AND CmdtyCode=").append(SIDBUtil.SQL2Str(getCmdtyCode()));
      HashMap lResMap=new HashMap();
      String lStndrdCode1="";
      try{
        lResMap=SIDBUtil.getOneRowData(lConnection,lSqlBuf.toString());
        lStndrdCode1=(String)lResMap.get("stndrdcode");
        if (SIUtil.isNotNull(lStndrdCode1)&&!lStndrdCode1.equals(getStndrdCode2())){
          errors.addError(new SICustomError("input.data.match.1","既存規格分類2"));
        }else if (SIUtil.isNull(lStndrdCode1)&&lResMap.containsKey("stndrdcode")&&SIUtil.isNotNull(getStndrdCode2())){
          errors.addError(new SICustomError("input.data.match.1","既存規格分類2"));
        }
      }catch(Exception e){e.printStackTrace();}
    }
    
    SICheckValid.checkValid(errors,"規格コード2",this.getElementCode2(),SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors,"規格コード2",this.getElementCode2(),SICheckDataConf.SICHECK_DATA_WORD_LEN_WITHIN_TYPE,4);
    
    if (SIUtil.isNull(getStndrdCode2())&&SIUtil.isNotNull(getElementCode2())){
      errors.addError(new SICustomError("input.data.require","規格分類コード2"));
    }else if (SIUtil.isNotNull(getStndrdCode2())&&SIUtil.isNull(getElementCode2())){
      errors.addError(new SICustomError("input.data.require","規格コード2"));
    }
    
    //エレメントコード2の存在のチェック
    if (SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getStndrdCode2())&&SIUtil.isNotNull(getElementCode2())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM StndrdContentMTbl ");
      lSqlBuf.append("WHERE ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND StndrdCode=").append(SIDBUtil.SQL2Str(this.getStndrdCode2()));
      lSqlBuf.append("AND ElementCode=").append(SIDBUtil.SQL2Str(this.getElementCode2()));
      SICheckValid.checkExist(errors,lConnection,"規格コード2",lSqlBuf.toString());
    }
    
    if (SIUtil.isNull(getStndrdCode1())&&SIUtil.isNull(getStndrdCode2())){
      errors.addError(new SICustomError("input.data.require","規格分類コード1または規格分類コード2"));
    }
    
    //商品規格マスターのチェック
    lSqlBuf=new StringBuffer();
    if (lCsvCheck&&SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getCmdtyCode())
        &&SIUtil.isNotNull(getStndrdCode1())&&SIUtil.isNotNull(getElementCode1())
        &&SIUtil.isNotNull(getStndrdCode2())&&SIUtil.isNotNull(getElementCode2())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM CmdtyStndrdMTbl ");
      lSqlBuf.append("WHERE ShopCode=" + SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND CmdtyCode="+SIDBUtil.SQL2Str(this.getCmdtyCode()," "));
      lSqlBuf.append("AND StndrdCode1="+SIDBUtil.SQL2Str(this.getStndrdCode1()," "));
      lSqlBuf.append("AND ElementCode1="+SIDBUtil.SQL2Str(this.getElementCode1()," "));
      lSqlBuf.append("AND StndrdCode2="+SIDBUtil.SQL2Str(this.getStndrdCode2()," "));
      lSqlBuf.append("AND ElementCode2="+SIDBUtil.SQL2Str(this.getElementCode2()," "));
    }else if (lCsvCheck&&SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getCmdtyCode())
        &&SIUtil.isNotNull(getStndrdCode1())&&SIUtil.isNotNull(getElementCode1())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM CmdtyStndrdMTbl ");
      lSqlBuf.append("WHERE ShopCode=" + SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND CmdtyCode="+SIDBUtil.SQL2Str(this.getCmdtyCode()," "));
      lSqlBuf.append("AND StndrdCode1="+SIDBUtil.SQL2Str(this.getStndrdCode1()," "));
      lSqlBuf.append("AND ElementCode1="+SIDBUtil.SQL2Str(this.getElementCode1()));
      lSqlBuf.append("AND StndrdCode2 IS NULL AND ElementCode2 IS NULL");
    }else if (lCsvCheck&&SIUtil.isNotNull(getShopCode())&&SIUtil.isNotNull(getCmdtyCode())
        &&SIUtil.isNotNull(getStndrdCode2())&&SIUtil.isNotNull(getElementCode2())){
      lSqlBuf=new StringBuffer("SELECT ShopCode FROM CmdtyStndrdMTbl ");
      lSqlBuf.append("WHERE ShopCode=" + SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND CmdtyCode="+SIDBUtil.SQL2Str(this.getCmdtyCode()," "));
      lSqlBuf.append("AND StndrdCode2="+SIDBUtil.SQL2Str(this.getStndrdCode2()," "));
      lSqlBuf.append("AND ElementCode2="+SIDBUtil.SQL2Str(this.getElementCode2()));
      lSqlBuf.append("AND StndrdCode1 IS NULL AND ElementCode1 IS NULL");
    }
    
    if (lCsvCheck&&getEditModeTxt().equals(SIConfig.SIEDIT_MODE_INSERT)&&SIUtil.isNotNull(lSqlBuf.toString())){
      SICheckValid.checkNotExist(errors,lConnection,"規格分類コードと規格コード",lSqlBuf.toString());
    }else if (lCsvCheck&&SIUtil.isNotNull(lSqlBuf.toString())){
      SICheckValid.checkExist(errors,lConnection,"規格分類コードと規格コード",lSqlBuf.toString());
    }
    
    if (lCsvCheck&&SIUtil.isNotNull(getStndrdCode1())&&SIUtil.isNotNull(getStndrdCode2())&&getStndrdCode1().equals(getStndrdCode2())){
      errors.addError(new SICustomError("manager.app.data.notsame","規格分類コード1","規格分類コード2"));
    }
    SICheckValid.checkValid(errors,"在庫数量",this.getAmount(),SICheckDataConf.SICHECK_DATA_DIGIT_NEGATIVE_TYPE);
    if (lCsvCheck){
      SICheckValid.checkValid(errors,"在庫数量",this.getAmount(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE,8);
    }
    
    //エラーメッセージをセッションに設定します。
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    
    return errors.isEmpty();
  }
  
  /**
   * <b>hasStndrd</b>
   * 対象の商品コードに対する規格があるかどうかをチェックします。
   * @param lConnection DBへのコネクション
   * @param lShopCode ショップコード
   * @param lCmdtyCode 商品コード
   * @return true 規格ある false 規格なし
   * @throws なし
   */
  public static boolean hasStndrd(Connection lConnection,String lShopCode,String lCmdtyCode){
    if (SIUtil.isNull(lShopCode)||SIUtil.isNull(lCmdtyCode)) return false;
    StringBuffer lSqlBuf=new StringBuffer("SELECT CmdtyCode FROM CmdtyStndrdMTbl WHERE ShopCode=? AND CmdtyCode=?");
    PreparedStatement lPreparedStatement =null;
    ResultSet lResultSet=null;
    
    try {
      lPreparedStatement = lConnection.prepareStatement(lSqlBuf.toString());
      
      lPreparedStatement.setString(1,lShopCode);
      lPreparedStatement.setString(2,lCmdtyCode);
      lResultSet=lPreparedStatement.executeQuery();
      return lResultSet.next();
    } catch (SQLException e) {
      e.printStackTrace();
      return false;
    }finally{
      SIDBUtil.close(lResultSet,lPreparedStatement);
    }
  }
  
  public static String getSubTotal(Connection lConnection,String lShopCode,String lCmdtyCode){
    if (SIUtil.isNull(lShopCode)||SIUtil.isNull(lCmdtyCode)) return "0";
    StringBuffer lSqlBuf=new StringBuffer("SELECT Sum(Amount) AS SubTotal FROM CmdtyStndrdMTbl WHERE ShopCode=? AND CmdtyCode=?");
    PreparedStatement lPreparedStatement =null;
    ResultSet lResultSet=null;
    
    try {
      lPreparedStatement = lConnection.prepareStatement(lSqlBuf.toString());
      
      lPreparedStatement.setString(1,lShopCode);
      lPreparedStatement.setString(2,lCmdtyCode);
      lResultSet=lPreparedStatement.executeQuery();
      
      if (lResultSet.next()) return lResultSet.getString("SubTotal");
      else return "0";
    } catch (SQLException e) {
      e.printStackTrace();
      return "0";
    }finally{
      SIDBUtil.close(lResultSet,lPreparedStatement);
    }
  }
}