<?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 think\facade\Db;
use think\facade\Env;


class Tdk extends BaseController
{
    /**
     * 获取网站tdk列表
     */
    public function getTdkList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        if (isset($params['tdk_type']) && $params['tdk_type']) {
            $con[] = ['tdk_type', '=', $params['tdk_type']];
        };
        if (isset($params['tdk_title']) && $params['tdk_title']) {
            $con[] = ['tdk_title', 'LIKE', '%' . $params['tdk_title'] . '%'];
        };

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

        $count = $query->count();

        return [
            'code' => 0,
            'data' => $select,
            'count' => $count,
            'msg' => 'ok'
        ];
    }

    /**
     * 编辑网站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 [
            'code' => 0,
            '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'
        ]);

        $type = ModelTdk::where('tdk_type',$params['tdk_type'])->find();
        if($type) throwErrorMsg("{$type->title} 已设置,请勿重复设置！");

        $model = ModelTdk::create($params, [
            'tdk_guid',
            'tdk_create_user_guid',
            'tdk_update_user_guid',
            'tdk_type',
            'tdk_title',
            'tdk_description',
            'tdk_keyword'
        ]);
        return [
            'code' => 0,
            '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 [
            'code' => 0,
            'msg' => "删除成功"
        ];
    }
}
