<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\GuessAsk as GuessAskModel;
use app\admin\model\GuessAskLanguages;
use app\admin\validate\GuessAskValidate;
use Exception;
use think\facade\Cache;

/**
 * 猜你想问管理
 */
class GuessAsk extends BaseController
{

    /**
     * @api {get} /guessask/list 列表
     */
    function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $language_code = $params['language_code'] ?? $this->lang;
            $query = GuessAskLanguages::where('language_code', $language_code); 
            if (!empty($params['name'])) {
                $query = $query->where('name', 'like', '%'.$params['name'].'%');
            }
            if (isset($params['type']) && $params['type'] != '') {
                $query = $query->where('type', $params['type']);
            }
            if (isset($params['is_rec']) && $params['is_rec'] != '') {
                $query = $query->where('is_rec', $params['is_rec']);
            }
            if (isset($params['is_top']) && $params['is_top'] != '') {
                $query = $query->where('is_top', $params['is_top']);
            }
            if (isset($params['status']) && $params['status'] != '') {
                $query = $query->where('status', $params['status']);
            }
            $count = $query->count();
            $list = $query->order('is_top','desc')
                        ->order('click_num','desc')
                        ->limit($limit)
                        ->page($page)
                        ->select();
            $typeList = GuessAskLanguages::typeList($language_code,false);
            foreach ($list as &$item) {
                $item->type_text = $typeList[$item->type] ?? '';
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        $typeList = GuessAskLanguages::typeList($language_code);
        return $this->success(['typeList' => $typeList, 'count' => $count, 'list' => $list]);
    }

    //新增/编辑
    public function update()
    { 
        try {
            $params = (new GuessAskValidate())->post()->goCheck('edit');
            $id = $this->request->param('id',0);
            $status = $params['status'] ?? 1;
            $guess_ask_id = $params['guess_ask_id'] ?? 0;
            $language_code = $params['language_code'] ?? $this->lang;

            $exists = GuessAskLanguages::where('name', $params['name'])->where('language_code', $language_code)->find();
            if ($exists ) {
                if (!$id || $id != $exists->id) {
                    return $this->error('关键词已存在');
                }
            }
            if (!$guess_ask_id) {
                $guess_ask = GuessAskModel::create([
                    'created_at' => date('Y-m-d H:i:s'),
                ]);
                $guess_ask_id = $guess_ask->id;
            }
            if (empty($id)) {
                $guessAskLanguages = new GuessAskLanguages();
            } else {
                $guessAskLanguages = GuessAskLanguages::where('id', $id)->find();
                if (!$guessAskLanguages) {
                    return $this->error('关键词不存在');
                }
            }
            $guessAskLanguages->guess_ask_id = $guess_ask_id;
            $guessAskLanguages->name = $params['name'];
            $guessAskLanguages->type = $params['type'];
            $guessAskLanguages->weight = $params['weight'];
            $guessAskLanguages->status = $status;
            $guessAskLanguages->language_code = $language_code;
            $guessAskLanguages->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        
        //删除缓存
        Cache::delete('guessaskList');
        return $this->success([], '操作成功');
    }

    //删除
    public function delete()
    {
        try {
            $params = (new GuessAskValidate())->goCheck('id');
            $info = GuessAskLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('猜你想问不存在');
            }
            $guess_ask_id = $info->guess_ask_id;
            $info->delete();
            if (GuessAskLanguages::where('guess_ask_id', $guess_ask_id)->count() == 0) {
                GuessAskModel::where('id', $guess_ask_id)->delete();    
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        //删除缓存
        Cache::delete('guessaskList');
        return $this->success([],'删除成功');
    }
    
    //设置状态、是否推荐、是否置顶
    public function setStatus()
    {
        try {
            $params = (new GuessAskValidate())->goCheck('status');
            $info = GuessAskLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('猜你想问不存在');
            }
            $field = $params['field'];
            $value = $info->$field == 0 ? 1 : 0;
            if ($field == 'is_top' && $value == 1) {
                GuessAskLanguages::where('is_top', 1)->update(['is_top' => 0]);
            }

            $info->$field = $value;
            $info->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        //删除缓存
        Cache::delete('guessaskList');
        return $this->success([], '操作成功');
    }

}