<?php

namespace app\admin\controller\Tdk;

use app\BaseController;
use app\common\model\Tdk\Tdk as ModelTdk;
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 Tdk extends BaseController
{
    /**
     * 获取网站tdk列表
     */
    public function getTdkList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['tdk_type', '='], ['tdk_title', 'LIKE'],);

        $query = ModelTdk::where($con)
            ->field([
                'tdk_id',
                'tdk_guid',
                'tdk_type',
                'tdk_title',
                'tdk_description',
                'tdk_keyword'
            ])
            ->order('tdk_update_time', 'desc');

        return msg("获取网站tdk列表成功!", $query);
    }

    /**
     * 编辑网站tdk
     */
    public function editTdk(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'tdk_type|tdk所属模块' => 'require',
            'tdk_title|网页标题' => 'require',
            'tdk_description|网页简介' => 'require',
            'tdk_keyword|网页关键词' => 'require'
        ]);
        $model = ModelTdk::where('tdk_guid', $params['tdk_guid'])->find();
        if (!$model) throwErrorMsg("该网站tdk不存在", 1);
        $model->allowField([
            'tdk_update_user_guid',
            'tdk_type',
            'tdk_title',
            'tdk_description',
            'tdk_keyword'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加网站tdk
     */
    public function addTdk(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'tdk_type|tdk所属模块' => 'require',
            'tdk_title|网页标题' => 'require',
            'tdk_description|网页简介' => 'require',
            'tdk_keyword|网页关键词' => 'require'
        ]);
        $model = ModelTdk::create($params, [
            'tdk_guid',
            'tdk_create_user_guid',
            'tdk_update_user_guid',
            'tdk_type',
            'tdk_title',
            'tdk_description',
            'tdk_keyword'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除网站tdk
     */
    public function deleteTdk(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'tdk_guid' => 'require',
        ]);
        $tdk = ModelTdk::where([
            'tdk_guid' => explode(',', $params['tdk_guid'])
        ])->select();
        $tdk->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelTdk::field([
            'tdk_type',
            'tdk_title',
            'tdk_description',
            'tdk_keyword'
        ])
            ->order('tdk_update_time', 'desc')
            ->select();
        return ModelTdk::exportExcel($select);
    }

    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = array_values(ModelTdk::EXCELFIELD);
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('网站tdk导入模板.xlsx');
    }

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

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