<?php
/*
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('id', 'message');
}
$query = $xc->db['source']->query("SELECT max(id) FROM {$source_tablepre}Message");
$maxid = $xc->db['source']->fetch_array($query);

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}Message WHERE id BETWEEN $start AND $end") or dexit("检索数据表 '{$source_tablepre}Message' 错误！<br>请将短消息表 '{$source_tablepre}Message' 字段类型为 nText 的改为 Text 类型。", 'mssql');
while($dvpms = $xc->db['source']->fetch_assoc($query)) {
	$dvpms = array_change_key_case(daddslashes($dvpms));

	if($toid = getuid($dvpms['incept'])) {
		//pms
		$fromusername	=	$dvpms['sender'];
		$fromuid	=	getuid($fromusername);
		$folder		=	 'inbox';
		$new		=	$dvpms['flag'] ? 0 : 1;
		$message	=	@strip_tags(trim($dvpms['content']));
		$lastsummary = cutstr(trim($message), 150);

		//add by elevensky
		//ucenter_pm_lists
		$authorid	=	getuid($fromusername);
		$pmtype		=	1;
		$subject	=	formatstr(cutstr(htmlspecialchars(trim(@strip_tags($dvpms['title']))), 78));
		$members	=	2;
		$min_max	=	relationship($authorid, $toid);
		$dateline	=	timetounix($dvpms['sendtime']);
		$lastmessage = array('lastauthorid' => $fromuid, 'lastauthor' => $fromusername, 'lastsummary' => $lastsummary);
		$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");
		}
		//add by elevensky
		$totalrows ++;
	}
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvpms['id']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
$end < $maxid ? $converted =1 : '';

?>