/**
  * @param $attr
  */
 private function translate($attr)
 {
     $ad = new AD();
     $types = $ad->get_all_labels(PDO::FETCH_KEY_PAIR);
     $attr['code'] = md5($attr['id'] . BAOBEI_SALT);
     $attr['quote_rmb'] = number_format($attr['quote_rmb'] / 100, 2);
     $attr['ad_type'] = $types[$attr['ad_type']];
     $attr['cate'] = ADModel::$CATE[$attr['cate']];
     $permissions = $ad->get_permissions(['ad_id' => $attr['id']]);
     $permissions = array_values($permissions);
     $attr['permissions'] = implode("\n<br>", $permissions);
     $attr['feedback'] = ADModel::$FEEDBACK[$attr['feedback']];
     $attr['ad_desc'] = preg_replace('/<span style="color: rgb\\(255, 0, 0\\);">(.*?)<\\/span>/', '', $attr['ad_desc']);
     // 过滤掉标红文字
     if (is_numeric($attr['channel'])) {
         $channel = new Channel();
         $attr['channel'] = $channel->get_channel(array('id' => $attr['channel']))[$attr['channel']];
     }
     if ($attr['agreement_id']) {
         $agreement = new Agreement();
         $agreements = $agreement->get_agreements_basic(['id' => $attr['agreement_id']]);
         $agreement = $agreements[$attr['agreement_id']];
         $attr['agreement'] = $agreement['company_short'] ? $agreement['company_short'] : $agreement['company'];
     }
     return $attr;
 }
 public function send()
 {
     $now = date('Y-m-d H:i:s');
     // 对同一属性的修改不能同时有多个
     $service = new Apply();
     if ($service->is_available_same_attr($this->ad_id, $this->key)) {
         if ($this->key == 'set_ad_url') {
             $apply = new Apply();
             $apply->update_ad_url($this->get($this->key), $this->ad_id);
             return;
         }
         throw new ADException('该属性上次修改申请还未审批,不能再次修改', 41, 400);
     }
     $DB = $this->get_write_pdo();
     $check = SQLHelper::insert($DB, self::$T_APPLY, $this->attributes);
     if (!$check) {
         throw new ADException('创建申请失败', 40, 403, SQLHelper::$info);
     }
     $this->id = SQLHelper::$lastInsertId;
     // 给运营发通知
     $notice = new Notification();
     $notice_status = $notice->send(array('ad_id' => $this->ad_id, 'uid' => $this->id, 'alarm_type' => $this->replace_id ? Notification::$REPLACE_AD : Notification::$EDIT_AD, 'create_time' => $now, 'app_id' => $this->replace_id));
     // 给运营发邮件
     $service = new AD();
     $info = $service->get_ad_info(array('id' => $this->replace_id ? $this->replace_id : $this->ad_id), 0, 1);
     $mail = new Mailer();
     $subject = $this->replace_id ? '替换成新广告' : '广告属性修改';
     $template = $this->replace_id ? 'apply-replace' : 'apply-new';
     $mail->send(OP_MAIL, $subject, $mail->create($template, array_merge((array) $info, array('id' => $this->ad_id, 'replace_id' => $this->replace_id, 'label' => $this->label, 'is_status' => $this->get('key') == 'set_status', 'value' => $this->get('value'), 'comment' => $this->get('send_msg'), 'owner' => $_SESSION['fullname']))));
     header('HTTP/1.1 201 Created');
     return true;
 }
 public function testSend()
 {
     $mailer = new Baobei_Mailer(true);
     $service = new AD();
     $_SESSION['id'] = $_SESSION['role'] = 'test';
     $ad_id = '946afe0b64e3003890c0325dc7c4c408';
     $info = $service->get_ad_info(array('id' => $ad_id), 0, 1);
     $this->assertTrue($mailer->send('*****@*****.**', 'test', $info));
 }
Example #4
0
 public function testCreate()
 {
     $mail = new Mailer(true);
     $service = new AD();
     $_SESSION['id'] = $_SESSION['role'] = 'test';
     $controller = new ADController();
     $template = 'baobei';
     $ad_id = 'a8b13c843a8f2c598381f9dfe6eec16b';
     $info = $service->get_ad_info(array('id' => $ad_id), 0, 1);
     $info = $controller->translate($info);
     $html = $mail->create($template, $info);
     file_put_contents('tests/mail.html', $html);
     $this->assertNotEmpty($html);
     $this->assertTrue($mail->send('*****@*****.**', 'test', $html));
 }
