<?php

namespace app\model;

/**
 * @desc 国家表
 * @property int id
 * @property string name_cn
 * @property string name_en
 * @property string code
 * @property int sort
 */
class CountryModel extends BaseModel
{

    protected $name = 'country';
    protected $pk = 'id';

    /**
     * @desc 根据id获取国家码数据
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getById($id)
    {
        $self = self::where('id', $id)->find();
        if(empty($self)){
            return [];
        }
        return $self->toArray();
    }

    public static function getAll()
    {
        return self::where('id', '>', 0)->order('sort', 'desc')->select();
    }

    /**
     * @desc 判断国家码是否存在
     * @param $code
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public static function checkCodeExists($code): bool
    {
        $count = self::where('code', $code)->count();
        return $count > 0;
    }
    public static function getNameById($id, $lang = 'en')
    {
        $self = self::where('id', $id)->find();
        if(empty($self)){
            return '';
        }
        if($lang == 'cn'){
            return $self->name_cn;
        }

        return $self->name_en;
    }
}