CurrencyConversions.php 4.7 KB

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