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;
 }
 /**
  * 删除广告
  *
  * @param $id
  */
 public function delete($id)
 {
     $ad = new ADModel(['id' => $id]);
     $ad->fetch();
     // 拒绝操作已经上线的广告
     if (in_array($ad->get('status'), [0, 1])) {
         $this->exit_with_error(50, '此广告已经推广,不能删除。您可以申请将其下线。', 400);
     }
     // 拒绝操作别人的广告
     $check = $ad->check_owner();
     if (!$check) {
         $this->exit_with_error(51, '您无权操作此广告', 403);
     }
     // 被编辑锁定的广告不能撤销
     $redis = $this->get_redis();
     $redis_key = self::REDIS_PREFIX . $id;
     $value = $redis->get($redis_key);
     if ($value) {
         $value = json_decode($value, true);
         $this->exit_with_error(52, '此广告正由' . $value['name'] . '审查中,请联系ta进行处理。', 403);
     }
     // 撤回替换广告申请
     $apply_service = new Apply();
     $apply_service->remove_replace_apply($id);
     // 撤回通知
     $notice = new Notification();
     $notice->set_status(array('ad_id' => $id, 'alarm_type' => array(Notification::$NEW_AD, Notification::$REPLACE_AD)), Notification::$HANDLED);
     $attr = array('status' => ADModel::DELETED);
     $this->update($id, $attr);
 }
 public function update($id)
 {
     $attr = $this->get_post_data();
     // 禁止用户修改运营正在审核的申请
     $this->judge_check($id);
     // 取当前申请
     $apply_service = new Apply();
     $apply_res = $apply_service->get_by_applyid($id);
     if (!$apply_res) {
         $this->exit_with_error(42, '该申请已经审核过,你不得修改该申请。', 401);
     }
     $apply_res = array_filter($apply_res, function ($value) {
         return isset($value);
     });
     // 修改每日限额同时修改今日余量
     if (array_key_exists('set_rmb', $apply_res) && array_key_exists('set_job_num', $apply_res)) {
         $res['attr'] = 'set_job_num';
     } else {
         // 普通处理
         foreach ($apply_res as $res_key => $value) {
             if (preg_match('/^set_\\w+/', $res_key)) {
                 $res['attr'] = $res_key;
                 break;
             }
         }
     }
     // 修改量级数据
     if (isset($attr['after'])) {
         if ($res['attr'] == 'set_job_num') {
             $attr = array_merge(array('set_job_num' => $attr['after'], 'set_rmb' => $attr['after']), $attr);
         } else {
             $attr[$res['attr']] = $attr['after'];
         }
         unset($attr['after']);
     }
     // 修改更换包地址
     if (isset($attr['ad_url'])) {
         $attr = array_merge(array('set_ad_url' => $attr['ad_url']), $attr);
         if (isset($attr['message'])) {
             unset($attr['message']);
         }
         unset($attr['ad_url']);
     }
     $apply = new ApplyModel($id, $attr);
     try {
         $res = $apply->update();
     } catch (ADException $e) {
         $this->exit_with_error($e->getCode(), $e->getMessage(), 400, SQLHelper::$info);
     }
     $this->output(array('code' => 0, 'msg' => '修改申请成功', 'apply' => $res));
 }
 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;
 }