Countries.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%COUNTRIES}}".
  7. *
  8. * @property string ID
  9. * @property string NAME 名字
  10. * @property string CODE 简码
  11. * @property string LOCAL_CURRENCY_ID 货币ID
  12. * @property string DEFAULT_LANGUAGE_ID 语言ID
  13. * @property string LANGUAGE_KEY 语言KEY
  14. * @property int ACTIVE 状态
  15. * @property int CREATED_AT 创建时间
  16. * @property int UPDATED_AT 更新时间
  17. */
  18. class Countries extends \common\components\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%COUNTRIES}}';
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['ID'], 'unique'],
  34. ];
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'ID' => 'ID',
  43. 'NAME' => '名字',
  44. 'CODE' => '简码',
  45. 'LOCAL_CURRENCY_ID' => '货币ID',
  46. 'DEFAULT_LANGUAGE_ID' => '语言ID',
  47. 'ACTIVE' => '状态:1正常 0异常',
  48. 'LANGUAGE_KEY' => '语言KEY',
  49. 'CREATED_AT' => '创建时间',
  50. 'UPDATED_AT' => '更新时间',
  51. ];
  52. }
  53. /**
  54. * 从缓存获取信息
  55. * @return array|mixed|\yii\db\ActiveRecord[]
  56. */
  57. public static function getFromCache()
  58. {
  59. $data = Yii::$app->cache->get(Cache::COUNTRIES_KEY);
  60. Yii::$app->cache->delete(Cache::COUNTRIES_KEY);
  61. if (!$data) {
  62. // 获取信息
  63. $data = self::getAllData();
  64. Yii::$app->cache->set(Cache::COUNTRIES_KEY, $data);
  65. }
  66. return $data;
  67. }
  68. /**
  69. * @return array|\yii\db\ActiveRecord[]
  70. */
  71. public static function getAllData()
  72. {
  73. return static::find()->where('ACTIVE=1')->orderBy('ACTIVE DESC, NAME ASC')->asArray()->all();
  74. }
  75. /**
  76. * 更新缓存
  77. * @return array|\yii\db\ActiveRecord[]
  78. */
  79. public static function updateToCache()
  80. {
  81. // 获取配置
  82. $data = self::getAllData();
  83. Yii::$app->cache->set(Cache::COUNTRIES_KEY, $data);
  84. return $data;
  85. }
  86. /**
  87. * 通过ID获取
  88. * @param string $id
  89. * @return array|null
  90. */
  91. public static function getById(string $id)
  92. {
  93. return self::findOneAsArray('ID=:ID', [':ID' => $id]);
  94. }
  95. /**
  96. * 通过ID获取
  97. * @param string $id
  98. * @return string
  99. */
  100. public static function getNameById(string $id): string
  101. {
  102. return self::findOneAsArray('ID=:ID', [':ID' => $id])['NAME'];
  103. }
  104. /**
  105. * 通过ID获取
  106. * @param string $id
  107. * @return string
  108. */
  109. public static function getCodeById(string $id): string
  110. {
  111. return self::findOneAsArray('ID=:ID', [':ID' => $id])['CODE'];
  112. }
  113. /**
  114. * 通过ID查询币种.
  115. * @param string $id
  116. * @return int|mixed
  117. */
  118. public static function getCurrency(string $id)
  119. {
  120. $record = self::findOneAsArray('ID=:ID', [':ID' => $id]);
  121. return $record['LOCAL_CURRENCY_ID'] ?? 0;
  122. }
  123. }