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\base\Exception;
  16. /**
  17. * Login form
  18. */
  19. class CurrenciesConversionsForm extends Model
  20. {
  21. public $modelClass = CurrencyConversions::class;
  22. public $from_currency_id = Currency::USD; // 美元USD
  23. public $to_currency_id;
  24. public $product_rate;
  25. public $bonuses_rate;
  26. public $synchronize;
  27. private $period;
  28. public function init() {
  29. parent::init();
  30. $this->adminOperateLogger = new AdminOperate([
  31. 'fetchClass' => CurrencyConversions::class,
  32. ]);
  33. $this->period = Period::instance();
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['from_currency_id', 'to_currency_id', 'product_rate', 'bonuses_rate'], 'required'],
  42. ];
  43. }
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'from_currency_id' => 'From Currency Name', // 源汇率
  48. 'to_currency_id' => 'To Currency Name', // 目标汇率
  49. 'product_rate' => 'Product Rate', // 商品汇率
  50. 'bonuses_rate' => 'Bonuses_rate Rate', // 奖金汇率
  51. ];
  52. }
  53. /**
  54. * 指定场景
  55. * @return array
  56. */
  57. public function scenarios()
  58. {
  59. $parentScenarios = parent::scenarios();
  60. $customScenarios = [
  61. 'setCurrenciesConversions' => ['from_currency_id', 'to_currency_id', 'product_rate', 'bonuses_rate'],
  62. ];
  63. return array_merge($parentScenarios, $customScenarios);
  64. }
  65. /**
  66. * 设置汇率.
  67. * @return true
  68. * @throws \yii\db\Exception
  69. */
  70. public function setCurrenciesConversions(): ?bool
  71. {
  72. if (!$this->validate()) {
  73. return null;
  74. }
  75. $postData = \Yii::$app->request->post();
  76. $transaction = \Yii::$app->db->beginTransaction();
  77. try {
  78. // 删除原记录
  79. $this->modelClass::deleteOne($this->from_currency_id, $this->to_currency_id);
  80. // 添加新数据
  81. $model = new CurrencyConversions();
  82. $model->ID = PageSnowFake::instance()->generateId();
  83. $model->PERIOD_NUM = $this->period->getNowPeriodNum();
  84. $model->FROM_CURRENCY_ID = $this->from_currency_id;
  85. $model->TO_CURRENCY_ID = $this->to_currency_id;
  86. $model->PRODUCT_RATE = $this->product_rate;
  87. $model->BONUSES_RATE = $this->bonuses_rate;
  88. $model->ACTIVE = StatusEnum::ACTIVE;
  89. $model->CREATED_AT = 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', $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. }