Esempio n. 1
0
 /**
  * @param $ad_id
  * @param $name
  * @param $desc
  * @param $step_rmb
  * @param $type
  * @param $delta
  * @param $param
  * @param $probability
  */
 private function create_task($ad_id, $name, $desc, $step_rmb, $type, $delta, $param, $probability)
 {
     $DB = $this->get_write_pdo();
     $task_id = Utils::create_id();
     $now = date('Y-m-d H:i:s');
     $sql = "INSERT INTO `t_task`\n            (`id`, `ad_id`, `step_rmb`, `type`, `delta`, `name`, `desc`,\n              `create_time`, `status`, `param`, `probability`)\n            VALUES (:id, :ad_id, :step_rmb, :type, :delta, :name, :desc,\n              '{$now}', :status, :param, :probability)\n            ON DUPLICATE KEY UPDATE `status`=:status, `create_time`='{$now}',\n              `name`=:name, `desc`=:desc, `probability`=:probability";
     $state = $DB->prepare($sql);
     $check = $state->execute(array(':id' => $task_id, ':ad_id' => $ad_id, ':step_rmb' => $step_rmb, ':type' => $type, ':delta' => $delta, ':name' => $name, ':desc' => $desc, ':status' => self::ON, ':param' => $param, ':probability' => $probability));
     if ($check) {
         $log = new ADOperationLogger($DB);
         $log->log($ad_id, 'task', 'add', "{$step_rmb}, {$type}, {$delta}, {$name}, {$desc}, {$param}, {$probability} => {$task_id}");
     }
 }
 /**
  * 创建新广告
  * @author Meathill
  * @since 0.1.0
  * @param string $key
  */
 public function create($key)
 {
     if ($key != 'init') {
         $this->exit_with_error(10, '请求错误', 400);
     }
     $CM = $this->get_cm();
     $im_cp = $_SESSION['role'] == Auth::$CP_PERMISSION;
     $now = date('Y-m-d H:i:s');
     $attr = $this->get_post_data();
     $id = $attr['id'] = $attr['id'] ? $attr['id'] : Utils::create_id();
     $user = $im_cp ? new User() : null;
     $left = 0;
     // 校验CP用户
     if ($im_cp && !$user->has_enough_money($attr)) {
         $this->exit_with_error(1, '您的余额不足', 402);
     }
     $ad = new ADModel($attr);
     if ($ad->error) {
         $this->exit_with_error($ad->error);
     }
     try {
         if ($im_cp) {
             // 扣费
             $left = $user->lock_money_for_ad($attr['quote_rmb'], $attr['total_num']);
         }
         $ad->save();
     } catch (ADException $e) {
         $this->exit_with_error($e->getCode(), $e->getMessage(), 400, SQLHelper::$info);
     }
     $client_email = $attr['file-email'];
     $replace_id = $ad->replace;
     // 给运营发新广告通知
     $notice = new Notification();
     $notice_status = $notice->send(array('ad_id' => $id, 'alarm_type' => Notification::$NEW_AD, 'create_time' => $now));
     // 给运营发邮件
     $mail = new Mailer();
     $subject = ($im_cp ? '广告主' : '商务') . '[' . $_SESSION['fullname'] . ']创建新广告:' . $attr['channel'] . ' ' . $attr['ad_name'];
     $mail->send(OP_MAIL, $subject, $mail->create('ad-new', $attr, array('owner' => $_SESSION['fullname'], 'cate' => $CM->ad_cate[$attr['cate']])));
     if (!$im_cp) {
         // 给广告主发送报备邮件
         if ($client_email) {
             $this->baobei($client_email, $attr);
         }
         // 给运营发修改申请
         if ($replace_id) {
             $this->send_apply($id, array('replace_id' => $replace_id, 'message' => $attr['others']));
             return;
         }
     }
     // log it
     $log = new ADOperationLogger();
     $log->log($id, 'ad', 'add', $attr['others'], 0);
     $result = array('code' => 0, 'msg' => '创建广告成功。相关通知' . ($notice_status ? '已发' : '失败'), 'notice' => $notice_status ? '通知已发' : '通知失败', 'ad' => array('id' => $id));
     if ($im_cp) {
         $result['me'] = ['balance' => $left];
     }
     $this->output($result);
 }