package com.engine.kq.biz;

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

/**
 * 考勤提醒，邮件提醒模板缓存类
 */
public class KQEmailRemindComInfo extends CacheBase {

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

    /**
     * 邮件内容
     */
    @CacheColumn(name = "emailContent")
    protected static int emailContent;

    @Override
    public CacheMap initCache() throws Exception {
        CacheMap localData = super.createCacheMap();
        RecordSet recordSet = new RecordSet();
        String sql = "select * from kq_emailRemindTemplate";
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String _id = recordSet.getString("id");
            String _serialId = recordSet.getString("serialId");
            String _signInOrSignOut = recordSet.getString("signInOrSignOut");
            String _emailTile = recordSet.getString("emailTtle");
            String _emailContent = recordSet.getString("emailContent");
            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(emailTitle, _emailTile);
            cacheItem.set(emailContent, _emailContent);
            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_emailRemindTemplate 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 _emailTile = recordSet.getString("emailTtle");
            String _emailContent = recordSet.getString("emailContent");
            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(emailTitle, _emailTile);
            cacheItem.set(emailContent, _emailContent);
            modifyCacheItem(key, cacheItem);
            return cacheItem;
        } else {
            CacheItem cacheItem = createCacheItem();
            return cacheItem;
        }
    }

    private String getEmailTitle(String key) {
        return (String) getValue(emailTitle, key);
    }

    public String getEmailTitle(String serialId, String signInOrSignOut) {
        String PK = serialId + "-" + signInOrSignOut;
        String emailTitle = getEmailTitle(PK);
        if ("".equals(emailTitle)) {
            if ("1".equals(signInOrSignOut)) {
                emailTitle = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005295,weaver.general.ThreadVarLanguage.getLang())+"";
            } else {
                emailTitle = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005296,weaver.general.ThreadVarLanguage.getLang())+"";
            }
        }
        return emailTitle;
    }

    private String getEmailContent(String key) {
        return (String) getValue(emailContent, key);
    }

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