package weaver.interfaces.htsc.comInfo;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.cache.*;
import weaver.conn.RecordSet;


public class PropComInfo extends CacheBase {

    // 需要定义的信息 begin
    protected static String TABLE_NAME = "uf_prop";
    protected static String TABLE_WHERE = null;
    protected static String tableWhere = null;
    protected static String TABLE_ORDER = " pkey asc";
    protected static String tableOrder = " pkey asc";

    @PKColumn(type = CacheColumnType.STRING)
    protected static String PK_NAME = "pkey";

    @CacheColumn(name = "pkey")
    protected static int pkey; //key

    @CacheColumn(name = "pvalue")
    protected static int pvalue	;//value

    @Override
    protected CacheMap initCache() throws Exception {
        if (tableName == null || tableName.trim().length() == 0) {
            throw new weaver.cache.exception.CacheException("TABLE_NAME should not be empty when use CacheBase.initCache() method.");
        }

        BaseBean bb = new BaseBean();
        bb.writeLog("PropComInfo===initCache()=====");

        CacheMap localData = createCacheMap();

        RecordSet rs = new RecordSet();
        String sql = "select " + getColumns() + " from " + tableName;
        if (tableWhere != null && tableWhere.trim().length() != 0) {
            sql = sql + " where " + tableWhere;
        }
        if (tableOrder != null && tableOrder.trim().length() != 0) {
            sql = sql + " order by " + tableOrder;
        }
        rs.execute(sql);
        while (rs.next()) {
            String id = Util.null2String(rs.getString(PK_NAME));
            CacheItem cacheItem = createCacheItem();
            parseResultSetToCacheItem(rs, cacheItem);
            modifyCacheItem(id, cacheItem);

            bb.writeLog(cacheItem.toString());
            localData.put(id, cacheItem);
        }

        return localData;
    }

    @Override
    protected CacheItem initCache(String key) {

        BaseBean bb = new BaseBean();
        bb.writeLog("PropComInfo=====initCache(key)===");

        if(key == null || key.trim().length() == 0){
            return null;
        }

        if (tableName == null || tableName.trim().length() == 0) {
            throw new weaver.cache.exception.CacheException("TABLE_NAME should not be empty when use CacheBase.initCache(String key) method.");
        }

        RecordSet rs = new RecordSet();
        String sql = "select " + getColumns() + " from " + tableName + " where " + PK_NAME + " = ? ";
        if (tableWhere != null && tableWhere.trim().length() != 0) {
            sql = sql + " and " + tableWhere;
        }

        rs.executeQuery(sql, key);
        if (rs.next()) {
            CacheItem cacheItem = createCacheItem();
            parseResultSetToCacheItem(rs, cacheItem);
            modifyCacheItem(key, cacheItem);
            return cacheItem;
        }
        return null;
    }

    public String getPvalue(String key)
    {
        return ((String)this.getValue(pvalue,key));
    }

    public boolean addPropCache(String key) {
        if ("".equals(key)){
            return false ;
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery("select pkey from "+tableName+" where "+PK_NAME+" = ? ",key);
        if (rs.next()) {
            rs.beforFirst() ;
            while(rs.next())
            {
                String pkey = Util.null2String(rs.getString("pkey"));
                addCache(pkey);
            }
        } else
            return false;

        return true;
    }

    public boolean removeLabeInfoCache(String key) {
        if ("".equals(key)){
            return false ;
        }
        RecordSet rs = new RecordSet();
        rs.execute("select pkey from "+tableName+" where "+PK_NAME+" = " + key + " order by indexid ");
        if (rs.next()) {
            rs.beforFirst() ;
            while(rs.next()){
                String pkey=Util.null2String(rs.getString("pkey"));
                deleteCache(pkey);
            }
        } else
            return false;

        return true;
    }

    public void removeLabelCache(){
        removeCache();
    }
}
