<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
include_once(CORE_LIB.'/class_apply.php');

//操作人和城市
$operator = $esfadmin['truename'];
$city = $esfadmin['now_city'];

//查询小区属性完善列表
$block_attrs = getBlockAttrs();
$attr_num = count($block_attrs);

//小区属性完善审核
if($do == 'improve_audit'){
	//组装前台数据
	$data = array();
	$sql = '';
	if(is_array($_POST)){
		foreach($_POST as $key=>$val){
			$arr = explode('_',$key);
			if($arr[0] == 'improveAudit' && $arr[1] && $val){
				 $data[$arr[1]] = $val;
			}
		}
	}

	//处理审核数据
	if(is_array($data)){
		$ids = '';
		$pass_id = '';
		$pass_array = array();
		foreach($data as $k=>$v){
			//获取经纪人id,增加的积分和成长值，用户提交的审核值
			$sql4 = "select attr_info,broker_id,credit_score,exp_score from growth_block_improve_audit where id ='{$k}' ";
			$audit_info = $dbback->GetRow($sql4);

			//$v == 1通过审核--操作
			if($v == 1){
			
				if(!$attr_field || !$block_id){
					js_alert('无效数据');
					die;
				}

				$pass_id .= $k.',';
 
				if($standard==$k){
					//将通过审核的数据更新到block表(百度地图特殊处理)
					if($attr_field == 'b_map'){
						$b_map_info = explode('||',$audit_info['attr_info']);
						$b_map['b_map_x'] = $b_map_info[0];
						$b_map['b_map_y'] = $b_map_info[1];
						//更新小区属性
						editblockAttr($block_id,$b_map);
					}else{
						$datas = array("{$attr_field}"=>"{$audit_info['attr_info']}");
						//更新小区属性
						editblockAttr($block_id,$datas);
					}

		            $block_info = getBlockInfo($block_id,array('lock_flag'));
					//小区未锁定
					if($block_info['lock_flag'] == 0){
						//锁定该属性
						lockBlockAttr($block_id,$attr_field);
						//重新整理属性锁定数量，更新block表
						blockAttrArrange($block_id,$attr_num);
					}
				
				}

				/*****************增加积分/成长值start******************/
				
				$BrokerGroup = new BrokerGroup();
				$brokerGroupInfo = $BrokerGroup->getBrokerGroupInfo($audit_info['broker_id']);
				$arrBroker = array(
					'id' => $audit_info['broker_id'],
					'groupid' => $brokerGroupInfo['groupid'],
				);

				//增加成长值
				$wb = new WayBlock();
				$alias = $wb->getInfoLevelWayAlias($block_id, 1);

				$gbe = new GrowthBrokerExp($arrBroker);
				$exp_res = $gbe->increase($alias,null,$audit_info['exp_score']);

				//增加积分
				$gbc = new GrowthBrokerCredit($arrBroker);
				$credit_res =$gbc->increase($alias,null,$audit_info['credit_score']);
                
				$pass_array[$k]['exp_res'] = $exp_res;
				$pass_array[$k]['credit_res'] = $credit_res;
				/*****************增加积分/成长值end******************/

				//发送系统消息
				if($exp_res['status'] ==2){
					$exp_add = "成长值今日已累计到达上限";
				}else{
					$exp_add = "成长值+".$audit_info['exp_score'];
				}
				$title = "小区属性完善审核通过";
				$content = "感谢您参与小区完善，您提交的关于<font class='fontcolor'>".$blockname."-".$attr_name."</font>的完善信息已经通过审核，奖励您积分+".$audit_info['credit_score']."，".$exp_add."！继续完善，获得更多积分！";
                apply::send_systemmsg($audit_info['broker_id'],$title,$content);


			}elseif($v == 2){//审核不通过，记录id
				$ids .= $k.",";
				//发送系统消息
		        $title = "小区属性完善审核未通过";
		        $content = "感谢您参与小区完善，很遗憾您提交的关于<font  class='fontcolor'>".$blockname."-".$attr_name."</font>的完善信息没有通过审核，请您查证准确资料信息后再来试试吧！";
                apply::send_systemmsg($audit_info['broker_id'],$title,$content);
			}
		}

		//通过审核，更新审核记录表
		$pass_id = rtrim($pass_id,',');
		$datas = array('status'=>1);
		$where = " id in ({$pass_id})";
		$result3 = updateBlockAudit($where,$datas);

		//审核不通过
		$ids = rtrim($ids,',');
		if($ids){
			$datas = array('status'=>2);
			$where = " id in ({$ids})";
			$result6 = updateBlockAudit($where,$datas);
		}

		/**************记录日志*******************/
		$log = array();
	    $log['pass'] = $pass_array;
		$log['nopass_id'] = $ids;
		$log = json_encode($log);
		$t = $operateRecord->insertData(90, '小区完善审核',$log , $operator);

		$back_url = "./block_improve_audit.php?do=check&block_id={$block_id}&attr_field={$attr_field}";
		js_alert('操作成功!',$back_url,3);die;
	}

}elseif($do == 'attr_lock'){ //小区完善属性编辑
	//查询小区总锁定情况
	$sql = "select id,lock_flag,lock_opennum  from block where  id='$block_id' ";
	$block_info = $dbback->GetRow($sql);

	//数据库block表中lock_flag字段默认为1（锁定）,lock_opennum为0
	//前台传入参数，设置总锁定状态
	if($lock_flag == '1'){
		if($block_info['lock_flag'] == 0){//总锁定为未锁定状态
			//将总锁定设置为锁定
			$data = array('lock_flag'=>1,'lock_opennum'=>0);
			$sql = SqlCombinHelper::update_sql('block', $data, " id = '$block_id' ");
			$db->Execute($sql);
			$result = $db->Affected_Rows();

			//同时删除growth_block_attr_lock表中锁定属性
			$db->Execute("delete from growth_block_attr_lock where block_id='$block_id';");
			$result2 = $db->Affected_Rows();
			if($result){
				js_alert('总锁定设置成功!',$back_url,3);
				die;
			}
		} 
	}elseif($lock_flag == '0'){//前台传入参数，将总锁定设置为未锁定 
				
		//当总锁定开启时
		if($block_info['lock_flag'] == 1){
			//总锁定设置为未锁定,并记录单条属性未锁定数量
			$db->Execute("update block set lock_flag = '0',lock_opennum = '{$attr_num}' where id = '$block_id' limit 1;");
			$result2 = $db->Affected_Rows();

			//同时删除growth_block_attr_lock表中锁定属性
			$db->Execute("delete from growth_block_attr_lock where block_id='$block_id';");

			if($result2){
				js_alert('设置成功,已经设置成未锁定!',$back_url,3);
				die;
			}
		}

		//查询已经锁定的属性列表
		$sql3 = "select id,block_id,attr_name  from growth_block_attr_lock where block_id='$block_id';";
		$block_locks = $dbback->GetAll($sql3);
		//过滤已经锁定的字段
		$lock_attrs = array();
		if(is_array($block_locks)){
			foreach($block_locks as $val){
				$lock_attrs[] = $val['attr_name'];
			}
		}
	 
		//属性锁定sql组装
		$con_sql = 'insert into growth_block_attr_lock (block_id,attr_name) values ';
		$del_lock_ids = '';
		if(is_array($block_attrs)){
			foreach($block_attrs  as $key=>$value){
				if(!in_array($key,$lock_attrs) && $$key == '1'){//锁定
					$insert_flag = 1;
					$con_sql .= "({$block_info['id']},'{$key}'),";
				}elseif($$key == '2'){//解锁
					$lock_name = $key.'_lock_id';
					$lock_id = $$lock_name;
					if($lock_id){
						$del_lock_flag = 1;
						$del_lock_ids .= "{$lock_id},";
					}
					
				}
			}
			$con_sql = trim($con_sql,',');	
			$del_lock_ids = trim($del_lock_ids,',');
		}
		//新增
		if($insert_flag == 1){
			 $result = $db->Execute($con_sql);
		}

		//解锁
		if($del_lock_flag == 1){
			$del_sql = "delete from growth_block_attr_lock where id in ({$del_lock_ids})";
			$result = $db->Execute($del_sql);
		}


 
		if($insert_flag == 1 || $del_lock_flag == 1){
			//重新整理属性锁定数量，更新block表
		    blockAttrArrange($block_id,$attr_num);
			js_alert('更新成功!',$back_url,3);
			die;
		}else{
			js_alert('无效操作!');
			die;
		}
	}
	js_alert('无效操作');
	die;
}
 
?>