Example #5
0
 public function get_list($userid, $keyword = '', $start = 0, $pagesize = 10)
 {
     $DB = $this->get_read_pdo();
     $keyword_sql = $keyword ? " AND (`ad_name` LIKE :keyword OR `channel` LIKE :keyword)" : '';
     $sql = "SELECT a.`id`, `adid`, `set_status`, `set_job_num`, `set_rmb`,\n              `set_ad_url`, `set_quote_rmb`, a.`create_time`, `handler`,\n              `handle_time`, `send_msg`, `reply_msg`, a.`status`, `ad_name`,\n              `channel`, `cid`\n            FROM `" . self::$TABLE . "` a LEFT JOIN `t_adinfo` i ON a.`adid`=i.`id`\n              LEFT JOIN `t_ad_source` c ON a.`adid`=c.`id`\n            WHERE `userid`='{$userid}' AND a.`status`<" . self::WITHDRAWN . " {$keyword_sql}\n            ORDER BY `create_time` DESC\n            LIMIT {$start}, {$pagesize}";
     if ($keyword) {
         $state = $DB->prepare($sql);
         $state->execute(array(':keyword' => '%' . $keyword . '%'));
         return $state->fetchAll(PDO::FETCH_ASSOC);
     }
     $result = $DB->query($sql)->fetchAll(PDO::FETCH_ASSOC);
     $ad = new AD();
     $result = $ad->get_ad_channels($result);
     return $result;
 }
