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

error_reporting(7);
ini_set("display_errors",true);
checkAuth('30');	//判断登录和权限

    
   


  if(is_array($stat) && !empty($stat))
{
     
 
	$imgcheck = new BlockimgCheck();
	foreach($stat as $key=>$value)
	{
		$value = intval($value);
		if($actdo == 'entry')	//加入小区图库
		{   
                     $sort_type = $sort == 1 ? $outside[$key] : $sort;  
                     if($sort_type!=0){
                         $brokerid = $imgcheck->getPicBrokerId($key);  //获取经纪人id
                         $blockname = $imgcheck->getBlockName($blockid);
                         $scoreArr = $imgcheck->getPicCreditExp($key);
                     }               
                
	             $imgcheck->checkentry($key,$blockid,$sort_type,$room[$key],$hall[$key],$pic[$key]);
                     if($config_GrowthCredit==1&&$sort_type!=0){
                          $wb = new WayBlock();
                          $alias = $wb->getInfoLevelWayAlias($blockid, 2);               
                   
                          $brokerGroup = new BrokerGroup();
                          $brokerInfo = $brokerGroup->getBrokerGroupInfo($brokerid);    
                        
                          $arrBroker = array(
                             'id' => $brokerInfo['uid'],
                             'groupid' => $brokerInfo['groupid'],
                          );               
                     
                          $gbc = new GrowthBrokerCredit($arrBroker);       
                          $creditStatus = $gbc->increase($alias,'',$scoreArr['credit_score']); 
                          $gbe = new GrowthBrokerExp($arrBroker);           
                          $expStatus = $gbe->increase($alias,'',$scoreArr['exp_score']);//增长代码
                          // $gcwp = new GrowthCreditWayPackage;
	                  // $score = $gcwp->getScoreByWayAliasAndPackageId($alias,$arrBroker['groupid']);
	                  //$gewp = new GrowthExpWayPackage();	
	                  //$exp = $gewp->findScoreByWayAliasAndPackageId($alias,$arrBroker['groupid']);	
                          $title = "小区外景图审核通过";
                          if($expStatus['status'] == 1)
                             $expMessage = "，成长值+".$scoreArr['exp_score'];
                          $msg = "感谢您参与小区外景图完善，您提交的关于".$blockname."的外景图已经通过审核，奖励您积分+".$scoreArr['credit_score'].$expMessage."！继续完善，获得更多积分！";
                          apply::send_systemmsg($brokerid, $title, $msg);
                          //超过35张锁小区外景图
                          $blockOutsidePicNum = $imgcheck->getOutsidePicNum($blockid);
                          if($blockOutsidePicNum>=35){
                              $imgcheck->lockOutsidePic($blockid);
                          }
                     }
                    
		}

                if($actdo == 'del')	//删除图库图片
		{
                   
                    $sort_type = $sort == 1 ? $outside[$key] : $sort;
                    //$blockname = $imgcheck->getBlockName($blockid);
		    $imgcheck->del_img($key,$blockid,$sort_type,$room[$key],$hall[$key],$pic[$key]);
                    //$brokerid = $imgcheck->getPicBrokerId($key);  //获取经纪人id                    
                   // $title = "小区外景图审核未通过";
                    //$msg = "感谢您参与小区外景图完善，您提交的关于".$blockname."的外景图没有通过审核，请您查证准确外景图片后再来试试吧！";
                    //apply::send_systemmsg($brokerid, $title, $msg);
           
		}

        

	}
	   
}

echo "<script>window.parent.window.location='/module/block/entry_audit.php?id=".$blockid."&sort=".$sort."';</script>";
exit;

  //经纪人审核
class BlockimgCheck
{
	private $systime = '';
        private $db;
        private $dbback;
        private $mc;
        private $conf_cityflag;




        public function __construct() {
            global $db,$dbback,$mc,$conf_cityflag;
            $this->db = $db;
            $this->dbback = $dbback;
            $this->mc = $mc;
            $this->conf_cityflag = $conf_cityflag;
         
        }

