package jp.co.sint.beans.mallmgr;

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.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Category;

import jp.co.sint.basic.SIBasic;
import jp.co.sint.basic.SIRankingAnalysis;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIFlagConf;
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.SICheckUtil;
import jp.co.sint.tools.SICheckValid;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIDateTime;
import jp.co.sint.tools.SIURLParameter;
import jp.co.sint.tools.SIUtil;

public class UIRankingAnalysisListCond extends SIBasic {
  
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //集計対象
  private String countType = "1";
  
  //ソート順
  private String sortOrder = "1";
  
  //集計期間Fromの年
  private String countDateFromYear = "";
  
  //集計期間Fromの月
  private String countDateFromMonth = "";
  
  //集計期間Fromの日
  private String countDateFromDay = "";
  
  //集計期間Toの年
  private String countDateToYear = "";
  
  //集計期間Toの月
  private String countDateToMonth = "";
  
  //集計期間Toの日
  private String countDateToDay = "";
  
  //在庫コード
  private String individualCode = "";
  
  //商品名
  private String cmdtyName = "";
  
  //ブランド
  private String makerCode = "";
  
  //顧客コード
  private String custCode = "";
  
  //営業形態
  private String job = "";
  
  //配送先都道府県
  private String address1 = "";
  
  //受注担当支店
  private String branchCode = "";
  
  //受注担当者
  private String chargeCode = "";
  
  //受注経路
  private String[] orderRouteChk = new String[SIFlagConf.SIFLAG_ORDER_ROUTE_INX_NAME.length];
  
  //取得件数From
  private String rowCountFrom = "0";
  
  //取得件数To
  private String rowCountTo = "0";
  
  //検索結果リスト
  private Collection result;
  
  //検索条件SQL
  private String conditionSQL = "";
  
  //検索実行flg
  private boolean searchFlg = false;
  
  public String getCountType() {
    return countType;
  }
  
  public String getSortOrder() {
    return sortOrder;
  }
  
  public String getCountDateFromYear() {
    return countDateFromYear;
  }
  
  public String getCountDateFromMonth() {
    return countDateFromMonth;
  }
  
  public String getCountDateFromDay() {
    return countDateFromDay;
  }
  
  public String getCountDateFrom() {
    return SIDateTime.getDate(getCountDateFromYear(), getCountDateFromMonth(), getCountDateFromDay());
  }
  
  public String getCountDateToYear() {
    return countDateToYear;
  }
  
  public String getCountDateToMonth() {
    return countDateToMonth;
  }
  
  public String getCountDateToDay() {
    return countDateToDay;
  }
  
  public String getCountDateTo() {
    return SIDateTime.getDate(getCountDateToYear(), getCountDateToMonth(), getCountDateToDay());
  }
  
  public String getIndividualCode() {
    return individualCode;
  }
  
  public String getCmdtyName() {
    return cmdtyName;
  }
  
  public String getMakerCode() {
    return makerCode;
  }
  
  public String getCustCode() {
    return custCode;
  }
  
  public String getJob() {
    return job;
  }
  
  public String getAddress1() {
    return address1;
  }
  
  public String getBranchCode() {
    return branchCode;
  }
  
  public String getChargeCode() {
    return chargeCode;
  }
  
  public String[] getOrderRouteChk() {
    return orderRouteChk;
  }
  
  public String getRowCountFrom() {
    return rowCountFrom;
  }
  
  public String getRowCountTo() {
    return rowCountTo;
  }
  
  public Collection getResult() {
    if (this.result==null) return new ArrayList();
    return result;
  }
  
  public String getConditionSQL(){
    return conditionSQL;
  }
  
  public void setCountType(String countType) {
    if (SIUtil.isNull(countType)) countType = "";
    this.countType = countType;
  }
  
  public void setSortOrder(String sortOrder) {
    if (SIUtil.isNull(sortOrder)) sortOrder = "";
    this.sortOrder = sortOrder;
  }
  
