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"); } }