<?php
/*
交易记录表表：pre_forum_tradelog
*/

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

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

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

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}tradeorder WHERE oid>$start AND oid<$start+$rpp") or dexit();
while ($pwtradeorder =	$xc->db['source']->fetch_array($query)) {
	$pwtradeorder = daddslashes($pwtradeorder);
	//pre_forum_tradelog
	$tid			=	$pwtradeorder['tid'];

	$dz_dateline	=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT dateline FROM {$discuz_tablepre}forum_post WHERE tid='$tid' AND first=1"), 0);
	$pid			=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT pid FROM {$discuz_tablepre}forum_post WHERE dateline='$dz_dateline' AND first=1"), 0);
	//$pid			=	"";								//暂时为空

	$orderid		=	gmdate('YmdHis', $pwtradeorder['buydate']).random(18);
	$tradeno		=	$pwtradeorder['order_no'];
	$subject		=	$pwtradeorder['subject'];
	$price			=	$pwtradeorder['price'];
	$quality		=	0;
	$itemtype		=	"";
	$number			=	$pwtradeorder['quantity'];
	$tax			=	"";
	$locus			=	$pwtradeorder['address'];
	$sellerid			=	$pwtradeorder['seller'];
	$seller		=	getuid($sellerid);
	$selleraccount	=	"";
	$buyerid		=	$pwtradeorder['buyer'];
	$buyer			=	getusername($buyerid);			//真实的名称：$pwtradeorder['consignee'];
	$buyercontact	=	"";
	$buyercredits	=	0;
	$buyermsg		=	$pwtradeorder['descrip'];
	$status			=	0;
	$lastupdate		=	$pwtradeorder['tradedate'];
	$offline		=	"";
	$buyername		=	"";
	$buyerzip		=	$pwtradeorder['zip'];
	$buyerphone		=	"";
	$buyermobile	=	$pwtradeorder['tel'];
	$transport		=	3;
	$transportfee	=	$pwtradeorder['transportfee'];
	$baseprice		=	$pwtradeorder['price'];
	$discount		=	0;
	$ratestatus		=	0;
	$message		=	"";
	$credit			=	"";
	$basecredit		=	"";

	$field1	=	array('tid','pid','orderid','tradeno','subject','price','quality','itemtype','number','tax','locus','sellerid','seller','selleraccount','buyerid','buyer','buyercontact','buyercredits','buyermsg','status','lastupdate','offline','buyername','buyerzip','buyerphone','buyermobile','transport','transportfee','baseprice','discount','ratestatus','message','credit','basecredit');
	$query1	=	getinsertsql("{$discuz_tablepre}forum_tradelog", $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='.($pwtradeorder['oid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>