  public void setCountDateFromYear(String countDateFromYear) {
    if (SIUtil.isNull(countDateFromYear)) countDateFromYear = "";
    this.countDateFromYear = countDateFromYear;
  }
  
  public void setCountDateFromMonth(String countDateFromMonth) {
    if (SIUtil.isNull(countDateFromMonth)) countDateFromMonth = "";
    this.countDateFromMonth = countDateFromMonth;
  }
  
  public void setCountDateFromDay(String countDateFromDay) {
    if (SIUtil.isNull(countDateFromDay)) countDateFromDay = "";
    this.countDateFromDay = countDateFromDay;
  }
  
  public void setCountDateToYear(String countDateToYear) {
    if (SIUtil.isNull(countDateToYear)) countDateToYear = "";
    this.countDateToYear = countDateToYear;
  }
  
  public void setCountDateToMonth(String countDateToMonth) {
    if (SIUtil.isNull(countDateToMonth)) countDateToMonth = "";
    this.countDateToMonth = countDateToMonth;
  }
  
  public void setCountDateToDay(String countDateToDay) {
    if (SIUtil.isNull(countDateToDay)) countDateToDay = "";
    this.countDateToDay = countDateToDay;
  }
  
  public void setIndividualCode(String individualCode) {
    if (SIUtil.isNull(individualCode)) individualCode = "";
    this.individualCode = individualCode;
  }
  
  public void setCmdtyName(String cmdtyName) {
    if (SIUtil.isNull(cmdtyName)) cmdtyName = "";
    this.cmdtyName = cmdtyName;
  }
  
  public void setMakerCode(String makerCode) {
    if (SIUtil.isNull(makerCode)) makerCode = "";
    this.makerCode = makerCode;
  }
  
  public void setCustCode(String custCode) {
    if (SIUtil.isNull(custCode)) custCode = "";
    this.custCode = custCode;
  }
  
  public void setJob(String job) {
    if (SIUtil.isNull(job)) job = "";
    this.job = job;
  }
  
  public void setAddress1(String address1) {
    if (SIUtil.isNull(address1)) address1 = "";
    this.address1 = address1;
  }
  
  public void setBranchCode(String branchCode) {
    if (SIUtil.isNull(branchCode)) branchCode = "";
    this.branchCode = branchCode;
  }
  
  public void setChargeCode(String chargeCode) {
    if (SIUtil.isNull(chargeCode)) chargeCode = "";
    this.chargeCode = chargeCode;
  }
  
  public void setOrderRouteChk(String[] orderRouteChk) {
    this.orderRouteChk = orderRouteChk;
  }
  
  public void setRowCountFrom(String rowCountFrom) {
    if (SIUtil.isNull(rowCountFrom)) rowCountFrom = "";
    this.rowCountFrom = rowCountFrom;
  }
  
  public void setRowCountTo(String rowCountTo) {
    if (SIUtil.isNull(rowCountTo)) rowCountTo = "";
    this.rowCountTo = rowCountTo;
  }
  
  public void setResult(Collection result) {
    if (result==null) result=new ArrayList();
    this.result = result;
  }
  
  public void setConditionSQL(String conditionSQL) {
    if (SIUtil.isNull(conditionSQL)) conditionSQL = "";
    this.conditionSQL = conditionSQL;
  }
  
