/**
  * 取存在sesssion里的用户数据
  *
  * @return array
  */
 private function get_user_info()
 {
     if ($_SESSION['role'] != Auth::$CP_PERMISSION) {
         return array('id' => $_SESSION['id'], 'user' => $_SESSION['user'], 'fullname' => $_SESSION['fullname'], 'role' => $_SESSION['role']);
     }
     $info = array('id' => $_SESSION['id'], 'email' => $_SESSION['email'], 'role' => 'cp', 'fullname' => $_SESSION['fullname'], 'balance' => $_SESSION['balance'], 'last_login' => $_SESSION['last_login'], 'sidebar' => 'cp');
     // 如果是cp,取财务状况
     $service = new User();
     $balance = $service->get_all_my_money();
     $info = array_merge($info, $balance);
     if ($_REQUEST['full']) {
         $service = new User();
         $full = $service->get_full_info();
         $info = array_merge($info, $full);
     }
     return $info;
 }
 /**
  * 修改广告
  * 部分属性的修改不会直接体现在表中,而是以请求的方式存在
  * 针对状态`status`、每日投放量`job_num`、今日余量`num`、广告链接`ad_url`,
  * 报价`quote_rmb`的修改会产生申请
  *
  * 其它修改会直接入库
  *
  * 暂时禁止CP用户修改报价
  *
  * @author Meathill
  * @since 0.1.0
  *
  * @param string $id 广告id
  * @param array [optional] $attr
  *
  * @return null
  */
 public function update($id, $attr = null)
 {
     $attr = $attr ? $attr : $this->get_post_data();
     $attr['id'] = $id;
     $ad = new ADModel($attr);
     $im_cp = $_SESSION['role'] == Auth::$CP_PERMISSION;
     $now = date('Y-m-d H:i:s');
     if ($ad->error) {
         $this->exit_with_error($ad->error);
     }
     $ad->fetch();
     if (!$ad->check_owner()) {
         $this->exit_with_error(10, '不是您的广告,您不能修改', 401);
     }
     if ($im_cp && array_diff_key($attr, ['id' => '', 'status' => 0])) {
         $this->exit_with_error(11, '您不能修改广告。', 401);
     }
     // 需要发申请的修改,只有未上线的需要申请
     $status = $ad->get('status');
     $to_status = null;
     $passed = [ADModel::ONLINE, ADModel::OFFLINE];
     if (array_intersect(self::$FIELDS_APPLY, array_keys($attr)) && in_array($status, $passed)) {
         $this->send_apply($id, $attr, $ad);
     }
     if (array_key_exists('status', $attr)) {
         $to_status = $attr['status'];
         if (!in_array($to_status, [ADModel::ONLINE, ADModel::OFFLINE, ADModel::DELETED])) {
             $this->exit_with_error(14, '您只能上下线或者删除广告', 403);
         }
         if ($status == ADModel::APPLY && in_array($to_status, $passed)) {
             $this->exit_with_error(12, '您的广告正在等待审核,不能申请上/下线。', 403);
         }
         if ($to_status == ADModel::DELETED && in_array($status, $passed)) {
             $this->exit_with_error(13, '您的广告已经通过审核,不能删除。', 403);
         }
         if ($status == ADModel::REJECTED && $to_status == ADModel::ONLINE) {
             $this->reapply_ad($id, $attr, $ad);
         }
         if ($to_status == ADModel::ONLINE) {
             // 上线需要审批
             $this->send_apply($id, $attr, $ad);
         }
         // 大家可以自己控制下线,不过要给运营发通知
         if ($to_status == ADModel::OFFLINE) {
             $mail = new Mailer();
             $mail->send(OP_MAIL, '广告自助下线通知', $mail->create('ad-offline', $ad->attributes, ['message' => $attr['message'], 'owner' => $_SESSION['fullname'], 'status-time' => $attr['status-time']]));
         }
         $job = new Job();
         if ($attr['status-time'] && $attr['status-time'] > $now) {
             // 定时下线,创建计划任务
             $job->remove_on_off_job($id, $attr['status-time']);
             $job->create_job($id, Job::OFFLINE, Job::AT, $attr['status-time']);
             $this->output(['code' => 0, 'msg' => '已添加计划任务,该广告届时将下线。']);
         }
         $job->remove_all_job($id);
         $attr['status_time'] = date('Y-m-d H:i:s');
     }
     if ($attr['comment']) {
         // 广告备注修改,需发送一枚通知
         //设置备注
         $ad->save_ad_comment($id, $attr['comment']);
         $commentID = SQLHelper::$lastInsertId;
         $notice = new Notification();
         $check = $notice->send(array('uid' => $commentID, 'ad_id' => $id, 'alarm_type' => Notification::$EDIT_AD_COMMENT, 'create_time' => $now));
         $mail = new Mailer();
         $mail->send(OP_MAIL, '广告备注修改', $mail->create('comment-modified', array('id' => $id)));
         //重新取出comment表的备注集合
         $service = new AD();
         $comments = $service->get_ad_comments(array($id));
         $this->output(array('code' => 0, 'msg' => '添加成功。' . ($check ? '通知已发送。' : ''), 'ad' => array('cm_others' => $comments[$id], 'comment' => '')));
     }
     try {
         $message = $attr['message'];
         unset($attr['message'], $attr['rmb'], $attr['status-time']);
         // 不需要的值
         $ad->update($attr, $message);
         if ($attr['ad_shoot']) {
             $attr['ad_shoot'] = preg_split('/,+/', $attr['ad_shoot']);
         }
     } catch (ADException $e) {
         $this->exit_with_error($e->getCode(), $e->getMessage(), 400, SQLHelper::$info);
     }
     $result = array('code' => 0, 'msg' => '修改完成', 'ad' => $attr);
     // cp用户撤回未审批广告,给他退钱
     if ($im_cp && $to_status == ADModel::DELETED && $status == ADModel::APPLY) {
         $user = new User();
         $balance = $user->unlock_money_for_ad($id);
         if (!$balance) {
             $this->exit_with_error(15, '退回暂扣费用失败。', 400);
         }
         $result['me'] = $balance;
     }
     $this->output($result);
     return null;
 }