<?php
/*
辩论主题表：pre_forum_debate
*/
if($start <= 1){
	truncatetable('forum_debate');
}

$maxtid = $xc->db['source']->result($xc->db['source']->query("SELECT MAX(tid) as maxtid FROM {$source_tablepre}debates "), 0, "MAXTID");
if($start < $maxtid){
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT ba.*, t.tid, t.posterid, t.postdatetime FROM {$source_tablepre}debates ba LEFT JOIN {$source_tablepre}topics t  ON t.tid = ba.tid WHERE ba.tid >= $start AND ba.tid < $start + $rpp") or die("检索数据表 '{$source_tablepre}debates' 错误！请将表 '{$source_tablepre}debates' 字段类型为 nText 的改为 Text 类型。");

while ($debate = $xc->db['source']->fetch_assoc($query)) {
	$debate			=	daddslashes(array_change_key_case($debate));

	//pre_forum_debate
	$tid			=	$debate['tid'];
	if(empty($tid)){
		continue;
	}

	$uid			=	$debate['posterid'];
	$starttime		=	$debate['postdatetime'];
	$endtime		=	timetounix($debate['terminaltime']);
	$affirmdebaters	=	$debate['positivediggs'];			//正方辩论人数
	$negadebaters	=	$debate['negativediggs'];			//反方辩论人数
	$affirmvotes	=	'';			//正方得票数
	$negavotes	= '';			//反方得票数
	$umpire		='';			//裁判用户名
	//$winner			=	$debate['judge'];				//获胜方(0:平局 1:为正方 2:为反方)
	//$bestdebater	=	$debate['debater'];			//最佳辩手用户名
	$affirmpoint	=	$debate['positiveopinion'];			//正方观点
	$negapoint		=	$debate['negativeopinion'];			//反方观点
	$umpirepoint	=	'';		//中立观点
	
	$affirmvoterids	= '';			//支持ID
	$negavoterids	= '';			//反对ID

	$affirmreplies	=	$xc->db['source']->result($xc->db['source']->query("SELECT COUNT(*) as affirm FROM {$source_tablepre}postdebatefields WHERE tid='$tid' and opinion='1'"), 0, "AFFIRM");		//正方回复次数，用来翻页
	
	$negareplies	=	$xc->db['source']->result($xc->db['source']->query("SELECT COUNT(*) as nega FROM {$source_tablepre}postdebatefields WHERE tid='$tid' and opinion='2'"), 0, "NEGA");		//反方回复次数，用来翻页

	//$affirmreplies	=	$debate['obposts'];			
	//$negareplies	=	$debate['reposts'];			

	$field1	=	array('tid','uid','starttime','endtime','affirmdebaters','negadebaters','affirmvotes','negavotes','umpire','affirmpoint','negapoint','umpirepoint','affirmvoterids','negavoterids','affirmreplies','negareplies');
	$query1	=	getinsertsql("{$discuz_tablepre}forum_debate", $field1);

	if ($xc->db['discuz']->query($query1)) {
		$convertedrows ++;
	} else {
		reportlog("无法转换辩论主题 tid = $tid ，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
	}
	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($debate['tid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>