HighestEmpLevelLogForm.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\libs\logging\operate\AdminOperate;
  8. use common\models\EmployLevel;
  9. use common\models\HighestEmpLevelLog;
  10. use common\models\Period;
  11. use common\models\User;
  12. use Yii;
  13. use yii\base\Exception;
  14. /**
  15. * Login form
  16. */
  17. class HighestEmpLevelLogForm extends Model
  18. {
  19. public $userName;
  20. public $levelId;
  21. public $periodNum;
  22. public $remark;
  23. private $_userId;
  24. private $_fromId;
  25. public function init() {
  26. parent::init();
  27. $this->adminOperateLogger = new AdminOperate([
  28. 'fetchClass' => User::class,
  29. ]);
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['userName', 'levelId', 'periodNum', 'remark'], 'trim'],
  38. [['userName', 'levelId', 'periodNum'], 'required'],
  39. [['userName'], 'exist', 'targetClass' => User::class, 'targetAttribute' => 'USER_NAME'],
  40. [['levelId'], 'exist', 'targetClass' => EmployLevel::class, 'targetAttribute' => 'ID'],
  41. [['userName'], 'isUser'],
  42. [['levelId'], 'isLevel'],
  43. [['periodNum'], 'integer'],
  44. ];
  45. }
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'userName' => '会员编号',
  50. 'levelId' => '级别',
  51. 'periodNum' => '期数',
  52. 'remark' => '备注',
  53. ];
  54. }
  55. /**
  56. * 指定校验场景
  57. * @return array
  58. */
  59. public function scenarios()
  60. {
  61. $parentScenarios = parent::scenarios();
  62. $customScenarios = [
  63. 'adminChange' => ['userName', 'levelId', 'remark'],
  64. ];
  65. return array_merge($parentScenarios, $customScenarios);
  66. }
  67. /**
  68. * 赋值UserId并校验会员是否存在
  69. * @param $attribute
  70. */
  71. public function isUser($attribute){
  72. $this->_userId = Info::getUserIdByUserName($this->userName);
  73. if(!$this->_userId){
  74. $this->addError($attribute, Yii::t('ctx', 'memberDoesNotExist'));
  75. }
  76. }
  77. /**
  78. * 查看级别是否有变化
  79. * @param $attribute
  80. * @throws \yii\db\Exception
  81. */
  82. public function isLevel($attribute){
  83. $this->_fromId = Info::getEmpLv($this->_userId);
  84. if ($this->levelId == $this->_fromId) {
  85. $this->addError($attribute, Yii::t('ctx', 'levelNoChange'));
  86. }
  87. }
  88. /**
  89. * 更改最高聘级.
  90. * @return HighestEmpLevelLog|null
  91. */
  92. public function adminChange()
  93. {
  94. if (!$this->validate()) {
  95. return null;
  96. }
  97. $this->adminOperateLogger->beforeUpdate($this->_userId,'ID',['select'=>'ID,EMP_LV']);
  98. $model = new HighestEmpLevelLog();
  99. $db = \Yii::$app->db;
  100. $transaction = $db->beginTransaction();
  101. try {
  102. $period = Period::instance();
  103. // 新增数据
  104. $model->USER_ID = $this->_userId;
  105. $model->FROM_ID = $this->_fromId;
  106. $model->TO_ID = $this->levelId;
  107. $model->PERIOD_NUM = $period->getNowPeriodNum();
  108. $model->CALC_MONTH = $period->getYearMonth($period->getNowPeriodNum());
  109. $model->REMARK = $this->remark ?? '';
  110. $model->STATUS = 1;
  111. $model->ADMIN_ID = \Yii::$app->user->id;
  112. $model->CREATED_AT = Date::nowTime();
  113. if (!$model->save()) {
  114. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  115. }
  116. // 修改最高聘级
  117. User::updateAll(['EMP_LV' => $this->levelId], 'ID=:USER_ID', [':USER_ID' => $this->_userId]);
  118. $transaction->commit();
  119. } catch (Exception $e) {
  120. $transaction->rollBack();
  121. $this->addError('adminChange', $e->getMessage());
  122. return null;
  123. }
  124. $this->adminOperateLogger->afterUpdate($this->_userId,'ID',['select'=>'ID,EMP_LV'])->clean()->save([
  125. 'optType' => '调整会员最高聘级',
  126. 'userId' => $this->_userId,
  127. 'userName' => Info::getUserNameByUserId($this->_userId),
  128. 'remark' => $this->remark
  129. ]);
  130. return $model;
  131. }
  132. }