CalculatedAmount.php
3.6 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace app\common\library;
use app\api\model\Level;
use app\api\model\MoneyLog;
use app\api\model\order\Order;
use app\api\model\promoter\PromoterLast;
use app\api\model\User;
use app\api\model\WithdrawalLog;
use EasyWeChat\Factory;
/**
* 计算方法
*/
class CalculatedAmount {
/**
* 计算可提现金额
*/
public static function calculation( $user_id ) {
$moneyModel = new MoneyLog();
$withdrawalModel = new WithdrawalLog();
$userModel = new User();
//计算退回直推佣金
$one = $moneyModel->where( ['user_id' => $user_id, 'type' => 'out', 'commission_type' => 1] )->sum( 'money' );
//计算退回推广佣金
$two = $moneyModel->where( ['user_id' => $user_id, 'type' => 'out', 'commission_type' => 2] )->sum( 'money' );
//①计算用户获得的直推佣金
$direct_get_money = $moneyModel->where( ['user_id' => $user_id, 'type' => 'get', 'commission_type' => 1] )->sum( 'money' );
//②计算用户获得的推广佣金
$wide_get_money = $moneyModel->where( ['user_id' => $user_id, 'type' => 'get', 'commission_type' => 2] )->sum( 'money' );
//③计算用户的退回佣金
$out_money = $moneyModel->where( ['user_id' => $user_id, 'type' => 'out'] )->sum( 'money' );
//④计算用户的提现总额
$total_money = $withdrawalModel->where( ['user_id' => $user_id, 'status' => 1] )->sum( 'withdrawal_price' );
//计算用户可提现金额
$user_total_money = $direct_get_money + $wide_get_money - $out_money - $total_money;
//更新用户信息
$userModel->where( 'id', $user_id )->update( [
'withdrawable_cash_price' => $user_total_money, //可提现金额
'cumulative_withdrawal_price' => $total_money, //累计提现金额
'direct_commission' => $direct_get_money - $one, //累计直推佣金
'promotion_commission' => $wide_get_money - $two, //累计推广佣金
] );
}
/**
* 计算用户级别
*/
public static function calculation_level( $user_id ) {
$userModel = new User();
$orderModel = new Order();
$levelModel = new Level();
$last_user_model = new PromoterLast();
$level = $levelModel->select();
//用户的下级用户
$last_user_ids = $last_user_model->where( 'user_id', $user_id )->column( 'last_user_id' );
//用户成为推广员时间
$user_become_time = $userModel->where( 'id', $user_id )->value( 'become_promoter_time' );
//成为推广员后的订单才算
$where['createtime'] = ['>', $user_become_time];
$where['user_id'] = ['=', $user_id];
$where['status'] = ['=', '2'];
//计算用户订单总金额
$user_total_order = $orderModel->where( $where )->sum( 'pay_price' );
//计算用户购买成功单量
$user_count_order = $orderModel->where( $where )->count();
//计算用户下一级订单总金额
$last_total_order = $orderModel->whereIn( 'user_id', $last_user_ids )->sum( 'pay_price' );
//计算用户下一级购买成功单量
$last_count_order = $orderModel->whereIn( 'user_id', $last_user_ids )->count();
//个人及直属下一级的累积金额
$total_money = $user_total_order + $last_total_order;
//个人及直属下一级的累积单量
$total_order = $user_count_order + $last_count_order;
//用户等级
$user_level = 0;
//判断用户是否满足升级条件
foreach ( $level as &$item ) {
if ( $item['cumulative_amount'] <= $user_total_order && $item['cumulative_order'] <= $user_count_order && $item['last_cumulative_amount'] <= $total_money && $item['last_cumulative_order'] <= $total_order ) {
$user_level = $item['level'];
} else {
break;
}
}
$userModel->where( 'id', $user_id )->update( ['level' => $user_level] );
}
}