UserGroupForm.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\libs\logging\operate\AdminOperate;
  6. use common\models\User;
  7. use common\models\UserInfo;
  8. use yii\base\Exception;
  9. /**
  10. * Login form
  11. */
  12. class UserGroupForm extends Model {
  13. public $userIds;
  14. public $isGroup;
  15. public function init() {
  16. parent::init();
  17. $this->adminOperateLogger = new AdminOperate([
  18. 'fetchClass' => UserInfo::class,
  19. ]);
  20. }
  21. /**
  22. * @inheritdoc
  23. */
  24. public function rules() {
  25. return [
  26. [['userIds', 'isGroup'], 'trim'],
  27. [['userIds'], 'required'],
  28. ];
  29. }
  30. /**
  31. * 指定校验场景
  32. * @return array
  33. */
  34. public function scenarios() {
  35. $parentScenarios = parent::scenarios();
  36. $customScenarios = [
  37. 'group' => ['userIds', 'isGroup'],
  38. ];
  39. return array_merge($parentScenarios, $customScenarios);
  40. }
  41. public function attributeLabels() {
  42. return [
  43. 'userIds' => '会员ID',
  44. 'isGroup' => '是否团队领导人',
  45. ];
  46. }
  47. public function beforeValidate() {
  48. return parent::beforeValidate();
  49. }
  50. /**
  51. * 是否团队领导人
  52. * @return null
  53. * @throws \yii\db\Exception
  54. */
  55. public function group() {
  56. if (!$this->validate()) {
  57. return null;
  58. }
  59. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'USER_ID',['select'=>'USER_ID,IS_GROUP_LEADER,GROUP_LEADER_AT']);
  60. $db = \Yii::$app->db;
  61. $transaction = $db->beginTransaction();
  62. try {
  63. $allUidIn = implode("','", $this->userIds);
  64. if (!UserInfo::updateAll(['IS_GROUP_LEADER' => $this->isGroup ? $this->isGroup : 0, 'GROUP_LEADER_AT' => Date::nowTime()], "USER_ID IN ('" . $allUidIn . "')")) {
  65. throw new Exception('团队领导人更新失败');
  66. }
  67. $transaction->commit();
  68. } catch (Exception $e) {
  69. $transaction->rollBack();
  70. return null;
  71. }
  72. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'USER_ID',['select'=>'USER_ID,IS_GROUP_LEADER,GROUP_LEADER_AT']);
  73. $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
  74. 'optType' => $this->isGroup ? '设置团队领导人' : '取消团队领导人',
  75. ]);
  76. return $this->userIds;
  77. }
  78. }