DecLevelLogForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\user\Info;
  7. use common\helpers\Validator;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\models\Ad;
  10. use common\models\AdLocation;
  11. use common\models\Article;
  12. use common\models\DecLevelLog;
  13. use common\models\Period;
  14. use common\models\DeclarationLevel;
  15. use common\models\User;
  16. use common\models\UserInfo;
  17. use yii\base\Exception;
  18. use yii\validators\UrlValidator;
  19. /**
  20. * Login form
  21. */
  22. class DecLevelLogForm extends Model
  23. {
  24. public $userName;
  25. public $levelId;
  26. public $periodNum;
  27. public $remark;
  28. private $_userId;
  29. private $_fromId;
  30. private $_lvPv;
  31. public function init() {
  32. parent::init();
  33. $this->adminOperateLogger = new AdminOperate([
  34. 'fetchClass' => User::class,
  35. ]);
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['userName', 'levelId', 'periodNum', 'remark'], 'trim'],
  44. [['userName', 'levelId', 'periodNum'], 'required'],
  45. [['userName'], 'exist', 'targetClass'=>User::class, 'targetAttribute'=>'USER_NAME'],
  46. [['levelId'], 'exist', 'targetClass'=>DeclarationLevel::class, 'targetAttribute'=>'ID'],
  47. [['userName'], 'isUser'],
  48. [['levelId'], 'isLevel'],
  49. [['periodNum'], 'integer'],
  50. [['periodNum'], 'isPeriodNum'],
  51. ];
  52. }
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'userName' => '会员编号',
  57. 'levelId' => '级别',
  58. 'periodNum' => '期数',
  59. 'remark' => '期数',
  60. ];
  61. }
  62. /**
  63. * 指定校验场景
  64. * @return array
  65. */
  66. public function scenarios()
  67. {
  68. $parentScenarios = parent::scenarios();
  69. $customScenarios = [
  70. 'adminChange' => ['userName', 'levelId', 'remark'],
  71. ];
  72. return array_merge($parentScenarios, $customScenarios);
  73. }
  74. /**
  75. * 赋值UserId并校验会员是否存在
  76. * @param $attribute
  77. */
  78. public function isUser($attribute){
  79. $this->_userId = Info::getUserIdByUserName($this->userName);
  80. if(!$this->_userId){
  81. $this->addError($attribute, 'Member does not exist'); // 会员不存在
  82. }
  83. }
  84. /**
  85. * 查看级别是否有变化
  86. * @param $attribute
  87. */
  88. public function isLevel($attribute){
  89. $this->_fromId = Info::getDecLv($this->_userId);
  90. if($this->levelId == $this->_fromId){
  91. $this->addError($attribute, '级别没有变化无需调整');
  92. }
  93. $decLv = DeclarationLevel::findOneAsArray('ID=:ID',[':ID'=>$this->levelId],'PERF');
  94. $this->_lvPv = $decLv['PERF'];
  95. }
  96. /**
  97. * 期数是否满足要求
  98. * @param $attribute
  99. * @throws \yii\db\Exception
  100. */
  101. public function isPeriodNum($attribute) {
  102. $period = Period::instance();
  103. if ($period->isSent($this->periodNum)) {
  104. $this->addError($attribute, '期数' . $this->periodNum . '已挂网');
  105. }
  106. }
  107. /**
  108. * 调价调整操作
  109. * @return Ad|null
  110. * @throws \yii\db\Exception
  111. */
  112. public function adminChange(){
  113. if(!$this->validate()){
  114. return null;
  115. }
  116. $this->adminOperateLogger->beforeUpdate($this->_userId,'ID',['select'=>'ID,LAST_DEC_LV,DEC_LV,DEC_LV_UPDATED_AT,DEC_LV_UPDATED_PERIOD,LAST_DEC_LV_UPDATED_PERIOD,LAST_DEC_LV_UPDATED_AT']);
  117. $db = \Yii::$app->db;
  118. $transaction = $db->beginTransaction();
  119. try {
  120. // 恢复所传期数的和这个会员相关的其他调整
  121. $period = Period::instance();
  122. DecLevelLog::updateAll(['STATUS' => 0], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID' => $this->_userId, ':PERIOD_NUM' => $this->periodNum]);
  123. // 新增数据
  124. $model = new DecLevelLog();
  125. $model->USER_ID = $this->_userId;
  126. $model->FROM_ID = $this->_fromId;
  127. $model->TO_ID = $this->levelId;
  128. // $model->PERIOD_NUM = $this->periodNum;
  129. // $model->CALC_MONTH = $period->getYearMonth($this->periodNum);
  130. $model->PERIOD_NUM = 0;
  131. $model->CALC_MONTH = 0;
  132. $model->REMARK = $this->remark;
  133. $model->STATUS = 1;
  134. $model->ADMIN_ID = \Yii::$app->user->id;
  135. $model->CREATED_AT = Date::nowTime();
  136. if (!$model->save()) {
  137. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  138. }
  139. //修改会员级别,如果是上期,则一起变化,如果不是,则只改变实时级别
  140. $nowPeriodNum = $period->getNowPeriodNum();
  141. //$changePeriodNum = $this->periodNum;
  142. $nowTime = Date::nowTime();
  143. //if ($changePeriodNum == $nowPeriodNum - 1) {
  144. User::updateAll(['LAST_DEC_LV' => $this->levelId, 'DEC_LV' => $this->levelId, 'DEC_LV_UPDATED_AT'=>$nowTime, 'DEC_LV_UPDATED_PERIOD'=>$nowPeriodNum, 'LAST_DEC_LV_UPDATED_AT'=>$nowTime, 'LAST_DEC_LV_UPDATED_PERIOD'=>$nowPeriodNum, 'ZG_UPGRADE_PV'=>$this->_lvPv], 'ID=:ID', [':ID' => $this->_userId]);
  145. // } else {
  146. // User::updateAll(['DEC_LV' => $this->levelId, 'DEC_LV_UPDATED_AT'=>$nowTime, 'DEC_LV_UPDATED_PERIOD'=>$nowPeriodNum, 'ZG_UPGRADE_PV'=>$this->_lvPv], 'ID=:ID', [':ID' => $this->_userId]);
  147. // }
  148. $transaction->commit();
  149. } catch (Exception $e) {
  150. $transaction->rollBack();
  151. $this->addError('adminChange', $e->getMessage());
  152. return null;
  153. }
  154. $this->adminOperateLogger->afterUpdate($this->_userId,'ID',['select'=>'ID,LAST_DEC_LV,DEC_LV,DEC_LV_UPDATED_AT,DEC_LV_UPDATED_PERIOD,LAST_DEC_LV_UPDATED_PERIOD,LAST_DEC_LV_UPDATED_AT'])->clean()->save([
  155. 'optType' => '修改会员级别',
  156. 'userId' => $this->_userId,
  157. 'userName' => Info::getUserNameByUserId($this->_userId),
  158. 'remark' => $this->remark
  159. ]);
  160. return $model;
  161. }
  162. }