| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Repositories;
- use App\Models\Mongo\AgentAttrsTodayModel;
- use App\Models\Mongo\PayLogsModel;
- use App\Models\Mongo\UsersModel;
- use App\Models\Mysql\AgentAttrsDayModel;
- use App\Models\Mysql\CommissionLogModel;
- use App\Models\Mysql\WithDrawalLogModel;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Redis;
- use KZ2\Laravel\Repositories\Eloquent\Repository;
- class AgentAttrsDayMongoRepository extends Repository
- {
- public function model()
- {
- return AgentAttrsTodayModel::class;
- }
- public function sumTotalData($starDate, $endDate)
- {
- $mongoSatarDate = toMongoIsoDate($starDate);
- $mongoEndDate = toMongoIsoDate(Carbon::parse($endDate)->subSeconds(1));
- // var_dump(mongoIsoDateToCarbonTime($mongoSatarDate), mongoIsoDateToCarbonTime($mongoEndDate));
- //获取范围内总数据
- $totalData = AgentAttrsTodayModel::raw(function ($query) use ($mongoSatarDate, $mongoEndDate) {
- return $query->aggregate([
- [
- '$match' => [
- 'date' => [
- '$gte' => $mongoSatarDate,
- '$lte' => $mongoEndDate,
- ],
- 'agent_uid' => ''
- ],
- ],
- [
- '$group' => [
- "_id" => '$id',
- 'order_success_money' => ['$sum' => '$td_order_success_money'], //成功总金额
- ]
- ],
- ]);
- })->toArray();
- //mysql代理统计数据
- $agentTodayDataMysql = AgentAttrsDayModel::whereBetween('td_date', [$starDate, $endDate])
- ->selectRaw('sum(td_order_success_money) as order_success_money, sum(td_deduction_success_money) as deduction_success_money, sum(td_deduction_money) as deduction_money')->first();
- // 获取代理返佣金额
- $agCommissionMoney = CommissionLogModel::query()->whereBetween('date', [$starDate, $endDate])->sum('commission_money');
- //提现金额
- $withAmount = WithDrawalLogModel::whereBetween('updated_at', [$starDate, $endDate])->where('status', 3)->sum('amount');
- //注册人数
- $resgCount = UsersModel::query()->whereBetween('created_at', [$mongoSatarDate, $mongoEndDate])->count();
- //总注册人数
- $totalCount = UsersModel::count();
- //充值人数
- $rechCount = PayLogsModel::query()->groupBy('user_uid')->whereBetween('created_at', [$mongoSatarDate, $mongoEndDate])->where('pay_status', 1)->get(['user_uid']);
- //今日收入(后台所有充值)
- $totalOrderSuccessMoney = $totalData[0]['order_success_money'] ?? 0;
- // 统计今日或者时间段日活
- $todayDauOne = 0;
- $todayDateObj = Carbon::now()->startOfDay();
- if (empty($starDate) && empty($endDate)) {
- $todayDauOne = AgentAttrsTodayModel::where('date', '>=', $todayDateObj)->where(['agent_uid' => ''])->orderBy('updated_at', 'ASC')->first('td_dau_total');
- } elseif (!empty($starDate) && empty($endDate)) {
- $startDateObj = Carbon::parse($starDate)->startOfDay();
- $someDaysDau = AgentAttrsTodayModel::where('date', '>=', $startDateObj)->where(['agent_uid' => ''])->orderBy('updated_at', 'ASC')->first('td_dau_total');
- } elseif (empty($starDate) && !empty($endDate)) {
- $endDateObj = Carbon::parse($endDate)->endOfDay()->subSeconds(1);
- $someDaysDau = AgentAttrsTodayModel::where('date', '<=', $endDateObj)->where(['agent_uid' => ''])->orderBy('updated_at', 'ASC')->first('td_dau_total');
- } else {
- $startDateObj = Carbon::parse($starDate)->startOfDay();
- $endDateObj = Carbon::parse($endDate)->endOfDay()->subSeconds(1);
- $someDaysDau = AgentAttrsTodayModel::whereBetween('date', [$startDateObj, $endDateObj])->where(['agent_uid' => ''])->sum('td_dau_total');
- }
- $toDayDau = $todayDauOne + $someDaysDau;
- $res = [
- //总收入
- 'total_income' => $totalOrderSuccessMoney,
- //代理总合(代理线下充值总和 包含扣量)
- 'total_agent_income' => (int)$agentTodayDataMysql->order_success_money,
- //代理抽成盈利 (代理充值金额-代理抽成)
- 'agent_commission_money' => $agentTodayDataMysql->order_success_money - $agCommissionMoney,
- //平台收益 (平台用户充值 = 总充值-代理充值)
- 'platform_revenue' => $totalOrderSuccessMoney - $agentTodayDataMysql->order_success_money,
- //平台总收益 (= 平台收益 - 今日代理抽成盈利)
- 'platform_total_revenue' => $totalOrderSuccessMoney - $agCommissionMoney,
- //扣量金额
- 'deduction_money' => (int)$agentTodayDataMysql->deduction_money,
- //提现金额
- 'withdraw_money' => (int)$withAmount,
- //注册人数
- 'reg_user' => $resgCount,
- //充值人数
- 'pay_user' => count($rechCount),
- //日活
- 'dau_total' => (int)$toDayDau,
- //总注册
- 'total_reg' => $totalCount,
- ];
- return $res;
- }
- }
|