quanwei
2025-12-12 b8961f178740f99ce54cfcbfd88235eaf8b79872
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
 
namespace app\api\model\branch;
 
use app\common\exception\BaseException;
use app\common\model\branch\Branch as BranchModel;
use app\common\model\branch\User as BranchUserModel;
 
/**
 * 分会模型
 */
class Branch extends BranchModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'app_id',
        'update_time'
    ];
 
    /**
     * 获取列表
     */
    public function getList($params = [])
    {
        $model = $this;
        $list = $model ->with(['logo'])
            ->where('is_delete', '=', 0)
            ->where('is_recycle', '=', 0)
            ->order(['sort' => 'desc', 'create_time' => 'desc'])
            ->paginate($params);
        return $list;
    }
 
    /**
     * 管理后台-获取列表数据
     */
    public function getListForAdmin($params, $branch_id)
    {
        $model = $this;
        if (isset($params['keyword']) && $params['keyword']) {
            $model = $model->where('name', 'like', '%' . $params['keyword'] . '%');
        }
        if(isset($params['branch_ids'])&&$params['branch_ids']){
            $model = $model->where('branch_id', 'in', $params['branch_ids']);
        }
        if(isset($params['is_takeout']) && $params['is_takeout'] > -1){
            $model = $model->where('is_takeout', '=', $params['is_takeout']);
        }
        // 查询列表数据
        return $model->with(['superUser'])
            ->where('parent_branch_id', '=', $branch_id)
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
 
    /**
     * 管理后台-添加
     */
    public function add($data, $branch_id)
    {
        // 开启事务
        $this->startTrans();
        try {
            if (BranchModel::checkExist($data['name'])) {
                $this->error = '分会名称已存在,请更换';
                return false;
            }
            if (BranchUserModel::checkExist($data['user_name'])) {
                $this->error = '登录账号已存在,请更换';
                return false;
            }
            if (BranchUserModel::isManager($data['user_id'])) {
                $this->error = '绑定的用户已经是管理员,请更换';
                return false;
            }
            // 添加分会
            $data['app_id'] = self::$app_id;
            $data['parent_branch_id'] = $branch_id;
            $this->save($data);
            // 添加登录用户
            $user_model = new BranchUserModel();
            $user_model->save([
                'user_id' => $data['user_id'],
                'user_name' => $data['user_name'],
                'password' => salt_hash($data['password']),
                'real_name' => $data['link_name'],
                'branch_id' => $this['branch_id'],
                'is_super' => 1,
                'app_id' => self::$app_id,
            ]);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 管理后台-修改
     */
    public function edit($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            if ($this['superUser'] && $data['user_name'] != $this['superUser']['user_name'] && BranchUserModel::checkExist($data['user_name'])) {
                $this->error = '登录账号已存在';
                return false;
            }
 
            $user = BranchUserModel::detail(['user_id' => $data['user_id']]);
            if ($user && $user['branch_id'] != $this['branch_id']) {
                $this->error = '绑定的用户已经是管理员,请更换';
                return false;
            }
 
            // 修改分会
            $this->save([
                'name' => $data['name'],
                'link_name' => $data['link_name'],
                'link_phone' => $data['link_phone'],
                'province_id' => $data['province_id'],
                'city_id' => $data['city_id'],
                'region_id' => $data['region_id'],
                'user_id' => $data['user_id'],
            ]);
            
            // 修改登录用户
            $user_model = $this['superUser'];
            $user_data = [
                'user_id' => $data['user_id'],
                'user_name' => $data['user_name']
            ];
            if (isset($data['password']) && !empty($data['password'])) {
                $user_data['password'] = salt_hash($data['password']);
            }
            $user_model->save($user_data);
            
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 管理后台-软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
 
}