ChangeShowEmpLvForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\EmployLevel;
  16. use common\models\User;
  17. use common\models\UserInfo;
  18. use yii\base\Exception;
  19. use yii\validators\UrlValidator;
  20. /**
  21. * Login form
  22. */
  23. class ChangeShowEmpLvForm extends Model
  24. {
  25. public $userName;
  26. public $levelId;
  27. public $isReset;
  28. public $remark;
  29. private $_userId;
  30. public function init() {
  31. parent::init();
  32. $this->adminOperateLogger = new AdminOperate([
  33. 'fetchClass' => UserInfo::class,
  34. ]);
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['userName', 'levelId', 'remark', 'isReset'], 'trim'],
  43. [['userName'], 'required'],
  44. [['userName'], 'exist', 'targetClass'=>User::class, 'targetAttribute'=>'USER_NAME'],
  45. [['levelId'], 'exist', 'targetClass'=>EmployLevel::class, 'targetAttribute'=>'ID'],
  46. [['userName'], 'isUser'],
  47. [['levelId'], 'isLevel'],
  48. ];
  49. }
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'userName' => '会员编号',
  54. 'levelId' => '级别',
  55. 'isReset' => '是否重置',
  56. 'remark' => '期数',
  57. ];
  58. }
  59. /**
  60. * 指定校验场景
  61. * @return array
  62. */
  63. public function scenarios()
  64. {
  65. $parentScenarios = parent::scenarios();
  66. $customScenarios = [
  67. 'adminChange' => ['userName', 'levelId', 'isReset', 'remark'],
  68. ];
  69. return array_merge($parentScenarios, $customScenarios);
  70. }
  71. /**
  72. * 赋值UserId并校验会员是否存在
  73. * @param $attribute
  74. */
  75. public function isUser($attribute){
  76. $this->_userId = Info::getUserIdByUserName($this->userName);
  77. if(!$this->_userId){
  78. $this->addError($attribute, 'Member does not exist'); // 会员不存在
  79. }
  80. }
  81. /**
  82. * 查看级别是否有变化
  83. * @param $attribute
  84. */
  85. public function isLevel($attribute){
  86. $userInfo = UserInfo::findOneAsArray('USER_ID=:USER_ID',[':USER_ID'=>$this->_userId],'SHOW_EMP_LV');
  87. if($this->levelId == $userInfo['SHOW_EMP_LV']){
  88. $this->addError($attribute, '前台显示级别没有变化无需调整');
  89. }
  90. }
  91. /**
  92. * 修改会员前台显示级别
  93. * @return bool|null
  94. * @throws \yii\db\Exception
  95. */
  96. public function adminChange(){
  97. if(!$this->validate()){
  98. return null;
  99. }
  100. $this->adminOperateLogger->beforeUpdate($this->_userId,'USER_ID',['select'=>'SHOW_EMP_LV']);
  101. $db = \Yii::$app->db;
  102. $transaction = $db->beginTransaction();
  103. try {
  104. if ($this->isReset) {
  105. UserInfo::updateAll(['SHOW_EMP_LV' => NULL], 'USER_ID=:USER_ID', [':USER_ID' => $this->_userId]);
  106. } else {
  107. UserInfo::updateAll(['SHOW_EMP_LV' => $this->levelId], 'USER_ID=:USER_ID', [':USER_ID' => $this->_userId]);
  108. }
  109. $transaction->commit();
  110. } catch (Exception $e) {
  111. $transaction->rollBack();
  112. $this->addError('adminChange', $e->getMessage());
  113. return null;
  114. }
  115. $this->adminOperateLogger->afterUpdate($this->_userId,'USER_ID',['select'=>'SHOW_EMP_LV'])->clean()->save([
  116. 'optType' => '修改会员前台显示聘级',
  117. 'userId' => $this->_userId,
  118. 'userName' => Info::getUserNameByUserId($this->_userId),
  119. 'remark' => $this->remark
  120. ]);
  121. return true;
  122. }
  123. }