<?php
/**
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/2/11
 * Time: 下午3:38
 */
namespace JinDouYun\Model\Manage;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouyun\Dao\Manage\DArticle;
use Mall\Framework\Core\StatusCode;

class MArticle
{
    protected $objDArticle;


    public function __construct()
    {
        $this->objDArticle = new DArticle();
    }

    // 添加文章
    public function addArticle($params)
    {
        $dbResult = $this->objDArticle->insert($params);

        if($dbResult === false){
            return ResultWrapper::fail($this->objDArticle->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }

    // 修改文章删除状态
    public function deleteArticle($params)
    {
        $dbResult = $this->objDArticle->update(['deleteStatus'=>StatusCode::$delete], $params['id']);

        if($dbResult === false){
            return ResultWrapper::fail($this->objDArticle->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }

    // 获取指定频道下所有的文章
    public function getListByCategory($categoryId, $selectParams)
    {
        $conditon = [
            'deleteStatus' => StatusCode::$standard,
        ];

        if($categoryId){
            $conditon['categoryId'] = $categoryId;
        }

        $dbResult = $this->objDArticle->select($conditon, 'id,title,createTime,image,description,content', 'id desc', $selectParams['limit'],$selectParams['offset']);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDArticle->error(), ErrorCode::$dberror);
        }
        $total = $this->objDArticle->count($conditon);
        $returnData = [
            'data' => $dbResult,
            'total' => $total ? $total : 0,
        ];

        return ResultWrapper::success($returnData);
    }

    // 获取指定文章信息
    public function getArticleInfo($params)
    {
        $dbResult = $this->objDArticle->get($params);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDArticle->error(), ErrorCode::$dberror);
        } else {
            return ResultWrapper::success($dbResult);
        }
    }
    
    // 修改文章信息
    public function editArticle($params)
    {
        if (empty($params['id'])) {
            return ResultWrapper::fail('没有指定要修改的文章id', ErrorCode::$paramError);
        }

        $dbResult = $this->objDArticle->update($params, $params['id']);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDArticle->error(), ErrorCode::$dberror);
        } else {
            return ResultWrapper::success($dbResult);
        }
    }
}
