<?php
namespace app\api\controller\publics;

use app\models\article\Article;
use app\models\article\ArticleCategory;
use app\Request;
use crmeb\services\UtilService;

/**
 * 文章类
 * Class ArticleController
 * @package app\api\controller\publics
 */
class ArticleController
{
    /**
     * @api {get} /article/list/:cid 文章列表
     * @apiName GetArticleList
     * @apiGroup Article
     * 
     */
    public function lst(Request $request, $cid)
    {
        list($page, $limit) = UtilService::getMore([
            ['page',1],
            ['limit',10],
        ],$request, true);
        $list = Article::cidByArticleList($cid,$page,$limit,"id,title,image_input,visit,from_unixtime(add_time,'%Y-%m-%d %H:%i') as add_time,synopsis,url") ?? [];
        if(is_object($list)) $list = $list->toArray();
        return app('json')->successful($list);
    }
    /**
     * @api {get} /article/details/:id 文章详情
     * @apiName GetArticleDetails
     * @apiGroup Article
     * 
     */
   public function details($id)
   {
       $content = Article::getArticleOne($id);
       if(!$content) return app('json')->fail('此文章已经不存在!');
       $content["visit"] = $content["visit"] + 1;
       $content["cart_name"] = ArticleCategory::getArticleCategoryField($content['cid']);
       $content['add_time'] = date('m月d日',$content['add_time']);
       Article::edit(['visit'=>$content["visit"]],$id);//增加浏览次数
       return app('json')->successful($content);
   }

    /**
     * @api {get} /article/hot/list 文章 热门
     * @apiName GetArticleHotList
     * @apiGroup Article
     * 
     */
    public function hot()
    {
        $list = Article::getArticleListHot("id,title,image_input,visit,from_unixtime(add_time,'%Y-%m-%d %H:%i') as add_time,synopsis,url") ?? [];
        if(is_object($list)) $list = $list->toArray();
        return app('json')->successful($list);
    }

    /**
     * @api {get} /article/banner/list 文章 banner
     * @apiName GetArticleBannerList
     * @apiGroup Article
     * 
     */
    public function banner()
    {
        $list = Article::getArticleListBanner("id,title,image_input,visit,from_unixtime(add_time,'%Y-%m-%d %H:%i') as add_time,synopsis,url") ?? [];
        if(is_object($list)) $list = $list->toArray();
        return app('json')->successful($list);
    }
}