| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2020 广东七件事集团
- * Created by PhpStorm
- * Author: ganxiaohao
- * Date: 2020-05-11
- * Time: 15:38
- */
- namespace addons\Store\common\models;
- use common\models\user\User;
- use addons\Store\common\models\BossLevelCommon;
- use addons\Store\common\models\Boss;
- class StoreBossUserEditForm extends BaseModel
- {
- public $keyword;
- public $id;
- public $store_id = 0;
- public $level;
- public $batch_ids;
- public function rules()
- {
- return [
- [['keyword'], 'trim'],
- [['keyword'], 'string'],
- [['id', 'level', 'store_id'], 'integer'],
- [['batch_ids'], 'safe']
- ];// TODO: Change the autogenerated stub
- }
- public function getUser()
- {
- if (!$this->validate()) {
- return $this->responseErrorInfo();
- }
- $store = \Yii::$app->session->get('o2o');
- $where[]=['=','status' ,1];
- $where[]=['=','mall_id', $store['mall_id']];
- $where[]=['=','store_id', $store['id']];
- if(!empty($this->keyword)){
- $where[]=['like', 'nickname', $this->keyword];
- }
- $params['order'] = 'id desc';
- $params['where'] = $where;
- $params['limit'] = 10;
- $params['select'] = 'id ,nickname';
- $lists = User::listPage($params, false);
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'list' => $lists['list']
- ]
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return $this->responseErrorInfo();
- }
- try {
- /* @var User $user */
- $user = User::findOne(['id' => $this->id, 'status' => 1]);
- if (!$user) {
- return ['code' => 1, 'msg' => '用户不存在'];
- }
- if($this->store_id){
- $store = Store::getOne([['id'=>$this->store_id]],true);
- }else{
- $store = \Yii::$app->session->get('o2o');
- }
- $boss = StoreBoss::findOne(['user_id' => $user->id,'store_id' => $store['id'], 'status' => 1]);
- if ($boss) {
- return ['code' => 1, 'msg' => '此用户已经是该门店股东,请勿重复提交'];
- } else {
- $boss = new StoreBoss();
- $boss->mall_id = $user->mall_id;
- $boss->user_id = $user->id;
- $boss->store_id = $store['id'];
- $boss->created_at = time();
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- $boss->level=$this->level;
- $boss->status = 1;
- if ($boss->save()) {
- $t->commit();
- return ['code' => 0, 'msg' => '保存成功'];
- } else {
- return $this->responseErrorMsg($boss);
- }
- } catch (\Exception $exception) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $exception->getMessage()
- ];
- }
- } catch (\Exception $exception) {
- return [
- 'code' => 1,
- 'msg' => $exception->getMessage()
- ];
- }
- }
- public function changeLevel()
- {
- if (!$this->validate()) {
- return $this->responseErrorInfo();
- }
- try {
- $common = new StoreBossLevelCommon;
- $common->userId = $this->id;
- if ($this->level) {
- $bossLevel = $common->getBossLevelByLevel($this->level,$this->store_id);
- if (!$bossLevel) {
- throw new \Exception('无效的股东等级');
- }
- } else {
- $this->level = 0;
- }
- $res = $common->changeLevel($this->level,StoreBoss::UPGRADE_STATUS_MANUAL,$this->store_id);
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => '修改成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $res
- ];
- }
- } catch (\Exception $exception) {
- return [
- 'code' =>1,
- 'msg' => $exception->getMessage()
- ];
- }
- }
- public function batchLevel()
- {
- if (!$this->validate()) {
- return $this->responseErrorInfo();
- }
- try {
- if ($this->level) {
- $common = new StoreBossLevelCommon;
- $bossLevel = $common->getBossLevelByLevel($this->level);
- if (!$bossLevel) {
- throw new \Exception('无效的股东等级');
- }
- } else {
- $this->level = 0;
- }
- StoreBoss::updateAll(
- ['level' => $this->level, 'upgrade_level_at' => time()],
- ['id' => $this->batch_ids]
- );
- return [
- 'code' => 0,
- 'msg' => '修改成功'
- ];
- } catch (\Exception $exception) {
- return [
- 'code' => 1,
- 'msg' => $exception->getMessage()
- ];
- }
- }
- }
|