123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Service;
- use App\Models\Es\OperateDataTrend;
- use App\Models\System\Users;
- use App\Models\TencentAdAuth;
- use App\Service\Admin\AdminService;
- class OperateDataService
- {
- /**
- * 运营公众号数据
- * */
- public static function getAccountData($params, $sortField, $sortType, $page, $pageSize)
- {
- $sortField = self::getSortField($sortField);
- # 表头处理
- $header = config('operateDataHeader.list');
- # 数据权限
- $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
- if($params['is_super_user']) {
- $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
- if(empty($params['user_list'])) return [[], 0, $header];
- }
- list($list, $count) = OperateDataTrend::getOperateAccountList($params, $sortField, $sortType, $page, $pageSize);
- if(empty($list)) return [$list, $count, $header];
- # 获取列表中涉及的公众号和运营人员
- $appidList = array_column($list, 'app_id');
- $operatorIds = array_column($list, 'operate_uid');
- $mpAccountData = TencentAdAuth::selectRaw('wechat_account_id, account_name')
- ->where('enable', 1)
- ->whereIn('wechat_account_id', $appidList)
- ->get();
- $sysUserData = Users::select('id', 'name')->whereIn('id', $operatorIds)->get();
- foreach ($list as &$item) {
- $accountInfo = $mpAccountData->where('wechat_account_id', $item['app_id'])->first();
- $item['account_name'] = $accountInfo['account_name'] ?? null;
- $operatorInfo = $sysUserData->where('id', $item['operate_uid'])->first();
- $item['operator_name'] = $operatorInfo['name'] ?? null;
- $item['paid'] = $item['paid'] / 100;
- $item['follow_cost'] = round($item['follow_cost'] / 100, 1);
- $item['pay_rate'] = round($item['pay_rate'], 4) * 100 . '%';
- $item['seven_days_paid_rate'] = round($item['seven_days_paid_rate'], 4) * 100 . '%';
- $item['pay_money_total'] = $item['pay_money_total'] / 100;
- $item['gross_profit_rate'] = round($item['gross_profit_rate'], 5) * 100 . '%';
- $item['cost_cover_rate'] = round($item['cost_cover_rate'], 4) * 100 . '%';
- }
- return [$list, $count, $header];
- }
- /**
- * 获取运营公众号数据汇总
- * */
- public static function getAccountDataSummary($params)
- {
- # 表头处理
- $header = config('operateDataHeader.summary');
- # 数据权限
- $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
- if($params['is_super_user']) {
- $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
- if(empty($params['user_list'])) return [[], $header];
- }
- $data = OperateDataTrend::getAccountDataCollect($params);
- if(empty($data)) return [[], $header];
- $data['paid'] = $data['paid'] / 100;
- $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
- $data['pay_rate'] = round($data['pay_rate'], 4) * 100 . '%';
- $data['seven_days_paid_rate'] = round($data['seven_days_paid_rate'], 4) * 100 . '%';
- $data['pay_money_total'] = $data['pay_money_total'] / 100;
- $data['gross_profit_rate'] = round($data['gross_profit_rate'], 5) * 100 . '%';
- $data['cost_cover_rate'] = round($data['cost_cover_rate'], 4) * 100 . '%';
- return [$data, $header];
- }
- /**
- * 获取运营数据
- * */
- public static function getOperateData($params, $sortField, $sortType, $page, $pageSize)
- {
- $sortField = self::getSortField($sortField);
- # 表头处理
- $header = config('operateDataHeader.operate_list');
- # 数据权限
- $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
- if($params['is_super_user']) {
- $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
- if(empty($params['user_list'])) return [[], 0, $header];
- }
- list($list, $count) = OperateDataTrend::getOperateDataList($params, $sortField, $sortType, $page, $pageSize);
- if(empty($list)) return [$list, $count, $header];
- # 获取列表中涉及的公众号和运营人员
- $operatorIds = array_column($list, 'operate_uid');
- $sysUserData = Users::select('id', 'name')->whereIn('id', $operatorIds)->get();
- foreach ($list as &$item) {
- $operatorInfo = $sysUserData->where('id', $item['operate_uid'])->first();
- $item['operator_name'] = $operatorInfo['name'] ?? null;
- $item['paid'] = $item['paid'] / 100;
- $item['follow_cost'] = round($item['follow_cost'] / 100, 1);
- $item['pay_rate'] = round($item['pay_rate'], 4) * 100 . '%';
- $item['seven_days_paid_rate'] = round($item['seven_days_paid_rate'], 4) * 100 . '%';
- $item['pay_money_total'] = $item['pay_money_total'] / 100;
- $item['gross_profit_rate'] = round($item['gross_profit_rate'], 5) * 100 . '%';
- $item['cost_cover_rate'] = round($item['cost_cover_rate'], 4) * 100 . '%';
- }
- return [$list, $count, $header];
- }
- /**
- * 获取运营数据汇总
- * */
- public static function getOperateDataSummary($params)
- {
- # 表头处理
- $header = config('operateDataHeader.operate_summary');
- # 数据权限
- $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
- if($params['is_super_user']) {
- $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
- if(empty($params['user_list'])) return [[], $header];
- }
- $data = OperateDataTrend::getOperateDataCollect($params);
- if(empty($data)) return [[], $header];
- $data['paid'] = $data['paid'] / 100;
- $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
- $data['pay_rate'] = round($data['pay_rate'], 4) * 100 . '%';
- $data['seven_days_paid_rate'] = round($data['seven_days_paid_rate'], 4) * 100 . '%';
- $data['pay_money_total'] = $data['pay_money_total'] / 100;
- $data['gross_profit_rate'] = round($data['gross_profit_rate'], 5) * 100 . '%';
- $data['cost_cover_rate'] = round($data['cost_cover_rate'], 4) * 100 . '%';
- return [$data, $header];
- }
- /**
- * 总表排序字段处理
- * */
- public static function getSortField($field)
- {
- $data = [
- 'paid' => 'day_paid_bucket > day_paid',
- 'operate_days' => 'operate_days_bucket > operate_days',
- 'follow_cost' => 'follow_cost',
- 'pay_user_total' => 'pay_user_total',
- 'pay_rate' => 'pay_rate',
- 'seven_days_paid_rate' => 'seven_days_paid_rate',
- 'pay_money_total' => 'pay_money_total',
- 'gross_profit_rate' => 'gross_profit_rate',
- 'cost_cover_rate' => 'cost_cover_rate',
- ];
- return isset($data[$field]) ? $data[$field] : 'day_paid_bucket > day_paid';
- }
- }
|