AgentAttrsDayMongoRepository.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Mongo\AgentAttrsTodayModel;
  4. use App\Models\Mongo\PayLogsModel;
  5. use App\Models\Mongo\UsersModel;
  6. use App\Models\Mysql\AgentAttrsDayModel;
  7. use App\Models\Mysql\CommissionLogModel;
  8. use App\Models\Mysql\WithDrawalLogModel;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\Redis;
  11. use KZ2\Laravel\Repositories\Eloquent\Repository;
  12. class AgentAttrsDayMongoRepository extends Repository
  13. {
  14. public function model()
  15. {
  16. return AgentAttrsTodayModel::class;
  17. }
  18. public function sumTotalData($starDate, $endDate)
  19. {
  20. $mongoSatarDate = toMongoIsoDate($starDate);
  21. $mongoEndDate = toMongoIsoDate(Carbon::parse($endDate)->subSeconds(1));
  22. // var_dump(mongoIsoDateToCarbonTime($mongoSatarDate), mongoIsoDateToCarbonTime($mongoEndDate));
  23. //获取范围内总数据
  24. $totalData = AgentAttrsTodayModel::raw(function ($query) use ($mongoSatarDate, $mongoEndDate) {
  25. return $query->aggregate([
  26. [
  27. '$match' => [
  28. 'date' => [
  29. '$gte' => $mongoSatarDate,
  30. '$lte' => $mongoEndDate,
  31. ],
  32. 'agent_uid' => ''
  33. ],
  34. ],
  35. [
  36. '$group' => [
  37. "_id" => '$id',
  38. 'order_success_money' => ['$sum' => '$td_order_success_money'], //成功总金额
  39. ]
  40. ],
  41. ]);
  42. })->toArray();
  43. //mysql代理统计数据
  44. $agentTodayDataMysql = AgentAttrsDayModel::whereBetween('td_date', [$starDate, $endDate])
  45. ->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();
  46. // 获取代理返佣金额
  47. $agCommissionMoney = CommissionLogModel::query()->whereBetween('date', [$starDate, $endDate])->sum('commission_money');
  48. //提现金额
  49. $withAmount = WithDrawalLogModel::whereBetween('updated_at', [$starDate, $endDate])->where('status', 3)->sum('amount');
  50. //注册人数
  51. $resgCount = UsersModel::query()->whereBetween('created_at', [$mongoSatarDate, $mongoEndDate])->count();
  52. //总注册人数
  53. $totalCount = UsersModel::count();
  54. //充值人数
  55. $rechCount = PayLogsModel::query()->groupBy('user_uid')->whereBetween('created_at', [$mongoSatarDate, $mongoEndDate])->where('pay_status', 1)->get(['user_uid']);
  56. //今日收入(后台所有充值)
  57. $totalOrderSuccessMoney = $totalData[0]['order_success_money'] ?? 0;
  58. // 统计今日或者时间段日活
  59. $todayDauOne = 0;
  60. $todayDateObj = Carbon::now()->startOfDay();
  61. if (empty($starDate) && empty($endDate)) {
  62. $todayDauOne = AgentAttrsTodayModel::where('date', '>=', $todayDateObj)->where(['agent_uid' => ''])->orderBy('updated_at', 'ASC')->first('td_dau_total');
  63. } elseif (!empty($starDate) && empty($endDate)) {
  64. $startDateObj = Carbon::parse($starDate)->startOfDay();
  65. $someDaysDau = AgentAttrsTodayModel::where('date', '>=', $startDateObj)->where(['agent_uid' => ''])->orderBy('updated_at', 'ASC')->first('td_dau_total');
  66. } elseif (empty($starDate) && !empty($endDate)) {
  67. $endDateObj = Carbon::parse($endDate)->endOfDay()->subSeconds(1);
  68. $someDaysDau = AgentAttrsTodayModel::where('date', '<=', $endDateObj)->where(['agent_uid' => ''])->orderBy('updated_at', 'ASC')->first('td_dau_total');
  69. } else {
  70. $startDateObj = Carbon::parse($starDate)->startOfDay();
  71. $endDateObj = Carbon::parse($endDate)->endOfDay()->subSeconds(1);
  72. $someDaysDau = AgentAttrsTodayModel::whereBetween('date', [$startDateObj, $endDateObj])->where(['agent_uid' => ''])->sum('td_dau_total');
  73. }
  74. $toDayDau = $todayDauOne + $someDaysDau;
  75. $res = [
  76. //总收入
  77. 'total_income' => $totalOrderSuccessMoney,
  78. //代理总合(代理线下充值总和 包含扣量)
  79. 'total_agent_income' => (int)$agentTodayDataMysql->order_success_money,
  80. //代理抽成盈利 (代理充值金额-代理抽成)
  81. 'agent_commission_money' => $agentTodayDataMysql->order_success_money - $agCommissionMoney,
  82. //平台收益 (平台用户充值 = 总充值-代理充值)
  83. 'platform_revenue' => $totalOrderSuccessMoney - $agentTodayDataMysql->order_success_money,
  84. //平台总收益 (= 平台收益 - 今日代理抽成盈利)
  85. 'platform_total_revenue' => $totalOrderSuccessMoney - $agCommissionMoney,
  86. //扣量金额
  87. 'deduction_money' => (int)$agentTodayDataMysql->deduction_money,
  88. //提现金额
  89. 'withdraw_money' => (int)$withAmount,
  90. //注册人数
  91. 'reg_user' => $resgCount,
  92. //充值人数
  93. 'pay_user' => count($rechCount),
  94. //日活
  95. 'dau_total' => (int)$toDayDau,
  96. //总注册
  97. 'total_reg' => $totalCount,
  98. ];
  99. return $res;
  100. }
  101. }