<?php
/*
ucenter短消息表：pre_ucenter_pms
*/

if($start <= 1) {
	truncatetable('ucenter_pm_lists');
	truncatetable('ucenter_pm_indexes');
	truncatetable('ucenter_pm_members');
	for($i = 0; $i < 10; $i ++) {
		truncatetable('ucenter_pm_messages_'.$i);
	}
	validid('pmid', 'pms');
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}pms WHERE pmid BETWEEN $start AND $end") or die("检索数据表 '{$source_tablepre}pms' 错误！请将表 '{$source_tablepre}pms' 字段类型为 nText 的改为 Text 类型。");

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

	$pmid		= $p['pmid'];
	$fromusername	= $p['msgfrom'];
	$fromuid	= $p['msgfromid'];
	$toid	= $p['msgtoid'];
	$folder		= $p['folder'];
	$new		= $p['new'];
	$subject	= $p['subject'];
	$message	= $p['message'];
	$delstatus	= 0;
	$related	= 0;
	$fromappid	= 1;

	if(!$fromuid || !$toid) {
		continue;
	}

	//ucenter_pm_lists
	$authorid	=	$p['msgfromid'];
	$pmtype		=	1;
	$subject	=	formatstr(cutstr(htmlspecialchars(trim(@strip_tags($p['subject']))), 78));
	$members	=	2;
	$min_max	=	relationship($authorid, $msgtoid);
	$dateline	= timetounix($p['postdatetime']);
	$lastmessage = array('lastauthorid' => $msgfromid, 'lastauthor' => $msgfrom, 'lastsummary' => $message);
	$lastmessage = addslashes(serialize($lastmessage));
	
	$sessionexists = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT plid FROM {$discuz_tablepre}ucenter_pm_lists WHERE min_max='".$min_max."' LIMIT 1"),0);

	if($sessionexists) {
		$plid = $sessionexists;
	} else {
		$field1 = array('authorid','pmtype','subject','members','min_max','dateline','lastmessage');
		$query1 = getinsertsql("{$discuz_tablepre}ucenter_pm_lists", $field1);

		if($xc->db['discuz']->query($query1)){
			$plid = $xc->db['discuz']->insert_id();
		} else {
			continue;
		}
	}

	//ucenter_pm_indexes
	$field2 = array('plid');
	$query2 = getinsertsql("{$discuz_tablepre}ucenter_pm_indexes", $field2);

	if($xc->db['discuz']->query($query2)){
		$pmid = $xc->db['discuz']->insert_id();
	} else {
		continue;
	}

	//ucenter_pm_message_X
	$tmpplid = (string)$plid;
	$tableid = $tmpplid{strlen($tmpplid)-1};
	$delstatus = 0;

	$field3 = array('pmid','plid','authorid','message','dateline','delstatus');
	$query3 = getinsertsql("{$discuz_tablepre}ucenter_pm_messages_{$tableid}", $field3);
	
	//ucenter_pm_members
	$isnew	=	0;
	$pmnum	=	1;
	$lastupdate	=	$dateline;
	$lastdateline	=	$dateline;

	if(!$sessionexists){
		$uid	=	$fromuid;
		$field4 = array('plid','uid','isnew','pmnum','lastupdate','lastdateline');
		$query4 = getinsertsql("{$discuz_tablepre}ucenter_pm_members", $field4);

		$uid	=	$toid;
		$field5 = array('plid','uid','isnew','pmnum','lastupdate','lastdateline');
		$query5 = getinsertsql("{$discuz_tablepre}ucenter_pm_members", $field5);
	}

	if ($xc->db['discuz']->query($query3)) {
		if(!$sessionexists){
			$xc->db['discuz']->query($query4);
			$xc->db['discuz']->query($query5);
		}
		$convertedrows ++;
	} else {
		reportlog("无法转换短消息 $subject");
	}

	$totalrows ++;
	$converted = 1;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($p['pmid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);

}

?>