StoreBossUserEditForm.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: ganxiaohao
  7. * Date: 2020-05-11
  8. * Time: 15:38
  9. */
  10. namespace addons\Store\common\models;
  11. use common\models\user\User;
  12. use addons\Store\common\models\BossLevelCommon;
  13. use addons\Store\common\models\Boss;
  14. class StoreBossUserEditForm extends BaseModel
  15. {
  16. public $keyword;
  17. public $id;
  18. public $store_id = 0;
  19. public $level;
  20. public $batch_ids;
  21. public function rules()
  22. {
  23. return [
  24. [['keyword'], 'trim'],
  25. [['keyword'], 'string'],
  26. [['id', 'level', 'store_id'], 'integer'],
  27. [['batch_ids'], 'safe']
  28. ];// TODO: Change the autogenerated stub
  29. }
  30. public function getUser()
  31. {
  32. if (!$this->validate()) {
  33. return $this->responseErrorInfo();
  34. }
  35. $store = \Yii::$app->session->get('o2o');
  36. $where[]=['=','status' ,1];
  37. $where[]=['=','mall_id', $store['mall_id']];
  38. $where[]=['=','store_id', $store['id']];
  39. if(!empty($this->keyword)){
  40. $where[]=['like', 'nickname', $this->keyword];
  41. }
  42. $params['order'] = 'id desc';
  43. $params['where'] = $where;
  44. $params['limit'] = 10;
  45. $params['select'] = 'id ,nickname';
  46. $lists = User::listPage($params, false);
  47. return [
  48. 'code' => 0,
  49. 'msg' => '',
  50. 'data' => [
  51. 'list' => $lists['list']
  52. ]
  53. ];
  54. }
  55. public function save()
  56. {
  57. if (!$this->validate()) {
  58. return $this->responseErrorInfo();
  59. }
  60. try {
  61. /* @var User $user */
  62. $user = User::findOne(['id' => $this->id, 'status' => 1]);
  63. if (!$user) {
  64. return ['code' => 1, 'msg' => '用户不存在'];
  65. }
  66. if($this->store_id){
  67. $store = Store::getOne([['id'=>$this->store_id]],true);
  68. }else{
  69. $store = \Yii::$app->session->get('o2o');
  70. }
  71. $boss = StoreBoss::findOne(['user_id' => $user->id,'store_id' => $store['id'], 'status' => 1]);
  72. if ($boss) {
  73. return ['code' => 1, 'msg' => '此用户已经是该门店股东,请勿重复提交'];
  74. } else {
  75. $boss = new StoreBoss();
  76. $boss->mall_id = $user->mall_id;
  77. $boss->user_id = $user->id;
  78. $boss->store_id = $store['id'];
  79. $boss->created_at = time();
  80. }
  81. $t = \Yii::$app->db->beginTransaction();
  82. try {
  83. $boss->level=$this->level;
  84. $boss->status = 1;
  85. if ($boss->save()) {
  86. $t->commit();
  87. return ['code' => 0, 'msg' => '保存成功'];
  88. } else {
  89. return $this->responseErrorMsg($boss);
  90. }
  91. } catch (\Exception $exception) {
  92. $t->rollBack();
  93. return [
  94. 'code' => 1,
  95. 'msg' => $exception->getMessage()
  96. ];
  97. }
  98. } catch (\Exception $exception) {
  99. return [
  100. 'code' => 1,
  101. 'msg' => $exception->getMessage()
  102. ];
  103. }
  104. }
  105. public function changeLevel()
  106. {
  107. if (!$this->validate()) {
  108. return $this->responseErrorInfo();
  109. }
  110. try {
  111. $common = new StoreBossLevelCommon;
  112. $common->userId = $this->id;
  113. if ($this->level) {
  114. $bossLevel = $common->getBossLevelByLevel($this->level,$this->store_id);
  115. if (!$bossLevel) {
  116. throw new \Exception('无效的股东等级');
  117. }
  118. } else {
  119. $this->level = 0;
  120. }
  121. $res = $common->changeLevel($this->level,StoreBoss::UPGRADE_STATUS_MANUAL,$this->store_id);
  122. if ($res) {
  123. return [
  124. 'code' => 0,
  125. 'msg' => '修改成功'
  126. ];
  127. } else {
  128. return [
  129. 'code' => 1,
  130. 'msg' => $res
  131. ];
  132. }
  133. } catch (\Exception $exception) {
  134. return [
  135. 'code' =>1,
  136. 'msg' => $exception->getMessage()
  137. ];
  138. }
  139. }
  140. public function batchLevel()
  141. {
  142. if (!$this->validate()) {
  143. return $this->responseErrorInfo();
  144. }
  145. try {
  146. if ($this->level) {
  147. $common = new StoreBossLevelCommon;
  148. $bossLevel = $common->getBossLevelByLevel($this->level);
  149. if (!$bossLevel) {
  150. throw new \Exception('无效的股东等级');
  151. }
  152. } else {
  153. $this->level = 0;
  154. }
  155. StoreBoss::updateAll(
  156. ['level' => $this->level, 'upgrade_level_at' => time()],
  157. ['id' => $this->batch_ids]
  158. );
  159. return [
  160. 'code' => 0,
  161. 'msg' => '修改成功'
  162. ];
  163. } catch (\Exception $exception) {
  164. return [
  165. 'code' => 1,
  166. 'msg' => $exception->getMessage()
  167. ];
  168. }
  169. }
  170. }