Example #6
0
 public function create_job($ad_id, $type, $at_every, $time, $num = 0, $options = null)
 {
     $create_time = date("Y-m-d H:i:s");
     $create_user = $_SESSION['id'];
     $params = '';
     if ($options['keywords']) {
         $params .= '"keywords":"' . $options['keywords'] . '",';
     }
     if ($options['ad_desc']) {
         $params .= '"ad_desc":"' . $options['ad_desc'] . '",';
     }
     if ($params) {
         $params = '{' . substr($params, 0, -1) . '}';
     }
     if ($type < 0 || $type > 4 || $at_every != 'at' && $at_every != 'every') {
         throw new Exception('错误的任务类型', 1);
     }
     if ($at_every == 'every' && $type == 2 && $this->has_daily_job($ad_id)) {
         throw new Exception('该广告已存在每日加分任务', 3);
     }
     if ($create_time > $time) {
         throw new Exception('时间已过', 2);
     }
     $ad = new AD();
     if (!$ad->exist($ad_id)) {
         throw new Exception('不存在ad', 4);
     }
     $id = $this->insert_ad_job(['ad_id' => $ad_id, 'jobtype' => $type, 'at_every' => $at_every, 'jobtime' => $time, 'jobnum' => $num, 'create_user' => $create_user, 'create_time' => $create_time, 'is_run' => -1, 'params' => $params]);
     if (!$id) {
         throw new Exception('操作失败', 5);
     }
     if ($options['share'] && $type == self::OFFLINE) {
         $this->update_share($ad_id, $options['show_countdown']);
     }
     // log it
     $log = new ADOperationLogger();
     $log->log($ad_id, 'job', 'add', "[{$type}, {$at_every}, {$time}, {$num}] => {$id}", 0);
     $stamp = strtotime($time);
     $event_url = 'event.php?ajid=' . $id;
     $link_id = $this->insert_job($stamp, $event_url);
     return $this->update_job($link_id, $id);
 }
 public function get_notice($admin_id, $role, $latest)
 {
     $DB = $this->get_read_pdo();
     $m = new Mustache_Engine();
     $ad_service = new AD();
     $sql = "SELECT `type`\n            FROM `t_alarm_group`\n            WHERE `group`={$role}";
     $types = $DB->query($sql)->fetchAll(PDO::FETCH_COLUMN);
     $types = implode(',', $types);
     $type_sql = $types ? " OR `alarm_type` IN ({$types})" : '';
     // 只取最近一周,再早的估计也没啥处理的必要了
     $date = date('Y-m-d', time() - 86400 * 6);
     $sql = "SELECT a.`id`, `uid`, `user_id`, `app_id`, `ad_id`, a.`status`,\n              `create_time`, `op_time`, `description`, `handler`\n            FROM `t_admin_alarm_log` a LEFT JOIN `t_alarm_type` t ON a.alarm_type=t.id\n            WHERE (`admin_id`='{$admin_id}' {$type_sql})\n              AND `create_time`>'{$date}' AND a.`status`=0 AND a.`id`>{$latest}\n            ORDER BY `id` DESC";
     $alarms = $DB->query($sql)->fetchAll(PDO::FETCH_ASSOC);
     foreach ($alarms as &$alarm) {
         $alarm['id'] = (int) $alarm['id'];
         if ($alarm['ad_id']) {
             if (strlen($alarm['ad_id']) == 32) {
                 $ad = $ad_service->get_ad_info(array('id' => $alarm['ad_id']), 0, 1);
                 $alarm['name'] = $ad['ad_name'];
             }
         } else {
             if ($alarm['uid']) {
                 // 发票通知提醒
                 $invoice_id = $alarm['uid'];
                 $invoice_service = new Invoice();
                 $agreement_service = new Agreement();
                 $invoice = $invoice_service->get_invoice_by_id($invoice_id);
                 $agreement = $agreement_service->get_agreement_by_id(array('id' => $invoice['agreement_id']));
                 $alarm['channel'] = $agreement['company_short'];
                 $admin_service = new Admin();
                 $sale = $admin_service->get_sales_info($alarm['user_id']);
                 $alarm['sale'] = $sale['NAME'];
             }
         }
         $alarm['status'] = (int) $alarm['status'];
         $alarm['handler'] = $m->render($alarm['handler'], $alarm);
     }
     return $alarms;
 }
 private function get_total_data($start, $end, $me)
 {
     $ad_service = new AD();
     $filters = array('salesman' => $me);
     $ad_info = $ad_service->get_all_basic_ad_info($filters);
     $adids = array_keys((array) $ad_info);
     $transfer_service = new TransferStat();
     $transfer = $transfer_service->get_ad_transfer_by_ads($start, $end, '');
     $quote_service = new Quote();
     $adquote = $quote_service->get_ad_quote_by_owner($start, $end, $me);
     $adminTask_service = new AdminTaskStat();
     $task = $adminTask_service->get_ad_task_outcome_by_sale($start, $end, $me);
     // 本月数据*4 + 我的广告
     $income_total = 0;
     $out_total = 0;
     $stat_total = 0;
     $cpa_total = 0;
     foreach ($adids as $adid) {
         if (!in_array($adid, $adids)) {
             continue;
         }
         $income = (int) $adquote[$adid]['income'];
         $out = (int) $transfer[$adid]['rmb'] + (int) $task[$adid];
         $stat = (int) $transfer[$adid]['transfer'];
         $cpa = (int) $adquote[$adid]['cpa'];
         $income_total += $income;
         $out_total += $out;
         $stat_total += $stat;
         $cpa_total += $cpa;
     }
     return array($income_total, $out_total, $stat_total, $cpa_total);
 }
 /**
  * 单个广告某天数据统计
  * @param $id
  * @param $date
  */
 public function get_ad_daily_stat($id, $date)
 {
     $ad = new AD();
     if (!$ad->check_ad_owner($id)) {
         $this->exit_with_error(20, '您无法查询此广告', 401);
     }
     $info = $ad->get_ad_info(array('id' => $id), 0, 1);
     $filter = array(':date' => $date, ':ad_id' => $id);
     $stat = new Stat();
     $transfer_res = $stat->get_ad_transfer_by_hour($filter);
     $click_res = $stat->get_ad_click_by_hour($filter);
     $result = array();
     for ($hour = 0; $hour < 24; $hour++) {
         if (!$transfer_res[$hour] && !$click_res[$hour]) {
             continue;
         }
         $result[] = array('hour' => $hour, 'transfer' => (int) $transfer_res[$hour], 'click' => (int) $click_res[$hour], 'cost' => $info['quote_rmb'] * (int) $transfer_res[$hour]);
     }
     $this->output(array('code' => 0, 'msg' => 'fetched', 'total' => 24, 'start' => 0, 'list' => $result));
 }
 private function get_permission_id($permissions)
 {
     $service = new AD();
     $all = $service->get_all_permissions();
     $result = array();
     foreach ($all as $permission) {
         if (in_array($permission['name'], $permissions)) {
             $result[] = $permission['id'];
         }
     }
     return $result;
 }
 public function get_stat_by_hour($id)
 {
     $ad_service = new AD();
     $ad = $ad_service->select_ad_join_source_create_time($id);
     $today = date("Y-m-d");
     $date = empty($_GET['start']) ? $today : $_GET['start'];
     $transferService = new TransferStat();
     $install_res = $transferService->get_offer_install_stat_ad_h($date, $id);
     $transfer_res = $transferService->get_transfer_stat_ad_h($date, $id);
     $click_res = $transferService->get_offer_click_stat_ad_h($date, $id);
     if ($ad['ad_app_type'] == 2) {
         $cpa_res = $transferService->get_income_transfer_stat_ios_h($date, $id);
     }
     $ymd = explode('-', $date);
     $datestamp = mktime(0, 0, 0, (int) $ymd[1], (int) $ymd[2], (int) $ymd[0]);
     $yesterday = date('Y-m-d', $datestamp - 3600 * 24);
     $tomorrow = date('Y-m-d', $datestamp + 3600 * 24);
     $hours = array_unique(array_merge(array_keys($transfer_res), array_keys($install_res), array_keys($click_res)));
     if ($ad['ad_app_type'] == 2) {
         $hours = array_unique(array_merge($hours, array_keys($cpa_res)));
     }
     sort($hours);
     $click = 0;
     $install = 0;
     $transfer = 0;
     $cpa = 0;
     foreach ($hours as $hour) {
         $stat[] = array('hour' => $hour, 'click' => $click_res[$hour], 'install' => $install_res[$hour], 'transfer' => $transfer_res[$hour], 'cpa' => $cpa_res[$hour], 'ratio' => $click_res[$hour] ? round($transfer_res[$hour] * 100 / $click_res[$hour], 2) : 0, 'click_ratio' => $click_res[$hour] ? round($cpa_res[$hour] * 100 / $click_res[$hour], 2) : 0);
         $click += $click_res[$hour];
         $install += $install_res[$hour];
         $transfer += $transfer_res[$hour];
         $cpa += $cpa_res[$hour];
     }
     $ratio = $click == 0 ? 0 : round($transfer * 100 / $click, 2);
     $click_ratio = $click == 0 ? 0 : round($cpa * 100 / $click, 2);
     $transfer_stat = array('hour' => '总计', 'click' => $click, 'install' => $install, 'transfer' => $transfer, 'cpa' => $cpa, 'ratio' => $ratio, 'click_ratio' => $click_ratio);
     $result = array_merge($ad, array('date' => $date, 'cid' => $ad['cid'], 'channel' => $ad['channel'], 'ctime' => substr($ad['create_time'], 5, 5), 'hour' => $stat, 'yesterday' => $date < date('Y-m-d', $today - 86400 * 90) ? NULL : $yesterday, 'tomorrow' => $date == $today ? NULL : $tomorrow, 'is_ios' => $ad['ad_app_type'] == 2, 'stat' => $transfer_stat));
     $this->output(array('code' => 0, 'msg' => 1, 'list' => $result['hour']));
 }
