<?php
/*
活动主表：pre_forum_activity
活动参加表：pre_forum_activityapply
*/

if($start <= 1){
	truncatetable('forum_activity');
	truncatetable('forum_activityapply');
}

$maxtid = $xc->db['source']->result($xc->db['source']->query("SELECT max(tid) FROM {$source_tablepre}activitydefaultvalue"), 0);

if($start < $maxtid){
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}activitydefaultvalue v LEFT JOIN {$source_tablepre}activitymodel m ON m.actmid=v.actmid WHERE tid >= $start AND tid <$start + $rpp") or dexit();
while ($pwactivitydefaultvalue 	=	$xc->db['source']->fetch_array($query)) {
	$cost_a			=	unserialize($pwactivitydefaultvalue['fees']);
	$pwactivitydefaultvalue	=	daddslashes($pwactivitydefaultvalue);
	//pre_forum_activity
	$tid			=	$pwactivitydefaultvalue['tid'];
	$uid			=	$xc->db['source']->result($xc->db['source']->query("SELECT authorid FROM {$source_tablepre}threads WHERE tid=$tid"));
	$aid			=	0;
	$cost			=	$cost_a[0]['money'];
	$starttimefrom		=	$pwactivitydefaultvalue['starttime'];
	$starttimeto		=	$pwactivitydefaultvalue['endtime'];
	$place			=	$pwactivitydefaultvalue['location'];
	$class			=	$pwactivitydefaultvalue['name'];
	$gender			=	$pwactivitydefaultvalue['genderlimit'];
	$number			=	$pwactivitydefaultvalue['maxparticipant'];
	$applynumber		=	$xc->db['source']->result($xc->db['source']->query("SELECT COUNT(*) FROM {$source_tablepre}activitymembers WHERE tid=$tid"), 0);		//重新计算参加人数
	$expiration		=	$pwactivitydefaultvalue['signupendtime'];

	$field1			=	array('tid','uid','aid','cost','starttimefrom','starttimeto','place','class','gender','number','applynumber','expiration');
	$query1			=	getinsertsql("{$discuz_tablepre}forum_activity", $field1);

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