<?php
/*
辩论帖子表：pre_forum_debatepost
*/
if($start <= 1){
	truncatetable('forum_debatepost');
}

$maxpid = $xc->db['source']->result($xc->db['source']->query("SELECT MAX(pid) as maxpid FROM {$source_tablepre}postdebatefields "), 0, "MAXPID");
if($start < $maxpid){
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}postdebatefields where pid >= $start AND pid < $start + $rpp") or dexit();
while ($debatedata = $xc->db['source']->fetch_assoc($query)) {
	$debatedata		=	daddslashes($debatedata);
	if($debatedata['pid'] == '0') continue;

	//pre_forum_debatepost
	$pid		=	$debatedata['pid'];
	$stand		=	$debatedata['opinion'];
	$tid		=	$debatedata['tid'];
	$voters		=	$debatedata['diggs'];

	$posttable = $xc->db['source']->result($xc->db['source']->query("SELECT id as tableid FROM {$source_tablepre}tablelist WHERE  mintid <= $tid AND ($tid <= maxtid OR maxtid=0)"), 0, "TABLEID") or die("检索数据表 'tablelist' 错误！请将表 'tablelist' 字段类型为 nText 的改为 Text 类型。");
	$posttable = "posts".$posttable;
	$post_query = $xc->db['source']->query("SELECT posterid, postdatetime FROM {$source_tablepre}$posttable WHERE pid = $pid") or die("检索数据表 '$posttable' 错误！请将表 '$posttable' 字段类型为 nText 的改为 Text 类型。");
	while ($post = $xc->db['source']->fetch_assoc($post_query)) {
		$uid		=	$post['posterid'];
		$dateline	=	timetounix($post['postdatetime']);
	}

	$voterids	=	'';			//支持ID
	$v_query	=	$xc->db['source']->query("SELECT diggerid FROM {$source_tablepre}debatediggs WHERE tid='$tid'");
	while($voter_uid = $xc->db['source']->fetch_assoc($v_query)) {
		$voterids	=	$voter_uid['diggerid']."\t";
	}

	$field1	=	array('pid','stand','tid','uid','dateline','voters','voterids');
	$query1	=	getinsertsql("{$discuz_tablepre}forum_debatepost", $field1);

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