CurrencyConversions.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%CURRENCIES_CONVERSIONS}}".
  7. *
  8. * @property string ID
  9. * @property int PERIOD_NUM 期数
  10. * @property int FROM_CURRENCY_ID 源
  11. * @property int TO_CURRENCY_ID 目标
  12. * @property double RATE 汇率
  13. * @property int ACTIVE 状态
  14. * @property int CREATED_AT 创建时间
  15. * @property int UPDATED_AT 更新时间
  16. * @property string CREATED_BY 创建人
  17. * @property string UPDATED_BY 更新人
  18. */
  19. class CurrencyConversions extends \common\components\ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%CURRENCIES_CONVERSIONS}}';
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['ID'], 'unique'],
  35. ];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'ID' => 'ID',
  44. 'PERIOD_NUM' => '期数',
  45. 'FROM_CURRENCY_ID' => '源',
  46. 'TO_CURRENCY_ID' => '目标',
  47. 'RATE' => '汇率',
  48. 'ACTIVE' => '状态:1正常 0异常',
  49. 'CREATED_AT' => '创建时间',
  50. 'UPDATED_AT' => '更新时间',
  51. 'CREATED_BY' => '创建人',
  52. 'UPDATED_BY' => '更新人',
  53. ];
  54. }
  55. /**
  56. * 从缓存获取信息
  57. * @return array|mixed|\yii\db\ActiveRecord[]
  58. */
  59. public static function getFromCache()
  60. {
  61. $data = Yii::$app->cache->get(Cache::CURRENCIES_CONVERSIONS_KEY);
  62. if (!$data) {
  63. // 获取信息
  64. $data = static::find()->where('ACTIVE=1')->orderBy('ID ASC')->indexBy('ID')->asArray()->all();
  65. Yii::$app->cache->set(Cache::CURRENCIES_CONVERSIONS_KEY, $data);
  66. }
  67. return $data;
  68. }
  69. /**
  70. * @return array|\yii\db\ActiveRecord[]
  71. */
  72. public static function getAllData()
  73. {
  74. return static::find()->where('ACTIVE=1')->indexBy('ID')->orderBy('ID ASC')->asArray()->all();
  75. }
  76. /**
  77. * 更新缓存
  78. * @return array|\yii\db\ActiveRecord[]
  79. */
  80. public static function updateToCache()
  81. {
  82. // 获取配置
  83. $data = static::find()->where('ACTIVE=1')->orderBy('ID ASC')->asArray()->indexBy('ID')->all();
  84. Yii::$app->cache->set(Cache::CURRENCIES_CONVERSIONS_KEY, $data);
  85. return $data;
  86. }
  87. /**
  88. * 查询汇率.
  89. * @param int $fromCurrencyId
  90. * @param int $toCurrencyId
  91. * @return mixed.
  92. */
  93. public static function getOne(int $fromCurrencyId, int $toCurrencyId): ?array
  94. {
  95. return self::findOneAsArray('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
  96. [
  97. ':FROM_CURRENCY_ID' => $fromCurrencyId,
  98. ':TO_CURRENCY_ID' => $toCurrencyId,
  99. ]);
  100. }
  101. /**
  102. * 查询汇率.
  103. * @param int $fromCurrencyId
  104. * @param int $toCurrencyId
  105. * @return mixed.
  106. */
  107. public static function getRate(int $fromCurrencyId, int $toCurrencyId): ?array
  108. {
  109. $data = self::findOneAsArray('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
  110. [
  111. ':FROM_CURRENCY_ID' => $fromCurrencyId,
  112. ':TO_CURRENCY_ID' => $toCurrencyId,
  113. ]);
  114. return $data['RATE'] ?? 0.0;
  115. }
  116. /**
  117. * 查询汇率.
  118. * @param int $toCurrencyId
  119. * @return mixed.
  120. */
  121. public static function getToUSDRate(int $toCurrencyId): ?array
  122. {
  123. $data = self::findOneAsArray('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
  124. [
  125. ':FROM_CURRENCY_ID' => 149, // 美元汇率
  126. ':TO_CURRENCY_ID' => $toCurrencyId,
  127. ]);
  128. return $data['RATE'] ?? 0.0;
  129. }
  130. public static function updateOne(int $fromCurrencyId, int $toCurrencyId): bool
  131. {
  132. return self::updateAll(['ACTIVE' => \StatusEnum::INACTIVE],
  133. 'FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
  134. [
  135. ':FROM_CURRENCY_ID' => $fromCurrencyId,
  136. ':TO_CURRENCY_ID' => $toCurrencyId,
  137. ]);
  138. }
  139. }