12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AdqAccountTrendData;
- use App\Models\DjOrder;
- use App\Models\Es\AccountDataTrend;
- use App\Models\MpAccountTrendData;
- use App\Models\OfficialAccount;
- use App\Models\OfficialAccountRelation;
- use App\Models\OfficialWebUserActionSetId;
- use App\Models\TencentAdAuth;
- class AccountDataTrendService
- {
- /**
- * 获取账号数据趋势
- * */
- public static function getAdqAccountDataTrend($params, $sortField, $sortType, $page, $pageSize)
- {
- # 表头处理
- $commonHeader = config('accountDataHeader.basic');
- # 获取day1至day150表头
- $daysHeader = [];
- for($i = 0;$i < AdqAccountTrendData::ADQ_ACCOUNT_TREND_DAYS;$i++) {
- $title = [
- 'name' => 'DAY'.($i+1),
- 'column' => 'day'.($i+1),
- ];
- $daysHeader[] = $title;
- }
- $header = array_merge($commonHeader, $daysHeader);
- # 数据项处理
- if(isset($params['account_id']) && $params['account_id']) {
- list($list, $count) = AdqAccountTrendData::getAccountDataTrend($params, $sortField, $sortType, $page, $pageSize);
- } else {
- # 获取企业下的adq账号
- $accountList = OfficialWebUserActionSetId::select('account_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->distinct()->pluck('account_id');
- if(empty($accountList)) return [[], 0, $header];
- list($list, $count) = AdqAccountTrendData::getAccountSummaryDataTrend(
- $params, $accountList, $sortField, $sortType, $page, $pageSize
- );
- }
- if(empty($list)) return [[], 0, $header];
- foreach ($list as $item) {
- # 消耗金额单位处理
- $item->paid = $item->paid / 100;
- # 企微关注成本
- $item->follow_cost = round($item->follow_cost / 100, 1);
- # 首日roi
- $item->first_roi = $item->first_roi * 100 . '%';
- # 总回本率
- $item->cost_cover_rate = $item->cost_cover_rate * 100 . '%';
- # 首日新增用户充值金额
- $item->pay_money = $item->pay_money / 100;
- # 用户累计充值金额
- $item->pay_money_total = $item->pay_money_total / 100;
- # 首日下单成本
- $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
- # arpu
- $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
- # 企微关注人数
- $item->scan_follow_count = intval($item->scan_follow_count);
- # 查询150天数据
- $dayInfo = [];
- if(isset($params['account_id']) && $params['account_id']) {
- $dayData = AdqAccountTrendData::where('enable', 1)
- ->where(function($query) use ($params) {
- if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
- $query->where('plat_order_type', $params['plat_order_type']);
- })
- ->where('account_id', $params['account_id'])
- ->where('expense_date', $item->expense_date)
- ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date.' +149 day')))
- ->selectRaw("sum(active_fans) as active_fans,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date")
- ->groupBy(['days_type'])
- ->get();
- if(!empty($dayData)){
- foreach($dayData as $val){
- $nd = (strtotime($val['ref_date']) - strtotime($item->expense_date)) / 86400;
- $new_roi = $item->paid ? round($val['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $total_roi = $item->paid ? round($val['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $income_times = $item->pay_money ? round($val['pay_money_total'] / 100 / $item->pay_money, 2) : 0;
- $dayInfo[$nd] = [
- 'days' => 'day' . ($nd+1),
- 'pay_money' => round($val['pay_money']/100, 2),
- 'new_roi' => $new_roi,
- 'total_roi' => $total_roi,
- 'income_times' => $income_times,
- 'charge_user' => $val['active_fans']
- ];
- }
- }
- } else {
- $dayData = AdqAccountTrendData::where('enable', 1)
- ->where(function($query) use ($params) {
- if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
- $query->where('plat_order_type', $params['plat_order_type']);
- })
- ->where('account_id', $item->account_id)
- ->where('expense_date', '>=', $item->expense_date)
- ->where('expense_date', '<=', $item->expense_date_end)
- ->where('ref_date', '<=', strtotime($item->expense_date_end.' +149 day'))
- ->selectRaw("sum(active_fans) as active_fans,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date, expense_date, plat_order_type")
- ->groupBy(['days_type'])
- ->get();
- $paidDataMid = [];
- $payMoneyTotal = 0;
- if(!empty($dayData)){
- foreach($dayData as $val){
- $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
- $payMoneyTotal += $val['pay_money'];
- if(isset($paidDataMid[$nd])) {
- $paidDataMid[$nd]['active_fans'] += $val['active_fans'];
- $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
- // $paidDataMid[$nd]['pay_money_total'] += $val['pay_money_total'];
- $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
- } else {
- $paidDataMid[$nd]['active_fans'] = $val['active_fans'];
- $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
- // $paidDataMid[$nd]['pay_money_total'] = $val['pay_money_total'];
- $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
- }
- }
- foreach ($paidDataMid as $i=>$v) {
- $new_roi = $item->paid ? round($v['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $total_roi = $item->paid ? round($v['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $income_times = $item->pay_money ? round($v['pay_money_total'] / 100 / $item->pay_money, 2) : 0;
- $dayInfo[$i] = [
- 'days' => 'day' . ($i+1),
- 'pay_money' => round($v['pay_money']/100, 2),
- 'new_roi' => $new_roi,
- 'total_roi' => $total_roi,
- 'income_times' => $income_times,
- 'charge_user' => $v['active_fans']
- ];
- }
- }
- }
- $item->day_info = $dayInfo;
- }
- return [$list, $count, $header];
- }
- /**
- * 获取MP账号数据趋势
- * */
- public static function getMpAccountDataTrend($params, $sortField, $sortType, $page, $pageSize)
- {
- # 表头处理
- $commonHeader = config('accountDataHeader.mp_basic');
- # 获取day1至day150表头
- $daysHeader = [];
- for($i = 0;$i < MpAccountTrendData::MP_ACCOUNT_TREND_DAYS;$i++) {
- $title = [
- 'name' => 'DAY'.($i+1),
- 'column' => 'day'.($i+1)
- ];
- $daysHeader[] = $title;
- }
- $header = array_merge($commonHeader, $daysHeader);
- # 数据项处理
- if(isset($params['app_id']) && $params['app_id']) {
- list($list, $count) = MpAccountTrendData::getAccountDataTrend($params, $sortField, $sortType, $page, $pageSize);
- } else {
- # 获取企业下的adq账号
- $accountList = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->distinct()->pluck('app_id');
- if(empty($accountList)) return [[], 0, $header];
- list($list, $count) = MpAccountTrendData::getAccountSummaryDataTrend(
- $params, $accountList, $sortField, $sortType, $page, $pageSize
- );
- }
- if(empty($list)) return [[], 0, $header];
- # 获取列表中的公众号appId信息
- $appIds = $list->pluck('app_id')->unique();
- $accountData = OfficialAccount::select('mp_app_id', 'mp_name')->whereIn('mp_app_id', $appIds)->get();
- foreach ($list as $item) {
- # 消耗金额单位处理
- $item->paid = $item->paid / 100;
- # 企微关注成本
- $item->follow_cost = round($item->follow_cost / 100, 1);
- # 首日roi
- $item->first_roi = $item->first_roi * 100 . '%';
- # 总回本率
- $item->cost_cover_rate = $item->cost_cover_rate * 100 . '%';
- # 首日新增用户充值金额
- $item->pay_money = $item->pay_money / 100;
- # 用户累计充值金额
- $item->pay_money_total = $item->pay_money_total / 100;
- # 首日下单成本
- $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
- # arpu
- $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
- # 企微关注人数
- $item->scan_follow_count = intval($item->scan_follow_count);
- # 公众号名称
- $accountInfo = $accountData->where('mp_app_id', $item->app_id)->first();
- $item->app_name = $accountInfo->mp_name ?? '-';
- # 查询150天数据
- $dayInfo = [];
- if(isset($params['app_id']) && $params['app_id']) {
- $dayData = MpAccountTrendData::where('enable', 1)
- ->where(function($query) use ($params) {
- if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
- $query->where('plat_order_type', $params['plat_order_type']);
- })
- ->where('app_id', $params['app_id'])
- ->where('expense_date', $item->expense_date)
- ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date.' +149 day')))
- ->selectRaw("sum(active_fans) as active_fans,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date")
- ->groupBy(['days_type'])
- ->get();
- if(!empty($dayData)){
- foreach($dayData as $val){
- $nd = (strtotime($val['ref_date']) - strtotime($item->expense_date)) / 86400;
- $new_roi = $item->paid ? round($val['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $total_roi = $item->paid ? round($val['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $income_times = $item->pay_money ? round($val['pay_money_total'] / 100 / $item->pay_money, 2) : 0;
- $dayInfo[$nd] = [
- 'days' => 'day' . ($nd+1),
- 'pay_money' => round($val['pay_money']/100, 2),
- 'new_roi' => $new_roi,
- 'total_roi' => $total_roi,
- 'income_times' => $income_times,
- 'charge_user' => $val['active_fans']
- ];
- }
- }
- } else {
- $dayData = MpAccountTrendData::where('enable', 1)
- ->where(function($query) use ($params) {
- if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
- $query->where('plat_order_type', $params['plat_order_type']);
- })
- ->where('app_id', $item->app_id)
- ->where('expense_date', '>=', $item->expense_date)
- ->where('expense_date', '<=', $item->expense_date_end)
- ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date_end.' +149 day')))
- ->selectRaw("sum(active_fans) as active_fans,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date, expense_date, plat_order_type")
- ->groupBy(['days_type'])
- ->get();
- $paidDataMid = [];
- $payMoneyTotal = 0;
- if(!empty($dayData)){
- foreach($dayData as $val){
- $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
- $payMoneyTotal += $val['pay_money'];
- if(isset($paidDataMid[$nd])) {
- $paidDataMid[$nd]['active_fans'] += $val['active_fans'];
- $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
- $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
- } else {
- $paidDataMid[$nd]['active_fans'] = $val['active_fans'];
- $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
- $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
- }
- }
- foreach ($paidDataMid as $i=>$v) {
- $new_roi = $item->paid ? round($v['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $total_roi = $item->paid ? round($v['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
- $income_times = $item->pay_money ? round($v['pay_money_total'] / 100 / $item->pay_money, 2) : 0;
- $dayInfo[$i] = [
- 'days' => 'day' . ($i+1),
- 'pay_money' => round($v['pay_money']/100, 2),
- 'new_roi' => $new_roi,
- 'total_roi' => $total_roi,
- 'income_times' => $income_times,
- 'charge_user' => $v['active_fans']
- ];
- }
- }
- }
- $item->day_info = $dayInfo;
- }
- return [$list, $count, $header];
- }
- /**
- * 获取ADQ账号总概数据
- * */
- public static function getAdqAccountDataSummary($params)
- {
- # 表头处理
- $header = config('accountDataHeader.summary');
- # 数据项处理
- if(isset($params['account_id']) && $params['account_id']) {
- $accountList = [$params['account_id']];
- } else {
- # 获取企业下的adq账号
- $accountList = OfficialWebUserActionSetId::select('account_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->distinct()->pluck('account_id');
- if(empty($accountList)) return [[], $header];
- }
- $data = AdqAccountTrendData::getAccountDataCollect($params, $accountList);
- if(empty($data)) return [[], $header];
- # 获取累计回收金额
- $recycleData = AdqAccountTrendData::getCumulativeMoney($params, $accountList, true, [1, 3, 7, 15, 30]);
- # 3日消耗最早日期和最晚日期
- $threeDaysBegin = $recycleData->where('days_type', 3)->min('expense_date');
- $threeDaysEnd = $recycleData->where('days_type', 3)->max('expense_date');
- # 首日新增用户累计充值金额
- $threeDaysFirstPaid = $recycleData->where('expense_date', '>=', $threeDaysBegin)->where('expense_date', '<=', $threeDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 3天累计回收金额
- $threeDaysMoney = $recycleData->where('days_type', 3)->sum('pay_money_total');
- $data->three_days_rate = $threeDaysFirstPaid ? round($threeDaysMoney / $threeDaysFirstPaid, 2) : "0";
- # 7日消耗最早日期和最晚日期
- $sevenDaysBegin = $recycleData->where('days_type', 7)->min('expense_date');
- $sevenDaysEnd = $recycleData->where('days_type', 7)->max('expense_date');
- # 首日新增用户累计充值金额
- $sevenDaysFirstPaid = $recycleData->where('expense_date', '>=', $sevenDaysBegin)->where('expense_date', '<=', $sevenDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 7天累计回收金额
- $sevenDaysMoney = $recycleData->where('days_type', 7)->sum('pay_money_total');
- $data->seven_days_rate = $sevenDaysFirstPaid ? round($sevenDaysMoney / $sevenDaysFirstPaid, 2) : "0";
- # 15日消耗最早日期和最晚日期
- $fifteenDaysBegin = $recycleData->where('days_type', 15)->min('expense_date');
- $fifteenDaysEnd = $recycleData->where('days_type', 15)->max('expense_date');
- # 首日新增用户累计充值金额
- $fifteenDaysFirstPaid = $recycleData->where('expense_date', '>=', $fifteenDaysBegin)->where('expense_date', '<=', $fifteenDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 15天累计回收金额
- $fifteenDaysMoney = $recycleData->where('days_type', 15)->sum('pay_money_total');
- $data->fifteen_days_rate = $fifteenDaysFirstPaid ? round($fifteenDaysMoney / $fifteenDaysFirstPaid, 2) : "0";
- # 30日消耗最早日期和最晚日期
- $thirtyDaysBegin = $recycleData->where('days_type', 30)->min('expense_date');
- $thirtyDaysEnd = $recycleData->where('days_type', 30)->max('expense_date');
- # 首日新增用户累计充值金额
- $thirtyDaysFirstPaid = $recycleData->where('expense_date', '>=', $thirtyDaysBegin)->where('expense_date', '<=', $thirtyDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 30天累计回收金额
- $thirtyDaysMoney = $recycleData->where('days_type', 30)->sum('pay_money_total');
- $data->thirty_days_rate = $thirtyDaysFirstPaid ? round($thirtyDaysMoney / $thirtyDaysFirstPaid, 2) : "0";
- // # 3天累计回收金额
- // $threeDaysInfo = $recycleData->where('days_type', 3)->first();
- // $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
- // $data->three_days_rate = $data->pay_money ? round($threeDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- // # 获取7天累计回收金额
- // $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
- // $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
- // $data->seven_days_rate = $data->pay_money ? round($sevenDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- // # 获取15天累计回收金额
- // $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
- // $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
- // $data->fifteen_days_rate = $data->pay_money ? round($fifteenDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- // # 获取30天累计回收金额
- // $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
- // $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
- // $data->thirty_days_rate = $data->pay_money ? round($thirtyDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- # 消耗金额单位处理
- $data->paid = $data->paid / 100;
- # 企微关注成本
- $data->follow_cost = round($data->follow_cost / 100, 1);
- # 首日roi
- $data->first_roi = $data->first_roi * 100 . '%';
- # 总回本率
- $data->cost_cover_rate = $data->cost_cover_rate * 100 . '%';
- # 首日新增用户充值金额
- $data->pay_money = $data->pay_money / 100;
- # 用户累计充值金额
- $data->pay_money_total = $data->pay_money_total / 100;
- # 首日下单成本
- $data->first_charge_user_cost = round($data->first_charge_user_cost / 100, 2);
- # arpu
- $data->day_paid_user_arpu = round($data->day_paid_user_arpu / 100, 2);
- # 企微关注人数
- $data->scan_follow_count = intval($data->scan_follow_count);
- return [$data, $header];
- }
- /**
- * 获取MP账号总概数据
- * */
- public static function getMpAccountDataSummary($params)
- {
- # 表头处理
- $header = config('accountDataHeader.summary');
- # 数据项处理
- if(isset($params['app_id']) && $params['app_id']) {
- $accountList = [$params['app_id']];
- } else {
- # 获取企业下的mp账号
- $accountList = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->distinct()->pluck('app_id');
- if(empty($accountList)) return [[], $header];
- }
- $data = MpAccountTrendData::getAccountDataCollect($params, $accountList);
- if(empty($data)) return [[], $header];
- # 获取累计回收金额
- $recycleData = MpAccountTrendData::getCumulativeMoney($params, $accountList, true, [1, 3, 7, 15, 30]);
- # 3日消耗最早日期和最晚日期
- $threeDaysBegin = $recycleData->where('days_type', 3)->min('expense_date');
- $threeDaysEnd = $recycleData->where('days_type', 3)->max('expense_date');
- # 首日新增用户累计充值金额
- $threeDaysFirstPaid = $recycleData->where('expense_date', '>=', $threeDaysBegin)->where('expense_date', '<=', $threeDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 3天累计回收金额
- $threeDaysMoney = $recycleData->where('days_type', 3)->sum('pay_money_total');
- $data->three_days_rate = $threeDaysFirstPaid ? round($threeDaysMoney / $threeDaysFirstPaid, 2) : "0";
- # 7日消耗最早日期和最晚日期
- $sevenDaysBegin = $recycleData->where('days_type', 7)->min('expense_date');
- $sevenDaysEnd = $recycleData->where('days_type', 7)->max('expense_date');
- # 首日新增用户累计充值金额
- $sevenDaysFirstPaid = $recycleData->where('expense_date', '>=', $sevenDaysBegin)->where('expense_date', '<=', $sevenDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 7天累计回收金额
- $sevenDaysMoney = $recycleData->where('days_type', 7)->sum('pay_money_total');
- $data->seven_days_rate = $sevenDaysFirstPaid ? round($sevenDaysMoney / $sevenDaysFirstPaid, 2) : "0";
- # 15日消耗最早日期和最晚日期
- $fifteenDaysBegin = $recycleData->where('days_type', 15)->min('expense_date');
- $fifteenDaysEnd = $recycleData->where('days_type', 15)->max('expense_date');
- # 首日新增用户累计充值金额
- $fifteenDaysFirstPaid = $recycleData->where('expense_date', '>=', $fifteenDaysBegin)->where('expense_date', '<=', $fifteenDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 15天累计回收金额
- $fifteenDaysMoney = $recycleData->where('days_type', 15)->sum('pay_money_total');
- $data->fifteen_days_rate = $fifteenDaysFirstPaid ? round($fifteenDaysMoney / $fifteenDaysFirstPaid, 2) : "0";
- # 30日消耗最早日期和最晚日期
- $thirtyDaysBegin = $recycleData->where('days_type', 30)->min('expense_date');
- $thirtyDaysEnd = $recycleData->where('days_type', 30)->max('expense_date');
- # 首日新增用户累计充值金额
- $thirtyDaysFirstPaid = $recycleData->where('expense_date', '>=', $thirtyDaysBegin)->where('expense_date', '<=', $thirtyDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 30天累计回收金额
- $thirtyDaysMoney = $recycleData->where('days_type', 30)->sum('pay_money_total');
- $data->thirty_days_rate = $thirtyDaysFirstPaid ? round($thirtyDaysMoney / $thirtyDaysFirstPaid, 2) : "0";
- // # 3天累计回收金额
- // $threeDaysInfo = $recycleData->where('days_type', 3)->first();
- // $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
- // $data->three_days_rate = $data->pay_money ? round($threeDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- // # 获取7天累计回收金额
- // $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
- // $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
- // $data->seven_days_rate = $data->pay_money ? round($sevenDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- // # 获取15天累计回收金额
- // $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
- // $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
- // $data->fifteen_days_rate = $data->pay_money ? round($fifteenDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- // # 获取30天累计回收金额
- // $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
- // $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
- // $data->thirty_days_rate = $data->pay_money ? round($thirtyDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
- # 消耗金额单位处理
- $data->paid = $data->paid / 100;
- # 企微关注成本
- $data->follow_cost = round($data->follow_cost / 100, 1);
- # 首日roi
- $data->first_roi = $data->first_roi * 100 . '%';
- # 总回本率
- $data->cost_cover_rate = $data->cost_cover_rate * 100 . '%';
- # 首日新增用户充值金额
- $data->pay_money = $data->pay_money / 100;
- # 用户累计充值金额
- $data->pay_money_total = $data->pay_money_total / 100;
- # 首日下单成本
- $data->first_charge_user_cost = round($data->first_charge_user_cost / 100, 2);
- # arpu
- $data->day_paid_user_arpu = round($data->day_paid_user_arpu / 100, 2);
- # 企微关注人数
- $data->scan_follow_count = intval($data->scan_follow_count);
- return [$data, $header];
- }
- /**
- * Adq账号数据汇总曲线
- * */
- public static function getAdqAccountDataCurve($params)
- {
- if(isset($params['account_id']) && $params['account_id']) {
- $accountList = [$params['account_id']];
- } else {
- # 获取企业下的adq账号
- $accountList = OfficialWebUserActionSetId::select('account_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->distinct()->pluck('account_id');
- if(empty($accountList)) return [];
- }
- # Adq账号数据汇总曲线
- $data = AdqAccountTrendData::getAccountDataCurve($params, $accountList);
- # 获取累计回收金额
- $recycleData = AdqAccountTrendData::getCumulativeMoney($params, $accountList, true);
- if(empty($data)) return [];
- # 数据处理
- foreach($data as $item) {
- # 消耗金额
- $item->paid = $item->paid / 100;
- # 首日下单成本
- $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
- # 当日付费用户arpu值
- $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
- # 首日roi
- $item->first_roi = $item->first_roi * 100;
- # 3天累计回收金额
- $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item->expense_date)->first();
- $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
- $item->three_days_rate = $item->pay_money ? round($threeDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 获取7天累计回收金额
- $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item->expense_date)->first();
- $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
- $item->seven_days_rate = $item->pay_money ? round($sevenDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 获取15天累计回收金额
- $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item->expense_date)->first();
- $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
- $item->fifteen_days_rate = $item->pay_money ? round($fifteenDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 获取30天累计回收金额
- $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item->expense_date)->first();
- $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
- $item->thirty_days_rate = $item->pay_money ? round($thirtyDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 首日新增用户充值金额
- $item->pay_money = $item->pay_money / 100;
- }
- return $data;
- }
- /**
- * mp账号数据汇总曲线
- * */
- public static function getMpAccountDataCurve($params)
- {
- if(isset($params['app_id']) && $params['app_id']) {
- $accountList = [$params['app_id']];
- } else {
- # 获取企业下的mp账号
- $accountList = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->distinct()->pluck('app_id');
- if(empty($accountList)) return [];
- }
- # mp账号数据汇总曲线
- $data = MpAccountTrendData::getAccountDataCurve($params, $accountList);
- # 获取累计回收金额
- $recycleData = MpAccountTrendData::getCumulativeMoney($params, $accountList, true);
- if(empty($data)) return [];
- # 数据处理
- foreach($data as $item) {
- # 消耗金额
- $item->paid = $item->paid / 100;
- # 首日下单成本
- $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
- # 当日付费用户arpu值
- $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
- # 首日roi
- $item->first_roi = $item->first_roi * 100;
- # 3天累计回收金额
- $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item->expense_date)->first();
- $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
- $item->three_days_rate = $item->pay_money ? round($threeDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 获取7天累计回收金额
- $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item->expense_date)->first();
- $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
- $item->seven_days_rate = $item->pay_money ? round($sevenDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 获取15天累计回收金额
- $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item->expense_date)->first();
- $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
- $item->fifteen_days_rate = $item->pay_money ? round($fifteenDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 获取30天累计回收金额
- $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item->expense_date)->first();
- $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
- $item->thirty_days_rate = $item->pay_money ? round($thirtyDaysMoney / $item->pay_money, 4) * 100 : 0;
- # 首日新增用户充值金额
- $item->pay_money = $item->pay_money / 100;
- }
- return $data;
- }
- /**
- * 获取账号数据趋势
- * */
- public static function getAccountDataTrend($params, $sortField, $sortType, $page, $pageSize)
- {
- $sortField = self::getSortField($sortField);
- # 表头处理
- $commonHeader = config('accountDataHeader.all');
- # 获取day1至day150表头
- $daysHeader = [];
- for($i = 0;$i < AccountDataTrend::ACCOUNT_TREND_DAYS;$i++) {
- $title = [
- 'name' => 'DAY'.($i+1),
- 'column' => 'day'.($i+1),
- ];
- $daysHeader[] = $title;
- }
- $header = array_merge($commonHeader, $daysHeader);
- # 获取当前登录账号可见的账号范围
- if($params['ad_type'] == 1) { // adq账号
- $authAccountList = PitcherService::adqAccountListForUser(
- $params['admin_id'], $params['sys_group_id'], $params['is_system_admin']
- );
- } elseif ($params['ad_type'] == 2) { // mp账号
- $authAccountList = OfficialAccount::getAccountMpAppIdList(
- $params['admin_id'], $params['sys_group_id'], $params['is_system_admin']
- );
- } else { // adq+mp
- $authAccountList = PitcherService::adAccountList('', $params['sys_group_id'], $params['admin_id'], $params['is_system_admin']);
- $authAccountList = array_column($authAccountList, 'account_id');
- }
- if(empty($authAccountList)) return [[], 0, $header];
- # 数据项处理
- if(isset($params['account_id']) && $params['account_id']) {
- $params['account_id'] = explode(',', $params['account_id']);
- foreach ($params['account_id'] as $index=>$accountId) {
- if(!in_array($accountId, $authAccountList)) unset($params['account_id'][$index]);
- }
- if(empty($params['account_id'])) return [[], 0, $header];
- list($list, $count) = AccountDataTrend::getAccountDataTrend($params, $sortField, $sortType, $page, $pageSize);
- } else {
- # 获取企业下的adq账号
- $adqAccountList = OfficialWebUserActionSetId::select('account_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->whereIn('account_id', $authAccountList)
- ->distinct()->pluck('account_id')->toArray();
- # 获取企业下的mp账号
- $mpAccountList = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->whereIn('app_id', $authAccountList)
- ->distinct()->pluck('app_id')->toArray();
- $accountList = array_merge($adqAccountList, $mpAccountList);
- if(empty($accountList)) return [[], 0, $header];
- list($list, $count) = AccountDataTrend::getAccountSummaryDataTrend($params, $accountList, $sortField, $sortType, $page, $pageSize);
- }
- if(empty($list)) return [[], 0, $header];
- # 获取对应的mp账号信息
- $mpAccountData = TencentAdAuth::selectRaw('wechat_account_id, account_name')
- ->where('enable', 1)
- ->where('wechat_account_id', '>', '')
- ->get();
- foreach ($list as &$item) {
- # 账号名称处理
- if(is_array($item['account_id'])) {
- $accountCount = count($item['account_id']);
- if($accountCount == 1) {
- $item['account_id'] = $item['account_id'][0];
- $mpAccountInfo = $mpAccountData->where('wechat_account_id', $item['account_id'])->first();
- $item['account_name'] = $mpAccountInfo->account_name ?? $item['account_id'];
- } else {
- $accountNameStr = '';
- foreach ($item['account_id'] as $v) {
- $mpAccountInfo = $mpAccountData->where('wechat_account_id', $v)->first();
- $accountNameStr .= $mpAccountInfo->account_name ?? $v;
- $accountNameStr .= ',';
- }
-
- $item['account_name'] = trim($accountNameStr, ',');
- }
- } else {
- $mpAccountInfo = $mpAccountData->where('wechat_account_id', $item['account_id'])->first();
- $item['account_name'] = $mpAccountInfo->account_name ?? $item['account_id'];
- }
- # 消耗金额单位处理
- $item['paid'] = $item['paid'] / 100;
- # 企微关注成本
- $item['follow_cost'] = round($item['follow_cost'] / 100, 1);
- # 复粉率-不区分用户是否流失
- $item['repeat_user_rate'] = round($item['repeat_user_rate'], 4) * 100 . '%';
- # 复粉率-是否重复仅参考留存用户
- $item['repeat_user_no_loss_rate'] = round($item['repeat_user_no_loss_rate'], 4) * 100 . '%';
- # 首日roi
- $item['first_roi'] = round($item['first_roi'], 4) * 100 . '%';
- # 首日首充roi
- $item['first_pay_roi'] = round($item['first_pay_roi'], 4) * 100 . '%';
- # 总回本率
- $item['cost_cover_rate'] = round($item['cost_cover_rate'], 4) * 100 . '%';
- # 首日新增用户充值金额
- $item['pay_money'] = $item['pay_money'] / 100;
- # 用户累计充值金额
- $item['pay_money_total'] = $item['pay_money_total'] / 100;
- # 首日下单成本
- $item['first_charge_user_cost'] = round($item['first_charge_user_cost'] / 100, 2);
- # arpu
- $item['day_paid_user_arpu'] = round($item['day_paid_user_arpu'] / 100, 2);
- # 企微关注人数
- $item['scan_follow_count'] = intval($item['scan_follow_count']);
- # 企微加粉人数
- $item['followers_number'] = intval($item['followers_number']);
- # 复粉人数(不区分用户是否流失)
- $item['repeat_user'] = intval($item['repeat_user']);
- # 复粉人数(是否重复仅参考留存用户)
- $item['repeat_user_no_loss'] = intval($item['repeat_user_no_loss']);
- # 企微加粉成本
- $item['followers_cost'] = round($item['followers_cost'] / 100, 1);
- # 查询150天数据
- $dayInfo = [];
- if(isset($params['account_id']) && $params['account_id']) {
- # 查询该投放日期的后150天数据
- $dayData = AccountDataTrend::getDataAfterExpense($params, $item['expense_date']);
- if(!empty($dayData)){
- foreach($dayData as $val){
- $nd = (strtotime($val['ref_date']) - strtotime($item['expense_date'])) / 86400;
- $new_roi = $item['paid'] ? round($val['pay_money'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
- $total_roi = $item['paid'] ? round($val['pay_money_total'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
- $income_times = $item['pay_money'] ? round($val['pay_money_total'] / 100 / $item['pay_money'], 2) : 0;
- $dayInfo[$nd] = [
- 'days' => 'day' . ($nd+1),
- 'pay_money' => round($val['pay_money']/100, 2),
- 'new_roi' => $new_roi,
- 'total_roi' => $total_roi,
- 'income_times' => $income_times,
- 'charge_user' => $val['pay_user']
- ];
- }
- }
- } else {
- # 获取账号在指定时间范围内的投放效果数据趋势
- $dayData = AccountDataTrend::getDataAfterExpense(
- $params, $item['expense_date'], $item['expense_date_end'], $item['account_id']
- );
- $paidDataMid = [];
- $payMoneyTotal = 0;
- if(!empty($dayData)){
- foreach($dayData as $val){
- $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
- $payMoneyTotal += $val['pay_money'];
- if(isset($paidDataMid[$nd])) {
- $paidDataMid[$nd]['active_fans'] += $val['active_fans'];
- $paidDataMid[$nd]['pay_user'] += $val['pay_user'];
- $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
- $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
- } else {
- $paidDataMid[$nd]['active_fans'] = $val['active_fans'];
- $paidDataMid[$nd]['pay_user'] = $val['pay_user'];
- $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
- $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
- }
- }
- foreach ($paidDataMid as $i=>$v) {
- $new_roi = $item['paid'] ? round($v['pay_money'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
- $total_roi = $item['paid'] ? round($v['pay_money_total'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
- $income_times = $item['pay_money'] ? round($v['pay_money_total'] / 100 / $item['pay_money'], 2) : 0;
- $dayInfo[$i] = [
- 'days' => 'day' . ($i+1),
- 'pay_money' => round($v['pay_money']/100, 2),
- 'new_roi' => $new_roi,
- 'total_roi' => $total_roi,
- 'income_times' => $income_times,
- 'charge_user' => $v['pay_user']
- ];
- }
- }
- }
- $item['day_info'] = $dayInfo;
- }
- return [$list, $count, $header];
- }
- /**
- * 获取账号总概数据(ADQ+MP)
- * */
- public static function getAccountDataSummary($params)
- {
- # 表头处理
- $header = config('accountDataHeader.summary');
- # 获取当前登录账号可见的账号范围
- if($params['ad_type'] == 1) { // adq账号
- $authAccountList = PitcherService::adqAccountListForUser(
- $params['admin_id'], $params['sys_group_id'], $params['is_system_admin']
- );
- } elseif ($params['ad_type'] == 2) { // mp账号
- $authAccountList = OfficialAccount::getAccountMpAppIdList(
- $params['admin_id'], $params['sys_group_id'], $params['is_system_admin']
- );
- } else { // adq+mp
- $authAccountList = PitcherService::adAccountList('', $params['sys_group_id'], $params['admin_id'], $params['is_system_admin']);
- $authAccountList = array_column($authAccountList, 'account_id');
- }
- if(empty($authAccountList)) return [[], $header];
- # 数据项处理
- if(isset($params['account_id']) && $params['account_id']) {
- $accountList = explode(',', $params['account_id']);
- foreach ($accountList as $index=>$accountId) {
- if(!in_array($accountId, $authAccountList)) unset($accountList[$index]);
- }
- } else {
- # 获取企业下的adq账号
- $adqAccountList = OfficialWebUserActionSetId::select('account_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->whereIn('account_id', $authAccountList)
- ->distinct()->pluck('account_id')->toArray();
- # 获取企业下的mp账号
- $mpAccountList = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->whereIn('app_id', $authAccountList)
- ->distinct()->pluck('app_id')->toArray();
- $accountList = array_merge($adqAccountList, $mpAccountList);
- }
- if(empty($accountList)) return [[], $header];
- $data = AccountDataTrend::getAccountDataCollect($params, $accountList);
- if(empty($data)) return [[], $header];
- # 获取累计回收金额
- // $recycleData = AdqAccountTrendData::getCumulativeMoney($params, $accountList, true, [1, 3, 7, 15, 30]);
- $recycleData = collect(AccountDataTrend::getCumulativeMoney($params, $accountList, [1, 3, 7, 15, 30]));
- # 3日消耗最早日期和最晚日期
- $threeDaysBegin = $recycleData->where('days_type', 3)->min('expense_date');
- $threeDaysEnd = $recycleData->where('days_type', 3)->max('expense_date');
- # 首日新增用户累计充值金额
- $threeDaysFirstPaid = $recycleData->where('expense_date', '>=', $threeDaysBegin)->where('expense_date', '<=', $threeDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 3天累计回收金额
- $threeDaysMoney = $recycleData->where('days_type', 3)->sum('pay_money_total');
- $data['three_days_rate'] = $threeDaysFirstPaid ? round($threeDaysMoney / $threeDaysFirstPaid, 2) : "0";
- # 7日消耗最早日期和最晚日期
- $sevenDaysBegin = $recycleData->where('days_type', 7)->min('expense_date');
- $sevenDaysEnd = $recycleData->where('days_type', 7)->max('expense_date');
- # 首日新增用户累计充值金额
- $sevenDaysFirstPaid = $recycleData->where('expense_date', '>=', $sevenDaysBegin)->where('expense_date', '<=', $sevenDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 7天累计回收金额
- $sevenDaysMoney = $recycleData->where('days_type', 7)->sum('pay_money_total');
- $data['seven_days_rate'] = $sevenDaysFirstPaid ? round($sevenDaysMoney / $sevenDaysFirstPaid, 2) : "0";
- # 15日消耗最早日期和最晚日期
- $fifteenDaysBegin = $recycleData->where('days_type', 15)->min('expense_date');
- $fifteenDaysEnd = $recycleData->where('days_type', 15)->max('expense_date');
- # 首日新增用户累计充值金额
- $fifteenDaysFirstPaid = $recycleData->where('expense_date', '>=', $fifteenDaysBegin)->where('expense_date', '<=', $fifteenDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 15天累计回收金额
- $fifteenDaysMoney = $recycleData->where('days_type', 15)->sum('pay_money_total');
- $data['fifteen_days_rate'] = $fifteenDaysFirstPaid ? round($fifteenDaysMoney / $fifteenDaysFirstPaid, 2) : "0";
- # 30日消耗最早日期和最晚日期
- $thirtyDaysBegin = $recycleData->where('days_type', 30)->min('expense_date');
- $thirtyDaysEnd = $recycleData->where('days_type', 30)->max('expense_date');
- # 首日新增用户累计充值金额
- $thirtyDaysFirstPaid = $recycleData->where('expense_date', '>=', $thirtyDaysBegin)->where('expense_date', '<=', $thirtyDaysEnd)
- ->where('days_type', 1)
- ->sum('pay_money');
- # 30天累计回收金额
- $thirtyDaysMoney = $recycleData->where('days_type', 30)->sum('pay_money_total');
- $data['thirty_days_rate'] = $thirtyDaysFirstPaid ? round($thirtyDaysMoney / $thirtyDaysFirstPaid, 2) : "0";
- # 消耗金额单位处理
- $data['paid'] = $data['paid'] / 100;
- # 企微关注成本
- $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
- # 首日roi
- $data['first_roi'] = round($data['first_roi'], 4) * 100 . '%';
- # 复粉率-不区分用户是否流失
- $data['repeat_user_rate'] = round($data['repeat_user_rate'], 4) * 100 . '%';
- # 复粉率-是否重复仅参考留存用户
- $data['repeat_user_no_loss_rate'] = round($data['repeat_user_no_loss_rate'], 4) * 100 . '%';
- # 总回本率
- $data['cost_cover_rate'] = round($data['cost_cover_rate'], 4) * 100 . '%';
- # 首日新增用户充值金额
- $data['pay_money'] = $data['pay_money'] / 100;
- # 用户累计充值金额
- $data['pay_money_total'] = $data['pay_money_total'] / 100;
- # 首日下单成本
- $data['first_charge_user_cost'] = round($data['first_charge_user_cost'] / 100, 2);
- # arpu
- $data['day_paid_user_arpu'] = round($data['day_paid_user_arpu'] / 100, 2);
- # 企微关注人数
- $data['scan_follow_count'] = intval($data['scan_follow_count']);
- # 企微加粉人数
- $data['followers_number'] = intval($data['followers_number']);
- # 企微加粉成本
- $data['followers_cost'] = round($data['followers_cost'] / 100, 1);
- # 复粉人数-不区分用户是否流失
- $data['repeat_user'] = intval($data['repeat_user']);
- # 复粉人数-是否重复仅参考留存用户
- $data['repeat_user_no_loss'] = intval($data['repeat_user_no_loss']);
- return [$data, $header];
- }
- /**
- * 账号数据汇总曲线(ADQ+MP)
- * */
- public static function getAccountDataCurve($params)
- {
- # 获取当前登录账号可见的账号范围
- if($params['ad_type'] == 1) { // adq账号
- $authAccountList = PitcherService::adqAccountListForUser(
- $params['admin_id'], $params['sys_group_id'], $params['is_system_admin']
- );
- } elseif ($params['ad_type'] == 2) { // mp账号
- $authAccountList = OfficialAccount::getAccountMpAppIdList(
- $params['admin_id'], $params['sys_group_id'], $params['is_system_admin']
- );
- } else { // adq+mp
- $authAccountList = PitcherService::adAccountList('', $params['sys_group_id'], $params['admin_id'], $params['is_system_admin']);
- $authAccountList = array_column($authAccountList, 'account_id');
- }
- if(empty($authAccountList)) return [];
- if(isset($params['account_id']) && $params['account_id']) {
- $accountList = explode(',', $params['account_id']);
- foreach ($accountList as $index=>$accountId) {
- if(!in_array($accountId, $authAccountList)) unset($accountList[$index]);
- }
- } else {
- # 获取企业下的adq账号
- $adqAccountList = OfficialWebUserActionSetId::select('account_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->whereIn('account_id', $authAccountList)
- ->distinct()->pluck('account_id')->toArray();
- # 获取企业下的mp账号
- $mpAccountList = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $params['sys_group_id'])
- ->whereIn('app_id', $authAccountList)
- ->distinct()->pluck('app_id')->toArray();
- $accountList = array_merge($adqAccountList, $mpAccountList);
- }
- if(empty($accountList)) return [];
- # 账号数据汇总曲线
- $data = AccountDataTrend::getAccountDataCurve($params, $accountList);
- if(empty($data)) return [];
- # 获取累计回收金额
- $recycleData = collect(AccountDataTrend::getCumulativeMoney($params, $accountList));
- # 数据处理
- foreach($data as &$item) {
- # 消耗金额
- $item['paid'] = $item['paid'] / 100;
- # 首日下单成本
- $item['first_charge_user_cost'] = round($item['first_charge_user_cost'] / 100, 2);
- # 当日付费用户arpu值
- $item['day_paid_user_arpu'] = round($item['day_paid_user_arpu'] / 100, 2);
- # 首日roi
- $item['first_roi'] = round($item['first_roi'], 4) * 100;
- # 3天累计回收金额
- $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item['expense_date'])->first();
- $threeDaysMoney = $threeDaysInfo['pay_money_total'] ?? 0;
- $item['three_days_rate'] = $item['pay_money'] ? round($threeDaysMoney / $item['pay_money'], 4) * 100 : 0;
- # 获取7天累计回收金额
- $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item['expense_date'])->first();
- $sevenDaysMoney = $sevenDaysInfo['pay_money_total'] ?? 0;
- $item['seven_days_rate'] = $item['pay_money'] ? round($sevenDaysMoney / $item['pay_money'], 4) * 100 : 0;
- # 获取15天累计回收金额
- $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item['expense_date'])->first();
- $fifteenDaysMoney = $fifteenDaysInfo['pay_money_total'] ?? 0;
- $item['fifteen_days_rate'] = $item['pay_money'] ? round($fifteenDaysMoney / $item['pay_money'], 4) * 100 : 0;
- # 获取30天累计回收金额
- $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item['expense_date'])->first();
- $thirtyDaysMoney = $thirtyDaysInfo['pay_money_total'] ?? 0;
- $item['thirty_days_rate'] = $item['pay_money'] ? round($thirtyDaysMoney / $item['pay_money'], 4) * 100 : 0;
- # 首日新增用户充值金额
- $item['pay_money'] = $item['pay_money'] / 100;
- unset($item['cost_cover_rate']);
- }
- return $data;
- }
- /**
- * 总表排序字段处理
- * */
- public static function getSortField($field)
- {
- $data = [
- 'expense_date' => '_key',
- 'paid' => 'day_paid_bucket > day_paid',
- 'scan_follow_count' => 'scan_follow_count_bucket > day_scan_follow_count',
- 'follow_cost' => 'follow_cost',
- 'first_roi' => 'first_roi',
- 'pay_money' => 'day_pay_money_bucket > day_pay_money',
- 'pay_money_total' => 'pay_money_total',
- 'pay_user' => 'day_pay_user_bucket > day_pay_user',
- 'pay_user_total' => 'pay_user_total',
- 'pay_count' => 'day_pay_count_bucket > day_pay_count',
- 'pay_count_total' => 'pay_count_total',
- 'first_charge_user_cost' => 'first_charge_user_cost',
- 'cost_cover_rate' => 'cost_cover_rate',
- 'day_paid_user_arpu' => 'day_paid_user_arpu'
- ];
- return isset($data[$field]) ? $data[$field] : 'day_paid_bucket > day_paid';
- }
- }
|