<?php

namespace app\api\controller\ExaminationInformation;

use app\BaseController;
use app\common\model\ExaminationInformation\InfoArticle as ModelinfoArticle;
use app\common\model\ExaminationInformation\InfoArticleType as ModelinfoArticleType;
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 InfoArticle extends BaseController
{
    /**
     * 获取资讯文章列表
     */
    public function getinfoArticleList(Request $request)
    {
        $params = $request->param();
        $con = [];

        $this->validate($params, [
            'idx|文章类型id' => 'require',
        ]);

        $type = ModelinfoArticleType::
            field([
                'info_article_type_id',
                'info_article_type_name' => 'name',
            ])
            ->order('info_article_type_sort', 'asc')
            ->select();

        $params['info_article_type_id'] = null;
        foreach ($type as $key => $value) {
            if($key == $params['idx']){
                $params['info_article_type_id'] = $value['info_article_type_id'];
            }
        }

        if(!$params['info_article_type_id']) throwErrorMsg("传值不正确！");


        $query = ModelinfoArticle::where('b.info_article_type_id',$params['info_article_type_id'])
            ->field([
                'a.info_article_id',
                'a.info_article_type_guid',
                'b.info_article_type_name',
                'a.info_article_title',
                'a.info_article_create_time',
                'a.info_article_cover',
            ])
            ->alias('a')
            ->leftjoin('info_article_type b', 'a.info_article_type_guid = b.info_article_type_guid')
            ->hidden(['info_article_type_guid'])
            ->order('info_article_order', 'asc');

        return msg("获取资讯文章列表成功!", $query);
    }

    /**
     * 获取资讯文章详情
     */
    public function getinfoArticleInfo(Request $request): array
    {
        $params = $request->param();

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

        $find = ModelinfoArticle::field([
            'info_article_id',
            'info_article_title',
            'info_article_content',
            'info_article_create_time',
        ])
            ->where('info_article_id', $params['info_article_id'])
            ->find();

        return msg(0, '获取资讯文章详情成功!', ['data' => $find]);
    }
}
