|
|
@@ -484,4 +484,109 @@ class UserLevelServices extends BaseServices
|
|
|
$list = $data['list'] ?? [];
|
|
|
return $list;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 等级分红
|
|
|
+ * @return string
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ */
|
|
|
+ public function levelDividend()
|
|
|
+ {
|
|
|
+ // 获取今天的开始和结束时间戳
|
|
|
+ $todayStart = strtotime(date('Y-m-d 00:00:00'));
|
|
|
+ $todayEnd = strtotime(date('Y-m-d 23:59:59'));
|
|
|
+
|
|
|
+ // 获取今天所有礼包订单的总金额
|
|
|
+ /** @var \app\model\order\StoreOrder $orderModel */
|
|
|
+ $orderModel = new \app\model\order\StoreOrder();
|
|
|
+ $totalGiftAmount = $orderModel->where([
|
|
|
+ ['paid', '=', 1],
|
|
|
+ ['is_del', '=', 0],
|
|
|
+ ['refund_status', '=', 0],
|
|
|
+ ['is_lb', '=', 1],
|
|
|
+ ['add_time', '>=', $todayStart],
|
|
|
+ ['add_time', '<=', $todayEnd]
|
|
|
+ ])->sum('pay_price');
|
|
|
+
|
|
|
+ if ($totalGiftAmount <= 0) {
|
|
|
+ return '今日无礼包订单金额';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取所有分红比例大于0的等级
|
|
|
+ /** @var SystemUserLevelServices $systemLevelServices */
|
|
|
+ $systemLevelServices = app()->make(SystemUserLevelServices::class);
|
|
|
+ $levels = $systemLevelServices->getList([
|
|
|
+ ['is_del', '=', 0],
|
|
|
+ ['is_show', '=', 1],
|
|
|
+ ['radio', '>', 0]
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if (!$levels) {
|
|
|
+ return '无分红等级';
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @var UserServices $userServices */
|
|
|
+ $userServices = app()->make(UserServices::class);
|
|
|
+ /** @var UserBrokerageServices $brokerageServices */
|
|
|
+ $brokerageServices = app()->make(UserBrokerageServices::class);
|
|
|
+
|
|
|
+ $totalDivided = 0;
|
|
|
+
|
|
|
+ foreach ($levels as $level) {
|
|
|
+ $radio = $level['radio'];
|
|
|
+ $levelId = $level['id'];
|
|
|
+
|
|
|
+ // 计算该等级能分到的总金额
|
|
|
+ $levelTotalAmount = bcmul((string)$totalGiftAmount, bcdiv((string)$radio, '100', 4), 2);
|
|
|
+
|
|
|
+ if ($levelTotalAmount <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取该等级的用户数量
|
|
|
+ $users = $userServices->getColumn([
|
|
|
+ ['level', '=', $levelId],
|
|
|
+ ['status', '=', 1],
|
|
|
+ ['is_del', '=', 0]
|
|
|
+ ], 'uid', 'uid');
|
|
|
+
|
|
|
+ if (!$users) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $userCount = count($users);
|
|
|
+
|
|
|
+ // 计算每个用户能分到的金额
|
|
|
+ $userAmount = bcdiv($levelTotalAmount, (string)$userCount, 2);
|
|
|
+
|
|
|
+ if ($userAmount <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 为该等级的每个用户分红
|
|
|
+ foreach ($users as $uid) {
|
|
|
+ $userInfo = $userServices->getUserInfo($uid);
|
|
|
+ if (!$userInfo) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $currentBrokerage = $userInfo['brokerage_price'] ?? 0;
|
|
|
+ $newBrokerage = bcadd((string)$currentBrokerage, $userAmount, 2);
|
|
|
+
|
|
|
+ // 更新用户佣金
|
|
|
+ $userServices->update($uid, ['brokerage_price' => $newBrokerage], 'uid');
|
|
|
+
|
|
|
+ // 添加佣金记录
|
|
|
+ $brokerageServices->income('level_dividend', $uid, [
|
|
|
+ 'number' => $userAmount
|
|
|
+ ], $newBrokerage, 0);
|
|
|
+
|
|
|
+ $totalDivided = bcadd((string)$totalDivided, $userAmount, 2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return '分红完成,共分红金额:' . $totalDivided . '元';
|
|
|
+ }
|
|
|
}
|