<?php
/*
转换商品交易：pre_forum_trade
*/

if($start <= 1 && $tableid == '0') {
	$posttables = $comma = '';
	$tablesql = "SELECT TableName FROM {$source_tablepre}TableList";
	$rs = $xc->db['source']->execute($tablesql);
	while(!$rs->EOF){
		$posttables .= $comma.$rs->fields[TableName]->value;
		$comma = ',';
		$rs->movenext();
	}
	$rs->Close();
}

$tablearray = explode(',', $posttables);
if(isset($tablearray[0])) {
	$maxtableid = is_array($tablearray) ? count($tablearray)-1 : 0;
} else {
	$maxtableid = is_array($tablearray) ? count($tablearray) : 0;
}

$tablecount = count($tablearray);
$tableid = isset($tableid) ? $tableid : 0;
$maxtableid = isset($maxtableid) ? $maxtableid : 0;
$multitables = $maxtableid > 0 ? 1 : 0;
$posttable = $tablearray[$tableid];

if($start <= 1 && $tableid == 0) {
	truncatetable('forum_trade');
}

$sql = "SELECT b.*,t.issmstopic FROM $posttable b LEFT OUTER JOIN {$source_tablepre}Topic t ON b.rootid=t.topicid WHERE  b.ParentID=0 AND t.issmstopic=2 AND (b.announceid BETWEEN $start AND $end)";
$rs = $xc->db['source']->execute($sql);
$fieldarray = array('announceid', 'parentid', 'boardid', 'username', 'postuserid', 'topic', 'body', 'dateandtime', 'rootid', 'ip', 'locktopic', 'isupload', 'issmstopic');

while (!$rs->EOF) {
	foreach($fieldarray AS $field) {
		$dvp[$field] = daddslashes($rs->fields[$field]->value);
	}

	//pre_forum_post
	//$pid	=	$dvp['announceid'];
	$fid	=	$dvp['boardid'];
	$tid	=	$dvp['rootid'];
	if(empty($tid)) {
		continue;
	}
	$first	=	$dvp['parentid'] == 0 ? 1 : 0;
	$author	=	$dvp['username'];
	$authorid	=	$dvp['postuserid'];
	$subject	=	cutstr(@strip_tags(trim($dvp['topic'])), 78);
	$dateline	=	timetounix($dvp['dateandtime']);
	$message	=	$dvp['body'];
	$useip		=	$dvp['ip'];
	$invisible	=	0;
	$anonymous	=	0;
	$usesig		=	1;
	$htmlon		=	@strip_tags($message) == $message ? 0 : 1;
	$bbcodeoff	=	0;
	$smileyoff	=	0;
	$parseurloff	=	0;
	$attachment	=	intval($dvp['isupload']);
	$rate		=	0;
	$ratetimes	=	0;
	$status		=	$dvp['locktopic'] == 2 ? 1 : 0;
	$tags		=	'';
	$comment	=	0;

	//插入交易
	preg_match_all('|\(([^\)]+)\)(.*)\(/([^\)]+)\)|U', trim($dvp['body']), $info);
	$body = array();
	foreach ($info[0] as $value) {
		preg_match('|\(([^\)]+)\)(.*)\(/([^\)]+)\)|U', trim($value), $a);
		if (!empty($a[1]) && strtolower($a[1]) == strtolower($a[3])) {
			$body[strtolower($a[1])] = $a[2];
		}
	}
	
	preg_match_all('|\[upload(.*)\](.*)\[/upload\]|U', $body['body'], $array);

	$att_array = array();
	
	for ($i = 1; $i <= count($array[0]); $i++) {
		$att_array[] = "[attachimg]".$i."[/attachimg]";
	}
	
	$att_array[count($array[0])-1] = "";

	$body['body'] = convertbbcode($body['body']);
	
	$tid = $tid;
	$pid = $dvp['announceid'];
	$typeid = 0;
	$sellerid = $authorid;
	$seller = $author;
	$account = '';
	$subject = $body['subject'];
	$price = $body['price'];
	$amount = 1;
	$quality = 1;
	$locus = '';
	$transport = $body['transport'];
	$ordinaryfee = $body['mail'];
	$expressfee = $body['express'];
	$emsfee = '';
	$itemtype = 1;
	$dateline = $dateline;
	$expiration = '1202486400';
	$lastbuyer = '';
	$lastupdate = $dateline;
	$totalitems = '';
	$tradesum = '';
	$closed = '';
	$aid = '';
	$displayorder = '';
	$costprice = '';

	$trade_sql = "INSERT INTO {$discuz_tablepre}forum_trade (tid, pid, typeid, sellerid, seller, account, subject, price, amount, quality, locus, transport, ordinaryfee, expressfee, emsfee, itemtype, dateline, expiration, lastbuyer, lastupdate, totalitems, tradesum, closed, aid, displayorder, costprice) VALUES ('$tid', '$pid', '$typeid', '$sellerid', '$seller', '$account', '$subject', '$price', '$amount', '$quality', '$locus', '$transport', '$ordinaryfee', '$expressfee', '$emsfee', '$itemtype', '$dateline', '$expiration', '$lastbuyer', '$lastupdate', '$totalitems', '$tradesum', '$closed', '$aid', '$displayorder', '$costprice');";
	$xc->db['discuz']->query($trade_sql);
	//插入交易 end

	$first = 1;
	$message = '';
	$attachment = 0;
	$field1 = array('pid');
	$query1 = getinsertsql("{$discuz_tablepre}forum_post_tableid", $field1);

	if($xc->db['discuz']->query($query1)) {
		$pid = $xc->db['discuz']->insert_id();
		$field2 = array('fid', 'tid', 'first', 'author', 'authorid', 'subject', 'dateline', 'message', 'useip', 'attachment', 'usesig', 'bbcodeoff', 'smileyoff', 'parseurloff', 'htmlon', 'rate', 'ratetimes', 'status');
		$query2 = getinsertsql("{$discuz_tablepre}post", $field2);
		$xc->db['discuz']->query($query2);
	} else {
		reportlog("转换商品交易出错 tid = $tid pid = $pid");
	}

	$converted = 1;
	$totalrows ++;
	$rs->movenext();
	if($multitables){
		$url ='index.php?mod=dealdata&step='.$step.'&start='.($dvp['announceid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&tableid='.$tableid.'&maxtableid='.$maxtableid.'&sqlfilenum='.$sqlfilenum.'&posttables='.$posttables;
	} else {
		$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvp['announceid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	}
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}

$rs->Close();

if($converted) {
	$nowtableid = $tableid + 1;
} elseif($tableid < $maxtableid) {
	$converted = 1;
	$nextid = $tableid + 1;
	$nexttableid = $tableid + 2;
	unset($start);
	unset($end);
}

?>