quanwei
2025-11-26 6ae85f8ddbae19f5586f4b0c37680fe21889ef14
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
 
namespace app\common\model\branch;
 
use app\common\model\BaseModel;
use app\shop\model\auth\User as AuthUserModel;
use app\shop\model\auth\UserRole as UserRoleModel;
use app\common\model\shop\Role as RoleModel;
use app\common\enum\branch\BranchTypeEnum;
use app\common\model\settings\Region;
use app\common\model\branch\PointsLog as PointsLogModel;
 
/**
 * 商家分会模型
 */
class Branch extends BaseModel
{
    protected $name = 'branch';
    protected $pk = 'branch_id';
 
    /**
     * 追加字段
     * @var string[]
     */
    protected $append = ['region', 'parent_branch'];
 
    /**
     * 关联应用表
     */
    public function app()
    {
        return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
    }
 
    /**
     * 关联logo
     */
    public function logo()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'logo_id');
    }
 
    /**
     * 关联超管
     */
    public function superUser()
    {
        return $this->hasOne('app\\common\\model\\branch\\User', 'branch_id', 'branch_id')
            ->where('is_super','=', 1);
    }
 
    /**
     * 地区名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getRegionAttr($value, $data)
    {
        if (!isset($data['province_id'])) {
            return false;
        }
        return [
            'province' => Region::getNameById($data['province_id']),
            'city' => Region::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : Region::getNameById($data['region_id']),
        ];
    }
 
    /**
     * 所属连盟名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getParentBranchAttr($value, $data)
    {
        if (!empty($data['parent_branch_id'])) {
            return Branch::getBranchNameById($data['parent_branch_id']);
        }
        return '';
    }
 
    /**
     * 级别
     * @param $value
     * @return array
     */
    public function getBranchTypeAttr($value)
    {
        return ['text' => BranchTypeEnum::data()[$value]['name'], 'value' => $value];
    }
 
    /**
     * 详情
     */
    public static function detail($branch_id, $with = [])
    {
        return (new static())->with($with)->find($branch_id);
    }
 
    /**
     * 更新分会人数
     */
    public static function incTotal($branch_id, $inc = 1)
    {
        $res = self::detail($branch_id);
        if ($res) {
            $total = $res['member_num'] + $inc;
            $res->save(['member_num' => $total]);
        }
    }
 
    /**
     * 累积分会的可用积分
     */
    public function setIncPoints($points, $describe, $decPoints = 0)
    {
        // 新增积分变动明细
        PointsLogModel::add([
            'branch_id' => $this['branch_id'],
            'value' => $points,
            'describe' => $describe,
            'app_id' => $this['app_id'],
        ]);
 
        // 更新用户可用积分
        $data['points'] = ($this['points'] + $points + $decPoints <= 0) ? 0 : $this['points'] + $points + $decPoints;
        $this->where('branch_id', '=', $this['branch_id'])->update($data);
        
        return true;
    }
 
    /**
     * 累积分会结算金额 (批量)
     */
    public function onBatchIncBranchMoney($data)
    {
        foreach ($data as $branchId => $branchMoney) {
            $this->where(['branch_id' => $branchId])
                ->inc('total_money', $branchMoney)
                ->inc('money', $branchMoney)
                ->update();
        }
        return true;
    }
 
    //获取名称获取准确的分会
    public static function getBranchIdByName($name)
    {
        $model = new static();
        $branch = $model->where('name', '=', $name)
            ->where('is_delete', '=', 0)
            ->find();
        return empty($branch) ? '-1' : $branch["branch_id"];
    }
 
    // 根据id获取名称
    public static function getBranchNameById($branch_id)
    {
        $model = new static();
        $name = $model->where('branch_id', '=', $branch_id)
            ->where('is_delete', '=', 0)
            ->value('name');
        return $name;
    }
 
    /**
     * 分会独立收款是否开启
     */
    public static function getIndependentOpen($branch_id)
    {
        $model = new static();
        $data = $model->where('branch_id', '=', $branch_id)
            ->where('is_delete', '=', 0)
            ->find();
        return empty($data['is_independent']) ? 0 : $data['is_independent'];
    }
 
    /**
     * 验证分会名称是否重复
     */
    public static function checkExist($name)
    {
        return !!static::withoutGlobalScope()
            ->where('name', '=', $name)
            ->value('branch_id');
    }
 
    /**
     * 获取分会总数
     */
    public function getBranchTotal($branch_id = null) {
        $model = $this;
        if ($branch_id) {
            $model = $model->where('parent_branch_id', '=', $branch_id);
        }
        return $model->where('is_delete', '=', 0)->count();
    }
    
}