123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Services;
- use App\Models\AdsReport;
- use App\Models\Order;
- class OmDataService
- {
- /**
- * 获取每日运营数据
- * @param $startDate string 起始时间
- * @param $endDate string 截止时间
- * */
- public static function getDataOfDay($startDate, $endDate, $page, $pageSize)
- {
- # 获取消耗数据
- list($list, $count) = AdsReport::getPaidOfDays($startDate, $endDate, $page, $pageSize);
- # 本年度用户关注起始时间
- $userCreatedAt = date('Y-01-01 00:00:00');
- # 当前月份
- $currentMonth = is_null($endDate) ? date('m') : date('m', strtotime($endDate));
- # 上年度年份
- $lastYear = date('Y', strtotime('-1 year'));
- $lastYearSt = $lastYear . '-01-01 00:00:00';
- $lastYearEt = $lastYear . '-12-31 23:59:59';
- foreach ($list as $item) {
- $recycleTotal = 0;
- $refDate = $item->ref_date;
- # 新用户充值
- $item->new_user_charge = Order::getChargeByDate($refDate, $refDate, $refDate) / 100;
- # 消耗
- $item->paid_total = $item->paid_total / 100;
- # 新用户Roi
- $item->roi = $item->paid_total > 0 ? round(($item->new_user_charge / 100) / $item->paid_total, 5) * 100 . '%' : 0;
- # 获取需统计回收数据的月份和年份
- # 获取本年度之前月份的用户回收数据
- $paidAtSt = $refDate . ' 00:00:00';
- $paidAtEt = $refDate . ' 23:59:59';
- $recycleYear = Order::getRecycleOfYear($paidAtSt, $paidAtEt, $userCreatedAt);
- for($i=1; $i<=$currentMonth; $i++) {
- $key = 'recycle'.$i;
- $monthNumber = sprintf('%02d', $i);
- $item->$key = isset($recycleYear[$monthNumber]['recycle_total']) ? ($recycleYear[$monthNumber]['recycle_total'] / 100) : 0;
- $recycleTotal += $item->$key;
- }
- # 获取上一年度用户回收数据
- $lastYearRecycle = Order::getRecycleLastYear($paidAtSt, $paidAtEt, $lastYearSt, $lastYearEt);
- $item->last_year_recycle = isset($lastYearRecycle->recycle_total) ? ($lastYearRecycle->recycle_total / 100) : 0;
- $recycleTotal += $item->last_year_recycle;
- $item->recycle_total = $recycleTotal;
- }
- if(!is_null($startDate) && !is_null($startDate)) {
- # 获取投放消耗汇总数据
- $paidTotal = AdsReport::getPaidTotal($startDate, $endDate);
- # 获取新用户充值
- $newUserCharge = Order::getNewUserChargeOfDate($startDate, $endDate);
- # roi
- $roi = $paidTotal > 0 ? round(($newUserCharge / 100) / $paidTotal, 5) * 100 . '%' : '-';
- # 获取月用户回收
- $recycleTotalData = Order::getRecycleOfYear($startDate . ' 00:00:00', $endDate . ' 23:59:59', $userCreatedAt);
- # 获取年度用户回收
- $lastYearRecycleTotalData = Order::getRecycleLastYear($startDate . ' 00:00:00', $endDate . ' 23:59:59', $lastYearSt, $lastYearEt);
- $lastYearRecycleTotal = isset($lastYearRecycleTotalData->recycle_total) ? ($lastYearRecycleTotalData->recycle_total / 100) : 0;
- } else {
- $paidTotal = $newUserCharge = $roi = $lastYearRecycleTotal = 0;
- $recycleTotalData = array();
- }
- $condition = [
- 'paid_total' => $paidTotal / 100,
- 'new_user_charge_total' => $newUserCharge / 100,
- 'roi' => $roi,
- 'recycle_total_data' => $recycleTotalData,
- 'last_year_recycle_total' => $lastYearRecycleTotal
- ];
- return [$list, $count, $currentMonth, $lastYear, $condition];
- }
- }
|