小说推广数据系统

OmDataService.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Services;
  3. use App\Models\AdsReport;
  4. use App\Models\Order;
  5. class OmDataService
  6. {
  7. /**
  8. * 获取每日运营数据
  9. * @param $startDate string 起始时间
  10. * @param $endDate string 截止时间
  11. * */
  12. public static function getDataOfDay($startDate, $endDate, $page, $pageSize)
  13. {
  14. # 获取消耗数据
  15. list($list, $count) = AdsReport::getPaidOfDays($startDate, $endDate, $page, $pageSize);
  16. # 本年度用户关注起始时间
  17. $userCreatedAt = date('Y-01-01 00:00:00');
  18. # 当前月份
  19. $currentMonth = is_null($endDate) ? date('m') : date('m', strtotime($endDate));
  20. # 上年度年份
  21. $lastYear = date('Y', strtotime('-1 year'));
  22. $lastYearSt = $lastYear . '-01-01 00:00:00';
  23. $lastYearEt = $lastYear . '-12-31 23:59:59';
  24. foreach ($list as $item) {
  25. $recycleTotal = 0;
  26. $refDate = $item->ref_date;
  27. # 新用户充值
  28. $item->new_user_charge = Order::getChargeByDate($refDate, $refDate, $refDate) / 100;
  29. # 消耗
  30. $item->paid_total = $item->paid_total / 100;
  31. # 新用户Roi
  32. $item->roi = $item->paid_total > 0 ? round(($item->new_user_charge / 100) / $item->paid_total, 5) * 100 . '%' : 0;
  33. # 获取需统计回收数据的月份和年份
  34. # 获取本年度之前月份的用户回收数据
  35. $paidAtSt = $refDate . ' 00:00:00';
  36. $paidAtEt = $refDate . ' 23:59:59';
  37. $recycleYear = Order::getRecycleOfYear($paidAtSt, $paidAtEt, $userCreatedAt);
  38. for($i=1; $i<=$currentMonth; $i++) {
  39. $key = 'recycle'.$i;
  40. $monthNumber = sprintf('%02d', $i);
  41. $item->$key = isset($recycleYear[$monthNumber]['recycle_total']) ? ($recycleYear[$monthNumber]['recycle_total'] / 100) : 0;
  42. $recycleTotal += $item->$key;
  43. }
  44. # 获取上一年度用户回收数据
  45. $lastYearRecycle = Order::getRecycleLastYear($paidAtSt, $paidAtEt, $lastYearSt, $lastYearEt);
  46. $item->last_year_recycle = isset($lastYearRecycle->recycle_total) ? ($lastYearRecycle->recycle_total / 100) : 0;
  47. $recycleTotal += $item->last_year_recycle;
  48. $item->recycle_total = $recycleTotal;
  49. }
  50. if(!is_null($startDate) && !is_null($startDate)) {
  51. # 获取投放消耗汇总数据
  52. $paidTotal = AdsReport::getPaidTotal($startDate, $endDate);
  53. # 获取新用户充值
  54. $newUserCharge = Order::getNewUserChargeOfDate($startDate, $endDate);
  55. # roi
  56. $roi = $paidTotal > 0 ? round(($newUserCharge / 100) / $paidTotal, 5) * 100 . '%' : '-';
  57. # 获取月用户回收
  58. $recycleTotalData = Order::getRecycleOfYear($startDate . ' 00:00:00', $endDate . ' 23:59:59', $userCreatedAt);
  59. # 获取年度用户回收
  60. $lastYearRecycleTotalData = Order::getRecycleLastYear($startDate . ' 00:00:00', $endDate . ' 23:59:59', $lastYearSt, $lastYearEt);
  61. $lastYearRecycleTotal = isset($lastYearRecycleTotalData->recycle_total) ? ($lastYearRecycleTotalData->recycle_total / 100) : 0;
  62. } else {
  63. $paidTotal = $newUserCharge = $roi = $lastYearRecycleTotal = 0;
  64. $recycleTotalData = array();
  65. }
  66. $condition = [
  67. 'paid_total' => $paidTotal / 100,
  68. 'new_user_charge_total' => $newUserCharge / 100,
  69. 'roi' => $roi,
  70. 'recycle_total_data' => $recycleTotalData,
  71. 'last_year_recycle_total' => $lastYearRecycleTotal
  72. ];
  73. return [$list, $count, $currentMonth, $lastYear, $condition];
  74. }
  75. }