quanwei
2025-12-09 ca425b889f3c1b5847ffc26a0229307f7f8ef43e
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
<?php
 
namespace app\common\model\branch;
 
use app\common\model\BaseModel;
 
/**
 * 商家供应商提现记录模型
 */
class Balance extends BaseModel
{
    protected $name = 'branch_user_balance';
    protected $pk = 'balance_id';
 
 
    /**
     * 关联供应商表
     */
    public function branch()
    {
        return $this->belongsTo('app\\common\\model\\branch\\Branch', 'branch_id', 'branch_id');
    }
 
    /**
     * 关联应用表
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id');
    }
 
    /**
     * 详情
     */
    public static function detail($where)
    {
        $model = new static;
        if (is_array($where)) {
            $filter = $where;
        } else {
            $filter['balance_id'] = (int)$where;
        }
        return $model->where($filter)->with(['branch', 'user'])->find();
    }
 
    /**
     * 增加余额
     */
    public function setIncBalance($branch_id,$user_id,$money)
    {
        $model = $this->where('user_id', '=', $user_id)
            ->where('branch_id', '=', $branch_id)
            ->find();
        if(!empty($detail)){
            return $model->where('user_id', '=', $user_id)
                ->where('branch_id', '=', $branch_id)
                ->inc('balance', $money)
                ->update();
        }else{
            $save_data = [
                'user_id' => $user_id,
                'branch_id' => $branch_id,
                'balance' => $money,
            ];
            return $this->save($save_data);
        }
 
    }
 
    /**
     * 减少余额
     */
    public function setDecBalance($branch_id,$user_id,$money)
    {
        return $this->where('user_id', '=', $user_id)
            ->where('branch_id', '=', $branch_id)
            ->dec('balance', $money)
            ->update();
    }
 
    /**
     * 获取总余额
     */
    public static function getTotalBalance($user_id)
    {
        $model = new static();
        return $model->where('user_id', '=', $user_id)
            ->sum("balance");
    }
}