/**
  * 单个广告某周期统计
  * @param $id
  */
 public function get_the_ad_stat($id)
 {
     $ad = new AD();
     if (!$ad->check_ad_owner($id)) {
         $this->exit_with_error(20, '您无法查询此广告', 401);
     }
     list($start, $end) = $this->getFilter($id);
     $info = $ad->get_ad_info(array('id' => $id), 0, 1);
     $service = new Transfer();
     $transfer_res = $service->get_ad_transfer(array('start' => $start, 'end' => $end, 'ad_id' => $id), 'transfer_date');
     if ($info['ad_app_type'] == ADModel::ANDROID) {
         $android = new AndroidStat();
         $click_res = $android->get_ad_click(array('start' => $start, 'end' => $end, 'ad_id' => $id), AndroidStat::TIME_FIELD);
     } else {
         $ios = new IOS_Stat();
         $click_res = $ios->get_ad_click(array('start' => $start, 'end' => $end, 'ad_id' => $id), IOS_Stat::TIME_FIELD);
     }
     $start = strtotime($start);
     $end = strtotime($end);
     $result = array();
     for ($i = $start; $i <= $end; $i += 86400) {
         $date = date('Y-m-d', $i);
         $transfer = (int) $transfer_res[$date];
         if (!$transfer && !$click_res[$date]) {
             continue;
         }
         $result[] = array('ad_id' => $id, 'date' => $date, 'transfer' => $transfer, 'click' => (int) $click_res[$date], 'cost' => $transfer * $info['quote_rmb']);
     }
     $this->output(array('code' => 0, 'msg' => 'fetched', 'total' => count($result), 'start' => 0, 'list' => $result));
 }
 public function get_list($output = true)
 {
     $query = trim($_REQUEST['keyword']);
     if (!$query) {
         $this->output(array('code' => 0, 'msg' => '没有关键词'));
     }
     // 取广告,100个基本等于不限
     $season = date('Y-m-d', time() - 86400 * 90);
     $today = date('Y-m-d');
     $yesterday = date('Y-m-d', time() - 86400);
     $service = new AD();
     $transfer = new Transfer();
     $ads = $service->get_ad_info(array('keyword' => $query, 'status' => array(0, 1), 'oversea' => 0, 'ad_app_type' => 1), 0, 100);
     $ad_ids = array_keys($ads);
     // 取广告运行状态
     $rmb_out = $transfer->get_ad_transfer(array('ad_id' => $ad_ids, 'start' => $season, 'end' => $today), 'ad_id');
     // 取下线申请
     $apply_service = new Apply();
     $applies = $apply_service->get_offline_apply(array('adid' => $ad_ids));
     // 取广告结算状态
     $payment_service = new Payment();
     $quote_service = new Quote();
     $payments = $payment_service->get_payment($ad_ids, $season, $today);
     $quotes = $quote_service->get_quote($ad_ids, $season, $today);
     foreach ($payments as $payment) {
         $ad_id = $payment['id'];
         $month = substr($payment['month'], 0, 7);
         $ads[$ad_id]['payment'] += (int) $payment['rmb'];
         $ads[$ad_id]['quote'] += (int) $quotes[$ad_id][$month];
     }
     // 取饱和度
     $job = new Job();
     $yesterday_job = $job->get_log(array('ad_id' => $ad_ids, 'start' => $yesterday, 'end' => $today));
     $yesterday = $transfer->get_ad_transfer(array('ad_id' => $ad_ids, 'date' => $yesterday), 'ad_id');
     $delivery = array();
     foreach ($ad_ids as $ad_id) {
         $pack_name = $ads[$ad_id]['pack_name'];
         $delivery[$pack_name] = $this->parse_history($ad_id, $ads[$ad_id], $yesterday_job[$ad_id], $yesterday[$ad_id], $delivery[$pack_name]);
     }
     // 取点评记录
     $pack_info = array();
     foreach ($ads as $ad) {
         $pack_info[$ad['pack_name']] = $ad['ad_name'];
     }
     $pack_names = array_unique(array_filter(array_keys($pack_info)));
     $comments_by_pack_name = array();
     if ($pack_names) {
         $comments = $service->get_comments(array('pack_name' => $pack_names));
         foreach ($comments as $comment) {
             $pack_name = $comment['pack_name'];
             $array = $comments_by_pack_name[$pack_name];
             $array = is_array($array) ? $array : array('ad_name' => $pack_info[$pack_name], 'pack_name' => $pack_name, 'comments' => array());
             $array['comments'][] = $comment;
             $comments_by_pack_name[$pack_name] = $array;
         }
     }
     $result = array();
     foreach ($ads as $key => $ad) {
         $item = Utils::array_pick($ad, 'ad_name', 'others', 'create_time', 'quote_rmb', 'payment', 'quote');
         $item['transfer'] = (int) $rmb_out[$key];
         $item['payment_percent'] = $item['quote'] != 0 ? round($item['payment'] / $item['quote'] * 100, 2) : 0;
         $item['id'] = $key;
         $item['offline_msg'] = $applies[$key];
         $item['feedback'] = $ad['feedback'];
         $item['is_full'] = $this->check_is_full($delivery[$ad['pack_name']]);
         if (!$item['is_full']) {
             $item['fullness'] = $this->get_fullness($delivery[$ad['pack_name']]);
         }
         $result[] = $item;
     }
     // 按照回款率第一,下线请求,有无备注,有无推广的优先级进行排序
     usort($result, function ($a, $b) {
         if ($a['payment_percent'] != $b['payment_percent']) {
             return $a['payment_percent'] < $b['payment_percent'] ? 1 : -1;
         }
         if ($a['offline_msg'] && !$b['offline_msg'] || !$a['offline_msg'] && $b['offline_msg']) {
             return $a['offline_msg'] ? -1 : 1;
         }
         if ($a['others'] && !$b['others'] || !$a['others'] && $b['others']) {
             return $a['others'] ? -1 : 1;
         }
         if ($a['transfer'] != $b['transfer']) {
             return $b['transfer'] - $a['transfer'];
         }
         return strcmp($a['create_time'], $b['create_time']);
     });
     $result = ['code' => 0, 'msg' => 'fetch', 'list' => array_slice($result, 0, 20), 'ad_comments' => array_values($comments_by_pack_name)];
     if ($output) {
         $this->output($result);
     }
     return $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);
 }