CurrenciesConversionsForm.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\LoggerTool;
  8. use common\helpers\snowflake\PageSnowFake;
  9. use common\libs\logging\operate\AdminOperate;
  10. use common\models\Currency;
  11. use common\models\CurrencyConversions;
  12. use common\models\Period;
  13. use common\models\ShopGoods;
  14. use StatusEnum;
  15. use Yii;
  16. use yii\base\Exception;
  17. /**
  18. * Login form
  19. */
  20. class CurrenciesConversionsForm extends Model
  21. {
  22. public $modelClass = CurrencyConversions::class;
  23. public $from_currency_id = Currency::USD; // 美元USD
  24. public $to_currency_id;
  25. public $product_rate;
  26. public $bonuses_rate;
  27. public $synchronize;
  28. private $period;
  29. public function init() {
  30. parent::init();
  31. $this->adminOperateLogger = new AdminOperate([
  32. 'fetchClass' => CurrencyConversions::class,
  33. ]);
  34. $this->period = Period::instance();
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['from_currency_id', 'to_currency_id', 'product_rate', 'bonuses_rate'], 'required'],
  43. ];
  44. }
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'from_currency_id' => 'From Currency Name', // 源汇率
  49. 'to_currency_id' => 'To Currency Name', // 目标汇率
  50. 'product_rate' => 'Product Rate', // 商品汇率
  51. 'bonuses_rate' => 'Bonuses_rate Rate', // 奖金汇率
  52. ];
  53. }
  54. /**
  55. * 指定场景
  56. * @return array
  57. */
  58. public function scenarios()
  59. {
  60. $parentScenarios = parent::scenarios();
  61. $customScenarios = [
  62. 'setCurrenciesConversions' => ['from_currency_id', 'to_currency_id', 'product_rate', 'bonuses_rate'],
  63. ];
  64. return array_merge($parentScenarios, $customScenarios);
  65. }
  66. /**
  67. * 设置汇率.
  68. * @return true
  69. * @throws \yii\db\Exception
  70. */
  71. public function setCurrenciesConversions(): ?bool
  72. {
  73. if (!$this->validate()) {
  74. return null;
  75. }
  76. $postData = \Yii::$app->request->post();
  77. $transaction = \Yii::$app->db->beginTransaction();
  78. try {
  79. // 删除原记录
  80. $this->modelClass::deleteOne($this->from_currency_id, $this->to_currency_id);
  81. // 添加新数据
  82. $model = new CurrencyConversions();
  83. $model->ID = PageSnowFake::instance()->generateId();
  84. $model->PERIOD_NUM = $this->period->getNowPeriodNum();
  85. $model->FROM_CURRENCY_ID = $this->from_currency_id;
  86. $model->TO_CURRENCY_ID = $this->to_currency_id;
  87. $model->PRODUCT_RATE = $this->product_rate;
  88. $model->BONUSES_RATE = $this->bonuses_rate;
  89. $model->CREATED_AT = date('Y-m-d H:i:s', Date::nowTime());
  90. $model->CREATED_BY = \Yii::$app->user->id;
  91. if (!$model->save()) {
  92. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  93. }
  94. // 选择商品汇率同步,更新商品属性
  95. if ($postData['synchronize']) {
  96. // 异步属性商品价格
  97. $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle('config/update-exchange-rate', ['currencyId' => $model->ID]);
  98. if($taskKey === false){
  99. throw new \yii\db\Exception('请求异步服务器失败');
  100. }
  101. }
  102. $transaction->commit();
  103. } catch (Exception $e) {
  104. $transaction->rollBack();
  105. $this->addError('edit', $e->getMessage());
  106. return null;
  107. }
  108. return true;
  109. }
  110. }