ImmigrantForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\helpers\Tool;
  7. use common\helpers\user\Info;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\models\Countries;
  10. use common\models\CurrencyConversions;
  11. use common\models\EmployLevel;
  12. use common\models\HighestEmpLevelLog;
  13. use common\models\Period;
  14. use common\models\User;
  15. use common\models\UserBonus;
  16. use common\models\UserImmigrant;
  17. use common\models\UserWallet;
  18. use Yii;
  19. use yii\base\Exception;
  20. /**
  21. * Login form
  22. */
  23. class ImmigrantForm extends Model
  24. {
  25. public $userName;
  26. public $beforeCountryId;
  27. public $afterCountryId;
  28. public $periodNum;
  29. private $_userId;
  30. private $beforeCountry;
  31. private $afterCountry;
  32. public function init() {
  33. parent::init();
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['userName'], 'trim'],
  42. [['userName', 'afterCountryId'], 'required'],
  43. [['userName'], 'exist', 'targetClass' => User::class, 'targetAttribute' => 'USER_NAME'],
  44. [['userName'], 'isUser'],
  45. [['afterCountryId'], 'isAfterCountry'],
  46. ];
  47. }
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'userName' => '会员编号',
  52. 'beforeCountryId' => '移民前国家ID',
  53. 'afterCountryId' => '移民后国家ID',
  54. 'periodNum' => '期数',
  55. ];
  56. }
  57. /**
  58. * 指定校验场景
  59. * @return array
  60. */
  61. public function scenarios()
  62. {
  63. $parentScenarios = parent::scenarios();
  64. $customScenarios = [
  65. 'adminChange' => ['userName', 'beforeCountryId', 'afterCountryId'],
  66. ];
  67. return array_merge($parentScenarios, $customScenarios);
  68. }
  69. /**
  70. * 赋值UserId并校验会员是否存在
  71. * @param $attribute
  72. */
  73. public function isUser($attribute){
  74. $this->_userId = Info::getUserIdByUserName($this->userName);
  75. if (!$this->_userId) {
  76. $this->addError($attribute, Yii::t('ctx', 'memberDoesNotExist'));
  77. }
  78. $this->beforeCountryId = Info::getUserCountryByUserId($this->_userId);
  79. $this->beforeCountry = Countries::getById($this->beforeCountryId);
  80. }
  81. public function isAfterCountry($attribute){
  82. $this->afterCountry = Countries::getById($this->afterCountryId);
  83. if (!$this->afterCountry) {
  84. $this->addError($attribute, Yii::t('ctx', 'countryDoesNotExist'));
  85. }
  86. }
  87. /**
  88. * 执行移民.
  89. */
  90. public function userImmigrant()
  91. {
  92. if (!$this->validate()) {
  93. return null;
  94. }
  95. $db = \Yii::$app->db;
  96. $transaction = $db->beginTransaction();
  97. try {
  98. // 移民记录
  99. $model = new UserImmigrant();
  100. $model->user_id = $this->_userId;
  101. $model->before_country_id = $this->beforeCountryId;
  102. $model->after_country_id = $this->afterCountryId;
  103. $model->period_num = Period::instance()->getNowPeriodNum();
  104. $model->created_by = \Yii::$app->user->id;
  105. if (!$model->save()) {
  106. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  107. }
  108. // 修改国家
  109. User::updateAll(['COUNTRY_ID' => $this->afterCountryId], 'ID=:USER_ID', [':USER_ID' => $this->_userId]);
  110. // 移民前汇率
  111. $beforeCurrency = CurrencyConversions::getToUSDRate($this->beforeCountry['LOCAL_CURRENCY_ID']);
  112. // 移民后汇率
  113. $afterCurrency = CurrencyConversions::getToUSDRate($this->afterCountry['LOCAL_CURRENCY_ID']);
  114. if (!$afterCurrency) {
  115. throw new Exception(Yii::t('app', 'currencyDoesNotExist'));
  116. }
  117. // 奖金账户余额转换(NG发放奖金为美元,所以不需要转换本地货币)
  118. // $userBonus = UserBonus::findOne(['USER_ID' => $this->_userId]);
  119. // if ($userBonus) {
  120. // foreach ($userBonus as $index => $bonus) {
  121. // if ($index == 'ID' || $index == 'USER_ID') {
  122. // continue;
  123. // }
  124. //
  125. // $userBonus->$index = Tool::convertAmount($bonus, $beforeCurrency, $afterCurrency);
  126. // }
  127. // if (!$userBonus->save()) {
  128. // $transaction->rollBack();
  129. // throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  130. // }
  131. // }
  132. // 现金钱包余额转换
  133. $userWallet = UserWallet::findOne(['USER_ID' => $this->_userId]);
  134. if ($userWallet && $userWallet->CASH > 0) {
  135. $userWallet->CASH = Tool::convertAmount($userWallet->CASH, $beforeCurrency, $afterCurrency);
  136. if (!$userWallet->save()) {
  137. $transaction->rollBack();
  138. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  139. }
  140. }
  141. $transaction->commit();
  142. } catch (Exception $e) {
  143. $transaction->rollBack();
  144. $this->addError('userImmigrant', $e->getMessage());
  145. return null;
  146. }
  147. return $model;
  148. }
  149. }