CurrenciesConversionsForm.php 3.4 KB

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