| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- /**
- * 描述.
- *
- * @author nj
- * @date 2021-07-12 14:41
- */
- namespace App\Repositories\Statistics;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Redis;
- use App\Models\Mysql\ChannelModel;
- class StatisticsActiveRepository
- {
- /**
- * 统计某一天日活
- * @param mixed $date 某1天的日期.
- * @param null|string $channelUid 渠道UID
- * @param null|string $deviceType 设备类型
- * @param null|string $momAppUid 母包UD
- * @return integer
- */
- public function countOneDayActive($date, $channelUid = null, $deviceType = null, $momAppUid = null)
- {
- $deviceType = strtoupper($deviceType);
- $keyFlag = 'day_active:';
- if (is_null($channelUid)) {
- $key = $keyFlag . Carbon::parse($date)->toDateString();
- if (!empty($deviceType)) {
- $key .= ':'. $deviceType;
- }
- return Redis::pfcount($key);
- } else {
- if ($channelUid == '') {
- // 空字符串,代表官方.
- $fixChannelUid = 'office__';
- } else {
- $fixChannelUid = $channelUid;
- }
- $key = $keyFlag . 'ch:'. $fixChannelUid . ':' . Carbon::parse($date)->toDateString();
- if (!empty($deviceType)) {
- $key .= ':'. $deviceType;
- }
- return Redis::pfcount($key);
- }
- }
- /**
- * 统计时间段的日活.
- * @param mixed $startDate 开始日期
- * @param mixed $endDate 结束日期
- * @param null|string $channelUid 渠道UID
- * @param null|string $deviceType 设备类型
- * @param null|string $momAppUid 母包UD
- * @return int
- */
- public function countSomeDaysActive($startDate, $endDate, $channelUid = null, $deviceType = null, $momAppUid = null)
- {
- $startDateObj = Carbon::parse($startDate);
- $endDateObj = Carbon::parse($endDate);
- if ($startDateObj > $endDateObj) {
- // 开始日期大于结束日期,错误的,为了容错,特意设置返回0.
- return 0;
- }
- if (abs(Carbon::parse($startDate)->diffInDays(Carbon::parse($endDateObj))) > 366) {
- // 不允许超过1年,为了容错,特意设置返回0.
- return 0;
- }
- $dateObj = Carbon::parse($startDate);
- $deviceType = strtoupper($deviceType);
- $keyFlag = 'day_active:';
- if (is_null($channelUid)) {
- $arrDates = [];
- while ($dateObj <= $endDateObj) {
- $key = $keyFlag . $dateObj->toDateString();
- if (!empty($deviceType)) {
- $key .= ':'. $deviceType;
- }
- $arrDates[] = $key;
- $dateObj = $dateObj->addDays(1);
- }
- return Redis::pfcount($arrDates);
- } else {
- if ($channelUid == '') {
- // 空字符串,代表官方.
- $fixChannelUid = 'office__';
- } else {
- $fixChannelUid = $channelUid;
- }
- $arrDates = [];
- while ($dateObj <= $endDateObj) {
- $date = $startDate;
- $dateObj = Carbon::parse($date);
- $key = $keyFlag . 'ch:' . $fixChannelUid . ':' . $dateObj->toDateString();
- if (!empty($deviceType)) {
- $key .= ':'. $deviceType;
- }
- $arrDates[] = $key;
- $dateObj = $dateObj->addDays(1);
- }
- return Redis::pfcount($arrDates);
- }
- }
- }
|