<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $icd_id 主键id
 * @property string $icd_name 疾病分类名称
 * @property string $icd_code icd编码
 * @property string $icd_spell icd简拼
 * @property int $icd_status 状态（0:删除 1:正常）
 * @property int $icd_enable 是否启用（0:否 1:是）
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 */
class DiseaseClassIcd extends Model
{
    use Snowflake;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'disease_class_icd';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['icd_id', 'icd_name', 'icd_code', 'icd_spell', 'icd_status', 'icd_enable', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['icd_id' => 'integer', 'icd_status' => 'integer', 'icd_enable' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected string $primaryKey = "icd_id";

    /**
     * 获取信息-单条
     * @param array $params
     * @param array $fields
     * @return object|null
     */
    public static function getOne(array $params, array $fields = ['*'],): object|null
    {
        return self::where($params)->first($fields);
    }

    /**
     * 获取数据-多
     * @param array $params
     * @param array $fields
     * @return Collection|array
     */
    public static function getList(array $params = [], array $fields = ['*']): Collection|array
    {
        return self::where($params)->get($fields);
    }

    /**
     * 获取数据-限制条数
     * @param array $params
     * @param int|string $limit
     * @param array $fields
     * @return Collection|array
     */
    public static function getLimit(array $params = [],int|string $limit = 5, array $fields = ['*']): Collection|array
    {
        return self::where($params)->limit($limit)->get($fields);
    }

    /**
     * 获取关键字搜索列表
     * @param array $params
     * @param string $keyword
     * @param array $fields
     * @return Collection|array
     */
    public static function getSearchKeywordList(array $params = [],string $keyword = '',array $fields = ['*']): Collection|array
    {
        return self::when($keyword, function ($query, $keyword) {
            $query->where(function ($query) use ($keyword) {
                $query->orwhere("icd_name", 'like', '%' . $keyword . '%');
                $query->orwhere("icd_code", 'like', '%' . $keyword . '%');
                $query->orwhere("icd_spell", 'like', '%' . $keyword . '%');
            });
        })
            ->where($params)
            ->get($fields);
    }

    /**
     * 获取关键字搜索数据
     * 限制数量
     * @param array $params
     * @param string $keyword
     * @param array $fields
     * @return Collection|array
     */
    public static function getSearchKeywordLimit(array $params = [],string $keyword = '',array $fields = ['*']): Collection|array
    {
        return self::when($keyword, function ($query, $keyword) {
            $query->where(function ($query) use ($keyword) {
                $query->orwhere("icd_name", 'like', '%' . $keyword . '%');
                $query->orwhere("icd_code", 'like', '%' . $keyword . '%');
                $query->orwhere("icd_spell", 'like', '%' . $keyword . '%');
            });
        })
            ->where($params)
            ->limit(10)
            ->get($fields);
    }
}
