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

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

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}debates WHERE tid >= $start AND tid < $start + $rpp") or dexit();
while ($pwdebate = $xc->db['source']->fetch_array($query)) {
	$pwdebate		=	daddslashes($pwdebate);
	//pre_forum_debate
	$t_post			=	getdiscuztid($pwdebate['tid'], $pwdebate['postdate']);
	$tid			=	$t_post['tid'];
	if(empty($tid)){
		continue;
	}
	$uid			=	$pwdebate['authorid'];
	$starttime		=	$pwdebate['postdate'];
	$endtime		=	$pwdebate['endtime'];
	$affirmdebaters	=	$pwdebate['obposts'];			//正方辩论人数
	$negadebaters	=	$pwdebate['reposts'];			//反方辩论人数
	$affirmvotes	=	$pwdebate['obvote'];			//正方得票数
	$negavotes		=	$pwdebate['revote'];			//反方得票数
	$umpire			=	$pwdebate['umpire'];			//裁判用户名
	$winner			=	$pwdebate['judge'];				//获胜方(0:平局 1:为正方 2:为反方)
	$bestdebater	=	$pwdebate['debater'];			//最佳辩手用户名
	$affirmpoint	=	$pwdebate['obtitle'];			//正方观点
	$negapoint		=	$pwdebate['retitle'];			//反方观点
	$umpirepoint	=	$pwdebate['umpirepoint'];		//中立观点
	
	$affirmvoterids	=	'';			//支持ID
	$aff_query	=	$xc->db['source']->query("SELECT authorid FROM {$source_tablepre}debatedata WHERE tid='$tid' and standpoint='1' GROUP BY authorid");
	while($aff_uid = $xc->db['source']->fetch_array($aff_query)) {
		$affirmvoterids	=	$aff_uid['authorid']."\t";
	}
	
	$negavoterids	=	'';			//反对ID
	$neg_query		=	$xc->db['source']->query("SELECT authorid FROM {$source_tablepre}debatedata WHERE tid='$tid' and standpoint='2' GROUP BY authorid");
	while($neg_uid = $xc->db['source']->fetch_array($neg_query)) {
		$negavoterids =	$neg_uid['authorid']."\t";
	}
	
	$affirmreplies	=	$pwdebate['obposts'];			//正方回复次数，用来翻页
	$negareplies	=	$pwdebate['reposts'];			//反方回复次数，用来翻页

	$field1	=	array('tid','uid','starttime','endtime','affirmdebaters','negadebaters','affirmvotes','negavotes','umpire','winner','bestdebater','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='.($pwdebate['tid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>