<?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\MCategory;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

class Category extends BaseController
{
    private $objMCategory;

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

    // 公共字段
    public function commonFieldFilter()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'categoryName'   => isset($paramsData['categoryName']) ? $paramsData['categoryName'] : '',
            'sort'  => isset($paramsData['sort']) ? $paramsData['sort'] : 1,
            'enableStatus' => isset($paramsData['enableStatus']) ? $paramsData['enableStatus'] : 5,
        ];

        foreach ($params as $k => $v) {
            if (empty($v) && $v !== 0) {
                parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }
        $params['type'] = isset($paramsData['type']) ? $paramsData['type'] : StatusCode::$delete;

        $params['description'] = isset($paramsData['description']) ? $paramsData['description'] : '';

        return $params;
    }

    // 添加频道
    public function addCategory()
    {
        $categoryData = self::commonFieldFilter();

        $result = $this->objMCategory->addCategory($categoryData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    // 修改频道启用/停用状态
    public function updateEnbaleStatus()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'id'   => isset($paramsData['id']) ? $paramsData['id'] : '',
            'enableStatus'  => isset($paramsData['enableStatus']) ? $paramsData['enableStatus'] : '',
        ];

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

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



    // 删除频道
    public function delCategory()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMCategory->delCategory($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

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

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

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

        $result = $this->objMCategory->editCategory($categoryData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    // 获取所有频道信息
    public function getAllCategory()
    {
        $params = $this->request->getRawJson();
        $type = isset($params['type']) ? $params['type'] : StatusCode::$delete;
        $result = $this->objMCategory->getAllCategory($type);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    //获取频道和文章信息
    public function getCategoryAndArticle()
    {
        $params = $this->request->getRawJson();
        $data = [
            'categoryPage' => isset($params['categoryPage']) ? $params['categoryPage'] : 1,
            'categoryPageSize' => isset($params['categoryPageSize']) ? $params['categoryPageSize'] : 4,
            'articlePage' => isset($params['articlePage']) ? $params['articlePage'] : 1,
            'articlePageSize' => isset($params['articlePageSize']) ? $params['articlePageSize'] : 4,
        ];

        $selectParams = [
            'categoryOffset' => ($data['categoryPage'] - 1) * $data['categoryPageSize'],
            'categoryLimit' => $data['categoryPageSize'],
            'articleOffset' => ($data['articlePage'] - 1) * $data['articlePageSize'],
            'articleLimit' => $data['articlePageSize'],
        ];

        $modelResult = $this->objMCategory->getCategoryAndArticle($selectParams);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }
}