package com.engine.kq.biz;

import com.engine.kq.log.KQLog;
import java.util.List;
import weaver.cache.*;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * 报表自定义字段
 */
public class KQFixedSchedulceComInfo extends CacheBase {
  protected static String TABLE_NAME = "kq_fixedschedulce";
  /**
   * sql中的where信息，不要以where开始
   */
  protected static String TABLE_WHERE = null;
  /**
   * sql中的order by信息，不要以order by开始
   */
  protected static String TABLE_ORDER = "id";

  @PKColumn(type = CacheColumnType.NUMBER)
  protected static String PK_NAME = "id";

  @CacheColumn(name = "weekday")
  protected static int weekday;

  @CacheColumn(name = "serialid")
  protected static int serialid;

  @CacheColumn(name = "groupid")
  protected static int groupid;

  // 需要关注：虚拟列，默认不是虚拟列
  @CacheColumn(isVirtual = true)
  protected static int virtualKey;

  private boolean isFormat = false;

  private KQLog kqLog = new KQLog();

  protected boolean autoInitIfNotFound() {
    return false;
  }

  @Override
  public CacheMap initCache() throws Exception {
    CacheMap localData = super.initCache();
    try {
      RecordSet rs = new RecordSet();
      String sql = " ";
      if ("mysql".equals(rs.getDBType())) {
        sql += " select CONCAT(groupid,'_',weekday) as id, serialid from kq_fixedschedulce ";
      } else if("sqlserver".equals(rs.getDBType())){
        sql += " select CONVERT(varchar,groupid)+'_'+CONVERT(varchar,weekday) as id, serialid from kq_fixedschedulce  ";
      }else {
        sql = " select groupid || '_'|| weekday as id, serialid from kq_fixedschedulce ";
      }
      rs.executeQuery(sql);
      while (rs.next()) {
        String id = Util.null2String(rs.getString(PK_NAME));
        CacheItem row = createCacheItem();
        parseResultSetToCacheItem(rs, row);
        modifyCacheItem(id, row);
        localData.put(id, row);
      }
    } catch (Exception e) {
      writeLog(e);
    }
    return localData;
  }

  public String getId(){
    return (String)getRowValue(PK_INDEX);
  }

  public String getSerialid() {
    return (String) getRowValue(serialid);
  }

  public String getSerialid(String groupid, int weekday) {

    if(!this.isFormat){
      String key = groupid + "_" + weekday;
      return (String) getValue(serialid, key);
    }
    return getSerialidBySql(groupid,weekday);
  }

  private String getSerialidBySql(String groupid, int weekday) {
    String sql_serialid = "";
    try {
      String key = groupid + "_" + weekday;
      RecordSet rs = new RecordSet();
      String sql = " ";
      if ("mysql".equals(rs.getDBType())) {
        sql += " select CONCAT(groupid,'_',weekday) as id, serialid from kq_fixedschedulce where CONCAT(groupid,'_',weekday)=? ";
      } else if("sqlserver".equals(rs.getDBType())){
        sql += " select CONVERT(varchar,groupid)+'_'+CONVERT(varchar,weekday) as id, serialid from kq_fixedschedulce where CONVERT(varchar,groupid)+'_'+CONVERT(varchar,weekday)=?  ";
      }else {
        sql = " select groupid || '_'|| weekday as id, serialid from kq_fixedschedulce where groupid || '_'|| weekday=? ";
      }
      rs.executeQuery(sql,key);
      if (rs.next()) {
        sql_serialid = rs.getString("serialid");
      }
      kqLog.writeLog("KQFixedSchedulceComInfo getSerialidBySql :sql:"+sql+":sql_serialid:"+sql_serialid);
    } catch (Exception e) {
      writeLog(e);
    }
    return sql_serialid;
  }

  public boolean isFormat() {
    return isFormat;
  }

  public void setFormat(boolean format) {
    isFormat = format;
  }
}