IntegralBill.php
2.49 KB
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
<?php
namespace App\Models;
use App\Http\Filters\IntegralBillFilter;
use App\Http\Filters\QueryFilter;
use http\Env\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class IntegralBill extends Model
{
public $timestamps = false;
public function integral()
{
return $this->belongsTo(Integral::class, 'integral_id', 'id');
}
public function scopePage($query, $page = 1, $pageSize = 10)
{
return $query->offset(($page - 1) * $pageSize)->limit($pageSize);
}
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}
//获取用户兑换红包列表
public function getIntegralBillList($page, $pageSize, $filter)
{
$request = new \Illuminate\Http\Request();
$bills = IntegralBill::with('integral')->filter($filter)->page($page, $pageSize)
->orderBy('id', 'desc')
->get()->toArray();
$count = IntegralBill::filter($filter)->count();
return ['data' => $bills, 'count' => $count];
}
public function createIntegralBill($data = [])
{
$res = DB::table('integral_bills')->insert($data);
return $res;
}
//更新用户红包兑换列表
public function updateIntegralBill($id, $data = [])
{
$res = DB::table('integral_bills')->where('id', $id)
->update($data);
return $res;
}
//删除
public function deleteIntegralBill($ids = [])
{
$res = DB::table('integral_bills')->whereIn('id', $ids)->delete();
return $res;
}
//获取用户红包统计信息
public function getUserIntegralStatistics($userId)
{
//一共获得的积分金额
$totalAmount = DB::table('integral_bills')
->leftJoin('integrals', 'integral_bills.integral_id', 'integrals.id')
->where('user_id', $userId)
->sum('amount');
//已经兑换的金额
$exchangedAmount = DB::table('user_exchanges')
->leftJoin('exchange_settings', 'user_exchanges.exchange_id', 'exchange_settings.id')
->where('user_id', $userId)
->sum('amount');
//剩余可兑换积分金额
$integral = DB::table('user_integrals')
->where('user_id', $userId)->value('integral');
return [
'total_amount' => $totalAmount,
'exchanged_amount' => $exchangedAmount,
'integral' => $integral,
];
}
}