<?php
/**
 * 案例管理Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/03/28
 * Time: 16:09
 */

namespace JinDouYun\Controller\Manage;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Manage\MCase;

class Example extends BaseController
{
    private $objMCase;

    public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
        $this->objMCase = new MCase();
    }

    /**
     * 案例添加
     */
    public function addCase()
    {
        $params = $this->request->getRawJson();
        $data = [
            'title' => isset($params['title']) ? $params['title'] : '',
            'thumbnail' => isset($params['thumbnail']) ? $params['thumbnail'] : '',
            'caseCategoryId' => isset($params['caseCategoryId']) ? $params['caseCategoryId'] : '',
            'slogan' => isset($params['slogan']) ? $params['slogan'] : '',
            'time' => isset($params['time']) ? $params['time'] : '',
            'content' => isset($params['content']) ? $params['content'] : '',
            'createTime' => time(),
            'updateTime' => time(),
        ];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        $data['clicks'] = 0;
        $modelResult = $this->objMCase->addCase($data);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 案例列表
     * @throws \Exception
     */
    public function getAllCase()
    {
        $params = $this->request->getRawJson();

        $data = [
            'page' => isset($params['page']) ? $params['page'] : 1,
            'pageSize' => isset($params['pageSize']) ? $params['pageSize'] : 10,
        ];

        $selectParams = [
            'offset' => ($data['page'] - 1) * $data['pageSize'],
            'limit' => $data['pageSize'],
        ];
        $result = $this->objMCase->getAllCase($selectParams);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 增加点击量
     */
    public function addCaseClicks()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMCase->addCaseClicks($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 案例详情
     */
    public function caseInfo()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMCase->caseInfo($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }
    /**
     * 案例编辑
     */
    public function updateCase()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = $this->request->getRawJson();
        $data = [
            'title' => isset($params['title']) ? $params['title'] : '',
            'thumbnail' => isset($params['thumbnail']) ? $params['thumbnail'] : '',
            'caseCategoryId' => isset($params['caseCategoryId']) ? $params['caseCategoryId'] : '',
            'time' => isset($params['time']) ? $params['time'] : '',
            'slogan' => isset($params['slogan']) ? $params['slogan'] : '',
            'content' => isset($params['content']) ? $params['content'] : '',
            'updateTime' => time(),
        ];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        $modelResult = $this->objMCase->updateCase($data, $id);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 案例删除
     */
    public function delCase()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $modelResult = $this->objMCase->delCase($id);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

}