<?php
function doaction($type,$id)
{
  GLOBAL $db,$tbl,$q_ext, $changeAgentCode;
  $id2=explode(",,",$id);
  $aa=explode(",",$id2[0]);


	switch ($type)
	{
		case 'del':
			user_del($id,$q_ext);
			break;
		case 'multdel':
			foreach ($aa as $v)
			{
				user_del($v,$q_ext);
              //echo "$v<BR>";
			}
			break;
		case 'ext':
			foreach($aa as $v){
				funcdate_ext($v);
		    }
			break;
		case 'changeCode':

				$intNum = $db->GetOne("SELECT COUNT(*) AS num FROM `agency` WHERE  agentcode = '{$changeAgentCode}'");
				if ($intNum == 0) {echo "<script type='text/javascript'>alert('门店编号有误，请重新输入！');</script>"; return false;}
				foreach ($aa as $v)
				{
					$strSql = "UPDATE broker SET agentcode = '$changeAgentCode' WHERE uid = {$v}";
					//$db->Execute($strSql);
				}
			
			break;
		default:
			return "";
	}
}

//删除
function user_del($uid) {
  GLOBAL $web_auth,$db;

//数据删除
	$q="delete from broker where uid=".intval($uid);
	//$db->Execute($q);
}

//账号延期
function funcdate_ext($uid){
	 GLOBAL $web_auth,$db;
	$q= "update broker set expiredate='".$_REQUEST[funcdate]."',valid_flag='1' where uid=$uid";
	//$db->Execute($q);
}


