<?php
/**
 * 系统模版
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/29
 * Time: 15:38
 */

namespace JinDouYun\Controller\System;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\System\MSystemTemplate;

class SystemTemplate extends BaseController
{
    private $objMSystemTemplate;

    /**
     * SystemTemplate constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMSystemTemplate = new MSystemTemplate($this->onlineUserId, $this->onlineEnterpriseId);
    }

    /**
     * 添加,编辑系统模版
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'title' => isset($params['title']) ? $params['title'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $data['enableStatus'] = isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$delete;
        return $data;
    }

    /**
     * 添加系统模版
     * @throws \Exception
     */
    public function add()
    {
        $data = $this->commonFieldFilter();
        $result = $this->objMSystemTemplate->add($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 系统模版启用/停用
     */
    public function updateEnableStatus()
    {
        $params['id'] = $this->request->param('request_id');
        $params['enableStatus'] = $this->request->param('enableStatus');
        foreach ($params as $key => $value) {
            if (empty($value)) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMSystemTemplate->updateEnableStatus($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 获取模版列表
     */
    public function getAll()
    {
        $page = $this->request->param('page') ?: 1;
        $pageSize = $this->request->param('pageSize') ?: 10;
        $offset = ($page - 1) * $pageSize;
        $selectParams = [
            'limit'  => $pageSize,
            'offset' => $offset,
        ];
        $dbResult = $this->objMSystemTemplate->getAll($selectParams);
        if ($dbResult->isSuccess()) {
            $returnData = $dbResult->getData();
            $pageData = [
                'pageIndex' => $page,
                'pageSize'  => $pageSize,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($dbResult->getData(), ErrorCode::$dberror);
    }

    /**
     * 编辑系统模版
     */
    public function edit()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $data = $this->commonFieldFilter();
        $data['id'] = $id;
        $result = $this->objMSystemTemplate->edit($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 删除系统模版
     */
    public function del()
    {
        $id = $this->request->param('request_id');
        if (!$id) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        if (!is_array($id)) {
            $id = [$id];
        }
        $result = $this->objMSystemTemplate->del($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}