<?php

namespace app\model;

use think\Collection;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;

class ArchivesCategory extends Base
{
    public const TOP_PID = 0;

    public const NAME_DIARY = 'diary'; //日记分享

    /**
     * 获取恒美 关于我们栏目列表
     *
     * @return string[]
     */
    public static function aboutCategoryId(): array
    {
        $aboutId = self::where('name', 'about')->value('id');
        return self::where('pid', $aboutId)->column('id');
    }

    /**
     * 获取病种相关模型的栏目 模型名称为:'question', 'diary', 'effect', 'science'
     *
     * @return string[]
     */
    public static function diseaseCategoryIds(): array
    {
        $modelIds = ArchivesModel::whereIn('name', ArchivesModel::models())->column('id');
        return self::whereIn('model_id', $modelIds)->column('id');
    }

    /**
     * 检测数据下 是否有子栏目
     *
     * @param  array  $ids
     * @return bool
     */
    public static function hasChildrenByIds(array $ids): bool
    {
        return self::whereIn('pid', $ids)->count() > 0;
    }

    /**
     * 检测数据下 是否有子内容
     *
     * @param  array  $ids
     * @return bool
     */
    public static function hasContentByIds(array $ids): bool
    {
        return Archives::whereIn('category_id', $ids)->count() > 0;
    }

    /**
     * 获取列表
     *
     * @return ArchivesCategory[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getList()
    {
        return self::field('id,pid,title,name,sort,path,true as open')
            ->order('sort', 'desc')
            ->order('id', 'asc')
            ->select();
    }

    /**
     * 模型
     *
     * @return HasOne
     */
    public function model(): HasOne
    {
        return $this->hasOne(ArchivesModel::class, 'id', 'model_id')->bind(['model' => 'name']);
    }
}