<?php
declare (strict_types = 1);

namespace app\admin\model\v1;

use app\common\model\FaqBaseModel;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class FaqModel extends FaqBaseModel
{
    // 启用软删除
    use SoftDelete;
    // 软删除字段
    protected $deleteTime = 'deleted_at';

    // 根据语言查询
    public function scopeLanguage($query, $value)
    {
        $query->where('language_id', '=', $value);
    }

    // 根据问题搜索
    public function searchQuestionAttr($query, $value, $data)
    {
        if (is_null($value)) {
            return;
        }
        $query->where('question', 'like', "%$value%");
    }

    // 根据添加时间搜索
    public function searchCreatedAtAttr($query, $value, $data)
    {
        if (is_null($value)) {
            return;
        }
        if (is_array($value)) {
            if (count($value) > 1) {
                $query->whereBetweenTime('created_at', $value[0], $value[1]);
            } else {
                $query->whereTime('created_at', '>=', $value[0]);
            }
        }
    }
}
