<?php
/*
pre_forum_trade
*/

if($start <= 1 && $tableid == 0) {
	$posttables = $comma = '';
	$tablequery = $xc->db['source']->query("SELECT TableName FROM {$source_tablepre}TableList");
	while ($tb = $xc->db['source']->fetch_assoc($tablequery)) {
		$tb['TableName'] = substr($tb['TableName'], 0, 3) == 'Dv_' || substr($tb['TableName'], 0, 3) == 'dv_' ? substr($tb['TableName'], 3) : $tb['TableName'];

		$posttables .= $comma.$tb['TableName'];
		$comma = ',';
	}
}

$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 == 1) {
	truncatetable('forum_trade');
}
$query = $xc->db['source']->query("SELECT max(AnnounceID) FROM {$source_tablepre}{$posttable}");
$maxid = $xc->db['source']->fetch_array($query);

$query = $xc->db['source']->query("SELECT b.AnnounceID,b.ParentID,b.BoardID,b.RootID,b.UserName,b.PostUserid,b.Topic,b.isupload,b.DateAndTime,b.ip,b.locktopic,b.signflag,cast(b.Body AS TEXT) AS Body,t.issmstopic FROM {$source_tablepre}{$posttable} b LEFT OUTER JOIN {$source_tablepre}Topic t ON t.TopicID=b.RootID WHERE b.ParentID=0 AND t.issmstopic=2 AND b.AnnounceID BETWEEN $start AND $end;") or dexit("检索数据表 '$posttable' 错误！<br>请将帖子表 '$posttable' 字段类型为 nText 的改为 Text 类型。", 'mssql');

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

	//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;

	$t_field2 = array('pid');
	$t_query2 = getinsertsql("{$discuz_tablepre}forum_post_tableid", $t_field2);

	if($t_v = $xc->db['discuz']->query($t_query2)) {
		//$pid = $t_v['pid'];
		$pid = $xc->db['discuz']->insert_id();
		$t_field1 = array('fid', 'tid', 'first', 'author', 'authorid', 'subject', 'dateline', 'message', 'useip', 'attachment', 'usesig', 'bbcodeoff', 'smileyoff', 'parseurloff', 'htmlon', 'rate', 'ratetimes', 'status');

		$t_query1 = getinsertsql("{$discuz_tablepre}forum_post", $t_field1);
		$xc->db['discuz']->query($sql);
	} else {
		reportlog("转换商品交易出错 tid = $tid , pid = $pid");
	}
	$converted = 1;
	$totalrows ++;
	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);
}
if( $end < $maxid ) {
	$converted = 1;
}
if($converted) {
	$nowtableid = $tableid + 1;
} elseif($tableid < $maxtableid) {
	$converted = 1;
	$nextid = $tableid + 1;
	$nexttableid = $tableid + 2;
	unset($start);
	unset($end);
}

?>