<?php

namespace app\admin\controller\ExaminationInformation;

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


class InfoArticleType extends BaseController
{
    /**
     * 获取资讯文章类型列表
     */
    public function getinfoArticleTypeList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery();

        $query = ModelinfoArticleType::where($con)
            ->field([
                'info_article_type_id',
                'info_article_type_guid',
                'info_article_type_name',
                'info_article_type_sort'
            ])
            ->order('info_article_type_sort', 'asc');

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

    /**
     * 编辑资讯文章类型
     */
    public function editinfoArticleType(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'info_article_type_name|资讯类型名称' => 'require',
                'info_article_type_sort|资讯类型排序' => 'require'
            ]);
            $model = ModelinfoArticleType::where('info_article_type_guid', $params['info_article_type_guid'])->find();


            if (!$model) throwErrorMsg("该资讯文章类型不存在", 1);
            $model->allowField([
                'info_article_type_update_user_guid',
                'info_article_type_name',
                'info_article_type_sort'
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加资讯文章类型
     */
    public function addinfoArticleType(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'info_article_type_name|资讯类型名称' => 'require',
                'info_article_type_sort|资讯类型排序' => 'require'
            ]);

            $model = ModelinfoArticleType::create($params, [
                'info_article_type_guid',
                'info_article_type_create_user_guid',
                'info_article_type_update_user_guid',
                'info_article_type_name',
                'info_article_type_sort'
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除资讯文章类型
     */
    public function deleteinfoArticleType(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'info_article_type_guid' => 'require',
            ]);
            $guids =  explode(',', $params['info_article_type_guid']);

            $info_article_type = ModelinfoArticleType::where([
                'info_article_type_guid' => explode(',', $params['info_article_type_guid'])
            ])->select();
            $info_article_type->delete();
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $params['limit'] = 10000;
        // if (isset($params['signup_guids']) && $params['signup_guids']) {
        //     $con['signup.signup_guid'] = explode(',', $params['signup_guids']);
        // }
        $select = self::getinfoArticleTypeList($request)['data'];
        return ModelinfoArticleType::exportExcel($select);
    }

    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = [
            array_values(ModelinfoArticleType::EXCELFIELD),
            ['招生新闻', '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_type');

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