Currency.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (!$data) {
  56. // 获取信息
  57. $data = self::getAllData();
  58. Yii::$app->cache->set(Cache::CURRENCIES_KEY, $data);
  59. }
  60. return $data;
  61. }
  62. /**
  63. * @return array|\yii\db\ActiveRecord[]
  64. */
  65. public static function getAllData()
  66. {
  67. return static::find()->where('ACTIVE=1')->orderBy('NAME ASC')->asArray()->all();
  68. }
  69. /**
  70. * 更新缓存
  71. * @return array|\yii\db\ActiveRecord[]
  72. */
  73. public static function updateToCache()
  74. {
  75. // 获取配置
  76. $data = self::getAllData();
  77. Yii::$app->cache->set(Cache::CURRENCIES_KEY, $data);
  78. return $data;
  79. }
  80. /**
  81. * 通过ID获取
  82. * @param string $id
  83. * @return array|null
  84. */
  85. public static function getById(string $id): ?array
  86. {
  87. return self::findOneAsArray('ID=:ID', [':ID' => $id]);
  88. }
  89. /**
  90. * 通过ID获取
  91. * @param string $id
  92. * @return string|null
  93. */
  94. public static function getNameById(string $id): ?string
  95. {
  96. $record = self::findOneAsArray('ID=:ID', [':ID' => $id]);
  97. return $record['NAME'] ?? '';
  98. }
  99. }