<?
date_default_timezone_set('UTC');
$server="localhost";
$user="chang";
$password="kan"; 
require("/opt/local/apache/htdocs/_switch.php");
$_DELETE_DIGITAL_PATH = "../remove/";

mysql_connect($server,$user,$password);
mysql_select_db("DE");

$d = date("Y-m-d");
$SQL = "Select _AUTO From MSN Where (_ORFILE = '' Or _ORCOUNT ='' ) And DATE_FORMAT(_DATE,'%Y-%m-%d %T') between '1970-01-01 01:00:00' and (DATE_ADD(DATE_SUB('".$d."',INTERVAL 1 DAY),INTERVAL '23:59:59' HOUR_SECOND))";
$result =  mysql_query($SQL);
while ($row=mysql_fetch_array($result)){
	$count_record[0] = 0;
	$count_file[0] = 0;

	$SQL1 = "Select count(0) from MSN_MSG where _IDX = ".$row['_AUTO'];
	$count_record = mysql_fetch_array(mysql_query($SQL1));
	//:q
	print $count_record[0];

	$SQL1 = "Select Count(0) from MSN_MSG where _IDX = ".$row['_AUTO']." and _FILENAME <>''";
	$count_file = mysql_fetch_array(mysql_query($SQL1));
	print $count_file[0];

	$SQL1 = "Update MSN Set _DATE = _DATE,_ORCOUNT = $count_record[0],_ORFILE = $count_file[0] Where _AUTO = ".$row['_AUTO'];
	mysql_query($SQL1);
}

if ((fn_digital_asset()==1)||(fn_data_mining()==1)){
	$_FILE = "MSN_".date("Y-m-d")."_".date("H:i:s");
	$TMP_FILE = "/tmp/".$_FILE;
	$fp = fopen($TMP_FILE,"w+");
	$SQL="Select _AUTO From MSN Where (_ORFILE = '0' and  _ORCOUNT = '0' ) And DATE_FORMAT(_DATE,'%Y-%m-%d %T') between '1970-01-01 01:00:00' and (DATE_ADD(DATE_SUB('".$d."',INTERVAL 1 DAY),INTERVAL '23:59:59' HOUR_SECOND))";
	$result =  mysql_query($SQL);
	while ($row=mysql_fetch_array($result)){
		fputs($fp,"MSN.".$row["_AUTO"]."\n");
		print "<br>auto is ".$row["_AUTO"]."<br>";
	}
	copy($TMP_FILE,$_DELETE_DIGITAL_PATH.$_FILE);
	unlink($TMP_FILE);
}

$SQL = "Delete From MSN Where (_ORFILE = '0' and  _ORCOUNT = '0' ) And DATE_FORMAT(_DATE,'%Y-%m-%d %T') between '1970-01-01 01:00:00' and (DATE_ADD(DATE_SUB('".$d."',INTERVAL 1 DAY),INTERVAL '23:59:59' HOUR_SECOND))";
mysql_query($SQL);


mysql_close();
?>
