<?php
/**
 * 系统设置控制器
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/11/19
 * Time: 16:00
 */

namespace JinDouYun\Controller\SystemSettings;

use Mall\Framework\Core\ErrorCode;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\SystemSettings\MSystem;

/**
 *  type
 *  1小程序设置
 *  2模板消息设置
 *  3分类模板设置
 *  4字节小程序设置
 * Class System
 * @package JinDouYun\Controller\SystemSettings
 */
class System extends BaseController
{
    private $objMSystem;

    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
        $this->objMSystem = new MSystem($this->onlineEnterpriseId);
    }

    /**
     * 修改系统设置
     */
    public function updateSystemSettings()
    {
        $id = $this->request->param('request_id');
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $updateData = [
            'enterpriseId' => $this->onlineEnterpriseId,
            'type'         => isset($params['type']) ? $params['type'] : '',
            'content'      => isset($params['content']) ? $params['content'] : '',
        ];
        foreach ($updateData as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        if (!isset($updateData['content']['originalID'])) {
            parent::sendOutput('originalId参数错误', ErrorCode::$paramError);
        }
        $id && $updateData['id'] = $id;
        !$id && $updateData['createTime'] = time();
        $updateData['updateTime'] = time();
        $content = $updateData['content'];
        $updateData['content'] = json_encode($updateData['content']);
        $modelResult = $this->objMSystem->updateSystemSettings($updateData, $content['originalID']);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 查询系统设置
     */
    public function getSystemSettingsInfo()
    {
        $type = $this->request->param('request_id');
        if (empty($type)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $modelResult = $this->objMSystem->getSystemSettingsInfo($type);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 获取预授权码(pre_auth_code)
     * 官网文档地址: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Authorization_Process_Technical_Description.html
     */
    public function preAuthCode()
    {
        $modelResult = $this->objMSystem->preAuthCode();
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * type 4 字节跳动设置
     * 保存字节跳动设置
     * {"type":4,"content":{"wxpay":{"micro_appid":"ttappid","micro_app_secret":"microappSecret","appid":"appid","merchant_id":"merchant_id","app_secret":"app_secret"},"alipay":{}}}
     *
     */
    public function saveByteDanceSetting()
    {
        $id = $this->request->param('request_id');
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'enterpriseId' => $this->onlineEnterpriseId,
            'type'         => isset($params['type']) ? $params['type'] : '',//4
            'content'      => isset($params['content']) ? $params['content'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        if (!empty($id)) $data['id'] = $id;
        $data['content']  = json_encode($data['content']);
        $modelResult = $this->objMSystem->saveByteDanceSetting($data);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }
}