<?php
/**
 * 官网文章管理模块
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/2/11
 * Time: 3:00 PM
 */
namespace JinDouYun\Controller\Manage;

use Mall\Framework\Core\ErrorCode;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Manage\MArticle;

class Article extends BaseController
{
    private $objMArticle;

    public function __construct()
    {
        parent::__construct(false, false, false);
        $this->objMArticle = new MArticle();
    }

    // 公共字段
    public function commonFieldFilter()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'title'   => isset($paramsData['title']) ? $paramsData['title'] : '',
            'content'  => isset($paramsData['content']) ? htmlspecialchars_decode($paramsData['content'], ENT_QUOTES) : '',
            'image'  => isset($paramsData['image']) ? $paramsData['image'] : '',
            'description'  => isset($paramsData['description']) ? $paramsData['description'] : '',
            'categoryId'=> isset($paramsData['categoryId']) ? $paramsData['categoryId'] : '',
            'createTime' => time(),
            'updateTime' => time(),
        ];

        foreach ($params as $k => $v) {
            if (empty($v) && $v !== 0) {
                parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }

        return $params;
    }

    // 添加文章
    public function addArticle()
    {
        $artileData = self::commonFieldFilter();

        $result = $this->objMArticle->addArticle($artileData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    // 删除文章
    public function deleteArticle()
    {
        $params['id']  = $this->request->param('request_id');
        if ( !$params['id'] ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }

        $result = $this->objMArticle->deleteArticle($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    // 获取指定文章信息
    public function getArticleInfo()
    {
        $params['id']  = $this->request->param('request_id');
        if ( !$params['id'] ) {
            $this->sendOutput('参数错误', ErrorCode::$paramError );
        }
        $result = $this->objMArticle->getArticleInfo($params);

        if($result->isSuccess()){
            $this->sendOutput($result->getData());
        }else{
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    // 编辑频道
    public function editArticle()
    {
        $artileId  = $this->request->param('request_id');
        if(empty($artileId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $artileData = self::commonFieldFilter();
        unset($artileData['createTime']);
        $artileData['id'] = $artileId;

        $result = $this->objMArticle->editArticle($artileData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    // 获取指定频道下所有文章标题
    public function getListByCategory()
    {
        $categoryId  = $this->request->param('request_id');
        $params = $this->request->getRawJson();
        $data = [
            'page' => isset($params['page']) ? $params['page'] : 1,
            'pageSize' => isset($params['pageSize']) ? $params['pageSize'] : 4,
        ];

        $selectParams = [
            'offset' => ($data['page'] - 1) * $data['pageSize'],
            'limit' => $data['pageSize'],
        ];
        $result = $this->objMArticle->getListByCategory($categoryId, $selectParams);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}