Currency.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  18. * @inheritdoc
  19. */
  20. public static function tableName()
  21. {
  22. return '{{%CURRENCIES}}';
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['ID'], 'unique'],
  31. ];
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function attributeLabels()
  37. {
  38. return [
  39. 'ID' => 'ID',
  40. 'NAME' => '币种',
  41. 'CODE' => '简码',
  42. 'ACTIVE' => '状态:1正常 0异常',
  43. 'CREATED_AT' => '创建时间',
  44. 'UPDATED_AT' => '更新时间',
  45. ];
  46. }
  47. /**
  48. * 从缓存获取信息
  49. * @return array|mixed|\yii\db\ActiveRecord[]
  50. */
  51. public static function getFromCache()
  52. {
  53. $data = Yii::$app->cache->get(Cache::CURRENCIES_KEY);
  54. Yii::$app->cache->delete(Cache::CURRENCIES_KEY);
  55. if (!$data) {
  56. // 获取信息
  57. $data = static::find()->where('ACTIVE=1')->orderBy('ACTIVE DESC, ID ASC')->asArray()->all();
  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('1=1')->orderBy('ACTIVE DESC, ID ASC')->asArray()->all();
  68. }
  69. /**
  70. * 更新缓存
  71. * @return array|\yii\db\ActiveRecord[]
  72. */
  73. public static function updateToCache()
  74. {
  75. // 获取配置
  76. $data = static::find()->where('1=1')->orderBy('ACTIVE DESC, ID ASC')->asArray()->indexBy('ID')->all();
  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. }