CurrenciesConversionsForm.php 3.6 KB

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