  public UIRankingAnalysisListCond(){
    this.setCountType("1");
    this.setSortOrder("1");
    
    SIDateTime lDateTime = new SIDateTime();
    lDateTime.addDay(-1);
    String Year = Integer.toString(lDateTime.getYear());
    String Month = Integer.toString(lDateTime.getMonth());
    String Day = Integer.toString(lDateTime.getDay());
    
    if (Month.length() == 1) Month = "0" + Month;
    if (Day.length() == 1) Day = "0" + Day;
    
    this.setCountDateToYear(Year);
    this.setCountDateToMonth(Month);
    this.setCountDateToDay(Day);
    
    lDateTime = new SIDateTime();
    lDateTime.addMonth(-1);
    Year = Integer.toString(lDateTime.getYear());
    Month = Integer.toString(lDateTime.getMonth());
    Day = Integer.toString(lDateTime.getDay());
    
    if (Month.length() == 1) Month = "0" + Month;
    if (Day.length() == 1) Day = "0" + Day;
    
    this.setCountDateFromYear(Year);
    this.setCountDateFromMonth(Month);
    this.setCountDateFromDay(Day);
    
    this.setRowCountFrom("1");
    this.setRowCountTo("30");
    this.orderRouteChk = new String[SIFlagConf.SIFLAG_ORDER_ROUTE_INX_NAME.length];
    for (int i=0;i<SIFlagConf.SIFLAG_ORDER_ROUTE_INX_NAME.length;i++) {
      this.orderRouteChk[i]=SIFlagConf.SIFLAG_ORDER_ROUTE_INX_NAME[i][1];
    }
    
    this.searchFlg = false;
  }
  
  public void init(HttpServletRequest lRequest, SIURLParameter lUrlParam) {
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    this.searchFlg = true;
    super.init(lRequest, lUrlParam);
    this.setCountType((String) lUrlParam.getParam("countType"));
    this.setSortOrder((String) lUrlParam.getParam("sortOrder"));
    this.setCountDateFromYear((String) lUrlParam.getParam("countDateFromYear"));
    this.setCountDateFromMonth((String) lUrlParam.getParam("countDateFromMonth"));
    this.setCountDateFromDay((String) lUrlParam.getParam("countDateFromDay"));
    this.setCountDateToYear((String) lUrlParam.getParam("countDateToYear"));
    this.setCountDateToMonth((String) lUrlParam.getParam("countDateToMonth"));
    this.setCountDateToDay((String) lUrlParam.getParam("countDateToDay"));
    this.setIndividualCode((String) lUrlParam.getParam("individualCode"));
    this.setCmdtyName((String) lUrlParam.getParam("cmdtyName"));
    this.setMakerCode((String) lUrlParam.getParam("makerCode"));
    this.setCustCode((String) lUrlParam.getParam("custCode"));
    this.setJob((String) lUrlParam.getParam("job"));
    this.setAddress1((String) lUrlParam.getParam("address1"));
    this.setBranchCode((String) lUrlParam.getParam("branchCode"));
    this.setChargeCode((String) lUrlParam.getParam("chargeCode"));
    this.setOrderRouteChk(lRequest.getParameterValues("orderRouteChk"));
    this.setRowCountFrom((String) lUrlParam.getParam("rowCountFrom"));
    this.setRowCountTo((String) lUrlParam.getParam("rowCountTo"));
  }

