<?php
/**
 * 用户常用应用Model
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/01/11
 * Time: 09:53
 */

namespace JinDouYun\Model\SystemSettings;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Cache\CacheMan;

use JinDouYun\Dao\SystemSettings\DCommonApp;

class MCommonApp
{

    // 授权回调地址
    private $objDCommonApp;
    private $objCacheMan;
    private $cacheKey = 'CommonApp';
    private $userCenterId;
    private $enterpriseId;

    public function __construct($enterpriseId, $userCenterId)
    {
        $this->enterpriseId = $enterpriseId;
        $this->userCenterId = $userCenterId;
        $this->objDCommonApp = new DCommonApp('default');
        //按照企业分表
        $this->objDCommonApp->setTable($this->objDCommonApp->get_Table().'_'.$this->enterpriseId);
        //引入redis
        $this->objCacheMan = new CacheMan($this->enterpriseId, $this->userCenterId);
    }

    /**
     * 修改常用应用
     * @param $params
     * @param $where
     * @return ResultWrapper
     */
    public function updateCommonApp($params, $where)
    {
        $dbResult = $this->objDCommonApp->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCommonApp->error(), ErrorCode::$dberror);
        }
        $commonData = $dbResult;
        unset($dbResult);
        if($commonData){
            //修改
            $updateData = [
                'content' => json_encode($params),
                'updateTime' => time(),
            ];
            $dbResult = $this->objDCommonApp->update($updateData, $where);
            //删除redis
            $this->objCacheMan->delCacheHash($where['userCenterId'], $this->cacheKey);
        }else{
            //新增
            $addCommonData = [
                'userCenterId' => $where['userCenterId'],
                'enterpriseId' => $where['enterpriseId'],
                'content' => json_encode($params),
                'updateTime' => time(),
                'createTime' => time(),
            ];
            $dbResult = $this->objDCommonApp->insert($addCommonData);
            //增加redis
            $this->objCacheMan->addCacheHash($where['userCenterId'], $addCommonData['content'], $this->cacheKey);
        }
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCommonApp->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 常用应用详情
     * @param $where
     * @return ResultWrapper
     */
    public function getCommonAppInfo($where)
    {
        //获取redis
        $cacheResult = $this->objCacheMan->getCacheHash($where['userCenterId'], $this->cacheKey);
        if(!empty($cacheResult)){
            return ResultWrapper::success($cacheResult);
        }
        $dbResult = $this->objDCommonApp->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDCommonApp->error(), ErrorCode::$dberror);
        }
        if(empty($dbResult)){
            return ResultWrapper::success($dbResult);
        }
        $dbResult['content'] = json_decode($dbResult['content'], true);
        //保存redis
        if($dbResult){
            $this->objCacheMan->addCacheHash($where['userCenterId'], $dbResult['content'], $this->cacheKey);
        }
        return ResultWrapper::success($dbResult['content']);
    }
}