<?php

namespace app\admin\controller\ExaminationInformation;

use app\BaseController;
use app\common\model\ExaminationInformation\InfoArticle as ModelinfoArticle;
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 app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class InfoArticle extends BaseController
{
    /**
     * 获取资讯文章列表
     */
    public function getinfoArticleList(Request $request, $isExport = false): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['info_article_title', 'LIKE'], ['a.info_article_type_guid', '='], ['info_article_status', '='],);

        $query = ModelinfoArticle::where($con)
            ->field([
                'a.info_article_id',
                'a.info_article_guid',
                'a.info_article_type_guid',
                'b.info_article_type_name',
                'a.info_article_title',
                'a.info_article_cover',
                'a.info_article_content',
                'a.info_article_order',
                'a.info_article_status',

            ])
            ->alias('a')
            ->leftjoin('info_article_type b', 'a.info_article_type_guid = b.info_article_type_guid')
            ->order('info_article_order', 'asc');

        return $isExport ? $query->select()->toArray() : msg("获取资讯文章列表成功!", $query);
    }

    /**
     * 编辑资讯文章
     */
    public function editinfoArticle(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'info_article_type_guid|文章类型' => 'require',
            'info_article_title|文章标题' => 'require',
            'info_article_cover|文章封面' => 'require',
            'info_article_content|文章内容' => 'require',
            'info_article_order|文章排序' => 'require',
        ]);
        $model = ModelinfoArticle::where('info_article_guid', $params['info_article_guid'])->find();

        if (!$model) throwErrorMsg("该资讯文章不存在", 1);
        $model->allowField([
            'info_article_update_user_guid',
            'info_article_title',
            'info_article_type_guid',
            'info_article_cover',
            'info_article_content',
            'info_article_order',
            'info_article_status',
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加资讯文章
     */
    public function addinfoArticle(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'info_article_type_guid|文章类型' => 'require',
            'info_article_title|文章标题' => 'require',
            'info_article_cover|文章封面' => 'require',
            'info_article_content|文章内容' => 'require',
            'info_article_order|文章排序' => 'require'
        ]);

        $model = ModelinfoArticle::create($params, [
            'info_article_guid',
            'info_article_create_user_guid',
            'info_article_update_user_guid',
            'info_article_type_guid',
            'info_article_title',
            'info_article_cover',
            'info_article_content',
            'info_article_order',
            'info_article_status',
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除资讯文章
     */
    public function deleteinfoArticle(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'info_article_guid' => 'require',
        ]);
        $info_article = ModelinfoArticle::where([
            'info_article_guid' => explode(',', $params['info_article_guid'])
        ])->select();

        $info_article->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        ModelinfoArticle::exportExcel(self::getinfoArticleList($request, true));
    }

    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = [
            array_values(ModelinfoArticle::EXCELFIELD),
            [
                '招生新闻',
                '【2023届厚德开展】艺术与灵感的写生之旅',
                'http://houde_web.aerwen.net/uploads/EnrolIntroImg/20230429/466799fb9c916c3562a9c311de272c81.jpg',
                '1',
                "展示",
            ]
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('资讯文章导入模板.xlsx');
    }

    /**
     * 导入excel
     */
    public function importExcel(Request $request)
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('info_article');

        $msg = ModelinfoArticle::importExcel($file);
        return [
            'code' => 0,
            'msg' => $msg
        ];
    }
}
