UserDecForm.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 UserDecForm extends Model {
  13. public $userIds;
  14. public $isDec;
  15. public $isAtlas;
  16. public $isRecharge;
  17. public function init() {
  18. parent::init();
  19. $this->adminOperateLogger = new AdminOperate([
  20. 'fetchClass' => User::class,
  21. ]);
  22. }
  23. /**
  24. * @inheritdoc
  25. */
  26. public function rules() {
  27. return [
  28. [['userIds', 'isDec', 'isAtlas', 'isRecharge'], 'trim'],
  29. [['userIds'], 'required'],
  30. ];
  31. }
  32. /**
  33. * 指定校验场景
  34. * @return array
  35. */
  36. public function scenarios() {
  37. $parentScenarios = parent::scenarios();
  38. $customScenarios = [
  39. 'isDec' => ['userIds', 'isDec'],
  40. 'isAtlas' => ['userIds', 'isAtlas'],
  41. 'isRecharge' => ['userIds', 'isRecharge'],
  42. ];
  43. return array_merge($parentScenarios, $customScenarios);
  44. }
  45. public function attributeLabels() {
  46. return [
  47. 'userIds' => '会员ID',
  48. 'isDec' => '是否报单中心',
  49. 'isAtlas' => '是否显示图谱',
  50. 'isRecharge' => '是否显示充值',
  51. ];
  52. }
  53. public function beforeValidate() {
  54. return parent::beforeValidate();
  55. }
  56. /**
  57. * 是否报单中心
  58. * @return null
  59. * @throws \yii\db\Exception
  60. */
  61. public function isDec() {
  62. if (!$this->validate()) {
  63. return null;
  64. }
  65. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_DEC']);
  66. $db = \Yii::$app->db;
  67. $transaction = $db->beginTransaction();
  68. try {
  69. $allUidIn = implode("','", $this->userIds);
  70. if (!User::updateAll(['IS_DEC' => $this->isDec ? $this->isDec : 0], "ID IN ('" . $allUidIn . "')")) {
  71. throw new Exception('设置报单中心更新失败');
  72. }
  73. $transaction->commit();
  74. } catch (Exception $e) {
  75. $transaction->rollBack();
  76. return null;
  77. }
  78. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_DEC']);
  79. $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
  80. 'optType' => $this->isDec ? '设置报单中心' : '取消报单中心',
  81. ]);
  82. return $this->userIds;
  83. }
  84. /**
  85. * 是否显示图谱
  86. * @return null
  87. * @throws \yii\db\Exception
  88. */
  89. public function isAtlas() {
  90. if (!$this->validate()) {
  91. return null;
  92. }
  93. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_ATLAS']);
  94. $db = \Yii::$app->db;
  95. $transaction = $db->beginTransaction();
  96. try {
  97. $allUidIn = implode("','", $this->userIds);
  98. if (!User::updateAll(['IS_ATLAS' => $this->isAtlas ? $this->isAtlas : 0], "ID IN ('" . $allUidIn . "')")) {
  99. throw new Exception('设置图谱更新失败');
  100. }
  101. $transaction->commit();
  102. } catch (Exception $e) {
  103. $transaction->rollBack();
  104. return null;
  105. }
  106. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_ATLAS']);
  107. $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
  108. 'optType' => $this->isAtlas ? '显示图谱' : '隐藏图谱',
  109. ]);
  110. return $this->userIds;
  111. }
  112. /**
  113. * 是否显示充值
  114. * @return null
  115. * @throws \yii\db\Exception
  116. */
  117. public function isRecharge() {
  118. if (!$this->validate()) {
  119. return null;
  120. }
  121. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_RECHARGE']);
  122. $db = \Yii::$app->db;
  123. $transaction = $db->beginTransaction();
  124. try {
  125. $allUidIn = implode("','", $this->userIds);
  126. if (!User::updateAll(['IS_RECHARGE' => $this->isRecharge ? $this->isRecharge : 0], "ID IN ('" . $allUidIn . "')")) {
  127. throw new Exception('充值管理设置更新失败');
  128. }
  129. $transaction->commit();
  130. } catch (Exception $e) {
  131. $transaction->rollBack();
  132. return null;
  133. }
  134. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_RECHARGE']);
  135. $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
  136. 'optType' => $this->isRecharge ? '显示充值' : '隐藏充值',
  137. ]);
  138. return $this->userIds;
  139. }
  140. }