DailytaskConfig.class.php 2.55 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 2017/7/27
 * Time: 11:21
 */
namespace Common\Common;

use Com\Cache as ComCache;
use Common\Service\DailytaskService;

class DailytaskConfig extends ComCache
{
    /**
     * 日常任务所有应用
     */
    private $_apps = [Constant::APP_SIGN, Constant::APP_WORKMATE, Constant::APP_ANSWER];

    /**
     * 实例化
     * @return DailytaskConfig
     */
    public static function &instance()
    {
        static $instance;

        if (empty($instance)) {
            $instance = new self();
        }

        return $instance;
    }

    /**
     * 获取应用设置
     * @param string $app 应用
     * @return array
     */
    private function _getConfig($app)
    {
        if (empty($app) || !in_array($app, $this->_apps)) {
            return [];
        }

        $taskServ = new DailytaskService();
        $config = $taskServ->get_by_conds(['app' => $app]);

        if (empty($config)) {
            return [
                'dailytask_id' => 0,
                'app' => $app,
                'rules' => [],
                'is_open' => Constant::DAILYTASK_IS_OPEN_FALSE,
                'update_time' => 0,
            ];
        }

        $config['rules'] = empty($config['rules']) ? [] : unserialize($config['rules']);
        return $config;
    }

    /**
     * 获取签到设置
     * @return array
     */
    public function signConfig()
    {
        return $this->_getConfig(Constant::APP_SIGN);
    }

    /**
     * 获取同事圈设置
     * @return array
     */
    public function workmateConfig()
    {
        return $this->_getConfig(Constant::APP_WORKMATE);
    }

    /**
     * 获取问答设置
     * @return array
     */
    public function answerConfig()
    {
        return $this->_getConfig(Constant::APP_ANSWER);
    }

    /**
     * 获取缓存数据
     * @param string $app 应用
     * @return array
     */
    public function getCacheData($app)
    {
        if (empty($app) || !in_array($app, $this->_apps)) {
            return [];
        }
        
        return $this->get("Common.{$app}Config");
    }

    /**
     * 清除缓存数据
     * @param string $app 应用(默认清除所有应用缓存数据)
     * @return bool
     */
    public function clearCacheData($app = '')
    {
        if (empty($app) || !in_array($app, $this->_apps)) {
            foreach ($this->_apps as $v) {
                $this->get("Common.{$v}Config", null);
            }

            return true;
        }

        return $this->get("Common.{$app}Config", null);
    }
}