Currency.php 2.4 KB

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