StatisticsActiveRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj
  6. * @date 2021-07-12 14:41
  7. */
  8. namespace App\Repositories\Statistics;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\Redis;
  11. use App\Models\Mysql\ChannelModel;
  12. class StatisticsActiveRepository
  13. {
  14. /**
  15. * 统计某一天日活
  16. * @param mixed $date 某1天的日期.
  17. * @param null|string $channelUid 渠道UID
  18. * @param null|string $deviceType 设备类型
  19. * @param null|string $momAppUid 母包UD
  20. * @return integer
  21. */
  22. public function countOneDayActive($date, $channelUid = null, $deviceType = null, $momAppUid = null)
  23. {
  24. $deviceType = strtoupper($deviceType);
  25. $keyFlag = 'day_active:';
  26. if (is_null($channelUid)) {
  27. $key = $keyFlag . Carbon::parse($date)->toDateString();
  28. if (!empty($deviceType)) {
  29. $key .= ':'. $deviceType;
  30. }
  31. return Redis::pfcount($key);
  32. } else {
  33. if ($channelUid == '') {
  34. // 空字符串,代表官方.
  35. $fixChannelUid = 'office__';
  36. } else {
  37. $fixChannelUid = $channelUid;
  38. }
  39. $key = $keyFlag . 'ch:'. $fixChannelUid . ':' . Carbon::parse($date)->toDateString();
  40. if (!empty($deviceType)) {
  41. $key .= ':'. $deviceType;
  42. }
  43. return Redis::pfcount($key);
  44. }
  45. }
  46. /**
  47. * 统计时间段的日活.
  48. * @param mixed $startDate 开始日期
  49. * @param mixed $endDate 结束日期
  50. * @param null|string $channelUid 渠道UID
  51. * @param null|string $deviceType 设备类型
  52. * @param null|string $momAppUid 母包UD
  53. * @return int
  54. */
  55. public function countSomeDaysActive($startDate, $endDate, $channelUid = null, $deviceType = null, $momAppUid = null)
  56. {
  57. $startDateObj = Carbon::parse($startDate);
  58. $endDateObj = Carbon::parse($endDate);
  59. if ($startDateObj > $endDateObj) {
  60. // 开始日期大于结束日期,错误的,为了容错,特意设置返回0.
  61. return 0;
  62. }
  63. if (abs(Carbon::parse($startDate)->diffInDays(Carbon::parse($endDateObj))) > 366) {
  64. // 不允许超过1年,为了容错,特意设置返回0.
  65. return 0;
  66. }
  67. $dateObj = Carbon::parse($startDate);
  68. $deviceType = strtoupper($deviceType);
  69. $keyFlag = 'day_active:';
  70. if (is_null($channelUid)) {
  71. $arrDates = [];
  72. while ($dateObj <= $endDateObj) {
  73. $key = $keyFlag . $dateObj->toDateString();
  74. if (!empty($deviceType)) {
  75. $key .= ':'. $deviceType;
  76. }
  77. $arrDates[] = $key;
  78. $dateObj = $dateObj->addDays(1);
  79. }
  80. return Redis::pfcount($arrDates);
  81. } else {
  82. if ($channelUid == '') {
  83. // 空字符串,代表官方.
  84. $fixChannelUid = 'office__';
  85. } else {
  86. $fixChannelUid = $channelUid;
  87. }
  88. $arrDates = [];
  89. while ($dateObj <= $endDateObj) {
  90. $date = $startDate;
  91. $dateObj = Carbon::parse($date);
  92. $key = $keyFlag . 'ch:' . $fixChannelUid . ':' . $dateObj->toDateString();
  93. if (!empty($deviceType)) {
  94. $key .= ':'. $deviceType;
  95. }
  96. $arrDates[] = $key;
  97. $dateObj = $dateObj->addDays(1);
  98. }
  99. return Redis::pfcount($arrDates);
  100. }
  101. }
  102. }