<?php
namespace app\model;

use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Paginator;

/**
 * 文章标签记录
 * 当前只收录【客户案例】和【得助社区】的标签
 *
 * Class ArticleTags
 * @package app\model
 */
class ArticleTags extends Base
{

    /**
     * @param string $name
     * @return array|\think\Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function findByName(string $name)
    {
        return self::where('name', $name)->find();
    }

    /**
     * @param array $nameList
     * @return Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function findByNames(array $nameList)
    {
        return self::whereIn('name', $nameList)->select();
    }

    // 获取推荐标签
    public static function getRecommendTags(int $size = 12, int $type = -1)
    {
        $where      = [];
        $sortOrder  = [];
        switch ($type) {
            case 1: // 限制查询 case_total
                $where[] = ['case_total', '>', 0];
                $sortOrder['case_total'] = 'desc';
                break;
            case 2: // 限制查询 news_total
                $where[] = ['news_total', '>', 0];
                $sortOrder['news_total'] = 'desc';
                break;
        }

        $q = new static();
        if (count($where) > 0) {
            $q = $q->where($where);
        }
        if (count($sortOrder) > 0) {
            $q = $q->order($sortOrder);
        }

        return $q->limit($size)->select();
    }

    /**
     * 分页器：分页数据查询
     *
     * @param array $where 查询条件，二维数组
     * @param array $paginateRows 分页参数 [list_rows 每页数量 | page 当前页 | path url路径 | query url额外参数| fragment url锚点 | var_page 分页变量]
     * @param bool $paginateSimple 是否为简单分页
     * @return Paginator
     * @throws DbException
     */
    public static function getListWithPaginate(array $where, array $paginateRows, bool $paginateSimple = false): Paginator
    {
        return self::where($where)->paginate($paginateRows, $paginateSimple);
    }
}