Example #12
0
 /**
  * @param $permissions
  *
  * @throws ADException
  */
 protected function save_permissions($permissions)
 {
     // 保存权限数据
     if ($permissions) {
         $service = new AD();
         $check = $service->set_permissions($this->id, $permissions);
         if (!$check) {
             throw new ADException('插入应用权限信息失败', 26);
         }
     }
 }
Example #13
0
 public function get_daily_stat($start, $end, $filters, $order, $seq, $page, $page_size)
 {
     $quote_service = new QuoteStat();
     $quote = $quote_service->get_ad_income($start, $end);
     $income = $quote_service->get_all_ad_income($start, $end);
     $rmb_change_service = new RmbChangeLog();
     $rmb_change = $rmb_change_service->get_all_ad_rmb_change($start, $end);
     $cut_service = new ADCut();
     $cut = $cut_service->get_cut_rmb($start, $end);
     $cut_rmb = $cut_service->get_all_cut_rmb($start, $end);
     $activity_service = new Activity();
     $activity = $activity_service->get_activity_rmb($start, $end);
     $activities = $activity_service->get_ads_activity_rmb($start, $end);
     $stat = $this->total_stat($start, $end);
     $ad_service = new AD();
     $ads = $ad_service->get_all_ad_info($filters);
     $ratio = ($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb']) / ($stat['total']['rmb_out'] + $stat['total']['task_out']);
     $android_average_rmb = $stat['total']['android_transfer'] ? $stat['total']['android_rmb'] / $stat['total']['android_transfer'] : 0;
     $ios_average_rmb = $stat['total']['ios_transfer'] ? $stat['total']['ios_rmb'] / $stat['total']['ios_transfer'] : 0;
     if ($page_size) {
         $total['all']['amount'] = array('count' => count(array_unique(array_merge(array_keys($stat['ads']), array_keys($quote)))), 'transfer' => $stat['total']['transfer'], 'cpa' => $income['nums'], 'ratio' => $stat['total']['transfer'] ? round($income['nums'] / $stat['total']['transfer'] * 100, 2) : 0, 'out' => $stat['total']['rmb_out'] + $stat['total']['task_out'], 'operation_cost' => $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'] + $stat['total']['transfer_cost'] + $activity['outcome'], 'total_cost' => $stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'] + $activity['outcome'], 'income' => ($income['income'] - $cut_rmb) / 100 + $activity['income'], 'average_rmb' => $stat['total']['transfer'] ? round(($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'] + $activity['outcome']) / $stat['total']['transfer'], 2) : 0, 'profit' => round($income['income'] / 100 * TAX_RATIO - ($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb']), 2), 'profit_ratio' => $income['income'] ? round((TAX_RATIO - ($stat['total']['rmb_out'] + $stat['total']['task_out'] + $stat['total']['transfer_cost'] + $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb']) / ($income['income'] / 100)) * 100, 2) : 0);
         $total['all']['happy_lock'] = array('transfer' => $stat['total']['happy_lock_transfer'], 'out' => $stat['total']['happy_lock_rmb'], 'operation_cost' => $stat['total']['happy_lock_total_cost'] - $stat['total']['happy_lock_rmb'], 'total_cost' => $stat['total']['happy_lock_total_cost'], 'average_rmb' => $stat['total']['happy_lock_transfer'] ? round($stat['total']['happy_lock_total_cost'] / $stat['total']['happy_lock_transfer'], 2) : 0);
         $total['all']['without_happy_lock'] = array('transfer' => $stat['total']['transfer'] - $stat['total']['happy_lock_transfer'], 'out' => $stat['total']['rmb_out'] + $stat['total']['task_out'] - $stat['total']['happy_lock_rmb'], 'operation_cost' => $stat['total']['transfer_cost'] + $activity['outcome'], 'total_cost' => $stat['total']['rmb_out'] + $stat['total']['task_out'] - $stat['total']['happy_lock_rmb'] + $stat['total']['transfer_cost'] + $activity['outcome'], 'average_rmb' => $stat['total']['transfer'] - $stat['total']['happy_lock_transfer'] ? round(($stat['total']['rmb_out'] + $stat['total']['task_out'] - $stat['total']['happy_lock_rmb'] + $stat['total']['transfer_cost'] + $activity['outcome']) / ($stat['total']['transfer'] - $stat['total']['happy_lock_transfer']), 2) : 0);
         $all_ad_ids = array_intersect(array_keys($stat['ads']), array_keys($quote));
         foreach ($all_ad_ids as $id) {
             $total['all']['happy_lock']['income'] += $stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] * $stat['ads'][$id]['happy_lock_transfer'] / $stat['ads'][$id]['transfer'], 2) : 0;
         }
         $total['all']['without_happy_lock']['income'] = $total['all']['amount']['income'] - $total['all']['happy_lock']['income'];
     }
     $ids = array_unique(array_intersect(array_keys($ads), array_unique(array_merge(array_keys($quote), array_keys($stat['ads'])))));
     $total['show']['amount']['count'] = count($ids);
     if (!$order && $page_size) {
         $ids = array_slice($ids, $page * $page_size, $page_size);
     }
     $list = array();
     foreach ($ids as $id) {
         $real = $stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] / $stat['ads'][$id]['transfer'], 2) : 0;
         $rmb_change_key = $ads[$id]['pack_name'] . $ads[$id]['ad_app_type'] . $ads[$id]['ad_sdk_type'] . $ads[$id]['cpc_cpa'];
         $ad = array_merge($ads[$id], array('id' => $id, 'step_rmb' => $rmb_change[$rmb_change_key] ? $rmb_change[$rmb_change_key]['min'] . '~' . $rmb_change[$rmb_change_key]['max'] : $ads[$id]['step_rmb'], 'quote_rmb' => isset($quote[$id]) ? $quote[$id]['max'] != $quote[$id]['min'] ? $quote[$id]['min'] / 100 . '~' . $quote[$id]['max'] / 100 : $quote[$id]['min'] / 100 : $ads[$id]['quote_rmb'] / 100, 'real' => $real, 'ratio' => $stat['ads'][$id]['transfer'] ? round($quote[$id]['nums'] / $stat['ads'][$id]['transfer'] * 100) : 0, 'cpa' => (int) $quote[$id]['nums'], 'income' => round($quote[$id]['income'] - $cut[$id] / 100 + $activities[$id]['income'], 2), 'profit' => round($quote[$id]['income'] * TAX_RATIO - ($stat['ads'][$id]['rmb_out'] + $stat['ads'][$id]['task_out']) * $ratio, 2), 'profit_ratio' => $quote[$id]['income'] ? round((TAX_RATIO - ($stat['ads'][$id]['rmb_out'] + $stat['ads'][$id]['task_out']) * $ratio / $quote[$id]['income']) * 100, 2) : 0, 'beprice' => $ads[$id]['ad_app_type'] == ADModel::ANDROID ? round($real * TAX_RATIO - $android_average_rmb * $ratio, 2) : round($real * TAX_RATIO - $ios_average_rmb * $ratio, 2), 'rmb_out' => (double) $stat['ads'][$id]['rmb_out'], 'task_out' => (double) $stat['ads'][$id]['task_out'], 'transfer' => (int) $stat['ads'][$id]['transfer'], 'happy_lock_rmb' => (double) $stat['ads'][$id]['happy_lock_rmb'], 'happy_lock_transfer' => (int) $stat['ads'][$id]['happy_lock_transfer'], 'happy_lock_income' => $stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] * $stat['ads'][$id]['happy_lock_transfer'] / $stat['ads'][$id]['transfer'], 2) : 0, 'other_income' => round($quote[$id]['income'], 2) - ($stat['ads'][$id]['transfer'] ? round($quote[$id]['income'] * $stat['ads'][$id]['happy_lock_transfer'] / $stat['ads'][$id]['transfer'], 2) : 0), 'happy_lock_cut' => $stat['ads'][$id]['rmb_out'] ? $cut[$id] * $stat['ads'][$id]['happy_lock_rmb'] / $stat['ads'][$id]['rmb_out'] / 100 : 0, 'other_cut' => $stat['ads'][$id]['rmb_out'] ? $cut[$id] * (1 - $stat['ads'][$id]['happy_lock_rmb'] / $stat['ads'][$id]['rmb_out']) / 100 : 0, 'activity_out' => (double) $activities[$id]['outcome']));
         $list[] = $ad;
     }
     if ($order) {
         if ($seq == 'desc') {
             function build_sorter($order)
             {
                 return function ($a, $b) use($order) {
                     if (is_numeric($a[$order])) {
                         return $b[$order] - $a[$order];
                     }
                     return strcmp($b[$order], $a[$order]);
                 };
             }
             usort($list, build_sorter($order));
         } else {
             function build_sorter($order)
             {
                 return function ($a, $b) use($order) {
                     if (is_numeric($a[$order])) {
                         return $a[$order] - $b[$order];
                     }
                     return strcmp($a[$order], $b[$order]);
                 };
             }
             usort($list, build_sorter($order));
         }
         $list = array_slice($list, $page * $page_size, $page_size);
     }
     if ($page_size) {
         foreach ($list as $ad) {
             $total['show']['amount']['transfer'] += $ad['transfer'];
             $total['show']['amount']['cpa'] += $ad['cpa'];
             $total['show']['amount']['out'] += $ad['rmb_out'] + $ad['task_out'];
             $total['show']['amount']['income'] += $ad['income'];
             $total['show']['happy_lock']['transfer'] += $ad['happy_lock_transfer'];
             $total['show']['happy_lock']['out'] += $ad['happy_lock_rmb'];
             $total['show']['happy_lock']['income'] += $ad['happy_lock_income'];
             $total['show']['without_happy_lock']['out'] += $ad['rmb_out'] + $ad['task_out'] - $ad['happy_lock_rmb'];
             $total['show']['without_happy_lock']['transfer'] += $ad['transfer'] - $ad['happy_lock_transfer'];
             $total['show']['without_happy_lock']['income'] += $ad['other_income'];
         }
         $total['show']['amount']['ratio'] = $total['show']['amount']['transfer'] ? round($total['show']['amount']['cpa'] / $total['show']['amount']['transfer'] * 100, 2) : 0;
         $total['show']['amount']['total_cost'] = round($total['show']['amount']['out'] * $ratio, 2);
         $total['show']['amount']['average_rmb'] = $total['show']['amount']['transfer'] ? round($total['show']['amount']['out'] * $ratio / $total['show']['amount']['transfer'], 2) : 0;
         $total['show']['amount']['profit'] = round($total['show']['amount']['income'] * TAX_RATIO - $total['show']['amount']['total_cost'], 2);
         $total['show']['amount']['profit_ratio'] = $total['show']['amount']['income'] ? round((TAX_RATIO - $total['show']['amount']['total_cost'] / $total['show']['amount']['income']) * 100, 2) : 0;
     }
     $result = array('list' => $list, 'amount' => $total, 'total' => $total['show']['amount']['count']);
     return $result;
 }
 /**
  * 取个人的所有申请
  */
 public function get_list()
 {
     $me = $_SESSION['id'];
     $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
     $page = (int) $_REQUEST['page'];
     $pagesize = isset($_REQUEST['pagesize']) ? (int) $_REQUEST['pagesize'] : 10;
     $start = $page * $pagesize;
     $service = $this->get_service();
     $ad_service = new AD();
     $applies = $service->get_list($me, $keyword, $start, $pagesize);
     $labels = array('set_status' => '上/下线', 'set_job_num' => '每日限量', 'set_rmb' => '今日余量', 'set_ad_url' => '替换包', 'set_quote_rmb' => '报价');
     $today = date('Y-m-d');
     $expires = array();
     $handler = array();
     $ad_ids = array();
     foreach ($applies as $index => $apply) {
         $apply = array_filter($apply, function ($value) {
             return isset($value);
         });
         // 修改每日限额同时修改今日余量
         if (array_key_exists('set_rmb', $apply) && array_key_exists('set_job_num', $apply)) {
             $apply['attr'] = 'set_job_num';
             $apply['label'] = $labels['set_job_num'];
             $apply['after'] = $apply['set_job_num'];
             $apply['extra'] = true;
         } else {
             // 普通处理
             foreach ($apply as $key => $value) {
                 if (preg_match('/^set_\\w+/', $key)) {
                     $apply['attr'] = $key;
                     $apply['label'] = $labels[$key];
                     $apply['after'] = $value;
                     break;
                 }
             }
         }
         if (!$apply['handler'] && $apply['attr']) {
             // 尚未处理,取之前的值
             $ad_ids[] = $apply['adid'];
         }
         if ($apply['attr'] == 'set_rmb') {
             if ($apply['create_time'] < $today) {
                 $expires[] = $apply['id'];
                 unset($applies[$index]);
                 break;
             }
             $ad_ids[] = $apply['adid'];
         }
         $apply['is_url'] = $apply['attr'] == 'set_ad_url';
         $apply['is_status'] = $apply['attr'] == 'set_status';
         // 没有匹配的值对,则是替换广告
         if (!$apply['attr'] && !$apply['value']) {
             $apply['label'] = '替换广告';
             $apply['after'] = $apply['adid'];
             $apply['is_replace'] = true;
         }
         $handler[] = $apply['handler'];
         $applies[$index] = $apply;
     }
     // 作废申请
     $service->update(array('status' => Apply::EXPIRED), $expires);
     // 取用户姓名
     $handlers = array_filter(array_unique($handler));
     if ($handlers) {
         $admin_service = new Admin();
         $users = $admin_service->get_user_info(array('id' => $handlers));
         foreach ($applies as $index => $apply) {
             $applies[$index]['handler'] = isset($users[$apply['handler']]) ? $users[$apply['handler']] : $apply['handler'];
         }
     }
     // 取广告信息然后回填数据
     $ad_info = $ad_service->get_ad_info(array('id' => array_filter(array_unique($ad_ids))));
     foreach ($applies as $index => $apply) {
         if (!$apply['handler'] && $apply['attr']) {
             // 尚未处理,取之前的值
             $key = substr($apply['attr'], 4);
             $apply['before'] = $ad_info[$apply['adid']][$key];
         }
         $applies[$index] = $apply;
     }
     $total = $service->get_total_number($me, $keyword);
     $this->output(array('code' => 0, 'msg' => 'fetched', 'total' => $total, 'list' => array_values($applies)));
 }
 public function get_transfer_ad($ad_id)
 {
     $me = $_SESSION['id'];
     $start = $_REQUEST['start'];
     $end = $_REQUEST['end'];
     $ad_service = new AD();
     $channel = $ad_service->get_ad_channel_by_id($ad_id);
     $agreement_service = new Agreement();
     $agreement = $agreement_service->get_agreement_by_adid($ad_id);
     $agreement_id = $agreement['id'];
     $filters = array('salesman' => $me, 'channel' => $channel);
     // 取出用户在该渠道下的所有广告
     $ads = $ad_service->get_all_ad_info($filters);
     // 取出这段期间内有量的广告
     $quote_service = new Quote();
     $quote_ads = $quote_service->get_all_quote_ad($start, $end);
     $ids = array_unique(array_intersect(array_keys($ads), array_keys($quote_ads)));
     $list = array();
     $invoice_service = new Invoice();
     foreach ($ids as $id) {
         // 剔除该段时间内已经开过票的广告
         $nums = $invoice_service->is_invoice($id, $start, $end);
         if ($nums > 0) {
             continue;
         }
         // 剔除没有关联合同的广告
         if (!isset($ads[$id]['agreement_id'])) {
             continue;
         }
         $ad = array('ad_id' => $id, 'ad_name' => $ads[$id]['ad_name'], 'channel_id' => $ads[$id]['cid'], 'channel' => $agreement_id, 'alias' => $ads[$id]['channel']);
         $list[] = $ad;
     }
     $this->output(array('code' => 0, 'msg' => "get", 'list' => $list));
 }
 /**
  * 再次发送报备邮件
  * @param $id
  */
 public function resend_baobei_email($id)
 {
     $service = new AD();
     $passed = $service->check_baobei_pass($id);
     if ($passed) {
         $this->output(array('code' => 0, 'msg' => '改广告已经完成报备,无需再发邮件'));
     }
     $info = $service->get_ad_info(array('id' => $id), 0, 1);
     $check = $this->baobei($_REQUEST['email'], $info);
     if ($check) {
         $this->output(array('code' => 0, 'msg' => '已重新发送报备邮件'));
     } else {
         $this->exit_with_error(1, '发送报备邮件失败,请稍后重试。', 400);
     }
 }
 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 re_choice_ads($attr, $old_invoice)
 {
     $DB = $this->get_read_pdo();
     $invoice_service = new Invoice();
     $invoice_adids = $invoice_service->get_invoice_adids_by_invoiceid($this->id);
     if (is_array($attr['ids'])) {
         $del_adids = array_diff($invoice_adids, $attr['ids']);
         $ins_adids = array_diff($attr['ids'], $invoice_adids);
     } else {
         $adids = array();
         array_push($adids, $attr['ids']);
         $del_adids = array_diff($invoice_adids, $adids);
         $ins_adids = array_diff($adids, $invoice_adids);
     }
     if (count($del_adids) > 0) {
         $del_rows = $invoice_service->delete_invoice_ad_by_invoiceid_adid($this->id, implode("','", $del_adids));
     }
     $ad_service = new AD();
     $start = $old_invoice['start'];
     $end = $old_invoice['end'];
     if (count($ins_adids) > 0) {
         $quote_res = $ad_service->get_quote_by_ads($start, $end, implode("','", $ins_adids));
         $invoice_arr = $this->construct_invoice_ads($quote_res, $this->id);
         $this->save_invoice_ad_info($DB, $invoice_arr);
     }
     $res = $invoice_service->get_invoice_ad_by_invoiceid($this->id);
     $invoice_attr = $this->get_params_by_income($res, $old_invoice['comment']);
     $check = SQLHelper::update($DB, self::$T_INVOICE, $invoice_attr, $this->id);
     if (!$check && $check != 0) {
         throw new Exception('更新发票信息失败。', 21);
     }
     $attr = array_merge(array('id' => $this->id, 'products' => $res));
     return $attr;
 }