Currency.php 2.2 KB

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