HighestEmpLevelLogForm.php 4.2 KB

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