<?php

namespace app\api\controller\News;

use app\BaseController;
use app\common\model\News\News as ModelNews;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use think\facade\Db;
use app\common\exception\Tool;
use think\facade\Env;


class News extends BaseController
{
    /**
     * 获取新闻列表
     */
    public function getNewsList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['news_type', '='],);

        $query = ModelNews::where($con)
            ->field([
                'news_id',
                'news_guid',
                'news_title',
                'news_intro',
                'news_type',
                'news_img',
                'news_create_time',
            ])
                ->append(['news_created_time'])
                ->hidden(['news_create_time'])
            // ->append('news_create_time')
            ->order('news_create_time', 'desc');



        return msg("获取新闻列表成功!", $query);
    }

    /**
     * 获取新闻详情
     */
    public function getNewsInfo(Request $request): array
    {
        $params = $request->param();

        $this->validate($params, ['news_id' => 'require']);

        $find = ModelNews::field([
            'news_id',
            'news_guid',
            'news_title',
            'news_author',
            'news_intro',
            'news_type',
            'news_img',
            'news_content',
            'news_num',
            'news_create_time'
        ])
            ->where('news_id', $params['news_id'])
            ->find();
        if (!$find) throwErrorMsg('该新闻不存在！');

        $last_next_id = Tool::getLastNextId(
            new ModelNews,
            ['news_id', $find->news_id],
            'all',
            [['news_type', '=', $find->news_type]]
        );

        return msg(0, '获取新闻详情成功!', [
            'data' => $find,
            'last_id' => $last_next_id[0],
            'next_id' => $last_next_id[1],
        ]);
    }
}
