<?php

namespace app\model;

use Exception;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;

class Tag extends Base
{
    /**
     * 根据父级ID 获取病种列表
     *
     * @param  int  $pid
     * @param  string[]  $fields
     * @return Disease[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getListByPid(int $pid = 0, array $fields = ['pid', 'name', 'id'])
    {
        return self::where('pid', $pid)->order('id', 'desc')->field($fields)->select();
    }

    /**
     * 获取全部列表
     *
     * @return Disease[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getList()
    {
        return self::field('id,pid,name')->order('id', 'desc')->order('id')->select();
    }

    /**
     * 病种 xmSelect json数据
     *
     * @param  int  $pid
     * @param  array  $selected
     * @param  array  $disabled
     * @return array|Disease[]|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function diseaseXmJson(int $pid = 0, array $selected = [], array $disabled = [])
    {
        $list = self::getListByPid($pid);
        foreach ($list as $k => $m) {
            $list[$k]['selected'] = in_array($m['id'], $selected);
            $list[$k]['disabled'] = in_array($m['id'], $disabled);
        }

        return $list;
    }
}