/*
* getBlockArrByKeyword 联想框查询小区
*/
if(!function_exists('getBlockArrByKeyword'))
{
	function getBlockArrByKeyword($keyword,$num = 20,$is_ajax=1)
	{
		global $mc,$db,$update,$conf_city;
		
		$return = array();
		if(!preg_match("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$keyword) && $keyword != '')
		{
			$num = intval($num);
			$ids = '';
			$temp = 0;

			$memkey = $conf_city."_block_info_new";
			if($update == 1) $mc->delete($memkey);

			$cache = $mc->get($memkey);
			if(is_array($cache) && !empty($cache))
			{
				$block_arr = $cache;
			}
			else
			{
				if($conf_city=='hz'){
					$block_arr = $db->GetAll("select id,blockname,bi_s,bi_spell FROM block where esta=2 ");
				}else{
					$block_arr = $db->GetAll("select id,blockname,bi_s,bi_spell FROM block ");
				}
				
				$mc->add($memkey, $block_arr, false, 86400);
			}
			
			if($block_arr)
			{
				foreach($block_arr as $block)
				{
					if($temp >= $num)
					{
						break;
					}
					if($is_ajax == 1){
						$block['blockname'] = iconv('GBK','UTF-8',$block['blockname']);
					}
					if(preg_match("/".$keyword."/",$block['blockname']." ".$block['bi_s']." ".$block['bi_spell']))
					{
						$id = intval($block["id"]);
						$ids .= $temp == 0 ? $id : ",".$id;

						$temp++;
					}
				}
				
				if($ids != '')
				{	
					if($conf_city=='hz'){
						$return = $db->GetAll("select id,blockname,district,streetid,address,averprice FROM block where id in (".$ids.") and esta=2 ");
					}else{
						$return = $db->GetAll("select id,blockname,district,streetid,address,averprice FROM block where id in (".$ids.")");
					}
					
				}
			}
		}
		return $return;
	}
}

/*
* getBlockAttrs   查询小区完善属性字段名和属性名称对应数据
* return array()
*/
function  getBlockAttrs(){
    global $mc,$dbback; 
	$sql = "select id,name_field,name  from growth_block_attr where del_flag = '0'";
	$attrs = $dbback->GetAll($sql);
	$block_attrs = array();
    if(is_array($attrs)){
		foreach($attrs as $val){
			$block_attrs[$val['name_field']] = $val['name'];
		}
	}
	return $block_attrs; 
}

/*
* blockAttrArrange   重新整理属性锁定数量，更新block表
* $parms int $block_id 小区id
* return int
*/
function  blockAttrArrange($block_id,$attr_num){
	global $db ,$dbback;

	$sql = SqlCombinHelper::select_sql('growth_block_attr_lock',array('count(*)'),"block_id='{$block_id}'");
	$lock_num = $dbback->GetOne($sql);

	$attr_open_num = $attr_num - $lock_num;
    $datas = array();
	if($attr_open_num <=0 ){
		//总锁定开启，删除该小区每个属性锁定
		$sql2 = SqlCombinHelper::delete_sql('growth_block_attr_lock', " block_id = '{$block_id}' ");
		$db->Execute($sql2);

		//小区锁定
		$datas['lock_opennum'] = 0;
		$datas['lock_flag'] = 1;
	
	}else{
		$datas['lock_opennum'] = $attr_open_num;
	}
    //更新小区锁定情况
	$sql3 = SqlCombinHelper::update_sql('block',$datas,"id='{$block_id}'");
	$db->Execute($sql3);
	 
	return 1;
}

/*
* editblockAttr   小区完善更新block表属性数据
* $parms int $block_id 小区id
* $parms array $datas  更新数据
* return  int
*/
function  editblockAttr($block_id,$datas){
	global $db ;
	if(!$block_id || !is_array($datas) || empty($datas)){
		return 0;
	}
    $sql = SqlCombinHelper::update_sql('block',$datas,"id='{$block_id}'");
	$db->Execute($sql);
	$result = $db->Affected_Rows();
 
	return $result;
}


/*
* lockBlockAttr   锁定小区属性
* $parms int $block_id 小区id
* $parms string  $attr_field   锁定的属性字段
* return  int
*/
function  lockBlockAttr($block_id,$attr_field){
    global $db ;
    $arrField = array('block_id'=>$block_id,'attr_name'=>$attr_field);
    $sql = SqlCombinHelper::replace_sql('growth_block_attr_lock', $arrField);
	$db->Execute($sql);
	$result = $db->Affected_Rows();
	return $result;
}

/*
* updateBlockAudit   更新小区审核表
* $parms int $id  表id
* $parms string  $attr_field   锁定的属性字段
* return  int
*/
function  updateBlockAudit($where,$datas){
	global $db ;
    $sql = SqlCombinHelper::update_sql('growth_block_improve_audit',$datas, $where);  
	$db->Execute($sql);
	$result = $db->Affected_Rows();
	return $result;
}

/**
 * isCheckBlockAudit 查询属性记录是否审核
 * @param $id 审核表记录ID
 * @return int
 */
function isCheckBlockAudit($id){
    global $db;
    $sql = SqlCombinHelper::select_sql('growth_block_improve_audit',array('status'),"id='{$id}'");
    $status = $db->GetOne($sql);
    return $status; 
}

/**
 * isLockBlockAudit 查询小区某个属性是否锁定
 * @param int $blockid 小区Id
 * @param string $attr 属性名
 *@ return blooen
 */
function isLockBlockAudit($blockid,$attr){
    global $db;
    $sql = SqlCombinHelper::select_sql('growth_block_attr_lock',array('count(*)'),"block_id='{$blockid}' AND attr_name = '{$attr}'");
    echo $sql;
    $num = $db->GetOne($sql);
    $result = $num == 0 ? FALSE : TRUE;
    return $result;
}

/*
* getBlockInfo   获取小区信息
* $parms int $blockId   小区id
* $parms string  $attr   查询的字段
* return  array
*/
function  getBlockInfo($blockId,$attr){
	if(!$blockId) return array();
	if(!$attr || !is_array($attr))  $attr = array('*');
	global $dbback ;

	$sql = SqlCombinHelper::select_sql('block',$attr,"id='{$blockId}'");
	return $dbback->GetRow($sql);
}

 
?>