package com.engine.kq.biz;

import weaver.cache.*;
import weaver.conn.RecordSet;
import weaver.general.ThreadVarLanguage;
import weaver.systeminfo.SystemEnv;

public class KQMessageRemindComInfo extends CacheBase {

    /**
     * 数据来源
     */
    protected static String TABLE_NAME = " kq_messageRemindTemplate ";
    /**
     * sql中的where信息，不要以where开始
     */
    protected static String TABLE_WHERE = "";
    /**
     * 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 = "serialId")
    protected static int serialId;

    /**
     * 签到还是签退
     */
    @CacheColumn(name = "signInOrSignOut")
    protected static int signInOrSignOut;

    /**
     * 内容
     */
    @CacheColumn(name = "messageContent")
    protected static int messageContent;

    @Override
    public CacheMap initCache() throws Exception {
        CacheMap localData = super.createCacheMap();
        RecordSet recordSet = new RecordSet();
        String sql = "select * from kq_messageRemindTemplate";
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String _id = recordSet.getString("id");
            String _serialId = recordSet.getString("serialId");
            String _signInOrSignOut = recordSet.getString("signInOrSignOut");
            String _messageTile = recordSet.getString("messageTtle");
            String _messageContent = recordSet.getString("messageContent");
            String PK = _serialId + "-" + _signInOrSignOut;
            CacheItem cacheItem = createCacheItem();
            cacheItem.set(PK_INDEX, PK);
            cacheItem.set(id, _id);
            cacheItem.set(serialId, _serialId);
            cacheItem.set(signInOrSignOut, _signInOrSignOut);
            cacheItem.set(messageContent, _messageContent);
            modifyCacheItem(PK, cacheItem);
            localData.put(PK, cacheItem);
        }
        return localData;
    }

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

        String[] keyArr = key.split("-");
        if (keyArr.length != 2) {
            return null;
        }
        String sql = "select * from kq_messageRemindTemplate where serialId=? and signInOrSignOut=?";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql, keyArr[0], keyArr[1]);
        if (recordSet.next()) {
            String _id = recordSet.getString("id");
            String _serialId = recordSet.getString("serialId");
            String _signInOrSignOut = recordSet.getString("signInOrSignOut");
            String _messageTile = recordSet.getString("messageTtle");
            String _messageContent = recordSet.getString("messageContent");
            String PK = _serialId + "-" + _signInOrSignOut;
            CacheItem cacheItem = createCacheItem();
            cacheItem.set(PK_INDEX, PK);
            cacheItem.set(id, _id);
            cacheItem.set(serialId, _serialId);
            cacheItem.set(signInOrSignOut, _signInOrSignOut);
            cacheItem.set(messageContent, _messageContent);
            modifyCacheItem(key, cacheItem);
            return cacheItem;
        } else {
            CacheItem cacheItem = createCacheItem();
            return cacheItem;
        }
    }

    private String getMessageContent(String key) {
        return (String) getValue(messageContent, key);
    }

    public String getMessageContent(String serialId, String signInOrSignOut) {
        String PK = serialId + "-" + signInOrSignOut;
        String messageContent = getMessageContent(PK);
        if ("".equals(messageContent)) {
            if ("1".equals(signInOrSignOut)) {
                messageContent = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005295,weaver.general.ThreadVarLanguage.getLang())+"";
            } else {
                messageContent = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005296,weaver.general.ThreadVarLanguage.getLang())+"";
            }
        }
        if (messageContent.contains("["+ SystemEnv.getHtmlLabelName(18843, ThreadVarLanguage.getLang())+"]") && "1".equals(signInOrSignOut)) {
            KQShiftManagementComInfo shiftManagementComInfo = new KQShiftManagementComInfo();
            String minsBeforeSignIn = shiftManagementComInfo.getMinsBeforeSignIn(serialId);
            messageContent = messageContent.replace("["+ SystemEnv.getHtmlLabelName(18843, ThreadVarLanguage.getLang())+"]", minsBeforeSignIn + ""+ SystemEnv.getHtmlLabelName(15049, ThreadVarLanguage.getLang())+"");
        }
        if (messageContent.contains("["+ SystemEnv.getHtmlLabelName(10005342, ThreadVarLanguage.getLang())+"]") && "2".equals(signInOrSignOut)) {
            KQShiftManagementComInfo shiftManagementComInfo = new KQShiftManagementComInfo();
            String minsAfterSignOut = shiftManagementComInfo.getMinsAfterSignOut(serialId);
            messageContent = messageContent.replace("["+ SystemEnv.getHtmlLabelName(10005342, ThreadVarLanguage.getLang())+"]", minsAfterSignOut + ""+ SystemEnv.getHtmlLabelName(15049, ThreadVarLanguage.getLang())+"");
        }
        return messageContent;
    }
}
