package com.engine.kq.biz;

import weaver.cache.*;
import weaver.conn.RecordSet;

/**
 * 节假日设置缓存类
 */
public class KQHolidaySetComInfo extends CacheBase {

    /**
     * 数据来源
     */
    protected static String TABLE_NAME = "kq_holidaySet";
    /**
     * 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";

    /**
     * ID
     */
    @CacheColumn(name = "id")
    protected static int id;

    /**
     * 考勤组ID
     */
    @CacheColumn(name = "groupId")
    protected static int groupId;

    /**
     * 日期
     */
    @CacheColumn(name = "holidayDate")
    protected static int holidayDate;

    /**
     * 说明
     */
    @CacheColumn(name = "holidayDesc")
    protected static int holidayDesc;

    /**
     * 类型
     * 1-公众假日、2-调配工作日、3-调配休息日
     */
    @CacheColumn(name = "changeType")
    protected static int changeType;

    /**
     * 对应工作日
     * 1-星期日、2-星期一、3-星期二、4-星期三、5-星期四、6-星期五、7-星期六
     * 当考勤组的考勤类型为固定班制和自由班制的时启用
     */
    @CacheColumn(name = "relatedDay")
    protected static int relatedDay;

    @Override
    protected boolean autoInitIfNotFound() {
        return false;
    }

    @Override
    protected CacheMap initCache() throws Exception {
        CacheMap localData = super.createCacheMap();
        RecordSet rs = new RecordSet();
        String sql = "select * from kq_HolidaySet order by id ";
        rs.executeQuery(sql);
        while (rs.next()) {
            String groupId = rs.getString("groupId");
            String holidayDate = rs.getString("holidayDate");
            String PK = groupId + "|" + holidayDate;

            CacheItem row = createCacheItem();
            parseResultSetToCacheItem(rs, row);
            //这里需要记得调用这个转化方法，否则这些自己加载的数据就不能正确转化，当然你也可以直接在转化逻辑写在这里，不过这样先让代码重复了
            modifyCacheItem(PK, row);
            localData.put(PK, row);
        }
        return localData;
    }

    @Override
    protected CacheItem initCache(String key) {
        if (key == null || "".equals(key.trim())) {
            return null;
        }

        String groupId = "";//考勤组的ID
        String holidayDate = "";//日期

        String[] keyArr = key.split("\\|");
        if (keyArr.length == 2) {
            groupId = keyArr[0];
            holidayDate = keyArr[1];

            String sql = "select * from kq_HolidaySet where groupId=? and holidayDate=?";
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql, groupId, holidayDate);
            if (recordSet.next()) {
                CacheItem cacheItem = createCacheItem();
                parseResultSetToCacheItem(recordSet, cacheItem);
                modifyCacheItem(key, cacheItem);
                return cacheItem;
            }
            return null;
        } else {
            return null;
        }
    }

    private String getId(String key) {
        return (String) getValue(id, key);
    }

    public String getId(String groupId, String holidayDate) {
        String PK = groupId + "|" + holidayDate;
        String id = getId(PK);
        return id;
    }

    public String getGroupId() {
        return (String) getRowValue(groupId);
    }

    private String getGroupId(String key) {
        return (String) getValue(groupId, key);
    }

    public String getGroupId(String groupId, String holidayDate) {
        String PK = groupId + "|" + holidayDate;
        return getGroupId(PK);
    }

    public String getHolidayDate() {
        return (String) getRowValue(holidayDate);
    }

    private String getHolidayDate(String key) {
        return (String) getValue(holidayDate, key);
    }

    public String getHolidayDate(String groupId, String holidayDate) {
        String PK = groupId + "|" + holidayDate;
        return getHolidayDate(PK);
    }

    public String getHolidayDesc() {
        return (String) getRowValue(holidayDesc);
    }

    private String getHolidayDesc(String key) {
        return (String) getValue(holidayDesc, key);
    }

    public String getHolidayDesc(String groupId, String holidayDate) {
        String PK = groupId + "|" + holidayDate;
        return getHolidayDesc(PK);
    }

    public String getChangeType() {
        return (String) getRowValue(changeType);
    }

    private String getChangeType(String key) {
        return (String) getValue(changeType, key);
    }

    public String getChangeType(String groupId, String holidayDate) {
        String PK = groupId + "|" + holidayDate;
        return getChangeType(PK);
    }

    public String getRelatedDay() {
        return (String) getRowValue(relatedDay);
    }

    private String getRelatedDay(String key) {
        return (String) getValue(relatedDay, key);
    }

    public String getRelatedDay(String groupId, String holidayDate) {
        String PK = groupId + "|" + holidayDate;
        return getRelatedDay(PK);
    }
}
