<?php
include(dirname(__file__)."/../includes.php");

//消息机制写操作
function message_set($mod,$content,$id='',$is_focus=0,$link='',$type=0){
	switch ($mod){
		case "remind":
		$type=intval($type);
		$is_focus=intval($is_focus);
		$touid=intval($id);
		message_set_remind($content,$touid,$is_focus,$link,$type);
		break;
		case "affair":
		$show_type_id=intval($id);
		$mod_type=intval($is_focus);
		$from_id=intval($link);
		message_set_affair($content,$show_type_id,$mod_type,$from_id);
		break;
		default:
		echo 'error';
		break;
	}
}

function message_set_del($type,$rid='',$uid=''){
	$rid=intval($rid);
	switch($type){
		case "remind":
		return message_del_remind($rid,$uid);
		break;
		case "affair":
		message_del_affair($rid='');
		break;
		default:
		echo 'error';
		break;
	}
}

function message_set_affair($content,$show_type_id,$mod_type,$from_id){
	global $tablePreStr;
	$t_recent_affair=$tablePreStr."recent_affair";
	$dbo=new dbex;
  dbplugin('w');
	$uid=get_sess_userid();
	$userico=get_sess_userico();
	$uname=get_sess_username();
	$sql=" insert into $t_recent_affair (content,user_id,user_name,user_ico,date_time,update_time,type_id,mod_type,for_content_id) values ('$content',$uid,'$uname','$userico',NOW(),NOW(),$show_type_id,$mod_type,$from_id) ";
	$dbo->exeUpdate($sql);
}

function message_set_remind($content,$touid,$is_focus=0,$link,$type){
	$uid=get_sess_userid();
	$userico=get_sess_userico();
	$uname=get_sess_username();	
	global $tablePreStr;
	$t_remind=$tablePreStr."remind";
	$dbo=new dbex;
  dbplugin('w');
  if($is_focus==0){
	  $sql=" select id from $t_remind where user_id=$touid and type_id=$type ";
	  $is_set=$dbo->getRow($sql);
	  if(empty($is_set)){
		  $sql=" insert into $t_remind (user_id,type_id,date,content,is_focus,from_uid,from_uname,from_uico,link) values ($touid,$type,NOW(),'$content',$is_focus,$uid,'$uname','$userico','$link') ";
		  $dbo->exeUpdate($sql);
	  }else{
			$sql=" update $t_remind set count = count+1,date = NOW() where type_id = $type and user_id = $touid  ";
			$dbo->exeUpdate($sql);
	  }
	}else{
	  $sql=" insert into $t_remind (user_id,type_id,date,content,is_focus,from_uid,from_uname,from_uico,link) values ($touid,$type,NOW(),'$content',$is_focus,$uid,'$uname','$userico','$link') ";
	  $dbo->exeUpdate($sql);
	}
}

function message_del_remind($rid,$uid){
	$uid=intval($uid);
	if($uid==''){
		$uid=get_sess_userid();
	}
	$condition=" and id=$rid ";
	if($rid==''){
		$condition=" limit 1 ";
	}
	global $tablePreStr;
  $t_remind=$tablePreStr."remind";
	$dbo=new dbex;
  dbplugin('w');
  $sql="delete from $t_remind where user_id=$uid $condition ";
  $dbo->exeUpdate($sql);
}

function message_del_affair($rid){
	global $tablePreStr;
  $t_recent_affair=$tablePreStr."recent_affair";
	$dbo=new dbex;
  dbplugin('w');
  $uid=get_sess_userid();
  $sql="delete from $t_recent_affair where id=$rid and user_id=$uid";
  $dbo->exeUpdate($sql);
}



?>