/**
  * 创建、更新 串联单
  * @name create
  * @access public
  * @author lijiaying
  * @category hogesoft
  * @copyright hogesoft
  * @param $channel_id int 频道ID
  * @param $date string 日期
  * @param $chg_plan_ids array 串联单ID
  * @param $start_time array 开始时间
  * @param $end_time array 结束时间
  * @param $type array 来源类型
  * @param $channel2_id array 来源类型ID
  * @param $channel2_name array 来源类型名称
  * @param $program_start_time array 时移开始时间
  * @param $epg_id array 32接口返回串联单ID
  * @param $hidden_temp array 标记该条串联单是否被修改过 (1-是 0-否)
  * @param $toff array 时长
  * @param $uri array 流地址
  * @param $create_time array 创建时间
  * @param $update_time array 更新时间
  * @param $admin_id array 用户ID
  * @param $admin_name array 用户名
  * @param $ip array 创建者IP
  * @return $ids array 创建成功串联单IDD
  * @include tvie_api.php
  */
 function edit()
 {
     $channel_id = intval($this->input['channel_id']);
     if (!$channel_id) {
         $this->errorOutput('请选择频道');
     }
     $date = urldecode($this->input['chg_date']);
     if ($date < date('Y-m-d')) {
         $this->errorOutput('此日期已过,无法设置串联单');
     }
     $sql = "SELECT chg_id, is_live FROM " . DB_PREFIX . "channel WHERE id=" . $channel_id;
     $channel_info = $this->db->query_first($sql);
     if (!$channel_info['is_live']) {
         $this->errorOutput('该频道不支持播控或频道不存在');
     }
     $chg_plan_ids = $this->input['ids'];
     $start_time = $this->input['start_times'];
     $end_time = $this->input['end_times'];
     $type = $this->input['type'];
     $channel2_id = $this->input['channel2_ids'];
     $channel2_name = $this->input['channel2_name'];
     $program_start_time = $this->input['program_start_time'];
     $epg_id = $this->input['epg_id'];
     $hidden_temp = $this->input['hidden_temp'];
     if (!$start_time) {
         $this->errorOutput('未设置任何串联单');
     }
     if (!$this->verify_timeline($this->input['start_times'], $this->input['end_times'], $this->input['chg_date'])) {
         $this->errorOutput('时间设置存在重复');
     }
     $this->set_chg_uris($channel2_id, $type);
     if ($this->settings['tvie']['open']) {
         include CUR_CONF_PATH . 'lib/tvie_api.php';
         $tvie_api = new TVie_api($this->settings['tvie']['up_stream_server']);
     }
     $ids = array();
     foreach ($chg_plan_ids as $i => $id) {
         if (strlen($id) >= 12) {
             $id = '';
         }
         $uri = $this->mChgUris[$i];
         if (!$uri) {
             continue;
         }
         $start = strtotime($date . ' ' . urldecode($start_time[$i]));
         $end = strtotime($date . ' ' . urldecode($end_time[$i]));
         $toff = $end - $start;
         if ($program_start_time[$i]) {
             $program_start = strtotime(urldecode($program_start_time[$i]));
             $uri .= $program_start . '000,' . ($program_start + $toff) . '000';
         }
         $epg = array();
         if ($tvie_api && $id) {
             if ($hidden_temp[$i]) {
                 $epg = $tvie_api->update_channel_epg($channel_info['chg_id'], $epg_id[$i], $start, $end, $uri, '播放' . urldecode($channel2_name[$i]) . '的节目');
             }
         } else {
             $epg = $tvie_api->create_channel_epg($channel_info['chg_id'], $start, $end, $uri, '播放' . urldecode($channel2_name[$i]) . '的节目');
         }
         $data = array('channel_id' => $channel_id, 'channel2_id' => $channel2_id[$i], 'channel2_name' => urldecode($channel2_name[$i]), 'change_time' => $start, 'toff' => $toff, 'type' => $type[$i], 'stream_uri' => $uri, 'program_start_time' => $program_start);
         if ($id) {
             if ($hidden_temp[$i]) {
                 $data['update_time'] = TIMENOW;
                 $sql = "UPDATE " . DB_PREFIX . "channel_chg_plan SET ";
                 $space = "";
                 $sql_extra = "";
                 foreach ($data as $key => $value) {
                     if ($value) {
                         $sql_extra .= $space . $key . "=" . "'" . $value . "'";
                         $space = ",";
                     }
                 }
                 if ($sql_extra) {
                     $sql .= $sql_extra . " WHERE id=" . $id;
                     $this->db->query($sql);
                 }
                 $ids[] = $id;
             }
         } else {
             $data['epg_id'] = $epg['result']['id'];
             $data['dates'] = $date;
             $data['create_time'] = TIMENOW;
             $data['update_time'] = TIMENOW;
             $data['admin_name'] = $this->user['user_name'];
             $data['admin_id'] = $this->user['user_id'];
             $data['ip'] = hg_getip();
             $createsql = "INSERT INTO " . DB_PREFIX . "channel_chg_plan SET ";
             $space = "";
             foreach ($data as $key => $value) {
                 $createsql .= $space . $key . "=" . "'" . $value . "'";
                 $space = ",";
             }
             $ret = array();
             $this->db->query($createsql);
             $ret['id'] = $this->db->insert_id();
             $ids[] = $ret['id'];
         }
     }
     return $ids;
     //	$this->setXmlNode('channel_chg_plan' ,'data');
     //	$this->addItem($ids);
     //	$this->output();
 }