/** * 全部广告数据统计 */ public function get_ad_stat() { list($start, $end, $pagesize, $page_start, $filter) = $this->getFilter(); $ad_service = new AD(); $ad_info = $ad_service->get_ad_info($filter, $page_start, $pagesize); $total = $ad_service->get_ad_number($filter); $ad_ids = array_unique(array_keys($ad_info)); $service = new Transfer(); $transfer_res = $service->get_ad_transfer(array('start' => $start, 'end' => $end, 'ad_id' => $ad_ids), 'ad_id'); // 点击得从两个地方读 $android = new AndroidStat(); $click_android = $android->get_ad_click(array('start' => $start, 'end' => $end, 'ad_id' => $ad_ids), 'ad_id'); $ios = new IOS_Stat(); $click_ios = $ios->get_ad_click(array('start' => $start, 'end' => $end, 'ad_id' => $ad_ids), 'ad_id'); $ad_stat = array(); foreach ($ad_info as $id => $value) { if ($value['oversea'] == 1) { //本页面不显示海外广告的统计数据 continue; } $transfer = (int) $transfer_res[$id]; $ad = array_merge($value, array('id' => $id, 'transfer' => $transfer, 'click' => (int) $click_android[$id] + (int) $click_ios[$id], 'cost' => $ad_info[$id]['quote_rmb'] * $transfer)); $ad_stat[] = $ad; } $this->output(array('code' => 0, 'msg' => 'fetched', 'total' => $total, 'start' => $page_start, 'list' => $ad_stat)); }
/** * 取广告列表 * @author Meathill * @since 0.1.0 */ public function get_list() { $service = new AD(); $job_service = new Job(); $admin = new Admin(); $me = $_SESSION['id']; $im_cp = Auth::is_cp(); $pagesize = isset($_REQUEST['pagesize']) ? (int) $_REQUEST['pagesize'] : 10; $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 0; $page_start = $page * $pagesize; $order = isset($_REQUEST['order']) ? trim($_REQUEST['order']) : 'create_time'; $seq = isset($_REQUEST['seq']) ? trim($_REQUEST['seq']) : 'DESC'; $filters = array('keyword' => $_REQUEST['keyword'], 'pack_name' => $_REQUEST['pack_name'], 'status' => [0, 1, 2, 3, 4], $im_cp ? 'create_user' : 'salesman' => $me); if (isset($_REQUEST['agreement_id'])) { $filters['agreement_id'] = $_REQUEST['agreement_id']; } if (isset($_REQUEST['channel'])) { $filters['channel'] = $_REQUEST['channel']; } if (isset($_REQUEST['ad_name'])) { $filters['ad_name'] = $_REQUEST['ad_name']; } $ads = $service->get_ad_info($filters, $page_start, $pagesize, array($order => $seq)); $total = $service->get_ad_number($filters); $ad_ids = array_unique(array_keys(array_filter($ads))); $ops = $admin->get_ad_ops($ad_ids); $users = array(); $decline = array(); foreach ($ads as $id => $ad) { $users[] = $ad['execute_owner']; if ($ad['status'] == ADModel::REJECTED) { $decline[] = $id; } } // 取商务名单 $user_service = new Admin(); $users = $user_service->get_user_info(array('id' => array_filter(array_unique($users)))); // 取当前申请 $apply = new Apply(); $applies = $apply->get_list_by_id($ad_ids); $applies_by_ad = array(); foreach ($applies as $id => $apply) { $adid = $apply['adid']; if (!is_array($applies_by_ad[$adid])) { $applies_by_ad[$adid] = array(); } unset($apply['adid']); $apply = array_filter($apply, function ($value) { return isset($value); }); // 同时有每日限量和今日余量说明是要修改每日限量 if (array_key_exists('set_job_num', $apply) && array_key_exists('set_rmb', $apply)) { unset($apply['set_rmb']); } $key = array_keys($apply)[0]; // 因为过滤掉了没有内容的键,又删掉了adid,只剩下要操作的key了 $apply[$key . '_id'] = $id; $applies_by_ad[$adid][] = $apply; } // 取计划任务 $ad_jobs = $job_service->get_ad_daily_job($ad_ids); // 取上下线计划任务 $on_off_jobs = $job_service->get_ad_on_off_job($ad_ids); // 取被拒绝的广告的附言 $decline = array_unique(array_filter($decline)); $declines = null; if ($decline) { $comment_service = new Comment(); $declines = $comment_service->get_comment(array('ad_id' => $decline, 'pack_name' => '')); } // 获取备注记录 $comments = $service->get_ad_comments($ad_ids); $result = array(); foreach ($ads as $id => $ad) { $apply = array(); if (is_array($applies_by_ad[$id])) { foreach ($applies_by_ad[$id] as $item) { $apply = array_merge($apply, $item); } } $decline = (array) $declines[$id]; $job_num = array_key_exists($id, $ad_jobs) ? $ad_jobs[$id]['jobnum'] : 0; $job_num = $job_num > 0 || !$im_cp ? $job_num : (int) $ad['job_num']; $on_off = $on_off_jobs[$id]; $result[] = array_merge($ad, $apply, array('id' => $id, 'status' => (int) $ad['status'], 'execute_owner' => $users[$ad['execute_owner']], 'job_num' => $job_num, 'job_time' => substr($ad_jobs[$id]['jobtime'], 11, 5), 'reject' => $decline, 'cm_others' => $comments[$id], 'op' => $ops[$id], 'search_flag' => (int) $ad['search_flag'], 'on_off' => $on_off)); } $this->output(array('code' => 0, 'msg' => 'get', 'total' => $total, 'list' => $result)); }
public function dashboard_cp() { $me = $_SESSION['id']; $im_cp = $_SESSION['role'] == Auth::$CP_PERMISSION; $redis = $this->get_redis(); $service = new AD(); // 取最近发生变化的5个广告,这个不缓存 $latest = $service->get_ad_info(array($im_cp ? 'create_user' : 'salesman' => $me), 0, 5, 'status_time'); foreach ($latest as $id => $item) { $item['id'] = $id; $latest[$id] = $item; } $latest = array_values($latest); // 读缓存 $redis_key = 'diy_user_dashboard_' . $me . '_' . date('Y-m-d'); $cache = $redis->get($redis_key); if ($cache) { $result = json_decode($cache, true); $result['data']['record'] = $latest; $this->output($result); } $week_ago = date('Y-m-d', time() - 691200); $month_ago = date('Y-m-d', time() - 2678400); $yesterday = date('Y-m-d', time() - 86400); $transfer = new Transfer(); // 取在线广告数 $filter = array($im_cp ? 'create_user' : 'salesman' => $me, 'status' => array(0, 1)); $ad = $service->get_ad_number($filter); $filter['start'] = $month_ago; $ad_info = $service->get_ad_info($filter); $adids = array_filter(array_keys($ad_info)); // 取一周内激活数 $transfers = $transfer->get_ad_transfer(array('start' => $week_ago, 'end' => $yesterday, 'ad_id' => $adids)); $transfer_total = $rmb_total = 0; foreach ($transfers as $num) { $transfer_total += $num; } // 取一周推广费用 $transfers = $transfer->get_ad_transfer(array('start' => $week_ago, 'end' => $yesterday, 'ad_id' => $adids), 'ad_id'); foreach ($transfers as $ad_id => $num) { $rmb_total += (int) $ad_info[$ad_id]['quote_rmb'] * (int) $num; } // 取一周内下载数 $download = 0; // 取一个月内的流量统计 $chart_transfer = $transfer->get_ad_transfer_detail(array('start' => $month_ago, 'end' => $yesterday, 'ad_id' => $adids)); $chart = array(); foreach ($chart_transfer as $item) { $date = $item['transfer_date']; if (is_array($chart[$date])) { $chart[$date]['transfer'] += (int) $item['transfer_total']; $chart[$date]['rmb'] += (int) $item['transfer_total'] * (int) $ad_info[$item['ad_id']]['quote_rmb']; } else { $chart[$date] = array('date' => $date, 'transfer' => (int) $item['transfer_total'], 'rmb' => (int) $item['transfer_total'] * (int) $ad_info[$item['ad_id']]['quote_rmb']); } } ksort($chart); $result = array('code' => 0, 'msg' => 'ok', 'data' => array('ad' => $ad, 'total_transfer' => $transfer_total, 'total_download' => $download, 'money' => $rmb_total / 100, 'cash' => 0, 'saved' => 0, 'percent' => 0, 'transfer' => array_values($chart), 'is_sale' => false)); // 缓存到凌晨 $redis->setex($redis_key, 86400 - time() % 86400, json_encode($result)); $result['data']['record'] = $latest; $this->output($result); }