<?php
/**
 * 素材库Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2020/04/27
 * Time: 14:20
 */

namespace JinDouYun\Controller\Material;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Material\MMaterial;

class Material extends BaseController
{
    private $objMMaterial;

    public function __construct($isCheckAcl = false, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMMaterial = new MMaterial($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 素材内容添加
     * @throws Exception
     */
    public function addMaterialContent()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'name' => isset($params['name']) ? $params['name'] : '',
            'content' => isset($params['content']) ? $params['content'] : '',
        ];

        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $this->shopId && $data['shopId'] = $this->shopId;

        $data['categoryId'] = isset($params['categoryId']) ? $params['categoryId'] : 0;

        $result = $this->objMMaterial->addMaterialContent($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 素材分类添加
     * @throws Exception
     */
    public function addMaterialCategory()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'title' => isset($params['title']) ? $params['title'] : '',
        ];

        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $data['pid'] = isset($params['pid']) ? $params['pid'] : 0;

        $this->shopId && $data['shopId'] = $this->shopId;

        $result = $this->objMMaterial->addMaterialCategory($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 素材内容编辑
     * @throws Exception
     */
    public function updateMaterialContent()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : '',
            'id' => isset($params['id']) ? $params['id'] : '',
        ];

        $result = $this->objMMaterial->updateMaterialContent($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 素材分类编辑
     * @throws Exception
     */
    public function updateMaterialCategory()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'title' => isset($params['title']) ? $params['title'] : '',
            'id' => isset($params['id']) ? $params['id'] : '',
        ];

        $result = $this->objMMaterial->updateMaterialCategory($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 素材内容删除
     */
    public function delMaterialContent()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        if (!isset($params['id']) || empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

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

    /**
     * 素材分类删除
     */
    public function delMaterialCategory()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMMaterial->delMaterialCategory($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 素材分类列表
     */
    public function getAllMaterialCategory()
    {
        $params = [];
        $this->shopId && $params['shopId'] = $this->shopId;
        $result = $this->objMMaterial->getAllMaterialCategory($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 素材内容列表
     */
    public function getAllMaterialContent()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        $selectParams['categoryId'] = isset($params['categoryId']) ? $params['categoryId'] : 0;
        $selectParams['name'] = isset($params['name']) ? $params['name'] : '';

        $this->shopId && $selectParams['shopId'] = $this->shopId;

        $result = $this->objMMaterial->getAllMaterialContent($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

}