<?php
/**
 * Created by PhpStorm.
 * User: ywl
 * Date: 2017/4/14
 * Time: 11:38
 */

namespace App\Web\Services;

use App\Exceptions\ApiException;
use App\Services\CommonUserBaseService;
use App\Web\Models\SysGlobalConfigModel;
use Illuminate\Support\Facades\Cache;


class GlobalConfigService extends CommonUserBaseService
{
    protected $cache = true;

    protected $cacheBucket = 'GlobalConfig:';





    /**
     * WebService construct
     * @param SysGlobalConfigModel $model
     * */
    public function __construct(SysGlobalConfigModel $model)
    {
        $this->model = $model;
    }


    /**
     * 获取全局配置列表
     * */
    public function getGlobalConfig($params){
        $data=$this->getCacheGlobalConfigData();
        if(empty($data)){
            $list= $this->model->getGlobalConfigList($params);
            if(!empty($list)){
                $data=[];
                foreach ($list as $value){
                    $value['value']=json_decode($value['value'],true);
                    $data[]=$value;
                }
                $this->setGlobalConfigCache($data);
            }
        }
       return $data;
    }

    /**
     * 文章全局配置数据保存
     * */
    public function saveGlobalConfigData($params){
        $saveData=[];
        if(!empty($params['id'])){
            $saveData['id']=$params['id'];
            if(isset($params['global_key'])){
                $globalKeyUnique =$this->model->checkGlobalKeyUnique($params['global_key'],$params['id']);
                if (!$globalKeyUnique) {
                    throw new ApiException(10018, ['name' => $params['global_key']]);
                }
                $saveData['global_key']=$params['global_key'];
            }
            if(isset($params['label'])){
                $saveData['label']=empty($params['label'])?'':$params['label'];
            }
            if(isset($params['value'])){
                $saveData['value']=empty($params['value'])?'':json_encode($params['value']);
            }
        }else{
            if(isset($params['global_key'])){
                $globalKeyUnique =$this->model->checkGlobalKeyUnique($params['global_key']);
                if (!$globalKeyUnique) {
                    throw new ApiException(10018, ['name' => $params['global_key']]);
                }
                $saveData['global_key']=$params['global_key'];
            }
            $saveData['label']=empty($params['label'])?'':$params['label'];
            $saveData['value']=empty($params['value'])?'':json_encode($params['value']);
        }
        $id= $this->model->saveGlobalConfigData($saveData);
        $this->clearGlobalConfigCache();
        return $id;
    }

    /**
     * 获取全局配置列表
     * */
    public function getGlobalConfigInfo($params){
        $info= $this->model->getGlobalConfigInfo($params);
        return $info;
    }

    /**
     * 获取页面详情
     * @return array
     */
    public function getCacheGlobalConfigData()
    {
        $key = $this->getCacheGlobalConfigKey();
        return Cache::get($key);
    }

    /**
     * 获取广告缓存key
     * @param $urla
     * @return string
     */
    private function getCacheGlobalConfigKey()
    {
        $pageParamsStr = 'web-global-config-list:' . md5('web-global-config-list');
        return $this->cacheBucket . $pageParamsStr;
    }

    /**
     * 缓存广告数据
     * @param array $GlobalConfigData
     */
    private function setGlobalConfigCache($GlobalConfigData)
    {
        Cache::put($this->getCacheGlobalConfigKey(), $GlobalConfigData, config('cache.page_time'));
    }

    /**
     * 清除广告数据缓存
     * */
    public function clearGlobalConfigCache()
    {
        $this->removeByKey('web-global-config-list');
    }


}
