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)); }
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)); }
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; }
/** * 单个广告某天数据统计 * @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)); }
public function fetch() { $service = new AD(); $attr = $service->get_ad_info(array('id' => $this->id), 0, 1); $this->attributes = $attr; }
/** * 取个人的所有申请 */ 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))); }
/** * 再次发送报备邮件 * @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 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); }