<?php
/**
 * 分类设置
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/
 * Time: 15:26
 */

namespace JinDouYun\Model\System;

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

use JinDouYun\Dao\SystemSettings\DSystem;
use Mall\Framework\Core\StatusCode;

class MClassSetting
{
    private $objDSystem;

    private $onlineUserId;

    private $onlineEnterpriseId;

    public function __construct($onlineUserId, $enterpriseId)
    {
        $this->onlineUserId = $onlineUserId;
        $this->onlineEnterpriseId = $enterpriseId;

        $this->objDSystem = new DSystem('default');
    }

    /**
     * 编辑,新增分类设置
     * @param $selectParams
     * @return ResultWrapper
     */
    public function setClassSetting($selectParams)
    {
        $dbResult = $this->objDSystem->get(['enterpriseId' => $selectParams['enterpriseId'], 'type' => StatusCode::$settingType['class']]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
        }
        if ($dbResult) {
            //修改
            $dbResult = $this->objDSystem->update(['content' => json_encode($selectParams['content']), 'updateTime' => time()], ['enterpriseId' => $selectParams['enterpriseId'], 'type' => StatusCode::$settingType['class']]);
            if ($dbResult === false) {
                return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
            }
        } else {
            //新增
            $insertData = [
                'enterpriseId' => $selectParams['enterpriseId'],
                'type'         => StatusCode::$settingType['class'],
                'content'      => json_encode($selectParams['content']),
                'createTime'   => time(),
                'updateTime'   => time(),
            ];
            $dbResult = $this->objDSystem->insert($insertData);
            if ($dbResult === false) {
                return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
            }
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 获取分类设置
     * @param $selectParams
     * @return bool|mixed
     */
    public function getClassSettingInfo($selectParams)
    {
        $dbResult = $this->objDSystem->get(['enterpriseId' => $selectParams['enterpriseId'], 'type' => StatusCode::$settingType['class']]);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
        }
        $dbResult && $dbResult['content'] = json_decode($dbResult['content'], true);
        if (isset($dbResult['content'])) {
            return ResultWrapper::success($dbResult['content']);
        }
        return ResultWrapper::success([]);
    }
}