  public void validate(HttpServletRequest lRequest) {
    SICustomErrors errors = new SICustomErrors();
    SITableConditionManager lConditionMan = new SITableConditionManager();
    
    //集計期間
    boolean countDateCheck = true;
    countDateCheck = countDateCheck & SICheckValid.checkValid(errors, "集計期間From", this.getCountDateFrom(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    countDateCheck = countDateCheck & SICheckValid.checkValid(errors, "集計期間To", this.getCountDateTo(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    countDateCheck = countDateCheck & SICheckValid.checkValid(errors, "集計期間From", this.getCountDateFrom(), SICheckDataConf.SICHECK_DATA_DATE_TYPE);
    countDateCheck = countDateCheck & SICheckValid.checkValid(errors, "集計期間To", this.getCountDateTo(), SICheckDataConf.SICHECK_DATA_DATE_TYPE);
    
    try {
      if (countDateCheck && !SICheckUtil.dateEqual(this.getCountDateFrom(), this.getCountDateTo())) {
        SICheckValid.checkValid(errors, "集計期間To", "集計期間From", this.getCountDateFrom(), this.getCountDateTo(), SICheckDataConf.SICHECK_DATA_DATE_LESS_TYPE);
        SIDateTime date = new SIDateTime(this.getCountDateFromYear() + "/" + this.getCountDateFromMonth() + "/" + this.getCountDateFromDay(),SIConfig.SIDATE_FORMAT0);
        date.addYear(1);
        SICheckValid.checkValid(errors, "集計期間", "最大集計期間（1年）", this.getCountDateTo(), date.getFullDate(), SICheckDataConf.SICHECK_DATA_DATE_LESS_TYPE);
      }
    } catch (Exception e) {}
    
    // 在庫コード
    if (SIUtil.isNotNull(individualCode)) {
      StringBuffer individualSql = new StringBuffer();
      String[] codes = individualCode.trim().split(" |　");
      for (int i = 0; i < codes.length; i++) {
        if (!SICheckUtil.isAlphaDigit(codes[i])) {
          errors.addError(new SICustomError("input.data.alphadigit", "在庫コード"));
          break;
        }
        if (SIUtil.isNotNull(codes[i])) {
          if (SIUtil.isNotNull(individualSql.toString())) individualSql.append("OR");
          individualSql.append(" list.individualcode LIKE '").append(SIDBUtil.SQL2Like(codes[i])).append("%' ");
        }
      }
      if (SIUtil.isNotNull(individualSql.toString())) {
        lConditionMan.add(new SITableCondition(" AND (" + individualSql.toString() + ") "));
      }
    }
    
    // 商品名
    if (SIUtil.isNotNull(cmdtyName)) {
      StringBuffer cmdtyNameSql = new StringBuffer();
      String[] codes = cmdtyName.trim().split(" |　");
      for (int i = 0; i < codes.length; i++) {
        if (SIUtil.isNotNull(codes[i])) {
          if (SIUtil.isNotNull(cmdtyNameSql.toString())) cmdtyNameSql.append("OR");
          cmdtyNameSql.append(" cm.cmdtyName LIKE '%").append(SIDBUtil.SQL2Like(codes[i])).append("%' ");
        }
      }
      if (SIUtil.isNotNull(cmdtyNameSql.toString())) {
        lConditionMan.add(new SITableCondition(" AND (" + cmdtyNameSql.toString() + ") "));
      }
    }
    
    //ブランド
    if (SIUtil.isNotNull(this.makerCode)) {
      lConditionMan.add(new SITableCondition("cm", "makerCode", this.makerCode, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //顧客コード
    if (SIUtil.isNotNull(this.custCode) && SICheckValid.checkValid(errors, "顧客コード", this.custCode, SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE)) {
      lConditionMan.add(new SITableCondition("cu", "custCode", this.custCode, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //営業形態
    if (SIUtil.isNotNull(this.job)) {
      lConditionMan.add(new SITableCondition("cu", "job", this.job, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //配送先都道府県
    if (SIUtil.isNotNull(this.address1)) {
      lConditionMan.add(new SITableCondition("list", "address1", this.address1, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //受注担当支店
    if (SIUtil.isNotNull(this.branchCode)) {
      lConditionMan.add(new SITableCondition("list", "branchcode", this.branchCode, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //受注担当者
    if (SIUtil.isNotNull(this.chargeCode)) {
      lConditionMan.add(new SITableCondition("list", "chargecode", this.chargeCode, SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND));
    }
    
    //受注経路
    if (this.orderRouteChk!=null&&this.orderRouteChk.length>0) {
      StringBuffer str = new StringBuffer();
      str.append(" AND list.orderRoute IN (");
      for (int i=0;i<this.orderRouteChk.length;i++) {
        if (i>0) str.append(",");
        str.append(SIDBUtil.SQL2Str(this.orderRouteChk[i]));
      }
      str.append(") ");
      lConditionMan.add(new SITableCondition(str.toString()));
    }else{
      errors.addError(new SICustomError("manager.message.freeword","受注経路は必ずひとつ以上選択して下さい"));
    }
    
    //取得件数
    boolean rowCountCheck = true;
    rowCountCheck = rowCountCheck & SICheckValid.checkValid(errors, "取得件数From", this.rowCountFrom,  SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    rowCountCheck = rowCountCheck & SICheckValid.checkValid(errors, "取得件数To", this.rowCountTo,  SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    if (rowCountCheck) {
      SICheckValid.checkValid(errors, "取得件数From", "取得件数To", this.rowCountFrom, this.rowCountTo, SICheckDataConf.SICHECK_DATA_VALUE_LESS_TYPE);
      SICheckValid.checkValid(errors, "取得件数", "最大取得件数（100件）", SIUtil.sub(this.rowCountTo, this.rowCountFrom),"100",  SICheckDataConf.SICHECK_DATA_VALUE_LESS_TYPE);
      SICheckValid.checkValid(errors, "取得件数From", "1位", this.rowCountFrom,"0",  SICheckDataConf.SICHECK_DATA_VALUE_GREATER_TYPE);
    }
    
    if (!errors.isEmpty()) {
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
      this.searchFlg = false;
    } else {
      lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    }
    
    // 条件文の設定
    this.setConditionSQL(lConditionMan.getCondtionSQL());
  }
  
  public void getCollection(Connection lConnection){
    if (!this.searchFlg) {this.setResult(new ArrayList());return;}
    Collection resultColl = new ArrayList();
    SIRankingAnalysis lData = new SIRankingAnalysis();
    StringBuffer lSqlBuf = new StringBuffer();
    Statement lStatement = null;
    ResultSet lResultSet = null;
    
    if ("1".equals(this.getCountType())) {
      lSqlBuf.append("SELECT cu.custcode,cu.custname,ch.chargename,cu.memberlevelcode ");
      lSqlBuf.append(",sum(list.price*list.amount) AS sales,sum(list.amount) AS amount,count(distinct count1)-count(distinct count2) AS count ");
    } else {
      lSqlBuf.append("SELECT list.individualcode,cm.cmdtyname,cl.colorname,mk.makername ");
      lSqlBuf.append(",sum(list.price*list.amount) AS sales,sum(list.amount) AS amount,count(distinct count1)-count(distinct count2) AS count ");
    }
    lSqlBuf.append("FROM cmdtymtbl cm,colortbl cl,makertbl mk,custtbl cu ");
    lSqlBuf.append("LEFT OUTER JOIN chargetbl ch ON cu.chargecode=ch.chargecode ");
    lSqlBuf.append(",(SELECT oh.ordercode AS count1,NULL AS count2,oh.custcode,dl.address1,dt.cmdtycode,dt.individualcode,dt.price,dt.amount,oh.branchcode,oh.chargecode,oh.orderroute");
    lSqlBuf.append(" FROM ordertbl oh,orderdeliverytbl dl,orderdetailtbl dt");
    lSqlBuf.append(" WHERE oh.ordercode=dl.ordercode AND oh.orderbranchcode=dl.orderbranchcode AND oh.enabledflg='1'");
    lSqlBuf.append(" AND oh.ordercode=dt.ordercode AND dl.deliverycode=dt.deliverycode AND oh.orderbranchcode=dt.orderbranchcode");
    lSqlBuf.append(" AND oh.status IN ('1','2') AND dt.shippmentdate IS NOT NULL");
    lSqlBuf.append(" AND dt.shippmentdate >= ").append(SIDBUtil.SQL2Str(this.getCountDateFrom()));
    lSqlBuf.append(" AND dt.shippmentdate <= ").append(SIDBUtil.SQL2Str(this.getCountDateTo()));
    lSqlBuf.append(" UNION ALL");
    lSqlBuf.append(" SELECT NULL AS count1,oh.ordercode AS count2,oh.custcode,dl.address1,dt.cmdtycode,dt.individualcode,dt.price,-1 * dt.amount AS amount,oh.branchcode,oh.chargecode,oh.orderroute");
    lSqlBuf.append(" FROM ordertbl oh,orderdeliverytbl dl,orderdetailtbl dt");
    lSqlBuf.append(" WHERE oh.ordercode=dl.ordercode AND oh.orderbranchcode=dl.orderbranchcode AND oh.enabledflg='1'");
    lSqlBuf.append(" AND oh.ordercode=dt.ordercode AND dl.deliverycode=dt.deliverycode AND oh.orderbranchcode=dt.orderbranchcode");
    lSqlBuf.append(" AND oh.status = '2' AND dt.shippmentdate IS NOT NULL");
    lSqlBuf.append(" AND oh.updatedatetime >= ").append(SIDBUtil.SQL2Str(this.getCountDateFrom()));
    lSqlBuf.append(" AND oh.updatedatetime <= ").append(SIDBUtil.SQL2Str(this.getCountDateTo()));
    lSqlBuf.append(") list ");
    lSqlBuf.append("WHERE list.custcode=cu.custcode AND list.cmdtycode=cm.cmdtycode  ");
    lSqlBuf.append("AND cm.colorcode=cl.colorcode AND cm.makercode=mk.makercode ");
    lSqlBuf.append(this.getConditionSQL());
    lSqlBuf.append(" ");
    if ("1".equals(this.getCountType())) lSqlBuf.append("GROUP BY cu.custcode,cu.custname,ch.chargename,cu.memberlevelcode ");
    else lSqlBuf.append("GROUP BY list.individualcode,cm.cmdtyname,cl.colorname,mk.makername ");
    lSqlBuf.append("ORDER BY ");
    if ("1".equals(this.getSortOrder())) lSqlBuf.append("sales DESC");
    else if ("2".equals(this.getSortOrder())) lSqlBuf.append("amount DESC");
    else lSqlBuf.append("count DESC");
    if ("1".equals(this.getCountType())) lSqlBuf.append(",cu.custcode ");
    else lSqlBuf.append(",list.individualcode ");
    lSqlBuf.append("LIMIT ").append(SIUtil.sub(this.rowCountTo, SIUtil.sub(this.rowCountFrom, "1")));
    lSqlBuf.append(" OFFSET ").append(SIUtil.sub(this.rowCountFrom, "1"));
    
    try {
      log.debug("getConnection:lSqlBuf=" + lSqlBuf.toString());
      
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlBuf.toString());
      
      while (lResultSet.next()) {
        lData = new SIRankingAnalysis();
        lData.setEncode(SIConfig.SIENCODE_NONE);
        if ("1".equals(this.getCountType())) {
          lData.setCustCode(lResultSet.getString("custCode"));
          lData.setCustName(lResultSet.getString("custName"));
          lData.setMemberLevelCode(lResultSet.getString("memberLevelCode"));
          lData.setChargeName(lResultSet.getString("chargeName"));
        } else {
          lData.setIndividualCode(lResultSet.getString("individualCode"));
          lData.setCmdtyName(lResultSet.getString("cmdtyName"));
          lData.setMakerName(lResultSet.getString("makerName"));
          lData.setColorName(lResultSet.getString("colorName"));
        }
        lData.setTotalSales(lResultSet.getString("sales"));
        lData.setTotalAmount(lResultSet.getString("amount"));
        lData.setTotalCount(lResultSet.getString("count"));
        resultColl.add(lData);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      SIDBUtil.close(lStatement, lResultSet);
    }
    this.setResult(resultColl);
  }
  
  public Collection getResultCSV(){
    if (this.result==null||this.result.size()==0) return new ArrayList();
    Collection resColl = new ArrayList();
    Iterator ite = this.result.iterator();
    while (ite.hasNext()){
      String[] str = new String[11];
      SIRankingAnalysis lData = (SIRankingAnalysis) ite.next();
      str[0]=lData.getCustCode();
      str[1]=lData.getCustName();
      str[2]=lData.getMemberLevelCode();
      str[3]=lData.getChargeName();
      str[4]=lData.getIndividualCode();
      str[5]=lData.getCmdtyName();
      str[6]=lData.getMakerName();
      str[7]=lData.getColorName();
      str[8]=lData.getTotalSales();
      str[9]=lData.getTotalAmount();
      str[10]=lData.getTotalCount();
      resColl.add(str);
    }
    return resColl;
  }
}