        public function checkentry($key,$blockid,$sort,$room,$hall,$pic)
	{           
            $arr['bi_add_time'] = time();
            $arr['bi_img'] = $pic;
            $arr['bi_blockid'] = $blockid;
            $arr['bi_room'] = $room;
            $arr['bi_hall'] = $hall;
            $arr['bi_type'] = $sort;
            $q = insert_sql("block_img",$arr);
            $mark=$this->db->Execute($q);
            //记录图片入库日志      
            $this->entry_log_b($blockid,$sort,$room,$hall,$pic,1);
            //删除已入库的待入图库图片
            $this->del($key);
            //外景图修改记录表状态
            if($sort!=0){
                $this->growth_pic_log($key,1);
            }
            

	}

    //删除图库图片
     	public function del_img($key,$blockid,$sort,$room,$hall,$pic)
	{             
            $q = "delete from block_img where id=".$key."";
            $this->db->Execute($q);
            //记录图片入库日志
            $this->entry_log_b($blockid,$sort,$room,$hall,$pic,2);
            //外景图修改记录表状态
          // if($sort!=0){
          //      $this->growth_pic_log($key,2);
           // }
           
	}
        
	//记录日志
	public function entry_log($blockid,$room,$hall,$pic,$flag=1){
            global $web_auth;		
            $arr['blockid'] = $blockid;
            $arr['sort'] = 2;
            $arr['room'] = $room;
            $arr['hall'] = $hall;
            $arr['admin'] = $_SESSION[$web_auth]["username"];
            $arr['dateline'] = time();
            $arr['pic_url'] = $pic;
            $arr['flag'] = $flag;
            $q = insert_sql("admin_pic_entry_log",$arr);
            $this->db->Execute($q);
	}
        
        //记录日志xue 2014-2-15
	public function entry_log_b($blockid,$sort,$room,$hall,$pic,$flag=1){            
	    global $web_auth;		
            $arr['blockid'] = $blockid;
            //amdin_pic_entry_log sort字段中户型用2表示，外景用1表示
            $sort = $sort ==0 ? "2" : "1";       
            $arr['sort'] = $sort;
            $arr['room'] = $room;
            $arr['hall'] = $hall;
            $arr['admin'] = $_SESSION[$web_auth]["username"];
            $arr['dateline'] = time();
            $arr['pic_url'] = $pic;
            $arr['flag'] = $flag;
            $q = insert_sql("admin_pic_entry_log",$arr);        
           $this->db->Execute($q);
	}
        
        //经纪人成长体系图片日志 xue 2014-2-15
        public function growth_pic_log($key,$pic_staus){                  
            $q = "update growth_pic_check_log set pic_status = $pic_staus where sid = $key";
            $this->db->Execute($q);
        
        }
        //记录日志
	public function del($key){         	
           $q = "delete from admin_pic_entry where id=".$key.""; 
           $this->db->Execute($q);
	}
        
        //获取该图片上传经纪人uid
        public function getPicBrokerId($id){
            $q = "select uid from admin_pic_entry where id= $id";         
            return $this->dbback->GetOne($q);
        }
        //获取小区名称
        public function getBlockName($blockId){
            $memKey = $this->conf_cityflag."_pic_blockname_".$blockId;
            $this->mc->delete($memKey);
            $data = $this->mc->get($memKey);
            
            if($data['isok']!=1){
                 $q = "select blockname from block where id =$blockId";
                 $blockname = $this->dbback->GetOne($q);
                 $data = array('isok'=>1,'data'=>$blockname);
                 $this->mc->add($memKey,$data,false,600);
            }          
            return $data['data'];
        }
        
        //获取经验值和积分
        public function getPicCreditExp($id){
            $q = "select exp_score,credit_score from `growth_pic_check_log` where sid = $id";
            return $this->dbback->GetRow($q);           
        }
        
        //获取图库外景图数量
        public function getOutsidePicNum($blockid){
            $sql = "select count(*) as num from block_img where bi_blockid = $blockid and bi_type in (51,52,53,54,55)";
            return $this->dbback->GetOne($sql);
        }
        
        //锁定小区外景图
        public function lockOutsidePic($blockid){
            $sql = "update block set lock_outside = 1 where id = $blockid";
            $this->db->Execute($sql);            
        }
        
       




}

?>